source: orange/Orange/OrangeCanvas/scheme/tests/test_readwrite.py @ 11391:6b2507ba9677

Revision 11391:6b2507ba9677, 4.0 KB checked in by Ales Erjavec <ales.erjavec@…>, 13 months ago (diff)

When possible, node properties are now saved as python literal strings.

Line 
1"""Test read write
2"""
3from ...gui import test
4from ...registry import global_registry
5
6from .. import Scheme, SchemeNode, SchemeLink, \
7               SchemeArrowAnnotation, SchemeTextAnnotation
8
9from .. import readwrite
10from ..readwrite import scheme_to_ows_stream, parse_scheme
11
12
13class TestReadWrite(test.QAppTestCase):
14    def test_io(self):
15        from StringIO import StringIO
16        reg = global_registry()
17
18        base = "Orange.OrangeWidgets"
19        file_desc = reg.widget(base + ".Data.OWFile.OWFile")
20        discretize_desc = reg.widget(base + ".Data.OWDiscretize.OWDiscretize")
21        bayes_desc = reg.widget(base + ".Classify.OWNaiveBayes.OWNaiveBayes")
22
23        scheme = Scheme()
24        file_node = SchemeNode(file_desc)
25        discretize_node = SchemeNode(discretize_desc)
26        bayes_node = SchemeNode(bayes_desc)
27
28        scheme.add_node(file_node)
29        scheme.add_node(discretize_node)
30        scheme.add_node(bayes_node)
31
32        scheme.add_link(SchemeLink(file_node, "Data",
33                                   discretize_node, "Data"))
34
35        scheme.add_link(SchemeLink(discretize_node, "Data",
36                                   bayes_node, "Data"))
37
38        scheme.add_annotation(SchemeArrowAnnotation((0, 0), (10, 10)))
39        scheme.add_annotation(SchemeTextAnnotation((0, 100, 200, 200), "$$"))
40
41        stream = StringIO()
42        scheme_to_ows_stream(scheme, stream)
43
44        stream.seek(0)
45
46        scheme_1 = parse_scheme(Scheme(), stream)
47
48        self.assertTrue(len(scheme.nodes) == len(scheme_1.nodes))
49        self.assertTrue(len(scheme.links) == len(scheme_1.links))
50        self.assertTrue(len(scheme.annotations) == len(scheme_1.annotations))
51
52        for n1, n2 in zip(scheme.nodes, scheme_1.nodes):
53            self.assertEqual(n1.position, n2.position)
54            self.assertEqual(n1.title, n2.title)
55
56        for link1, link2 in zip(scheme.links, scheme_1.links):
57            self.assertEqual(link1.source_type(), link2.source_type())
58            self.assertEqual(link1.sink_type(), link2.sink_type())
59
60            self.assertEqual(link1.source_channel.name,
61                             link2.source_channel.name)
62
63            self.assertEqual(link1.sink_channel.name,
64                             link2.sink_channel.name)
65
66            self.assertEqual(link1.enabled, link2.enabled)
67
68        for annot1, annot2 in zip(scheme.annotations, scheme_1.annotations):
69            self.assertIs(type(annot1), type(annot2))
70            if isinstance(annot1, SchemeTextAnnotation):
71                self.assertEqual(annot1.text, annot2.text)
72                self.assertEqual(annot1.rect, annot2.rect)
73            else:
74                self.assertEqual(annot1.start_pos, annot2.start_pos)
75                self.assertEqual(annot1.end_pos, annot2.end_pos)
76
77    def test_safe_evals(self):
78        s = readwrite.string_eval(r"'\x00\xff'")
79        self.assertEquals(s, chr(0) + chr(255))
80
81        with self.assertRaises(ValueError):
82            readwrite.string_eval("[1, 2]")
83
84        t = readwrite.tuple_eval("(1, 2.0, 'a')")
85        self.assertEqual(t, (1, 2.0, 'a'))
86
87        with self.assertRaises(ValueError):
88            readwrite.tuple_eval("u'string'")
89
90        with self.assertRaises(ValueError):
91            readwrite.tuple_eval("(1, [1, [2, ]])")
92
93        self.assertIs(readwrite.terminal_eval("True"), True)
94        self.assertIs(readwrite.terminal_eval("False"), False)
95        self.assertIs(readwrite.terminal_eval("None"), None)
96
97        self.assertEqual(readwrite.terminal_eval("42"), 42)
98        self.assertEqual(readwrite.terminal_eval("'42'"), '42')
99
100    def test_literal_dump(self):
101        struct = {1: [{(1, 2): ""}],
102                  True: 1.0,
103                  None: None}
104
105        s = readwrite.literal_dumps(struct)
106        self.assertEqual(readwrite.literal_loads(s), struct)
107
108        with self.assertRaises(ValueError):
109            recur = [1]
110            recur.append(recur)
111            readwrite.literal_dumps(recur)
112
113        with self.assertRaises(TypeError):
114            readwrite.literal_dumps(self)
Note: See TracBrowser for help on using the repository browser.