/
test_graph.py
60 lines (50 loc) · 1.6 KB
/
test_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
import os
import subprocess
import shutil
import tempfile
from chempy import Reaction, ReactionSystem, Substance
from ..graph import rsys2dot, rsys2graph
from ..testing import requires, skipif
try:
dot_missing = subprocess.run(["dot", "-?"]).returncode != 0
except OSError:
dot_missing = True
def _get_rsys():
r1 = Reaction({"A": 2}, {"B": 1}, param=3.0)
A = Substance("A", latex_name="\\boldsymbol{A}")
B = Substance("B", latex_name="\\boldsymbol{B}")
rsys = ReactionSystem([r1], [A, B])
return rsys
@requires("numpy")
@skipif(dot_missing, reason="graphviz not installed? (dot command missing)")
def test_rsys2dot():
rsys = _get_rsys()
assert list(map(str.strip, rsys2dot(rsys))) == [
'digraph "None" {',
'"A" [fontcolor=maroon label="A"];',
'"B" [fontcolor=darkgreen label="B"];',
"{",
'node [label="r1",shape=diamond]',
"r1",
"}",
'"A" -> "r1" [color=maroon,fontcolor=maroon,label="2"];',
'"r1" -> "B" [color=darkgreen,fontcolor=darkgreen,label=""];',
"}",
]
@requires("numpy")
@skipif(dot_missing, reason="graphviz not installed? (dot command missing)")
def test_rsys2graph():
rsys = _get_rsys()
tempdir = tempfile.mkdtemp()
try:
rsys2graph(rsys, os.path.join(tempdir, "out.png"))
rsys2graph(rsys, os.path.join(tempdir, "out.ps"))
try:
subprocess.call(["dot2tex", "-v"])
except Exception:
pass
else:
rsys2graph(rsys, os.path.join(tempdir, "out.tex"))
finally:
shutil.rmtree(tempdir)