source: orange/Orange/OrangeCanvas/scheme/tests/test_scheme.py @ 11111:8b86d1ff9919

Revision 11111:8b86d1ff9919, 3.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Fixed scheme's annotation signal signatures and added tests.

Line 
1"""
2Tests for Scheme
3"""
4
5from ...gui import test
6from ...registry.tests import small_testing_registry
7
8from .. import Scheme, SchemeNode, SchemeLink, \
9               SchemeTextAnnotation, SchemeArrowAnnotation, \
10               SchemeTopologyError, IncompatibleChannelTypeError
11
12
13class TestScheme(test.QCoreAppTestCase):
14    def test_scheme(self):
15        import Orange
16        reg = small_testing_registry()
17        base = "Orange.OrangeWidgets"
18        file_desc = reg.widget(base + ".Data.OWFile.OWFile")
19        discretize_desc = reg.widget(base + ".Data.OWDiscretize.OWDiscretize")
20        bayes_desc = reg.widget(base + ".Classify.OWNaiveBayes.OWNaiveBayes")
21        # Create the scheme
22        scheme = Scheme()
23
24        self.assertEqual(scheme.title, "untitled")
25        self.assertEqual(scheme.description, "")
26        self.assertEqual(scheme.path, "")
27
28        nodes_added = []
29        links_added = []
30        annotations_added = []
31
32        scheme.node_added.connect(nodes_added.append)
33        scheme.node_removed.connect(nodes_added.remove)
34
35        scheme.link_added.connect(links_added.append)
36        scheme.link_removed.connect(links_added.remove)
37
38        scheme.annotation_added.connect(annotations_added.append)
39        scheme.annotation_removed.connect(annotations_added.remove)
40
41        w1 = scheme.new_node(file_desc)
42        self.assertTrue(len(nodes_added) == 1)
43        self.assertTrue(isinstance(nodes_added[-1], SchemeNode))
44        self.assertTrue(nodes_added[-1] is w1)
45
46        w2 = scheme.new_node(discretize_desc)
47        self.assertTrue(len(nodes_added) == 2)
48        self.assertTrue(isinstance(nodes_added[-1], SchemeNode))
49        self.assertTrue(nodes_added[-1] is w2)
50
51        w3 = scheme.new_node(bayes_desc)
52        self.assertTrue(len(nodes_added) == 3)
53        self.assertTrue(isinstance(nodes_added[-1], SchemeNode))
54        self.assertTrue(nodes_added[-1] is w3)
55
56        self.assertTrue(len(links_added) == 0)
57        l1 = SchemeLink(w1, "Data", w2, "Data")
58        scheme.add_link(l1)
59        self.assertTrue(len(links_added) == 1)
60        self.assertTrue(isinstance(links_added[-1], SchemeLink))
61        self.assertTrue(links_added[-1] is l1)
62
63        l2 = SchemeLink(w1, "Data", w3, "Data")
64        scheme.add_link(l2)
65        self.assertTrue(len(links_added) == 2)
66        self.assertTrue(isinstance(links_added[-1], SchemeLink))
67        self.assertTrue(links_added[-1] is l2)
68
69        # Test find_links.
70        found = scheme.find_links(w1, None, w2, None)
71        self.assertSequenceEqual(found, [l1])
72        found = scheme.find_links(None, None, w3, None)
73        self.assertSequenceEqual(found, [l2])
74
75        scheme.remove_link(l2)
76        self.assertTrue(l2 not in links_added)
77
78        # Add a link to itself.
79        self.assertRaises(SchemeTopologyError, scheme.new_link,
80                          w2, "Data", w2, "Data")
81
82        # Add an link with incompatible types
83        self.assertRaises(IncompatibleChannelTypeError,
84                          scheme.new_link, w3, "Learner", w2, "Data")
85
86        # Add a link to a node with no input channels
87        self.assertRaises(ValueError, scheme.new_link,
88                          w2, "Data", w1, "Data")
89
90        text_annot = SchemeTextAnnotation((0, 0, 100, 20), "Text")
91        scheme.add_annotation(text_annot)
92        self.assertSequenceEqual(annotations_added, [text_annot])
93        self.assertSequenceEqual(scheme.annotations, annotations_added)
94
95        arrow_annot = SchemeTextAnnotation((0, 100), (100, 100))
96        scheme.add_annotation(arrow_annot)
97        self.assertSequenceEqual(annotations_added, [text_annot, arrow_annot])
98        self.assertSequenceEqual(scheme.annotations, annotations_added)
99
100        scheme.remove_annotation(text_annot)
101        self.assertSequenceEqual(annotations_added, [arrow_annot])
102        self.assertSequenceEqual(scheme.annotations, annotations_added)
103
Note: See TracBrowser for help on using the repository browser.