def test_open_context(self): context = Context() context.attributes = () context.class_vars = () self.handler.new_context = Mock(return_value=context) self.handler.open_context(self.widget, self.domain) self.handler.new_context.assert_called_with(*self.args)
def test_migrate_str_to_variable(self): values = dict(foo=("foo", 1), baz=("baz", 2), qux=("qux", 102), bar=13) context = Context(values=values.copy()) migrate_str_to_variable(context) self.assertDictEqual( context.values, dict(foo=("foo", 101), baz=("baz", 102), qux=("qux", 102), bar=13), ) context = Context(values=values.copy()) migrate_str_to_variable(context, ("foo", "qux")) self.assertDictEqual( context.values, dict(foo=("foo", 101), baz=("baz", 2), qux=("qux", 102), bar=13), ) context = Context(values=values.copy()) migrate_str_to_variable(context, "foo") self.assertDictEqual( context.values, dict(foo=("foo", 101), baz=("baz", 2), qux=("qux", 102), bar=13), ) self.assertRaises(KeyError, migrate_str_to_variable, context, "quuux")
def test_migrate_settings_from_version_1_disc(self): settings = { '__version__': 1, 'context_settings': [ Context(attributes={ 'Type': 1, "foo": 2 }, metas={'name': 4}, values={ 'agg_attr': ([('Type', 101)], -3), 'agg_func': (0, -2), 'ax1': ('months', -2), 'ax2': ('years', -2) }) ], 'controlAreaVisible': True, 'invert_date_order': False, 'savedWidgetGeometry': None } w = self.create_widget(OWSpiralogram, stored_settings=settings) self.send_signal(w.Inputs.time_series, self.philadelphia, widget=w) self.assertEqual(w.agg_attr.name, 'Type') self.assertEqual(w.agg_func, 'Mode')
def test_find_or_create_context(self): widget = SimpleWidget() handler = ContextHandler() handler.match = lambda context, i: (context.i == i) * 2 handler.clone_context = lambda context, i: copy(context) c1, c2, c3, c4, c5, c6, c7, c8, c9 = (Context(i=i) for i in range(1, 10)) # finding a perfect match in global_contexts should copy it to # the front of context_settings (and leave globals as-is) widget.context_settings = [c2, c5] handler.global_contexts = [c3, c7] context, new = handler.find_or_create_context(widget, 7) self.assertEqual(context.i, 7) self.assertEqual([c.i for c in widget.context_settings], [7, 2, 5]) self.assertEqual([c.i for c in handler.global_contexts], [3, 7]) # finding a perfect match in context_settings should move it to # the front of the list widget.context_settings = [c2, c5] handler.global_contexts = [c3, c7] context, new = handler.find_or_create_context(widget, 5) self.assertEqual(context.i, 5) self.assertEqual([c.i for c in widget.context_settings], [5, 2]) self.assertEqual([c.i for c in handler.global_contexts], [3, 7])
def test_migrate_settings_from_version_1(self): # Settings from Orange 3.4.0 settings = { '__version__': 1, 'alpha_value': 255, 'auto_commit': True, 'class_density': False, 'context_settings': [ Context(attributes={'iris': 1, 'petal length': 2, 'petal width': 2, 'sepal length': 2, 'sepal width': 2}, metas={}, ordered_domain=[('sepal length', 2), ('sepal width', 2), ('petal length', 2), ('petal width', 2), ('iris', 1)], time=1504865133.098991, values={'__version__': 1, 'color_index': (5, -2), 'shape_index': (1, -2), 'size_index': (1, -2), 'variable_state': ({}, -2)})], 'jitter_value': 0, 'legend_anchor': ((1, 0), (1, 0)), 'point_size': 8, 'savedWidgetGeometry': None } w = self.create_widget(OWLinearProjection, stored_settings=settings) iris = Table("iris") self.send_signal(w.Inputs.data, iris, widget=w) self.assertEqual(w.graph.point_width, 8) self.assertEqual(w.attr_color, iris.domain["iris"]) self.assertEqual(w.attr_shape, iris.domain["iris"]) self.assertEqual(w.attr_size, iris.domain["sepal length"])
def test_migrate_settings_from_version_1(self): context_settings = [ Context( attributes={ "iris": 1, "petal length": 2, "petal width": 2, "sepal length": 2, "sepal width": 2, }, metas={}, ordered_domain=[ ("sepal length", 2), ("sepal width", 2), ("petal length", 2), ("petal width", 2), ("iris", 1), ], time=1500000000, values={ "__version__": 1, "color_value": ("iris", 1), "shape_value": ("iris", 2), "size_value": ("Stress", -2), "label_value": ("sepal length", 2), }, ) ] settings = { "__version__": 1, "autocommit": False, "connected_pairs": 5, "initialization": 0, "jitter": 0.5, "label_only_selected": True, "legend_anchor": ((1, 0), (1, 0)), "max_iter": 300, "refresh_rate": 3, "symbol_opacity": 230, "symbol_size": 8, "context_settings": context_settings, "savedWidgetGeometry": None, } w = self.create_widget(OWMDS, stored_settings=settings) data = self.data self.send_signal(w.Inputs.data, data, widget=w) g = w.graph for a, value in ( (g.attr_color, "iris"), (g.attr_shape, "iris"), (g.attr_size, "Stress"), (g.attr_label, "sepal length"), (g.label_only_selected, True), (g.alpha_value, 230), (g.point_width, 8), (g.jitter_size, 0.5), ): self.assertTrue(a, value) self.assertFalse(w.auto_commit)
def test_formats_contexts(self): settings = dict( key1=1, key2=2, context_settings=[ Context(param1=1, param2=2, values=dict(value1=1, value2=2)), Context(param1=3, param2=4, values=dict(value1=5, value2=6)), ], ) pp = SettingsPrinter() output = pp.pformat(settings) # parameter of all contexts should be visible in the output self.assertIn("param1=1", output) self.assertIn("param2=2", output) self.assertIn("param1=3", output) self.assertIn("param2=4", output)
def test_rename_settings(self): some_settings = dict(foo=42, bar=13) rename_setting(some_settings, "foo", "baz") self.assertDictEqual(some_settings, dict(baz=42, bar=13)) self.assertRaises(KeyError, rename_setting, some_settings, "qux", "quux") context = Context(values=dict(foo=42, bar=13)) rename_setting(context, "foo", "baz") self.assertDictEqual(context.values, dict(baz=42, bar=13))
def test_migrate_settings_from_version_1(self): # Settings from Orange 3.4.0 settings = { "__version__": 1, "alpha_value": 255, "auto_commit": True, "class_density": False, "context_settings": [ Context( attributes={ "iris": 1, "petal length": 2, "petal width": 2, "sepal length": 2, "sepal width": 2, }, metas={}, ordered_domain=[ ("sepal length", 2), ("sepal width", 2), ("petal length", 2), ("petal width", 2), ("iris", 1), ], time=1504865133.098991, values={ "__version__": 1, "color_index": (5, -2), "shape_index": (1, -2), "size_index": (1, -2), "variable_state": ({}, -2), }, ) ], "jitter_value": 0, "legend_anchor": ((1, 0), (1, 0)), "point_size": 8, "savedWidgetGeometry": None, } w = self.create_widget(OWLinearProjection, stored_settings=settings) iris = Table("iris") self.send_signal(w.Inputs.data, iris, widget=w) self.assertEqual(w.graph.point_width, 8) self.assertEqual(w.graph.attr_color, iris.domain["iris"]) self.assertEqual(w.graph.attr_shape, iris.domain["iris"]) self.assertEqual(w.graph.attr_size, iris.domain["sepal length"])
def test_migrate_settings_from_version_1(self): context_settings = [ Context(attributes={ 'iris': 1, 'petal length': 2, 'petal width': 2, 'sepal length': 2, 'sepal width': 2 }, metas={}, ordered_domain=[('sepal length', 2), ('sepal width', 2), ('petal length', 2), ('petal width', 2), ('iris', 1)], time=1500000000, values={ '__version__': 1, 'color_value': ('iris', 1), 'shape_value': ('iris', 2), 'size_value': ('Stress', -2), 'label_value': ('sepal length', 2) }) ] settings = { '__version__': 1, 'autocommit': False, 'connected_pairs': 5, 'initialization': 0, 'jitter': 0.5, 'label_only_selected': True, 'legend_anchor': ((1, 0), (1, 0)), 'max_iter': 300, 'refresh_rate': 3, 'symbol_opacity': 230, 'symbol_size': 8, 'context_settings': context_settings, 'savedWidgetGeometry': None } w = self.create_widget(OWMDS, stored_settings=settings) domain = self.data.domain self.send_signal(w.Inputs.data, self.data, widget=w) g = w.graph for a, value in ((w.attr_color, domain["iris"]), (w.attr_shape, domain["iris"]), (w.attr_size, "Stress"), (w.attr_label, domain["sepal length"]), (g.label_only_selected, True), (g.alpha_value, 230), (g.point_width, 8), (g.jitter_size, 0.5)): self.assertEqual(a, value) self.assertFalse(w.auto_commit)
def test_migrates_settings_removes_incompatible(self): handler = ContextHandler() handler.bind(SimpleWidget) widget = SimpleWidget() contexts = [Context(foo=i) for i in (13, 13, 0, 1, 13, 2, 13)] def migrate_context(context, _): if context.foo == 13: raise IncompatibleContext() with patch.object(SimpleWidget, "migrate_context", migrate_context): handler.initialize(widget, dict(context_settings=contexts)) contexts = widget.context_settings self.assertEqual(len(contexts), 3) self.assertTrue( all(context.foo == i for i, context in enumerate(contexts)))
def _create_context(self, attrs, class_vars, metas): context = Context() context.attributes = attrs context.class_vars = class_vars context.metas = metas return context