Passed
Push — main ( 2bcd5c...05fcff )
by Eran
01:27
created

math._test_materializers.valid_materialization()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import functools
2
3
import pytest
4
from matplotlib import pyplot as plt
5
6
import examples.math.materializers
7
import graphinate
8
9
10
def test_materialize(map_graph_model, capsys):
11
    # Arrange
12
    expected_snippet = '"graph": {\n    "name": "Map",'
13
    *_, graph_model = map_graph_model
14
    builder, handler = examples.math.materializers.Materializers.D3Graph.value
15
16
    # Act
17
    examples.math.materializers.materialize(graph_model, builder=builder, builder_output_handler=handler)
18
    captured = capsys.readouterr()
19
20
    # Assert
21
    assert expected_snippet in captured.out
22
    assert captured.err == ""
23
24
25
def test_materialize_d3graph(map_graph_model, monkeypatch, capsys):
26
    # Arrange
27
    monkeypatch.setattr(plt, 'show', lambda: None)
28
    *_, graph_model = map_graph_model
29
    builder, handler = examples.math.materializers.Materializers.D3Graph.value
30
31
    expected_snippet = '"graph": {\n    "name": "Map",'
32
33
    # Act
34
    examples.math.materializers.materialize(graph_model, builder=builder, builder_output_handler=handler)
35
    captured = capsys.readouterr()
36
37
    # Assert
38
    assert expected_snippet in captured.out
39
    assert captured.err == ""
40
41
42
def valid_materialization(*args, **kwargs) -> bool:
43
    examples.math.materializers.materialize(*args, **kwargs)
44
    return True
45
46
47
def test_materialize_graphql(map_graph_model, monkeypatch):
48
    with monkeypatch.context():
49
        # Arrange
50
        import uvicorn
51
        monkeypatch.setattr(uvicorn, "run", lambda *args, **kwargs: None)
52
        *_, graph_model = map_graph_model
53
        builder, handler = examples.math.materializers.Materializers.GraphQL.value
54
55
        # Act & Assert
56
        assert valid_materialization(graph_model, builder=builder, builder_output_handler=handler)
57
58
59
networkx_materializers = [
60
    examples.math.materializers.Materializers.NetworkX.value,
61
    examples.math.materializers.Materializers.NetworkX_with_edge_labels.value,
62
    (graphinate.builders.NetworkxBuilder,
63
     functools.partial(graphinate.materializers.matplotlib.plot, with_node_labels=False))
64
]
65
66
67
@pytest.mark.parametrize('materializer', networkx_materializers)
68
def test_materialize_networkx(map_graph_model, materializer, monkeypatch):
69
    with monkeypatch.context():
70
        monkeypatch.setattr(plt, 'show', lambda: None)
71
72
        # Arrange
73
        *_, graph_model = map_graph_model
74
        builder, handler = materializer
75
76
        # Act & Assert
77
        assert valid_materialization(graph_model, builder=builder, builder_output_handler=handler)
78
79
80
def test_materialize_none(map_graph_model, monkeypatch):
81
    # Arrange
82
    import uvicorn
83
    monkeypatch.setattr(uvicorn, "run", lambda *args, **kwargs: None)
84
    *_, graph_model = map_graph_model
85
86
    # Act & Assert
87
    with pytest.raises(ValueError, match="Missing: builder, builder_output_handler"):
88
        examples.math.materializers.materialize(graph_model, builder=None, builder_output_handler=None)
89