source: orange/Orange/OrangeCanvas/registry/tests/test_base.py @ 11157:047fa908edbc

Revision 11157:047fa908edbc, 3.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Fixed widget ordering by priority, added tests.

Line 
1"""
2Test WidgetRegistry.
3"""
4
5import logging
6from operator import attrgetter
7
8import unittest
9
10from ..base import WidgetRegistry
11from .. import description
12
13
14class TestRegistry(unittest.TestCase):
15    def setUp(self):
16        logging.basicConfig()
17
18    def test_registry_const(self):
19        reg = WidgetRegistry()
20
21        data_desc = description.CategoryDescription.from_package(
22            "Orange.OrangeWidgets.Data"
23        )
24
25        reg.register_category(data_desc)
26
27        self.assertTrue(reg.has_category(data_desc.name))
28        self.assertSequenceEqual(reg.categories(), [data_desc])
29        self.assertIs(reg.category(data_desc.name), data_desc)
30
31        file_desc = description.WidgetDescription.from_module(
32            "Orange.OrangeWidgets.Data.OWFile"
33        )
34
35        reg.register_widget(file_desc)
36
37        self.assertTrue(reg.has_widget(file_desc.qualified_name))
38        self.assertSequenceEqual(reg.widgets("Data"), [file_desc])
39        self.assertIs(reg.widget(file_desc.qualified_name), file_desc)
40
41        # ValueError adding a description with the same qualified name
42        with self.assertRaises(ValueError):
43            desc = description.WidgetDescription(
44                name="A name", qualified_name=file_desc.qualified_name
45            )
46            reg.register_widget(desc)
47
48        discretize_desc = description.WidgetDescription.from_module(
49            "Orange.OrangeWidgets.Data.OWDiscretize"
50        )
51        reg.register_widget(discretize_desc)
52
53        self.assertTrue(reg.has_widget(discretize_desc.qualified_name))
54        self.assertIs(reg.widget(discretize_desc.qualified_name),
55                      discretize_desc)
56
57        self.assertSetEqual(set(reg.widgets("Data")),
58                            set([file_desc, discretize_desc]))
59
60        classify_desc = description.CategoryDescription.from_package(
61            "Orange.OrangeWidgets.Classify"
62        )
63        reg.register_category(classify_desc)
64
65        self.assertTrue(reg.has_category(classify_desc.name))
66        self.assertIs(reg.category(classify_desc.name), classify_desc)
67        self.assertSetEqual(set(reg.categories()),
68                            set([data_desc, classify_desc]))
69
70        bayes_desc = description.WidgetDescription.from_module(
71            "Orange.OrangeWidgets.Classify.OWNaiveBayes"
72        )
73        reg.register_widget(bayes_desc)
74
75        self.assertTrue(reg.has_widget(bayes_desc.qualified_name))
76        self.assertIs(reg.widget(bayes_desc.qualified_name), bayes_desc)
77        self.assertSequenceEqual(reg.widgets("Classify"), [bayes_desc])
78
79        info_desc = description.WidgetDescription.from_file(
80            __import__("Orange.OrangeWidgets.Data.OWDataInfo",
81                       fromlist=[""]).__file__
82        )
83        reg.register_widget(info_desc)
84
85        # Test copy constructor
86        reg1 = WidgetRegistry(reg)
87        self.assertTrue(reg1.has_category(data_desc.name))
88        self.assertTrue(reg1.has_category(classify_desc.name))
89        self.assertSequenceEqual(reg.categories(), reg1.categories())
90
91        # Test 'widgets()'
92        self.assertSetEqual(set(reg1.widgets()),
93                            set([file_desc, info_desc, discretize_desc,
94                                 bayes_desc]))
95
96        # Test ordering by priority
97        self.assertSequenceEqual(
98             reg.widgets("Data"),
99             sorted([file_desc, discretize_desc, info_desc],
100                    key=attrgetter("priority"))
101        )
102
103        self.assertTrue(all(isinstance(desc.priority, int)
104                            for desc in [file_desc, info_desc, discretize_desc,
105                                         bayes_desc])
106                        )
Note: See TracBrowser for help on using the repository browser.