Passed
Push — main ( da19e4...14c1e8 )
by Eran
01:21
created

test_cli.test_import_from_string()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import sys
2
3
import graphinate
4
import pytest
5
from click.testing import CliRunner
6
from graphinate.cli import ImportFromStringError, cli, import_from_string
7
8
9
@pytest.fixture()
10
def runner():
11
    return CliRunner()
12
13
14
def test_save_model(octagonal_graph_model, runner):
15
    with runner.isolated_filesystem():
16
        result = runner.invoke(cli, ['save', '-m', octagonal_graph_model])
17
        assert result.exit_code == 0
18
19
20
def test_save_model_reference(runner):
21
    sys.path.append('examples/math')
22
    result = runner.invoke(cli, ['save', '-m', "polygonal_graph:model"])
23
    assert result.exit_code == 0
24
25
26
def test_save_malformed_model_reference(runner):
27
    with runner.isolated_filesystem():
28
        result = runner.invoke(cli, ['save', '-m', "malformed_model_reference"])
29
30
    assert result.exit_code == 2
31
32
33
def test_import_from_string():
34
    sys.path.append('examples/math')
35
    actual = import_from_string("polygonal_graph:model")
36
    assert isinstance(actual, graphinate.GraphModel)
37
    assert actual.name == "Octagonal Graph"
38
39
40
import_from_string_error_cases = [
41
    ("does_not_exist:model", "Could not import module 'does_not_exist'."),
42
    ("polygonal_graph:does_not_exist", "Attribute 'does_not_exist' not found in module 'polygonal_graph'."),
43
    ("wrong_format", "Import string 'wrong_format' must be in format '<module>:<attribute>'.")
44
]
45
46
47
@pytest.mark.parametrize(('case', 'message'), import_from_string_error_cases)
48
def test_import_from_string__error(case, message):
49
    sys.path.append('examples/math')
50
    with pytest.raises(ImportFromStringError, match=message):
51
        _ = import_from_string(case)
52
53
54
import_from_string_not_str_cases = [
55
    0,
56
    None
57
]
58
59
60
@pytest.mark.parametrize('case', import_from_string_not_str_cases)
61
def test_import_from_string__not_str(case):
62
    actual = import_from_string(case)
63
    assert actual == case
64