Passed
Push — main ( 64e16f...631b22 )
by Eran
01:33
created

test_cli.test_save_model_reference()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
        result = runner.invoke(cli, ['save', '-m', octagonal_graph_model])
21
        assert result.exit_code == 0
22
23
24
def test_save_model_reference(runner):
25
    # Arrange
26
    sys.path.append(EXAMPLES_MATH)
27
    if (saved_file := Path('Octagonal Graph.d3_graph.json')).exists():
28
        saved_file.unlink()
29
30
    # Act
31
    result = runner.invoke(cli, ['save', '-m', "polygonal_graph:model"])
32
33
    # Assert
34
    assert result.exit_code == 0
35
36
37
def test_save_malformed_model_reference(runner):
38
    with runner.isolated_filesystem():
39
        result = runner.invoke(cli, ['save', '-m', "malformed_model_reference"])
40
41
    assert result.exit_code == 2
42
43
44
def test_import_from_string():
45
    sys.path.append(EXAMPLES_MATH)
46
    actual = import_from_string("polygonal_graph:model")
47
    assert isinstance(actual, graphinate.GraphModel)
48
    assert actual.name == "Octagonal Graph"
49
50
51
import_from_string_error_cases = [
52
    ("does_not_exist:model", "Could not import module 'does_not_exist'."),
53
    ("polygonal_graph:does_not_exist", "Attribute 'does_not_exist' not found in module 'polygonal_graph'."),
54
    ("wrong_format", "Import string 'wrong_format' must be in format '<module>:<attribute>'.")
55
]
56
57
58
@pytest.mark.parametrize(('case', 'message'), import_from_string_error_cases)
59
def test_import_from_string__error(case, message):
60
    sys.path.append(EXAMPLES_MATH)
61
    with pytest.raises(ImportFromStringError, match=message):
62
        _ = import_from_string(case)
63
64
65
import_from_string_not_str_cases = [
66
    0,
67
    None
68
]
69
70
71
@pytest.mark.parametrize('case', import_from_string_not_str_cases)
72
def test_import_from_string__not_str(case):
73
    actual = import_from_string(case)
74
    assert actual == case
75