source: orange/Orange/OrangeCanvas/scheme/tests/test_scheme.py @ 11210:bb3860029714

Revision 11210:bb3860029714, 3.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Fixed some errors caught by failing uniitests.

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