|
1 | | -from copy import copy |
| 1 | +import pickle |
| 2 | +from copy import copy, deepcopy |
| 3 | +from io import BytesIO |
2 | 4 | from unittest import TestCase |
3 | | -from unittest.mock import Mock |
4 | | -from Orange.widgets.settings import ContextHandler, Setting, ContextSetting, Context |
| 5 | +from unittest.mock import Mock, patch, call |
| 6 | +from Orange.widgets.settings import ContextHandler, Setting, ContextSetting, Context, VERSION_KEY |
5 | 7 |
|
6 | 8 | __author__ = 'anze' |
7 | 9 |
|
8 | 10 |
|
9 | 11 | class SimpleWidget: |
| 12 | + settings_version = 1 |
| 13 | + |
10 | 14 | setting = Setting(42) |
11 | 15 |
|
12 | 16 | context_setting = ContextSetting(42) |
13 | 17 |
|
| 18 | + migrate_settings = Mock() |
| 19 | + migrate_context = Mock() |
| 20 | + |
| 21 | + |
| 22 | +class DummyContext(Context): |
| 23 | + id = 0 |
| 24 | + |
| 25 | + def __init__(self, version=None): |
| 26 | + super().__init__() |
| 27 | + DummyContext.id += 1 |
| 28 | + self.id = DummyContext.id |
| 29 | + if version: |
| 30 | + self.values[VERSION_KEY] = version |
| 31 | + |
| 32 | + def __repr__(self): |
| 33 | + return "Context(id={})".format(self.id) |
| 34 | + __str__ = __repr__ |
| 35 | + |
| 36 | + def __eq__(self, other): |
| 37 | + if not isinstance(other, DummyContext): |
| 38 | + return False |
| 39 | + return self.id == other.id |
| 40 | + |
| 41 | + |
| 42 | +def create_defaults_file(contexts): |
| 43 | + b = BytesIO() |
| 44 | + pickle.dump({"x": 5}, b) |
| 45 | + pickle.dump(contexts, b) |
| 46 | + b.seek(0) |
| 47 | + return b |
| 48 | + |
14 | 49 |
|
15 | 50 | class TestContextHandler(TestCase): |
| 51 | + def test_read_defaults(self): |
| 52 | + contexts = [DummyContext() for _ in range(3)] |
| 53 | + |
| 54 | + handler = ContextHandler() |
| 55 | + handler.widget_class = SimpleWidget |
| 56 | + |
| 57 | + # Old settings without version |
| 58 | + migrate_context = Mock() |
| 59 | + with patch.object(SimpleWidget, "migrate_context", migrate_context): |
| 60 | + handler.read_defaults_file(create_defaults_file(contexts)) |
| 61 | + self.assertSequenceEqual(handler.global_contexts, contexts) |
| 62 | + migrate_context.assert_has_calls([call(c, None) for c in contexts]) |
| 63 | + |
| 64 | + # Settings with version |
| 65 | + contexts = [DummyContext(version=i) for i in range(1, 4)] |
| 66 | + migrate_context.reset_mock() |
| 67 | + with patch.object(SimpleWidget, "migrate_context", migrate_context): |
| 68 | + handler.read_defaults_file(create_defaults_file(contexts)) |
| 69 | + self.assertSequenceEqual(handler.global_contexts, contexts) |
| 70 | + migrate_context.assert_has_calls([call(c, c.values[VERSION_KEY]) for c in contexts]) |
| 71 | + |
16 | 72 | def test_initialize(self): |
17 | 73 | handler = ContextHandler() |
18 | 74 | handler.provider = Mock() |
| 75 | + handler.widget_class = SimpleWidget |
19 | 76 |
|
20 | 77 | # Context settings from data |
21 | 78 | widget = SimpleWidget() |
22 | | - handler.initialize(widget, {'context_settings': 5}) |
| 79 | + context_settings = [DummyContext()] |
| 80 | + handler.initialize(widget, {'context_settings': context_settings}) |
23 | 81 | self.assertTrue(hasattr(widget, 'context_settings')) |
24 | | - self.assertEqual(widget.context_settings, 5) |
| 82 | + self.assertEqual(widget.context_settings, context_settings) |
25 | 83 |
|
26 | 84 | # Default (global) context settings |
27 | 85 | widget = SimpleWidget() |
28 | 86 | handler.initialize(widget) |
29 | 87 | self.assertTrue(hasattr(widget, 'context_settings')) |
30 | 88 | self.assertEqual(widget.context_settings, handler.global_contexts) |
31 | 89 |
|
| 90 | + def test_initialize_migrates_contexts(self): |
| 91 | + handler = ContextHandler() |
| 92 | + handler.bind(SimpleWidget) |
| 93 | + |
| 94 | + widget = SimpleWidget() |
| 95 | + |
| 96 | + # Old settings without version |
| 97 | + contexts = [DummyContext() for _ in range(3)] |
| 98 | + migrate_context = Mock() |
| 99 | + with patch.object(SimpleWidget, "migrate_context", migrate_context): |
| 100 | + handler.initialize(widget, dict(context_settings=contexts)) |
| 101 | + migrate_context.assert_has_calls([call(c, None) for c in contexts]) |
| 102 | + |
| 103 | + # Settings with version |
| 104 | + contexts = [DummyContext(version=i) for i in range(1, 4)] |
| 105 | + migrate_context = Mock() |
| 106 | + with patch.object(SimpleWidget, "migrate_context", migrate_context): |
| 107 | + handler.initialize(widget, dict(context_settings=deepcopy(contexts))) |
| 108 | + migrate_context.assert_has_calls([call(c, c.values[VERSION_KEY]) for c in contexts]) |
| 109 | + |
32 | 110 | def test_fast_save(self): |
33 | 111 | handler = ContextHandler() |
34 | 112 | handler.bind(SimpleWidget) |
@@ -68,3 +146,16 @@ def test_find_or_create_context(self): |
68 | 146 | self.assertEqual(context.i, 5) |
69 | 147 | self.assertEqual([c.i for c in widget.context_settings], [5, 2]) |
70 | 148 | self.assertEqual([c.i for c in handler.global_contexts], [3, 7]) |
| 149 | + |
| 150 | + def test_pack_settings_stores_version(self): |
| 151 | + handler = ContextHandler() |
| 152 | + handler.bind(SimpleWidget) |
| 153 | + |
| 154 | + widget = SimpleWidget() |
| 155 | + handler.initialize(widget) |
| 156 | + widget.context_setting = [DummyContext() for _ in range(3)] |
| 157 | + |
| 158 | + settings = handler.pack_data(widget) |
| 159 | + self.assertIn("context_settings", settings) |
| 160 | + for c in settings["context_settings"]: |
| 161 | + self.assertIn(VERSION_KEY, c.values) |
0 commit comments