test_modeling   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 31
eloc 126
dl 0
loc 198
rs 9.92
c 0
b 0
f 0

21 Functions

Rating   Name   Duplication   Size   Complexity  
A test_validate_type_raises_on_invalid_identifier() 0 4 2
A test_elements_empty_iterable() 0 4 1
A test_elements_callable_element_type() 0 14 1
A test_validate_type_raises_on_empty_string() 0 4 2
A test_validate_type_accepts_callable() 0 6 1
A test_elements_invalid_identifier_type() 0 8 2
A test_elements_multiple_getters() 0 11 2
A test_validate_type_non_callable_valid_identifier() 0 4 1
A test_validate_type_accepts_identifier_and_callable() 0 8 1
A test_validate_type_accepts_valid_identifier() 0 4 1
A test_graph_model_validate_node_parameters() 0 7 2
A test_validate_type_non_string_callable() 0 8 1
A test_graph_model__add__() 0 7 1
A test_validate_type_empty_string() 0 4 2
A test_validate_type_neither_callable_nor_identifier() 0 4 2
A test_elements_yields_correct_count() 0 11 2
A test_graph_model() 0 15 1
A test_validate_type_special_characters() 0 4 2
A test_elements_element_type_not_callable_invalid() 0 5 2
A test_validate_type_callable_string() 0 10 1
A test_validate_type_identifier_and_callable() 0 8 1
1
import pytest
2
3
import graphinate
4
import graphinate.typing
5
from graphinate.modeling import GraphModel, elements
6
7
8
def test_graph_model(map_graph_model):
9
    # arrange
10
    expected_country_count, expected_city_count, graph_model = map_graph_model
11
    country_type_id = (graphinate.typing.UniverseNode, 'country')
12
    city_type_id = ('country', 'city')
13
14
    # act
15
    actual_model_count = len(graph_model._node_models)
16
    actual_country_count = len(list(graph_model._node_models[country_type_id][0].generator()))
17
    actual_city_count = len(list(graph_model._node_models[city_type_id][0].generator()))
18
19
    # assert
20
    assert actual_model_count == 3
21
    assert actual_country_count == expected_country_count  # len(country_ids)
22
    assert actual_city_count == expected_city_count  # len(city_ids)
23
24
25
def test_graph_model__add__():
26
    first_model = graphinate.model(name='First Model')
27
    second_model = graphinate.model(name='Second Model')
28
29
    actual_model = first_model + second_model
30
31
    assert actual_model.name == 'First Model + Second Model'
32
33
34
def test_graph_model_validate_node_parameters():
35
    graph_model = graphinate.model(name='Graph with invalid node supplier')
36
37
    with pytest.raises(graphinate.modeling.GraphModelError):
38
        @graph_model.node()
39
        def invalid_node_supplier(wrong_parameter=None):
40
            yield 1
41
42
43
def test_validate_type_identifier_and_callable():
44
    class CallableStr(str):
45
        def __call__(self):
46
            return "called"
47
48
    node_type = CallableStr("valididentifier")
49
    # Should not raise
50
    GraphModel._validate_type(node_type)
51
52
53
def test_validate_type_non_callable_valid_identifier():
54
    node_type = "valid_identifier"
55
    # Should not raise
56
    GraphModel._validate_type(node_type)
57
58
59
def test_validate_type_non_string_callable():
60
    class DummyCallable:
61
        def __call__(self):
62
            return "called"
63
64
    node_type = DummyCallable()
65
    # Should not raise
66
    GraphModel._validate_type(node_type)
67
68
69
def test_validate_type_neither_callable_nor_identifier():
70
    node_type = "123 invalid!"
71
    with pytest.raises(ValueError, match="Invalid Type:"):
72
        GraphModel._validate_type(node_type)
73
74
75
def test_validate_type_empty_string():
76
    node_type = ""
77
    with pytest.raises(ValueError, match="Invalid Type:"):
