blob: 9e0b9d9add5e2f5729de11fbf1767a7aaf73c0b1 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# 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 Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
Scott Bakerbba67b62019-01-28 17:38:21 -080016import unittest
17from mock import patch, call, Mock, PropertyMock
18import json
19
20import os
21import sys
22
23test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
25xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
26services_dir = os.path.join(xos_dir, "../../xos_services")
27
Zack Williams5c2ea232019-01-30 15:23:01 -070028
Scott Bakerbba67b62019-01-28 17:38:21 -080029class 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 Williams5c2ea232019-01-30 15:23:01 -070044 from xossynchronizer.mock_modelaccessor_build import build_mock_modelaccessor
Scott Bakerbba67b62019-01-28 17:38:21 -080045
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 Williams5c2ea232019-01-30 15:23:01 -070055 ) in xossynchronizer.modelaccessor.model_accessor.all_model_classes.items():
Scott Bakerbba67b62019-01-28 17:38:21 -080056 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 Williams5c2ea232019-01-30 15:23:01 -070069 self.assertEqual(list(site.changed_fields), ["name"])
Scott Bakerbba67b62019-01-28 17:38:21 -080070 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 Williams5c2ea232019-01-30 15:23:01 -070094 self.assertEqual(list(site.changed_fields), [])
Scott Bakerbba67b62019-01-28 17:38:21 -080095 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
107if __name__ == "__main__":
108 unittest.main()