source: orange/Orange/OrangeCanvas/registry/tests/test_base.py @ 11098:b743937fe90a

Revision 11098:b743937fe90a, 2.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 19 months ago (diff)

Added new widget/category description classes and new widget discovery.

This includes a Qt item model interface for the registry.

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