|
1
|
|
|
import sys |
|
2
|
|
|
from pathlib import Path |
|
3
|
|
|
|
|
4
|
|
|
import pytest |
|
5
|
|
|
from click.testing import CliRunner |
|
6
|
|
|
|
|
7
|
|
|
import graphinate |
|
8
|
|
|
from graphinate.cli import ImportFromStringError, cli, import_from_string |
|
9
|
|
|
|
|
10
|
|
|
EXAMPLES_MATH = 'examples/math' |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
@pytest.fixture |
|
14
|
|
|
def runner(): |
|
15
|
|
|
return CliRunner() |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def test_save_model(octagonal_graph_model, runner): |
|
19
|
|
|
with runner.isolated_filesystem(): |
|
20
|
|
|
# Act |
|
21
|
|
|
result = runner.invoke(cli, ['save', '-m', octagonal_graph_model]) |
|
22
|
|
|
|
|
23
|
|
|
# Assert |
|
24
|
|
|
assert result.exit_code == 0 |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def test_save_model_reference(runner): |
|
28
|
|
|
# Arrange |
|
29
|
|
|
sys.path.append(str(Path(EXAMPLES_MATH).resolve())) |
|
30
|
|
|
|
|
31
|
|
|
with runner.isolated_filesystem(): |
|
32
|
|
|
# Act |
|
33
|
|
|
result = runner.invoke(cli, ['save', '-m', "polygonal_graph:model"]) |
|
34
|
|
|
|
|
35
|
|
|
# Assert |
|
36
|
|
|
assert result.exit_code == 0 |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def test_save_malformed_model_reference(runner): |
|
40
|
|
|
with runner.isolated_filesystem(): |
|
41
|
|
|
# Act |
|
42
|
|
|
result = runner.invoke(cli, ['save', '-m', "malformed_model_reference"]) |
|
43
|
|
|
|
|
44
|
|
|
# Assert |
|
45
|
|
|
assert result.exit_code == 2 |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
def test_import_from_string(): |
|
49
|
|
|
# Arrange |
|
50
|
|
|
sys.path.append(EXAMPLES_MATH) |
|
51
|
|
|
|
|
52
|
|
|
# Act |
|
53
|
|
|
actual = import_from_string("polygonal_graph:model") |
|
54
|
|
|
|
|
55
|
|
|
# Assert |
|
56
|
|
|
assert isinstance(actual, graphinate.GraphModel) |
|
57
|
|
|
assert actual.name == "Octagonal Graph" |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
import_from_string_error_cases = [ |
|
61
|
|
|
("does_not_exist:model", "Could not import module 'does_not_exist'."), |
|
62
|
|
|
("polygonal_graph:does_not_exist", |
|
63
|
|
|
"Attribute 'does_not_exist' not found in import string reference 'polygonal_graph:does_not_exist'."), |
|
64
|
|
|
("wrong_format", "Import string 'wrong_format' must be in format '<module>:<attribute>'.") |
|
65
|
|
|
] |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
@pytest.mark.parametrize(('case', 'message'), import_from_string_error_cases) |
|
69
|
|
|
def test_import_from_string__error(case, message): |
|
70
|
|
|
# Arrange |
|
71
|
|
|
sys.path.append(EXAMPLES_MATH) |
|
72
|
|
|
|
|
73
|
|
|
# Act & Assert |
|
74
|
|
|
with pytest.raises(ImportFromStringError, match=message): |
|
75
|
|
|
_ = import_from_string(case) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
import_from_string_not_str_cases = [ |
|
79
|
|
|
0, |
|
80
|
|
|
None |
|
81
|
|
|
] |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
@pytest.mark.parametrize('case', import_from_string_not_str_cases) |
|
85
|
|
|
def test_import_from_string__not_str(case): |
|
86
|
|
|
# Act & Assert |
|
87
|
|
|
with pytest.raises(ImportFromStringError, match=f"{case} is not a string"): |
|
88
|
|
|
_actual = import_from_string(case) |
|
89
|
|
|
|