blob: 9e09c0fe3b8789e02930bc2303113a93039bc5b3 [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
15import unittest
16from mock import patch, call, Mock, PropertyMock
17import json
18
19import os
20import sys
21
22test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
24xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
25services_dir = os.path.join(xos_dir, "../../xos_services")
26
27class TestDiffs(unittest.TestCase):
28
29 """ These tests are for the mock modelaccessor, to make sure it behaves like the real one """
30
31 def setUp(self):
32
33 self.sys_path_save = sys.path
34 # Setting up the config module
35 from xosconfig import Config
36
37 config = os.path.join(test_path, "test_config.yaml")
38 Config.clear()
39 Config.init(config, "synchronizer-config-schema.yaml")
40 # END Setting up the config module
41
42 from xossynchronizer.mock_modelaccessor_build import (
43 build_mock_modelaccessor,
44 )
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,
55 ) in (
56 xossynchronizer.modelaccessor.model_accessor.all_model_classes.items()
57 ):
58 globals()[k] = v
59
60 self.log = Mock()
61
62 def tearDown(self):
63 sys.path = self.sys_path_save
64
65 def test_new_diff(self):
66 site = Site(name="mysite")
67
68 self.assertEqual(site.is_new, True)
69 self.assertEqual(site._dict, {"name": "mysite"})
70 self.assertEqual(site.diff, {})
71 self.assertEqual(site.changed_fields, ["name"])
72 self.assertEqual(site.has_field_changed("name"), False)
73 self.assertEqual(site.has_field_changed("login_base"), False)
74
75 site.login_base = "bar"
76
77 self.assertEqual(site._dict, {"login_base": "bar", "name": "mysite"})
78 self.assertEqual(site.diff, {"login_base": (None, "bar")})
79 self.assertIn("name", site.changed_fields)
80 self.assertIn("login_base", site.changed_fields)
81 self.assertEqual(site.has_field_changed("name"), False)
82 self.assertEqual(site.has_field_changed("login_base"), True)
83 self.assertEqual(site.get_field_diff("login_base"), (None, "bar"))
84
85 def test_existing_diff(self):
86 site = Site(name="mysite", login_base="foo")
87
88 # this is what would happen after saving and re-loading
89 site.is_new = False
90 site.id = 1
91 site._initial = site._dict
92
93 self.assertEqual(site.is_new, False)
94 self.assertEqual(site._dict, {"id": 1, "name": "mysite", "login_base": "foo"})
95 self.assertEqual(site.diff, {})
96 self.assertEqual(site.changed_fields, [])
97 self.assertEqual(site.has_field_changed("name"), False)
98 self.assertEqual(site.has_field_changed("login_base"), False)
99
100 site.login_base = "bar"
101
102 self.assertEqual(site._dict, {"id": 1, "login_base": "bar", "name": "mysite"})
103 self.assertEqual(site.diff, {"login_base": ("foo", "bar")})
104 self.assertIn("login_base", site.changed_fields)
105 self.assertEqual(site.has_field_changed("name"), False)
106 self.assertEqual(site.has_field_changed("login_base"), True)
107
108
109if __name__ == "__main__":
110 unittest.main()