1
|
|
|
import pytest |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
@pytest.fixture |
5
|
|
|
def subclass_registry_metaclass(): |
6
|
|
|
from so_magic.utils import SubclassRegistry |
7
|
|
|
return SubclassRegistry |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
@pytest.fixture |
11
|
|
|
def register_class(subclass_registry_metaclass): |
12
|
|
|
def _register_class(subclass_id: str, inherit=False): |
13
|
|
|
class ParentClass(metaclass=subclass_registry_metaclass): |
14
|
|
|
pass |
15
|
|
|
|
16
|
|
|
if inherit: |
17
|
|
|
@ParentClass.register_as_subclass(subclass_id) |
18
|
|
|
class Child(ParentClass): |
19
|
|
|
pass |
20
|
|
|
else: |
21
|
|
|
@ParentClass.register_as_subclass(subclass_id) |
22
|
|
|
class Child: |
23
|
|
|
pass |
24
|
|
|
|
25
|
|
|
child_instance = ParentClass.create(subclass_id) |
26
|
|
|
|
27
|
|
|
return {'class_registry': ParentClass, 'child': Child, 'child_instance': child_instance} |
28
|
|
|
return _register_class |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@pytest.fixture |
32
|
|
|
def use_metaclass(register_class, assert_correct_metaclass_behaviour): |
33
|
|
|
is_instance = {True: lambda classes: isinstance(classes['child_instance'], classes['class_registry']), |
34
|
|
|
False: lambda classes: not isinstance(classes['child_instance'], classes['class_registry'])} |
35
|
|
|
def _use_metaclass_in_scenario(subclass_id: str, inherit=False): |
36
|
|
|
classes = register_class(subclass_id, inherit=inherit) |
37
|
|
|
assert_correct_metaclass_behaviour(classes, subclass_id) |
38
|
|
|
assert is_instance[inherit] |
39
|
|
|
return classes['child_instance'], classes['child'], classes['class_registry'] |
40
|
|
|
return _use_metaclass_in_scenario |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
@pytest.fixture |
44
|
|
|
def assert_correct_metaclass_behaviour(): |
45
|
|
|
def assert_metaclass_behaviour(classes, subclass_id): |
46
|
|
|
assert classes['class_registry'].subclasses[subclass_id] == classes['child'] |
47
|
|
|
assert type(classes['child_instance']) == classes['child'] |
48
|
|
|
assert isinstance(classes['child_instance'], classes['child']) |
49
|
|
|
return assert_metaclass_behaviour |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def test_metaclass_usage(subclass_registry_metaclass): |
53
|
|
|
class ParentClass(metaclass=subclass_registry_metaclass): |
54
|
|
|
pass |
55
|
|
|
|
56
|
|
|
assert type(ParentClass) == subclass_registry_metaclass |
57
|
|
|
assert hasattr(ParentClass, 'subclasses') |
58
|
|
|
assert hasattr(ParentClass, 'create') |
59
|
|
|
assert hasattr(ParentClass, 'register_as_subclass') |
60
|
|
|
assert ParentClass.subclasses == {} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
def test_subclass_registry(use_metaclass): |
64
|
|
|
child1_instance1, Child1, ParentClass = use_metaclass('child1', inherit=True) |
65
|
|
|
|
66
|
|
|
non_existent_identifier = 'child2' |
67
|
|
|
|
68
|
|
|
exception_message_regex = \ |
69
|
|
|
f'Bad "{str(ParentClass.__name__)}" subclass request; requested subclass with identifier ' \ |
70
|
|
|
f'{non_existent_identifier}, but known identifiers are ' \ |
71
|
|
|
rf'\[{", ".join(subclass_identifier for subclass_identifier in ParentClass.subclasses.keys())}\]' |
72
|
|
|
|
73
|
|
|
with pytest.raises(ValueError, match=exception_message_regex): |
74
|
|
|
ParentClass.create(non_existent_identifier) |
75
|
|
|
|
76
|
|
|
child1_instance2, Child2, ParentClass2 = use_metaclass('child2', inherit=False) |
77
|
|
|
assert ParentClass.subclasses['child1'] == Child1 |
78
|
|
|
|