test_modeling   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

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

21 Functions

Rating   Name   Duplication   Size   Complexity  
A test_graph_model() 0 15 1
A test_validate_type_raises_on_invalid_identifier() 0 7 2
A test_elements_empty_iterable() 0 9 1
A test_elements_callable_element_type() 0 18 1
A test_validate_type_raises_on_empty_string() 0 7 2
A test_validate_type_accepts_callable() 0 8 1
A test_elements_invalid_identifier_type() 0 10 2
A test_elements_multiple_getters() 0 16 2
A test_validate_type_non_callable_valid_identifier() 0 7 1
A test_validate_type_accepts_identifier_and_callable() 0 11 1
A test_validate_type_accepts_valid_identifier() 0 7 1
A test_graph_model_validate_node_parameters() 0 9 2
A test_validate_type_non_string_callable() 0 11 1
A test_graph_model__add__() 0 10 1
A test_validate_type_empty_string() 0 7 2
A test_validate_type_neither_callable_nor_identifier() 0 7 2
A test_elements_yields_correct_count() 0 16 2
A test_validate_type_special_characters() 0 7 2
A test_elements_element_type_not_callable_invalid() 0 8 2
A test_validate_type_callable_string() 0 13 1
A test_validate_type_identifier_and_callable() 0 11 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
    # Arrange
27
    first_model = graphinate.model(name='First Model')
28
    second_model = graphinate.model(name='Second Model')
29
30
    # Act
31
    actual_model = first_model + second_model
32
33
    # Assert
34
    assert actual_model.name == 'First Model + Second Model'
35
36
37
def test_graph_model_validate_node_parameters():
38
    # Arrange
39
    graph_model = graphinate.model(name='Graph with invalid node supplier')
40
41
    # Act & Assert
42
    with pytest.raises(graphinate.modeling.GraphModelError):
43
        @graph_model.node()
44
        def invalid_node_supplier(wrong_parameter=None):
45
            yield 1
46
47
48
def test_validate_type_identifier_and_callable():
49
    # Arrange
50
    class CallableStr(str):
51
        def __call__(self):
52
            return "called"
53
54
    node_type = CallableStr("valididentifier")
55
56
    # Act & Assert
57
    # Should not raise
58
    GraphModel._validate_type(node_type)
59
60
61
def test_validate_type_non_callable_valid_identifier():
62
    # Arrange
63
    node_type = "valid_identifier"
64
65
    # Act & Assert
66
    # Should not raise
67
    GraphModel._validate_type(node_type)
68
69
70
def test_validate_type_non_string_callable():
71
    # Arrange
72
    class DummyCallable:
73
        def __call__(self):
74
            return "called"
75
76
    node_type = DummyCallable()
77
78
    # Act & Assert
79
    # Should not raise
80
    GraphModel._validate_type(node_type)
81
82
83
def test_validate_type_neither_callable_nor_identifier():
84
    # Arrange
85
    node_type = "123 invalid!"
86
87
    # Act & Assert
88
    with pytest.raises(ValueError, match="Invalid Type:"):
89
        GraphModel._validate_type(node_type)
90
91
92
def test_validate_type_empty_string():
93
    # Arrange
94
    node_type = ""
95
96
    # Act & Assert
97
    with pytest.raises(ValueError, match="Invalid Type:"):
98
        GraphModel._validate_type(node_type)
99
100
101
def test_validate_type_special_characters():
102
    # Arrange
103
    node_type = "invalid-type!"
104
105
    # Act & Assert
106
    with pytest.raises(ValueError, match="Invalid Type:"):
107
        GraphModel._validate_type(node_type)
108
109
110
def test_validate_type_accepts_valid_identifier():
111
    # Arrange
112
    node_type = "node_type"
113
114
    # Act & Assert
115
    # Should not raise
116
    GraphModel._validate_type(node_type)
117
118
119
def test_validate_type_accepts_callable():
120
    # Arrange
121
    def some_callable():
122
        pass
123
124
    # Act & Assert