78
        GraphModel._validate_type(node_type)
79
80
81
def test_validate_type_special_characters():
82
    node_type = "invalid-type!"
83
    with pytest.raises(ValueError, match="Invalid Type:"):
84
        GraphModel._validate_type(node_type)
85
86
87
def test_validate_type_accepts_valid_identifier():
88
    node_type = "node_type"
89
    # Should not raise
90
    GraphModel._validate_type(node_type)
91
92
93
def test_validate_type_accepts_callable():
94
    def some_callable():
95
        pass
96
97
    # Should not raise
98
    GraphModel._validate_type(some_callable)
99
100
101
def test_validate_type_accepts_identifier_and_callable():
102
    class CallableStr(str):
103
        def __call__(self):
104
            return "called"
105
106
    node_type = CallableStr("anotheridentifier")
107
    # Should not raise
108
    GraphModel._validate_type(node_type)
109
110
111
def test_validate_type_raises_on_invalid_identifier():
112
    node_type = "not valid!"
113
    with pytest.raises(ValueError, match="Invalid Type:"):
114
        GraphModel._validate_type(node_type)
115
116
117
def test_validate_type_raises_on_empty_string():
118
    node_type = ""
119
    with pytest.raises(ValueError, match="Invalid Type:"):
120
        GraphModel._validate_type(node_type)
121
122
123
def test_validate_type_callable_string():
124
    # In Python, a string is never callable, but for the sake of the test, let's simulate
125
    # a string subclass that is callable
126
    class CallableStr(str):
127
        def __call__(self):
128
            return "called"
129
130
    node_type = CallableStr("not_an_identifier_but_callable")
131
    # Should not raise
132
    GraphModel._validate_type(node_type)
133
134
135
def test_elements_yields_correct_count():
136
    data = [
137
        {"id": 1, "name": "A"},
138
        {"id": 2, "name": "B"},
139
        {"id": 3, "name": "C"},
140
    ]
141
    result = list(elements(data, element_type="Node", id="id", name="name"))
142
    assert len(result) == len(data)
143
    for i, el in enumerate(result):
144
        assert el.id == data[i]["id"]
145
        assert el.name == data[i]["name"]
146
147
148
def test_elements_multiple_getters():
149
    data = [
150
        {"id": 1, "name": "A", "value": 10},
151
        {"id": 2, "name": "B", "value": 20},
152
    ]
153
    result = list(elements(data, element_type="Node", id="id", name="name", value="value"))
154
    assert len(result) == 2
155
    for i, el in enumerate(result):
156
        assert el.id == data[i]["id"]
157
        assert el.name == data[i]["name"]
158
        assert el.value == data[i]["value"]
159
160
161
def test_elements_empty_iterable():
162
    data = []
163
    result = list(elements(data, element_type="Node", id="id"))
164
    assert result == []
165
166
167
def test_elements_element_type_not_callable_invalid():
168
    data = [{"id": 1}]
169
    # element_type is not callable and not a valid identifier
170
    with pytest.raises(ValueError, match="Invalid Type:"):
171
        list(elements(data, element_type="123 invalid!", id="id"))
172
173
174
def test_elements_callable_element_type():
175
    data = [
176
        {"type": "Alpha", "id": 1},
177
        {"type": "Beta", "id": 2},
178
    ]
179
180
    def type_extractor(item):
181
        return item["type"]
182
183
    result = list(elements(data, element_type=type_extractor, id="id"))
184
    assert result[0].__class__.__name__ == "Alpha"
185
    assert result[1].__class__.__name__ == "Beta"
186
    assert result[0].id == 1
187
    assert result[1].id == 2
188
189
190
def test_elements_invalid_identifier_type():
191
    data = [{"id": 1}]
192
193
    def bad_type(item):
194
        return "not valid!"
195
196
    with pytest.raises(ValueError, match="Invalid Type:"):
197
        list(elements(data, element_type=bad_type, id="id"))
198