|
1
|
|
|
import dataclasses |
|
2
|
|
|
import sys |
|
3
|
|
|
|
|
4
|
|
|
import pytest |
|
5
|
|
|
|
|
6
|
|
|
import graphinate.tools.mutators |
|
7
|
|
|
from graphinate.tools.importer import ImportFromStringError, import_from_string |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
@dataclasses.dataclass |
|
11
|
|
|
class Data: |
|
12
|
|
|
a: int |
|
13
|
|
|
b: str |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
data = Data(1, 'alpha') |
|
17
|
|
|
|
|
18
|
|
|
cases = [ |
|
19
|
|
|
([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]), |
|
20
|
|
|
({'a': 1, 'b': 2}, {'a': 1, 'b': 2}), |
|
21
|
|
|
((1, 2, 3, 4, 'a'), [1, 2, 3, 4, 'a']), |
|
22
|
|
|
(data, {'a': 1, 'b': 'alpha'}) |
|
23
|
|
|
] |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
@pytest.mark.parametrize(('case', 'expected'), cases) |
|
27
|
|
|
def test_dictify(case, expected): |
|
28
|
|
|
actual = graphinate.tools.mutators.dictify(case) |
|
29
|
|
|
assert actual == expected |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
value_as_str_cases = [ |
|
33
|
|
|
([0.5, 0.5, 0.5], ['0.5', '0.5', '0.5']), |
|
34
|
|
|
({'a': 1, 'b': 2}, {'a': '1', 'b': '2'}), |
|
35
|
|
|
((1, 2, 3, 4, 'a'), ['1', '2', '3', '4', 'a']), |
|
36
|
|
|
(data, {'a': '1', 'b': 'alpha'}) |
|
37
|
|
|
] |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
@pytest.mark.parametrize(('case', 'expected'), value_as_str_cases) |
|
41
|
|
|
def test_dictify__value_to_str(case, expected): |
|
42
|
|
|
actual = graphinate.tools.mutators.dictify(case, value_converter=str) |
|
43
|
|
|
assert actual == expected |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_import_from_string(monkeypatch): |
|
47
|
|
|
sys.path.append('examples/math') |
|
48
|
|
|
actual = import_from_string("polygonal_graph:model") |
|
49
|
|
|
assert isinstance(actual, graphinate.GraphModel) |
|
50
|
|
|
assert actual.name == "Octagonal Graph" |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
import_from_string_error_cases = [ |
|
54
|
|
|
("does_not_exist:model", "Could not import module 'does_not_exist'."), |
|
55
|
|
|
("polygonal_graph:does_not_exist", "Attribute 'does_not_exist' not found in module 'polygonal_graph'."), |
|
56
|
|
|
("wrong_format", "Import string 'wrong_format' must be in format '<module>:<attribute>'.") |
|
57
|
|
|
] |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
@pytest.mark.parametrize(('case', 'message'), import_from_string_error_cases) |
|
61
|
|
|
def test_import_from_string__error(case, message): |
|
62
|
|
|
sys.path.append('examples/math') |
|
63
|
|
|
with pytest.raises(ImportFromStringError, match=message): |
|
64
|
|
|
_ = import_from_string(case) |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
import_from_string_not_str_cases = [ |
|
68
|
|
|
0, |
|
69
|
|
|
None |
|
70
|
|
|
] |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
@pytest.mark.parametrize('case', import_from_string_not_str_cases) |
|
74
|
|
|
def test_import_from_string__not_str(case): |
|
75
|
|
|
actual = import_from_string(case) |
|
76
|
|
|
assert actual == case |
|
77
|
|
|
|