125
    # Should not raise
126
    GraphModel._validate_type(some_callable)
127
128
129
def test_validate_type_accepts_identifier_and_callable():
130
    # Arrange
131
    class CallableStr(str):
132
        def __call__(self):
133
            return "called"
134
135
    node_type = CallableStr("anotheridentifier")
136
137
    # Act & Assert
138
    # Should not raise
139
    GraphModel._validate_type(node_type)
140
141
142
def test_validate_type_raises_on_invalid_identifier():
143
    # Arrange
144
    node_type = "not valid!"
145
146
    # Act & Assert
147
    with pytest.raises(ValueError, match="Invalid Type:"):
148
        GraphModel._validate_type(node_type)
149
150
151
def test_validate_type_raises_on_empty_string():
152
    # Arrange
153
    node_type = ""
154
155
    # Act & Assert
156
    with pytest.raises(ValueError, match="Invalid Type:"):
157
        GraphModel._validate_type(node_type)
158
159
160
def test_validate_type_callable_string():
161
    # Arrange
162
    # In Python, a string is never callable, but for the sake of the test, let's simulate
163
    # a string subclass that is callable
164
    class CallableStr(str):
165
        def __call__(self):
166
            return "called"
167
168
    node_type = CallableStr("not_an_identifier_but_callable")
169
170
    # Act & Assert
171
    # Should not raise
172
    GraphModel._validate_type(node_type)
173
174
175
def test_elements_yields_correct_count():
176
    # Arrange
177
    data = [
178
        {"id": 1, "name": "A"},
179
        {"id": 2, "name": "B"},
180
        {"id": 3, "name": "C"},
181
    ]
182
183
    # Act
184
    result = list(elements(data, element_type="Node", id="id", name="name"))
185
186
    # Assert
187
    assert len(result) == len(data)
188
    for i, el in enumerate(result):
189
        assert el.id == data[i]["id"]
190
        assert el.name == data[i]["name"]
191
192
193
def test_elements_multiple_getters():
194
    # Arrange
195
    data = [
196
        {"id": 1, "name": "A", "value": 10},
197
        {"id": 2, "name": "B", "value": 20},
198
    ]
199
200
    # Act
201
    result = list(elements(data, element_type="Node", id="id", name="name", value="value"))
202
203
    # Assert
204
    assert len(result) == 2
205
    for i, el in enumerate(result):
206
        assert el.id == data[i]["id"]
207
        assert el.name == data[i]["name"]
208
        assert el.value == data[i]["value"]
209
210
211
def test_elements_empty_iterable():
212
    # Arrange
213
    data = []
214
215
    # Act
216
    result = list(elements(data, element_type="Node", id="id"))
217
218
    # Assert
219
    assert result == []
220
221
222
def test_elements_element_type_not_callable_invalid():
223
    # Arrange
224
    data = [{"id": 1}]
225
226
    # Act & Assert
227
    # element_type is not callable and not a valid identifier
228
    with pytest.raises(ValueError, match="Invalid Type:"):
229
        list(elements(data, element_type="123 invalid!", id="id"))
230
231
232
def test_elements_callable_element_type():
233
    # Arrange
234
    data = [
235
        {"type": "Alpha", "id": 1},
236
        {"type": "Beta", "id": 2},
237
    ]
238
239
    def type_extractor(item):
240
        return item["type"]
241
242
    # Act
243
    result = list(elements(data, element_type=type_extractor, id="id"))
244
245
    # Assert
246
    assert result[0].__class__.__name__ == "Alpha"
247
    assert result[1].__class__.__name__ == "Beta"
248
    assert result[0].id == 1
249
    assert result[1].id == 2
250
251
252
def test_elements_invalid_identifier_type():
253
    # Arrange
254
    data = [{"id": 1}]
255
256
    def bad_type(item):
257
        return "not valid!"
258
259
    # Act & Assert
260
    with pytest.raises(ValueError, match="Invalid Type:"):
261
        list(elements(data, element_type=bad_type, id="id"))
262