summaryrefslogtreecommitdiffstats
path: root/python/altgraph/altgraph_tests/test_dot.py
blob: 83993dad542d3acad524a2b8c1b4e33396f992c7 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import unittest
import os

from altgraph import Dot
from altgraph import Graph
from altgraph import GraphError


class TestDot (unittest.TestCase):

    def test_constructor(self):
        g = Graph.Graph([
                (1,2),
                (1,3),
                (1,4),
                (2,4),
                (2,6),
                (2,7),
                (7,4),
                (6,1),
            ]
        )

        dot = Dot.Dot(g)

        self.assertEqual(dot.name, 'G')
        self.assertEqual(dot.attr, {})
        self.assertEqual(dot.temp_dot, 'tmp_dot.dot')
        self.assertEqual(dot.temp_neo, 'tmp_neo.dot')
        self.assertEqual(dot.dot, 'dot')
        self.assertEqual(dot.dotty, 'dotty')
        self.assertEqual(dot.neato, 'neato')
        self.assertEqual(dot.type, 'digraph')

        self.assertEqual(dot.nodes, dict([(x, {}) for x in g]))

        edges = {}
        for head in g:
            edges[head] = {}
            for tail in g.out_nbrs(head):
                edges[head][tail] = {}

        self.assertEqual(dot.edges[1], edges[1])
        self.assertEqual(dot.edges, edges)


        dot = Dot.Dot(g, nodes=[1,2],
                edgefn=lambda node: list(sorted(g.out_nbrs(node)))[:-1],
                nodevisitor=lambda node: {'label': node},
                edgevisitor=lambda head, tail: {'label': (head, tail) },
                name="testgraph",
                dot='/usr/local/bin/dot',
                dotty='/usr/local/bin/dotty',
                neato='/usr/local/bin/neato',
                graphtype="graph")

        self.assertEqual(dot.name, 'testgraph')
        self.assertEqual(dot.attr, {})
        self.assertEqual(dot.temp_dot, 'tmp_dot.dot')
        self.assertEqual(dot.temp_neo, 'tmp_neo.dot')
        self.assertEqual(dot.dot, '/usr/local/bin/dot')
        self.assertEqual(dot.dotty, '/usr/local/bin/dotty')
        self.assertEqual(dot.neato, '/usr/local/bin/neato')
        self.assertEqual(dot.type, 'graph')

        self.assertEqual(dot.nodes, dict([(x, {'label': x}) for x in [1,2]]))

        edges = {}
        for head in [1,2]:
            edges[head] = {}
            for tail in list(sorted(g.out_nbrs(head)))[:-1]:
                if tail not in [1,2]: continue
                edges[head][tail] = {'label': (head, tail) }

        self.assertEqual(dot.edges[1], edges[1])
        self.assertEqual(dot.edges, edges)

        self.assertRaises(GraphError, Dot.Dot, g, nodes=[1,2, 9])

    def test_style(self):
        g = Graph.Graph([])

        dot = Dot.Dot(g)

        self.assertEqual(dot.attr, {})

        dot.style(key='value')
        self.assertEqual(dot.attr, {'key': 'value'})

        dot.style(key2='value2')
        self.assertEqual(dot.attr, {'key2': 'value2'})

    def test_node_style(self):
        g = Graph.Graph([
                (1,2),
                (1,3),
                (1,4),
                (2,4),
                (2,6),
                (2,7),
                (7,4),
                (6,1),
            ]
        )

        dot = Dot.Dot(g)

        self.assertEqual(dot.nodes[1], {})

        dot.node_style(1, key='value')
        self.assertEqual(dot.nodes[1], {'key': 'value'})

        dot.node_style(1, key2='value2')
        self.assertEqual(dot.nodes[1], {'key2': 'value2'})
        self.assertEqual(dot.nodes[2], {})

        dot.all_node_style(key3='value3')
        for n in g:
            self.assertEqual(dot.nodes[n], {'key3': 'value3'})

        self.assertTrue(9 not in dot.nodes)
        dot.node_style(9, key='value')
        self.assertEqual(dot.nodes[9], {'key': 'value'})

    def test_edge_style(self):
        g = Graph.Graph([
                (1,2),
                (1,3),
                (1,4),
                (2,4),
                (2,6),
                (2,7),
                (7,4),
                (6,1),
            ]
        )

        dot = Dot.Dot(g)

        self.assertEqual(dot.edges[1][2], {})
        dot.edge_style(1,2, foo='bar')
        self.assertEqual(dot.edges[1][2], {'foo': 'bar'})

        dot.edge_style(1,2, foo2='2bar')
        self.assertEqual(dot.edges[1][2], {'foo2': '2bar'})

        self.assertEqual(dot.edges[1][3], {})

        self.assertFalse(6 in dot.edges[1])
        dot.edge_style(1,6, foo2='2bar')
        self.assertEqual(dot.edges[1][6], {'foo2': '2bar'})

        self.assertRaises(GraphError, dot.edge_style, 1, 9, a=1)
        self.assertRaises(GraphError, dot.edge_style, 9, 1, a=1)


    def test_iter(self):
        g = Graph.Graph([
                (1,2),
                (1,3),
                (1,4),
                (2,4),
                (2,6),
                (2,7),
                (7,4),
                (6,1),
            ]
        )

        dot = Dot.Dot(g)
        dot.style(graph="foobar")
        dot.node_style(1, key='value')
        dot.node_style(2, key='another', key2='world')
        dot.edge_style(1,4, key1='value1', key2='value2')
        dot.edge_style(2,4, key1='valueA')

        self.assertEqual(list(iter(dot)), list(dot.iterdot()))

        for item in dot.iterdot():
            self.assertTrue(isinstance(item, str))

        first = list(dot.iterdot())[0]
        self.assertEqual(first, "digraph %s {\n"%(dot.name,))

        dot.type = 'graph'
        first = list(dot.iterdot())[0]
        self.assertEqual(first, "graph %s {\n"%(dot.name,))

        dot.type = 'foo'
        self.assertRaises(GraphError, list, dot.iterdot())
        dot.type = 'digraph'

        self.assertEqual(list(dot), [
            'digraph G {\n',
              'graph="foobar";',
              '\n',

            '\t"1" [',
              'key="value",',
            '];\n',

            '\t"2" [',
              'key="another",',
              'key2="world",',
            '];\n',

            '\t"3" [',
            '];\n',

            '\t"4" [',
            '];\n',

            '\t"6" [',
            '];\n',

            '\t"7" [',
            '];\n',

            '\t"1" -> "2" [',
            '];\n',

            '\t"1" -> "3" [',
            '];\n',

            '\t"1" -> "4" [',
              'key1="value1",',
              'key2="value2",',
            '];\n',

             '\t"2" -> "4" [',
               'key1="valueA",',
             '];\n',

             '\t"2" -> "6" [',
             '];\n',

             '\t"2" -> "7" [',
             '];\n',

             '\t"6" -> "1" [',
             '];\n',

             '\t"7" -> "4" [',
             '];\n',
           '}\n'])


    def test_save(self):
        g = Graph.Graph([
                (1,2),
                (1,3),
                (1,4),
                (2,4),
                (2,6),
                (2,7),
                (7,4),
                (6,1),
            ]
        )

        dot = Dot.Dot(g)
        dot.style(graph="foobar")
        dot.node_style(1, key='value')
        dot.node_style(2, key='another', key2='world')
        dot.edge_style(1,4, key1='value1', key2='value2')
        dot.edge_style(2,4, key1='valueA')

        fn = 'test_dot.dot'
        self.assertTrue(not os.path.exists(fn))

        try:
            dot.save_dot(fn)

            fp = open(fn, 'r')
            data = fp.read()
            fp.close()
            self.assertEqual(data, ''.join(dot))

        finally:
            if os.path.exists(fn):
                os.unlink(fn)


    def test_img(self):
        g = Graph.Graph([
                (1,2),
                (1,3),
                (1,4),
                (2,4),
                (2,6),
                (2,7),
                (7,4),
                (6,1),
            ]
        )

        dot = Dot.Dot(g, dot='/usr/local/bin/!!dot', dotty='/usr/local/bin/!!dotty', neato='/usr/local/bin/!!neato')
        dot.style(size='10,10', rankdir='RL', page='5, 5' , ranksep=0.75)
        dot.node_style(1, label='BASE_NODE',shape='box', color='blue')
        dot.node_style(2, style='filled', fillcolor='red')
        dot.edge_style(1,4, style='dotted')
        dot.edge_style(2,4, arrowhead='dot', label='binds', labelangle='90')

        system_cmds = []
        def fake_system(cmd):
            system_cmds.append(cmd)
            return None

        try:
            real_system = os.system
            os.system = fake_system

            system_cmds = []
            dot.save_img('foo')
            self.assertEqual(system_cmds, ['/usr/local/bin/!!dot -Tgif tmp_dot.dot -o foo.gif'])

            system_cmds = []
            dot.save_img('foo', file_type='jpg')
            self.assertEqual(system_cmds, ['/usr/local/bin/!!dot -Tjpg tmp_dot.dot -o foo.jpg'])

            system_cmds = []
            dot.save_img('bar', file_type='jpg', mode='neato')
            self.assertEqual(system_cmds, [
                '/usr/local/bin/!!neato -o tmp_dot.dot tmp_neo.dot',
                '/usr/local/bin/!!dot -Tjpg tmp_dot.dot -o bar.jpg',
            ])

            system_cmds = []
            dot.display()
            self.assertEqual(system_cmds, [
                '/usr/local/bin/!!dotty tmp_dot.dot'
            ])

            system_cmds = []
            dot.display(mode='neato')
            self.assertEqual(system_cmds, [
                '/usr/local/bin/!!neato -o tmp_dot.dot tmp_neo.dot',
                '/usr/local/bin/!!dotty tmp_dot.dot'
            ])

        finally:
            if os.path.exists(dot.temp_dot):
                os.unlink(dot.temp_dot)
            if os.path.exists(dot.temp_neo):
                os.unlink(dot.temp_neo)
            os.system = real_system

        if os.path.exists('/usr/local/bin/dot') and os.path.exists('/usr/local/bin/neato'):
            try:
                dot.dot='/usr/local/bin/dot'
                dot.neato='/usr/local/bin/neato'
                self.assertFalse(os.path.exists('foo.gif'))
                dot.save_img('foo')
                self.assertTrue(os.path.exists('foo.gif'))
                os.unlink('foo.gif')

                self.assertFalse(os.path.exists('foo.gif'))
                dot.save_img('foo', mode='neato')
                self.assertTrue(os.path.exists('foo.gif'))
                os.unlink('foo.gif')

            finally:
                if os.path.exists(dot.temp_dot):
                    os.unlink(dot.temp_dot)
                if os.path.exists(dot.temp_neo):
                    os.unlink(dot.temp_neo)


if __name__ == "__main__": # pragma: no cover
    unittest.main()