Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 16 | import unittest |
| 17 | from mock import patch, call, Mock, PropertyMock |
| 18 | import json |
| 19 | |
| 20 | import os |
| 21 | import sys |
| 22 | |
| 23 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer") |
| 25 | xos_dir = os.path.join(test_path, "..", "..", "..", "xos") |
Matteo Scandolo | e157680 | 2019-03-06 15:23:40 -0800 | [diff] [blame] | 26 | services_dir = os.path.join(xos_dir, "../../xos-services") |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 27 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 28 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 29 | class TestDiffs(unittest.TestCase): |
| 30 | |
| 31 | """ These tests are for the mock modelaccessor, to make sure it behaves like the real one """ |
| 32 | |
| 33 | def setUp(self): |
| 34 | |
| 35 | self.sys_path_save = sys.path |
| 36 | # Setting up the config module |
| 37 | from xosconfig import Config |
| 38 | |
| 39 | config = os.path.join(test_path, "test_config.yaml") |
| 40 | Config.clear() |
| 41 | Config.init(config, "synchronizer-config-schema.yaml") |
| 42 | # END Setting up the config module |
| 43 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 44 | from xossynchronizer.mock_modelaccessor_build import build_mock_modelaccessor |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 45 | |
| 46 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 47 | # and apparently it is not overriding the generated model accessor |
| 48 | build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir, []) |
| 49 | import xossynchronizer.modelaccessor |
| 50 | |
| 51 | # import all class names to globals |
| 52 | for ( |
| 53 | k, |
| 54 | v, |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 55 | ) in xossynchronizer.modelaccessor.model_accessor.all_model_classes.items(): |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 56 | globals()[k] = v |
| 57 | |
| 58 | self.log = Mock() |
| 59 | |
| 60 | def tearDown(self): |
| 61 | sys.path = self.sys_path_save |
| 62 | |
| 63 | def test_new_diff(self): |
| 64 | site = Site(name="mysite") |
| 65 | |
| 66 | self.assertEqual(site.is_new, True) |
| 67 | self.assertEqual(site._dict, {"name": "mysite"}) |
| 68 | self.assertEqual(site.diff, {}) |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 69 | self.assertEqual(list(site.changed_fields), ["name"]) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 70 | self.assertEqual(site.has_field_changed("name"), False) |
| 71 | self.assertEqual(site.has_field_changed("login_base"), False) |
| 72 | |
| 73 | site.login_base = "bar" |
| 74 | |
| 75 | self.assertEqual(site._dict, {"login_base": "bar", "name": "mysite"}) |
| 76 | self.assertEqual(site.diff, {"login_base": (None, "bar")}) |
| 77 | self.assertIn("name", site.changed_fields) |
| 78 | self.assertIn("login_base", site.changed_fields) |
| 79 | self.assertEqual(site.has_field_changed("name"), False) |
| 80 | self.assertEqual(site.has_field_changed("login_base"), True) |
| 81 | self.assertEqual(site.get_field_diff("login_base"), (None, "bar")) |
| 82 | |
| 83 | def test_existing_diff(self): |
| 84 | site = Site(name="mysite", login_base="foo") |
| 85 | |
| 86 | # this is what would happen after saving and re-loading |
| 87 | site.is_new = False |
| 88 | site.id = 1 |
| 89 | site._initial = site._dict |
| 90 | |
| 91 | self.assertEqual(site.is_new, False) |
| 92 | self.assertEqual(site._dict, {"id": 1, "name": "mysite", "login_base": "foo"}) |
| 93 | self.assertEqual(site.diff, {}) |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 94 | self.assertEqual(list(site.changed_fields), []) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 95 | self.assertEqual(site.has_field_changed("name"), False) |
| 96 | self.assertEqual(site.has_field_changed("login_base"), False) |
| 97 | |
| 98 | site.login_base = "bar" |
| 99 | |
| 100 | self.assertEqual(site._dict, {"id": 1, "login_base": "bar", "name": "mysite"}) |
| 101 | self.assertEqual(site.diff, {"login_base": ("foo", "bar")}) |
| 102 | self.assertIn("login_base", site.changed_fields) |
| 103 | self.assertEqual(site.has_field_changed("name"), False) |
| 104 | self.assertEqual(site.has_field_changed("login_base"), True) |
| 105 | |
| 106 | |
| 107 | if __name__ == "__main__": |
| 108 | unittest.main() |