Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [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 | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 16 | import exceptions |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 17 | import os |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 18 | import random |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 19 | import string |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 20 | import sys |
| 21 | import unittest |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 22 | from mock import patch |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 23 | from StringIO import StringIO |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 24 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 25 | # by default, use fake stub rather than real core |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 26 | USE_FAKE_STUB = True |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 27 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 28 | PARENT_DIR = os.path.join(os.path.dirname(__file__), "..") |
| 29 | |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 30 | |
| 31 | class TestORM(unittest.TestCase): |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 32 | def setUp(self): |
Scott Baker | e5f0f68 | 2018-06-18 10:21:35 -0700 | [diff] [blame] | 33 | from xosconfig import Config |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 34 | |
Scott Baker | e5f0f68 | 2018-06-18 10:21:35 -0700 | [diff] [blame] | 35 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 36 | config = os.path.join(test_path, "test_config.yaml") |
| 37 | Config.clear() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 38 | Config.init(config, "synchronizer-config-schema.yaml") |
| 39 | if USE_FAKE_STUB: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 40 | sys.path.append(PARENT_DIR) |
| 41 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 42 | # Import these after config, in case they depend on config |
| 43 | from xosapi.orm import ORMQuerySet, ORMLocalObjectManager |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 44 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 45 | self.ORMQuerySet = ORMQuerySet |
| 46 | self.ORMLocalObjectManager = ORMLocalObjectManager |
| 47 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 48 | def tearDown(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 49 | if USE_FAKE_STUB: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 50 | sys.path.remove(PARENT_DIR) |
| 51 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 52 | def make_coreapi(self): |
| 53 | if USE_FAKE_STUB: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 54 | import xosapi.orm |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 55 | from fake_stub import FakeStub, FakeProtos, FakeObj |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 56 | |
Scott Baker | c001703 | 2018-05-01 10:54:26 -0700 | [diff] [blame] | 57 | xosapi.orm.import_convenience_methods() |
| 58 | |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 59 | stub = FakeStub() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 60 | api = xosapi.orm.ORMStub( |
| 61 | stub=stub, |
| 62 | package_name="xos", |
| 63 | protos=FakeProtos(), |
| 64 | empty=FakeObj, |
| 65 | enable_backoff=False, |
| 66 | ) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 67 | return api |
| 68 | else: |
| 69 | return xos_grpc_client.coreapi |
| 70 | |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 71 | def test_repr_name(self): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 72 | orm = self.make_coreapi() |
| 73 | s = orm.Slice(name="foo") |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 74 | self.assertNotEqual(s, None) |
| 75 | self.assertEqual(repr(s), "<Slice: foo>") |
| 76 | |
| 77 | def test_str_name(self): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 78 | orm = self.make_coreapi() |
| 79 | s = orm.Slice(name="foo") |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 80 | self.assertNotEqual(s, None) |
| 81 | self.assertEqual(str(s), "foo") |
| 82 | |
| 83 | def test_dumpstr_name(self): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 84 | orm = self.make_coreapi() |
| 85 | s = orm.Slice(name="foo") |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 86 | self.assertNotEqual(s, None) |
| 87 | self.assertEqual(s.dumpstr(), 'name: "foo"\n') |
| 88 | |
| 89 | def test_repr_noname(self): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 90 | orm = self.make_coreapi() |
| 91 | s = orm.Slice() |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 92 | self.assertNotEqual(s, None) |
| 93 | self.assertEqual(repr(s), "<Slice: id-0>") |
| 94 | |
| 95 | def test_str_noname(self): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 96 | orm = self.make_coreapi() |
| 97 | s = orm.Slice() |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 98 | self.assertNotEqual(s, None) |
| 99 | self.assertEqual(str(s), "Slice-0") |
| 100 | |
| 101 | def test_dumpstr_noname(self): |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 102 | orm = self.make_coreapi() |
| 103 | s = orm.Slice() |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 104 | self.assertNotEqual(s, None) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 105 | self.assertEqual(s.dumpstr(), "") |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 106 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 107 | def test_dump(self): |
| 108 | """ dump() is like dumpstr() but prints to stdout. Mock stdout by using a stringIO. """ |
| 109 | |
| 110 | orm = self.make_coreapi() |
| 111 | s = orm.Slice(name="foo") |
| 112 | self.assertNotEqual(s, None) |
| 113 | with patch("sys.stdout", new_callable=StringIO) as mock_stdout: |
| 114 | s.dump() |
| 115 | self.assertEqual(mock_stdout.getvalue(), 'name: "foo"\n\n') |
| 116 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 117 | def test_create(self): |
| 118 | orm = self.make_coreapi() |
| 119 | site = orm.Site(name="mysite") |
| 120 | site.save() |
| 121 | self.assertTrue(site.id > 0) |
| 122 | |
| 123 | def test_get(self): |
| 124 | orm = self.make_coreapi() |
| 125 | site = orm.Site(name="mysite") |
| 126 | site.save() |
| 127 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 128 | got_site = orm.Site.objects.get(id=site.id) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 129 | self.assertNotEqual(got_site, None) |
| 130 | self.assertEqual(got_site.id, site.id) |
| 131 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 132 | def test_invalidate_cache(self): |
| 133 | orm = self.make_coreapi() |
| 134 | testModel = orm.TestModel() |
| 135 | |
| 136 | # populate the caches with some placeholders we can test for |
| 137 | testModel.cache = {"a": 1} |
| 138 | testModel.reverse_cache = {"b": 2} |
| 139 | |
| 140 | testModel.invalidate_cache() |
| 141 | |
| 142 | self.assertEqual(testModel.cache, {}) |
| 143 | self.assertEqual(testModel.reverse_cache, {}) |
| 144 | |
| 145 | def test_save_new(self): |
| 146 | orm = self.make_coreapi() |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 147 | site = orm.Site(name="mysite") |
| 148 | site.save() |
| 149 | self.assertTrue(site.id > 0) |
| 150 | |
| 151 | def test_save_existing(self): |
| 152 | orm = self.make_coreapi() |
| 153 | orig_len_sites = len(orm.Site.objects.all()) |
| 154 | site = orm.Site(name="mysite") |
| 155 | site.save() |
| 156 | self.assertTrue(site.id > 0) |
| 157 | |
| 158 | # there should be one new site |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 159 | self.assertEqual(len(orm.Site.objects.all()), orig_len_sites + 1) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 160 | |
| 161 | # retrieve the site, and update it |
| 162 | created_site_id = site.id |
| 163 | site = orm.Site.objects.get(id=created_site_id) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 164 | site.name = "mysitetwo" |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 165 | site.save() |
| 166 | |
| 167 | # the site_id should not have changed |
| 168 | self.assertEqual(site.id, created_site_id) |
| 169 | |
| 170 | # there should still be only one new site |
| 171 | self.assertEqual(len(orm.Site.objects.all()), orig_len_sites + 1) |
| 172 | |
| 173 | # the name should have changed |
| 174 | self.assertEqual(orm.Site.objects.get(id=created_site_id).name, "mysitetwo") |
| 175 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 176 | def test_delete(self): |
| 177 | orm = self.make_coreapi() |
| 178 | orig_len_sites = len(orm.Site.objects.all()) |
| 179 | site = orm.Site(name="mysite") |
| 180 | site.save() |
| 181 | self.assertTrue(site.id > 0) |
| 182 | site.delete() |
| 183 | sites = orm.Site.objects.all() |
| 184 | self.assertEqual(len(sites), orig_len_sites) |
| 185 | |
| 186 | def test_objects_all(self): |
| 187 | orm = self.make_coreapi() |
| 188 | orig_len_sites = len(orm.Site.objects.all()) |
| 189 | site = orm.Site(name="mysite") |
| 190 | site.save() |
| 191 | sites = orm.Site.objects.all() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 192 | self.assertEqual(len(sites), orig_len_sites + 1) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 193 | |
| 194 | def test_objects_first(self): |
| 195 | orm = self.make_coreapi() |
| 196 | site = orm.Site(name="mysite") |
| 197 | site.save() |
| 198 | site = orm.Site.objects.first() |
| 199 | self.assertNotEqual(site, None) |
| 200 | |
| 201 | def test_content_type_map(self): |
| 202 | orm = self.make_coreapi() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 203 | self.assertTrue("Slice" in orm.content_type_map.values()) |
| 204 | self.assertTrue("Site" in orm.content_type_map.values()) |
| 205 | self.assertTrue("Tag" in orm.content_type_map.values()) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 206 | |
| 207 | def test_foreign_key_get(self): |
| 208 | orm = self.make_coreapi() |
| 209 | site = orm.Site(name="mysite") |
| 210 | site.save() |
| 211 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 212 | user = orm.User( |
| 213 | email="fake_" |
| 214 | + "".join( |
| 215 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 216 | ), |
| 217 | site_id=site.id, |
| 218 | ) |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 219 | user.save() |
| 220 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 221 | slice = orm.Slice(name="mysite_foo", site_id=site.id, creator_id=user.id) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 222 | slice.save() |
| 223 | self.assertTrue(slice.id > 0) |
| 224 | self.assertNotEqual(slice.site, None) |
| 225 | self.assertEqual(slice.site.id, site.id) |
| 226 | |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 227 | def test_foreign_key_set_with_invalidate(self): |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 228 | orm = self.make_coreapi() |
| 229 | site = orm.Site(name="mysite") |
| 230 | site.save() |
| 231 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 232 | user = orm.User( |
| 233 | email="fake_" |
| 234 | + "".join( |
| 235 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 236 | ), |
| 237 | site_id=site.id, |
| 238 | ) |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 239 | user.save() |
| 240 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 241 | slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 242 | slice.save() |
| 243 | slice.invalidate_cache() |
| 244 | self.assertTrue(slice.id > 0) |
| 245 | self.assertNotEqual(slice.site, None) |
| 246 | self.assertEqual(slice.site.id, site.id) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 247 | if not USE_FAKE_STUB: |
| 248 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 249 | |
| 250 | def test_foreign_key_set_without_invalidate(self): |
| 251 | orm = self.make_coreapi() |
| 252 | site = orm.Site(name="mysite") |
| 253 | site.save() |
| 254 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 255 | user = orm.User( |
| 256 | email="fake_" |
| 257 | + "".join( |
| 258 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 259 | ), |
| 260 | site_id=site.id, |
| 261 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 262 | user.save() |
| 263 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 264 | slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 265 | slice.save() |
| 266 | self.assertTrue(slice.id > 0) |
| 267 | self.assertNotEqual(slice.site, None) |
| 268 | self.assertEqual(slice.site.id, site.id) |
| 269 | if not USE_FAKE_STUB: |
| 270 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 271 | ids_from_models = [x.id for x in slice.site.slices.all()] |
| 272 | self.assertTrue(slice.id in ids_from_models) |
| 273 | |
| 274 | def test_foreign_key_reset(self): |
| 275 | orm = self.make_coreapi() |
| 276 | site = orm.Site(name="mysite") |
| 277 | site.save() |
| 278 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 279 | user = orm.User( |
| 280 | email="fake_" |
| 281 | + "".join( |
| 282 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 283 | ), |
| 284 | site_id=site.id, |
| 285 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 286 | user.save() |
| 287 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 288 | slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 289 | slice.save() |
| 290 | self.assertTrue(slice.id > 0) |
| 291 | self.assertNotEqual(slice.site, None) |
| 292 | self.assertEqual(slice.site.id, site.id) |
| 293 | if not USE_FAKE_STUB: |
| 294 | self.assertTrue(slice.id in site.slices_ids) |
| 295 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 296 | |
| 297 | site2 = orm.Site(name="mysite2") |
| 298 | site2.save() |
| 299 | slice.name = "mysite2_foo" |
| 300 | slice.site = site2 |
| 301 | slice.save() |
| 302 | self.assertNotEqual(slice.site, None) |
| 303 | self.assertEqual(slice.site.id, site2.id) |
| 304 | if not USE_FAKE_STUB: |
| 305 | self.assertTrue(slice.id not in site.slices_ids) |
| 306 | self.assertTrue(slice.id in site2.slices_ids) |
| 307 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 308 | ids_from_models1 = [x.id for x in site.slices.all()] |
| 309 | self.assertTrue(slice.id not in ids_from_models1) |
| 310 | ids_from_models2 = [x.id for x in site2.slices.all()] |
| 311 | self.assertTrue(slice.id in ids_from_models2) |
| 312 | |
| 313 | def test_foreign_key_back_and_forth_even(self): |
| 314 | orm = self.make_coreapi() |
| 315 | site = orm.Site(name="mysite") |
| 316 | site.save() |
| 317 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 318 | user = orm.User( |
| 319 | email="fake_" |
| 320 | + "".join( |
| 321 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 322 | ), |
| 323 | site_id=site.id, |
| 324 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 325 | user.save() |
| 326 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 327 | slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 328 | slice.save() |
| 329 | self.assertTrue(slice.id > 0) |
| 330 | self.assertNotEqual(slice.site, None) |
| 331 | self.assertEqual(slice.site.id, site.id) |
| 332 | if not USE_FAKE_STUB: |
| 333 | self.assertTrue(slice.id in site.slices_ids) |
| 334 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 335 | |
| 336 | site2 = orm.Site(name="mysite2") |
| 337 | site2.save() |
| 338 | slice.name = "mysite2_foo" |
| 339 | slice.site = site2 |
| 340 | slice.site = site |
| 341 | slice.site = site2 |
| 342 | slice.site = site |
| 343 | slice.save() |
| 344 | self.assertNotEqual(slice.site, None) |
| 345 | self.assertEqual(slice.site.id, site.id) |
| 346 | if not USE_FAKE_STUB: |
| 347 | self.assertTrue(slice.id not in site2.slices_ids) |
| 348 | self.assertTrue(slice.id in site.slices_ids) |
| 349 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 350 | |
| 351 | def test_foreign_key_back_and_forth_odd(self): |
| 352 | orm = self.make_coreapi() |
| 353 | site = orm.Site(name="mysite") |
| 354 | site.save() |
| 355 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 356 | user = orm.User( |
| 357 | email="fake_" |
| 358 | + "".join( |
| 359 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 360 | ), |
| 361 | site_id=site.id, |
| 362 | ) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 363 | user.save() |
| 364 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 365 | slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 366 | slice.save() |
| 367 | self.assertTrue(slice.id > 0) |
| 368 | self.assertNotEqual(slice.site, None) |
| 369 | self.assertEqual(slice.site.id, site.id) |
| 370 | if not USE_FAKE_STUB: |
| 371 | self.assertTrue(slice.id in site.slices_ids) |
| 372 | self.assertTrue(slice.id in slice.site.slices_ids) |
| 373 | |
| 374 | site2 = orm.Site(name="mysite2") |
| 375 | site2.save() |
| 376 | slice.name = "mysite2_foo" |
| 377 | slice.site = site2 |
| 378 | slice.site = site |
| 379 | slice.site = site2 |
| 380 | slice.site = site |
| 381 | slice.site = site2 |
| 382 | slice.save() |
| 383 | self.assertNotEqual(slice.site, None) |
| 384 | self.assertEqual(slice.site.id, site2.id) |
| 385 | if not USE_FAKE_STUB: |
| 386 | self.assertTrue(slice.id not in site.slices_ids) |
| 387 | self.assertTrue(slice.id in site2.slices_ids) |
| 388 | self.assertTrue(slice.id in slice.site.slices_ids) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 389 | |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 390 | def test_foreign_key_create_null(self): |
| 391 | orm = self.make_coreapi() |
| 392 | site = orm.Site(name="mysite") |
| 393 | site.save() |
| 394 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 395 | user = orm.User( |
| 396 | email="fake_" |
| 397 | + "".join( |
| 398 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 399 | ), |
| 400 | site_id=site.id, |
| 401 | ) |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 402 | user.save() |
| 403 | self.assertTrue(user.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 404 | slice = orm.Slice( |
| 405 | name="mysite_foo", site=site, service=None, creator_id=user.id |
| 406 | ) |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 407 | slice.save() |
| 408 | slice.invalidate_cache() |
| 409 | self.assertTrue(slice.id > 0) |
| 410 | self.assertEqual(slice.service, None) |
| 411 | |
| 412 | def test_foreign_key_set_null(self): |
| 413 | orm = self.make_coreapi() |
| 414 | site = orm.Site(name="mysite") |
| 415 | site.save() |
| 416 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 417 | user = orm.User( |
| 418 | email="fake_" |
| 419 | + "".join( |
| 420 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 421 | ), |
| 422 | site_id=site.id, |
| 423 | ) |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 424 | user.save() |
| 425 | self.assertTrue(user.id > 0) |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 426 | service = orm.Service(name="myservice") |
| 427 | service.save() |
| 428 | self.assertTrue(service.id > 0) |
| 429 | # start out slice.service is non-None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 430 | slice = orm.Slice( |
| 431 | name="mysite_foo", site=site, service=service, creator_id=user.id |
| 432 | ) |
Scott Baker | a1eae7a | 2017-06-06 09:20:15 -0700 | [diff] [blame] | 433 | slice.save() |
| 434 | slice.invalidate_cache() |
| 435 | self.assertTrue(slice.id > 0) |
| 436 | self.assertNotEqual(slice.service, None) |
| 437 | self.assertEqual(slice.service.id, service.id) |
| 438 | # now set it to None |
| 439 | slice.service = None |
| 440 | slice.save() |
| 441 | slice.invalidate_cache() |
| 442 | self.assertEqual(slice.service, None) |
| 443 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 444 | def test_generic_foreign_key_get(self): |
| 445 | orm = self.make_coreapi() |
| 446 | service = orm.Service(name="myservice") |
| 447 | service.save() |
| 448 | site = orm.Site(name="mysite") |
| 449 | site.save() |
| 450 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 451 | tag = orm.Tag( |
| 452 | service=service, |
| 453 | name="mytag", |
| 454 | value="somevalue", |
| 455 | content_type=site.self_content_type_id, |
| 456 | object_id=site.id, |
| 457 | ) |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 458 | tag.save() |
| 459 | self.assertTrue(tag.id > 0) |
| 460 | self.assertNotEqual(tag.content_object, None) |
| 461 | self.assertEqual(tag.content_object.id, site.id) |
| 462 | |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 463 | def test_generic_foreign_key_get_decl(self): |
| 464 | orm = self.make_coreapi() |
| 465 | service = orm.Service(name="myservice") |
| 466 | service.save() |
| 467 | site = orm.Site(name="mysite") |
| 468 | site.save() |
| 469 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 470 | tag = orm.Tag( |
| 471 | service=service, |
| 472 | name="mytag", |
| 473 | value="somevalue", |
| 474 | content_type=site.self_content_type_id + "_decl", |
| 475 | object_id=site.id, |
| 476 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 477 | tag.save() |
| 478 | self.assertTrue(tag.id > 0) |
| 479 | self.assertNotEqual(tag.content_object, None) |
| 480 | self.assertEqual(tag.content_object.id, site.id) |
| 481 | |
| 482 | def test_generic_foreign_key_get_bad_contenttype(self): |
| 483 | orm = self.make_coreapi() |
| 484 | service = orm.Service(name="myservice") |
| 485 | service.save() |
| 486 | site = orm.Site(name="mysite") |
| 487 | site.save() |
| 488 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 489 | tag = orm.Tag( |
| 490 | service=service, |
| 491 | name="mytag", |
| 492 | value="somevalue", |
| 493 | content_type="does_not_exist", |
| 494 | object_id=site.id, |
| 495 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 496 | tag.save() |
| 497 | self.assertTrue(tag.id > 0) |
| 498 | with self.assertRaises(Exception) as e: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 499 | |
| 500 | self.assertEqual( |
| 501 | e.exception.message, |
| 502 | "Content_type does_not_exist not found in self.content_type_map", |
| 503 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 504 | |
| 505 | def test_generic_foreign_key_get_bad_id(self): |
| 506 | orm = self.make_coreapi() |
| 507 | service = orm.Service(name="myservice") |
| 508 | service.save() |
| 509 | site = orm.Site(name="mysite") |
| 510 | site.save() |
| 511 | self.assertTrue(site.id > 0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 512 | tag = orm.Tag( |
| 513 | service=service, |
| 514 | name="mytag", |
| 515 | value="somevalue", |
| 516 | content_type=site.self_content_type_id, |
| 517 | object_id=4567, |
| 518 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 519 | tag.save() |
| 520 | self.assertTrue(tag.id > 0) |
| 521 | with self.assertRaises(Exception) as e: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 522 | self.assertEqual( |
| 523 | e.exception.message, "Object 4567 of model Site was not found" |
| 524 | ) |
Scott Baker | d0f1dc1 | 2018-04-23 12:05:32 -0700 | [diff] [blame] | 525 | |
Scott Baker | 500f8c7 | 2017-05-19 09:41:50 -0700 | [diff] [blame] | 526 | def test_generic_foreign_key_set(self): |
| 527 | orm = self.make_coreapi() |
| 528 | service = orm.Service(name="myservice") |
| 529 | service.save() |
| 530 | site = orm.Site(name="mysite") |
| 531 | site.save() |
| 532 | self.assertTrue(site.id > 0) |
| 533 | tag = orm.Tag(service=service, name="mytag", value="somevalue") |
| 534 | tag.content_object = site |
| 535 | tag.invalidate_cache() |
| 536 | self.assertEqual(tag.content_type, site.self_content_type_id) |
| 537 | self.assertEqual(tag.object_id, site.id) |
| 538 | tag.save() |
| 539 | self.assertTrue(tag.id > 0) |
| 540 | self.assertNotEqual(tag.content_object, None) |
| 541 | self.assertEqual(tag.content_object.id, site.id) |
| 542 | |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 543 | def test_leaf_model_trivial(self): |
| 544 | orm = self.make_coreapi() |
| 545 | service = orm.Service(name="myservice") |
| 546 | service.save() |
| 547 | self.assertEqual(service.leaf_model_name, "Service") |
| 548 | |
| 549 | def test_leaf_model_descendant(self): |
| 550 | orm = self.make_coreapi() |
| 551 | onos_service = orm.ONOSService(name="myservice") |
| 552 | onos_service.save() |
| 553 | self.assertEqual(onos_service.model_name, "ONOSService") |
| 554 | self.assertEqual(onos_service.leaf_model_name, "ONOSService") |
| 555 | |
| 556 | service = orm.Service.objects.get(id=onos_service.id) |
| 557 | self.assertEqual(service.id, onos_service.id) |
| 558 | self.assertEqual(service.model_name, "Service") |
| 559 | self.assertEqual(service.leaf_model_name, "ONOSService") |
| 560 | |
| 561 | onos_service_cast = service.leaf_model |
| 562 | self.assertEqual(onos_service_cast.model_name, "ONOSService") |
| 563 | self.assertEqual(onos_service_cast.leaf_model_name, "ONOSService") |
| 564 | self.assertEqual(onos_service_cast.id, onos_service.id) |
| 565 | |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 566 | def test_field_null(self): |
| 567 | """ In a saved object, if a nullable field is left set to None, make sure the ORM returns None """ |
| 568 | |
| 569 | orm = self.make_coreapi() |
| 570 | tm = orm.TestModel() |
| 571 | tm.save() |
| 572 | |
| 573 | tm = orm.TestModel.objects.all()[0] |
| 574 | self.assertFalse(tm._wrapped_class.HasField("intfield")) |
| 575 | self.assertEqual(tm.intfield, None) |
| 576 | |
| 577 | def test_field_null_new(self): |
| 578 | """ For models that haven't been saved yet, reading the field should return the gRPC default """ |
| 579 | |
| 580 | orm = self.make_coreapi() |
| 581 | tm = orm.TestModel() |
| 582 | |
| 583 | self.assertEqual(tm.intfield, 0) |
| 584 | |
| 585 | def test_field_non_null(self): |
| 586 | """ In a saved object, if a nullable field is set to a value, then make sure the ORM returns the value """ |
| 587 | |
| 588 | orm = self.make_coreapi() |
| 589 | tm = orm.TestModel(intfield=3) |
| 590 | tm.save() |
| 591 | |
| 592 | tm = orm.TestModel.objects.all()[0] |
| 593 | self.assertEqual(tm.intfield, 3) |
| 594 | |
| 595 | def test_field_set_null(self): |
| 596 | """ Setting a field to None is not allowed """ |
| 597 | |
| 598 | orm = self.make_coreapi() |
| 599 | tm = orm.TestModel() |
| 600 | with self.assertRaises(Exception) as e: |
| 601 | tm.intfile = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 602 | self.assertEqual( |
| 603 | e.exception.message, |
| 604 | "Setting a non-foreignkey field to None is not supported", |
| 605 | ) |
Scott Baker | 37cf9e2 | 2018-08-20 14:39:33 -0700 | [diff] [blame] | 606 | |
Scott Baker | e1607b8 | 2018-09-20 14:10:59 -0700 | [diff] [blame] | 607 | def test_query_iexact(self): |
| 608 | orm = self.make_coreapi() |
| 609 | with patch.object(orm.grpc_stub, "FilterTestModel", autospec=True) as filter: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 610 | orm.TestModel.objects.filter(name__iexact="foo") |
Scott Baker | e1607b8 | 2018-09-20 14:10:59 -0700 | [diff] [blame] | 611 | self.assertEqual(filter.call_count, 1) |
| 612 | q = filter.call_args[0][0] |
| 613 | |
| 614 | self.assertEqual(q.kind, q.DEFAULT) |
| 615 | self.assertEqual(len(q.elements), 1) |
| 616 | self.assertEqual(q.elements[0].operator, q.elements[0].IEXACT) |
| 617 | self.assertEqual(q.elements[0].sValue, "foo") |
| 618 | |
| 619 | def test_query_equal(self): |
| 620 | orm = self.make_coreapi() |
| 621 | with patch.object(orm.grpc_stub, "FilterTestModel", autospec=True) as filter: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 622 | orm.TestModel.objects.filter(name="foo") |
Scott Baker | e1607b8 | 2018-09-20 14:10:59 -0700 | [diff] [blame] | 623 | self.assertEqual(filter.call_count, 1) |
| 624 | q = filter.call_args[0][0] |
| 625 | |
| 626 | self.assertEqual(q.kind, q.DEFAULT) |
| 627 | self.assertEqual(len(q.elements), 1) |
| 628 | self.assertEqual(q.elements[0].operator, q.elements[0].EQUAL) |
| 629 | self.assertEqual(q.elements[0].sValue, "foo") |
| 630 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 631 | def test_ORMWrapper_dict(self): |
| 632 | orm = self.make_coreapi() |
| 633 | |
| 634 | testModel = orm.TestModel(intfield=7, stringfield="foo") |
| 635 | |
| 636 | self.assertDictEqual(testModel._dict, {"intfield": 7, "stringfield": "foo"}) |
| 637 | |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 638 | def test_ORMWrapper_new_diff(self): |
| 639 | orm = self.make_coreapi() |
| 640 | site = orm.Site(name="mysite") |
| 641 | |
| 642 | self.assertEqual(site.is_new, True) |
| 643 | self.assertEqual(site._dict, {"name": "mysite"}) |
| 644 | self.assertEqual(site.diff, {}) |
| 645 | self.assertEqual(site.changed_fields, ["name"]) |
| 646 | self.assertEqual(site.has_field_changed("name"), False) |
| 647 | self.assertEqual(site.has_field_changed("login_base"), False) |
| 648 | |
| 649 | site.login_base = "bar" |
| 650 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 651 | self.assertEqual(site._dict, {"login_base": "bar", "name": "mysite"}) |
| 652 | self.assertEqual(site.diff, {"login_base": (None, "bar")}) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 653 | self.assertIn("name", site.changed_fields) |
| 654 | self.assertIn("login_base", site.changed_fields) |
| 655 | self.assertEqual(site.has_field_changed("name"), False) |
| 656 | self.assertEqual(site.has_field_changed("login_base"), True) |
| 657 | self.assertEqual(site.get_field_diff("login_base"), (None, "bar")) |
| 658 | |
| 659 | def test_ORMWrapper_existing_diff(self): |
| 660 | orm = self.make_coreapi() |
| 661 | site = orm.Site(name="mysite", login_base="foo") |
| 662 | site.save() |
| 663 | site = orm.Site.objects.first() |
| 664 | |
| 665 | self.assertEqual(site.is_new, False) |
| 666 | self.assertEqual(site._dict, {"id": 1, "name": "mysite", "login_base": "foo"}) |
| 667 | self.assertEqual(site.diff, {}) |
| 668 | self.assertEqual(site.changed_fields, []) |
| 669 | self.assertEqual(site.has_field_changed("name"), False) |
| 670 | self.assertEqual(site.has_field_changed("login_base"), False) |
| 671 | |
| 672 | site.login_base = "bar" |
| 673 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 674 | self.assertEqual(site._dict, {"id": 1, "login_base": "bar", "name": "mysite"}) |
| 675 | self.assertEqual(site.diff, {"login_base": ("foo", "bar")}) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 676 | self.assertIn("login_base", site.changed_fields) |
| 677 | self.assertEqual(site.has_field_changed("name"), False) |
| 678 | self.assertEqual(site.has_field_changed("login_base"), True) |
| 679 | |
| 680 | def test_ORMWrapper_diff_after_save(self): |
| 681 | orm = self.make_coreapi() |
| 682 | site = orm.Site(name="mysite", login_base="foo") |
| 683 | site.save() |
| 684 | site = orm.Site.objects.first() |
| 685 | |
| 686 | self.assertEqual(site.diff, {}) |
| 687 | |
| 688 | site.login_base = "bar" |
| 689 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 690 | self.assertEqual(site.diff, {"login_base": ("foo", "bar")}) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 691 | |
| 692 | site.save() |
| 693 | |
| 694 | self.assertEqual(site.diff, {}) |
| 695 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 696 | def test_ORMWrapper_recompute_initial(self): |
| 697 | """ For saved models, Recompute_initial should take recompute the set of initial values, removing all items |
| 698 | from the diff set. |
| 699 | """ |
| 700 | |
| 701 | orm = self.make_coreapi() |
| 702 | |
| 703 | testModel = orm.TestModel() |
| 704 | testModel.save() |
| 705 | |
| 706 | testModel.intfield = 9 |
| 707 | self.assertEqual(testModel.changed_fields, ["intfield"]) |
| 708 | |
| 709 | testModel.recompute_initial() |
| 710 | self.assertEqual(testModel.changed_fields, []) |
| 711 | |
| 712 | def test_ORMWrapper_create_attr(self): |
| 713 | orm = self.make_coreapi() |
| 714 | |
| 715 | testModel = orm.TestModel() |
| 716 | testModel.create_attr("some_new_attribute", "foo") |
| 717 | self.assertEqual(testModel.some_new_attribute, "foo") |
| 718 | |
| 719 | def test_ORMWrapper_save_changed_fields(self): |
| 720 | orm = self.make_coreapi() |
| 721 | |
| 722 | testModel = orm.TestModel(intfield=7, stringfield="foo") |
| 723 | testModel.save() |
| 724 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 725 | testModel.intfield = 9 |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 726 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 727 | with patch.object( |
| 728 | orm.grpc_stub, "UpdateTestModel", wraps=orm.grpc_stub.UpdateTestModel |
| 729 | ) as update: |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 730 | testModel.save_changed_fields() |
Scott Baker | 5dc7cc5 | 2019-01-18 15:05:27 -0800 | [diff] [blame] | 731 | |
| 732 | self.assertEqual(update.call_count, 1) |
| 733 | self.assertIn("metadata", update.call_args[1]) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 734 | update_fields_arg = [ |
| 735 | x[1] for x in update.call_args[1]["metadata"] if x[0] == "update_fields" |
| 736 | ] |
Scott Baker | 5dc7cc5 | 2019-01-18 15:05:27 -0800 | [diff] [blame] | 737 | self.assertEqual(update_fields_arg, ["intfield"]) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 738 | |
| 739 | def test_ORMWrapper_get_generic_foreignkeys(self): |
| 740 | """ Currently this is a placeholder that returns an empty list """ |
| 741 | |
| 742 | orm = self.make_coreapi() |
| 743 | |
| 744 | testModel = orm.TestModel() |
| 745 | self.assertEqual(testModel.get_generic_foreignkeys(), []) |
| 746 | |
| 747 | def test_ORMWrapper_gen_fkmap(self): |
| 748 | """ TestModelTwo includes a foreignkey relation to TestModel, and the fkmap should contain that relation """ |
| 749 | |
| 750 | orm = self.make_coreapi() |
| 751 | |
| 752 | testModelTwo = orm.TestModelTwo() |
| 753 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 754 | self.assertDictEqual( |
| 755 | testModelTwo.gen_fkmap(), |
| 756 | { |
| 757 | "testmodel": { |
| 758 | "kind": "fk", |
| 759 | "modelName": "TestModel", |
| 760 | "reverse_fieldName": "testmodeltwos", |
| 761 | "src_fieldName": "testmodel_id", |
| 762 | } |
| 763 | }, |
| 764 | ) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 765 | |
| 766 | def test_ORMWrapper_gen_reverse_fkmap(self): |
| 767 | """ TestModel includes a reverse relation back to TestModelTwo, and the reverse_fkmap should contain that |
| 768 | relation. |
| 769 | """ |
| 770 | |
| 771 | orm = self.make_coreapi() |
| 772 | |
| 773 | testModel = orm.TestModel() |
| 774 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 775 | self.assertDictEqual( |
| 776 | testModel.gen_reverse_fkmap(), |
| 777 | { |
| 778 | "testmodeltwos": { |
| 779 | "modelName": "TestModelTwo", |
| 780 | "src_fieldName": "testmodeltwos_ids", |
| 781 | "writeable": False, |
| 782 | } |
| 783 | }, |
| 784 | ) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 785 | |
| 786 | def test_ORMWrapper_fk_resolve(self): |
| 787 | """ If we create a TestModelTwo that has a foreign key reference to a TestModel, then calling fk_resolve should |
| 788 | return that model. |
| 789 | """ |
| 790 | |
| 791 | orm = self.make_coreapi() |
| 792 | |
| 793 | testModel = orm.TestModel() |
| 794 | testModel.save() |
| 795 | |
| 796 | testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id) |
| 797 | |
| 798 | testModel_resolved = testModelTwo.fk_resolve("testmodel") |
| 799 | self.assertEqual(testModel_resolved.id, testModel.id) |
| 800 | |
Scott Baker | 7ab456b | 2019-01-08 14:58:13 -0800 | [diff] [blame] | 801 | # the cache should have been populated |
| 802 | self.assertIn(("testmodel", testModel_resolved), testModelTwo.cache.items()) |
| 803 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 804 | def test_ORMWrapper_reverse_fk_resolve(self): |
| 805 | """ If a TestModelTwo has a relation to TestModel, then TestModel's reverse_fk should be resolvable to a list |
| 806 | of TestModelTwo objects. |
| 807 | """ |
| 808 | |
| 809 | orm = self.make_coreapi() |
| 810 | |
| 811 | testModel = orm.TestModel() |
| 812 | testModel.save() |
| 813 | |
| 814 | testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id) |
| 815 | testModelTwo.save() |
| 816 | |
| 817 | # fake_stub.py doesn't populate the reverse relations for us, so force what the server would have done... |
| 818 | testModel._wrapped_class.testmodeltwos_ids = [testModelTwo.id] |
| 819 | |
| 820 | testModelTwos_resolved = testModel.reverse_fk_resolve("testmodeltwos") |
| 821 | self.assertEqual(testModelTwos_resolved.count(), 1) |
| 822 | |
Scott Baker | 7ab456b | 2019-01-08 14:58:13 -0800 | [diff] [blame] | 823 | # the reverse_cache should have been populated |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 824 | self.assertIn( |
| 825 | ("testmodeltwos", testModelTwos_resolved), testModel.reverse_cache.items() |
| 826 | ) |
Scott Baker | 7ab456b | 2019-01-08 14:58:13 -0800 | [diff] [blame] | 827 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 828 | def test_ORMWrapper_fk_set(self): |
| 829 | """ fk_set will set the testmodel field on TesTModelTwo to point to the TestModel. """ |
| 830 | |
| 831 | orm = self.make_coreapi() |
| 832 | |
| 833 | testModel = orm.TestModel() |
| 834 | testModel.save() |
| 835 | |
| 836 | testModelTwo = orm.TestModelTwo() |
| 837 | |
| 838 | testModelTwo.fk_set("testmodel", testModel) |
| 839 | |
| 840 | self.assertEqual(testModelTwo.testmodel_id, testModel.id) |
| 841 | |
| 842 | def test_ORMWrapper_post_save_fixups_remove(self): |
| 843 | """ Apply a post_save_fixup that removes a reverse foreign key """ |
| 844 | |
| 845 | orm = self.make_coreapi() |
| 846 | |
| 847 | testModel = orm.TestModel() |
| 848 | testModel.save() |
| 849 | |
| 850 | testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id) |
| 851 | |
| 852 | # fake_stub.py doesn't populate the reverse relations for us, so force what the server would have done... |
| 853 | testModel._wrapped_class.testmodeltwos_ids = [testModelTwo.id] |
| 854 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 855 | post_save_fixups = [ |
| 856 | { |
| 857 | "src_fieldName": "testmodel", |
| 858 | "dest_id": None, # this field appears to not be used... |
| 859 | "dest_model": testModel, |
| 860 | "remove": True, |
| 861 | "reverse_fieldName": "testmodeltwos", |
| 862 | } |
| 863 | ] |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 864 | |
| 865 | testModelTwo.post_save_fixups = post_save_fixups |
| 866 | testModelTwo.do_post_save_fixups() |
| 867 | |
| 868 | self.assertEqual(testModel._wrapped_class.testmodeltwos_ids, []) |
| 869 | |
| 870 | def test_ORMWrapper_post_save_fixups_add(self): |
| 871 | """ Apply a post_save_fixup that adds a reverse foreign key """ |
| 872 | |
| 873 | orm = self.make_coreapi() |
| 874 | |
| 875 | testModel = orm.TestModel() |
| 876 | testModel.save() |
| 877 | |
| 878 | testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id) |
| 879 | testModelTwo.save() |
| 880 | |
| 881 | # Make sure the reverse_relation is unpopulated. This should be the case, as fake_stub.py() doesn't populate |
| 882 | # the reverse relation. But let's be sure, in case someone fixes that. |
| 883 | testModel._wrapped_class.testmodeltwos_ids = [] |
| 884 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 885 | post_save_fixups = [ |
| 886 | { |
| 887 | "src_fieldName": "testmodel", |
| 888 | "dest_id": None, # this field appears to not be used... |
| 889 | "dest_model": testModel, |
| 890 | "remove": False, |
| 891 | "reverse_fieldName": "testmodeltwos", |
| 892 | } |
| 893 | ] |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 894 | |
| 895 | testModelTwo.post_save_fixups = post_save_fixups |
| 896 | testModelTwo.do_post_save_fixups() |
| 897 | |
| 898 | self.assertEqual(testModel._wrapped_class.testmodeltwos_ids, [testModelTwo.id]) |
| 899 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 900 | def test_ORMWrapper_tologdict(self): |
| 901 | """ Tologdict contains the model name and id, used for structured logging """ |
| 902 | orm = self.make_coreapi() |
| 903 | |
| 904 | testModel = orm.TestModel(intfield=7, stringfile="foo") |
| 905 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 906 | self.assertDictEqual( |
| 907 | testModel.tologdict(), {"model_name": "TestModel", "pk": 0} |
| 908 | ) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 909 | |
| 910 | def test_ORMWrapper_ansible_tag(self): |
| 911 | """ Ansible_tag is used by old-style synchronizers. Deprecated. """ |
| 912 | |
| 913 | orm = self.make_coreapi() |
| 914 | |
| 915 | testModel = orm.TestModel(id=7) |
| 916 | |
| 917 | self.assertEqual(testModel.ansible_tag, "TestModel_7") |
| 918 | |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 919 | def test_deleted_objects_all(self): |
| 920 | orm = self.make_coreapi() |
| 921 | orig_len_sites = len(orm.Site.objects.all()) |
| 922 | orig_len_deleted_sites = len(orm.Site.deleted_objects.all()) |
| 923 | site = orm.Site(name="mysite") |
| 924 | site.save() |
| 925 | site.delete() |
| 926 | sites = orm.Site.objects.all() |
| 927 | self.assertEqual(len(sites), orig_len_sites) |
| 928 | deleted_sites = orm.Site.deleted_objects.all() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 929 | self.assertEqual(len(deleted_sites), orig_len_deleted_sites + 1) |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 930 | |
| 931 | def test_deleted_objects_filter(self): |
| 932 | orm = self.make_coreapi() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 933 | with patch.object( |
| 934 | orm.grpc_stub, "FilterTestModel", wraps=orm.grpc_stub.FilterTestModel |
| 935 | ) as filter: |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 936 | foo = orm.TestModel(name="foo") |
| 937 | foo.save() |
| 938 | foo.delete() |
| 939 | |
| 940 | # There should be no live objects |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 941 | objs = orm.TestModel.objects.filter(name="foo") |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 942 | self.assertEqual(len(objs), 0) |
| 943 | |
| 944 | # There should be one deleted object |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 945 | deleted_objs = orm.TestModel.deleted_objects.filter(name="foo") |
Scott Baker | ea1f4d0 | 2018-12-17 10:21:50 -0800 | [diff] [blame] | 946 | self.assertEqual(len(deleted_objs), 1) |
| 947 | |
| 948 | # Two calls, one for when we checked live objects, the other for when we checked deleted objects |
| 949 | self.assertEqual(filter.call_count, 2) |
| 950 | q = filter.call_args[0][0] |
| 951 | |
| 952 | # Now spy on the query that was generated, to make sure it looks like we expect |
| 953 | self.assertEqual(q.kind, q.SYNCHRONIZER_DELETED_OBJECTS) |
| 954 | self.assertEqual(len(q.elements), 1) |
| 955 | self.assertEqual(q.elements[0].operator, q.elements[0].EQUAL) |
| 956 | self.assertEqual(q.elements[0].sValue, "foo") |
| 957 | |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 958 | def test_ORMQuerySet_first_nonempty(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 959 | qs = self.ORMQuerySet([1, 2, 3]) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 960 | self.assertEqual(qs.first(), 1) |
| 961 | |
| 962 | def test_ORMQuerySet_first_empty(self): |
| 963 | qs = self.ORMQuerySet([]) |
| 964 | self.assertEqual(qs.first(), None) |
| 965 | |
| 966 | def test_ORMQuerySet_exists_nonempty(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 967 | qs = self.ORMQuerySet([1, 2, 3]) |
Scott Baker | 9e47725 | 2019-01-07 11:49:45 -0800 | [diff] [blame] | 968 | self.assertEqual(qs.exists(), True) |
| 969 | |
| 970 | def test_ORMQuerySet_exists_empty(self): |
| 971 | qs = self.ORMQuerySet() |
| 972 | self.assertEqual(qs.exists(), False) |
| 973 | |
| 974 | def test_ORMLocalObjectManager_nonempty(self): |
| 975 | """ Test all(), first(), exists(), and count() together since they're all closely related. Use a nonempty |
| 976 | list. |
| 977 | """ |
| 978 | orm = self.make_coreapi() |
| 979 | |
| 980 | t = orm.TestModel() |
| 981 | t.save() |
| 982 | |
| 983 | lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], False) |
| 984 | self.assertEqual(len(lobjs.all()), 1) |
| 985 | self.assertEqual(lobjs.all()[0].id, t.id) |
| 986 | self.assertEqual(lobjs.exists(), True) |
| 987 | self.assertEqual(lobjs.count(), 1) |
| 988 | self.assertEqual(lobjs.first().id, t.id) |
| 989 | |
| 990 | def test_ORMLocalObjectManager_empty(self): |
| 991 | """ Test all(), first(), exists(), and count() together since they're all closely related. Use an empty |
| 992 | list. |
| 993 | """ |
| 994 | orm = self.make_coreapi() |
| 995 | |
| 996 | t = orm.TestModel() |
| 997 | t.save() |
| 998 | |
| 999 | lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [], False) |
| 1000 | self.assertEqual(len(lobjs.all()), 0) |
| 1001 | self.assertEqual(lobjs.exists(), False) |
| 1002 | self.assertEqual(lobjs.count(), 0) |
| 1003 | self.assertEqual(lobjs.first(), None) |
| 1004 | |
| 1005 | def test_ORMLocalObjectManager_not_writeable(self): |
| 1006 | """ An ORMLocalObjectManager that is not writeable should throw exceptions on add() and remove() """ |
| 1007 | orm = self.make_coreapi() |
| 1008 | |
| 1009 | t = orm.TestModel() |
| 1010 | t.save() |
| 1011 | |
| 1012 | lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], False) |
| 1013 | |
| 1014 | with self.assertRaises(Exception) as e: |
| 1015 | lobjs.add(123) |
| 1016 | self.assertEqual(e.exception.message, "Only ManyToMany lists are writeable") |
| 1017 | |
| 1018 | with self.assertRaises(Exception) as e: |
| 1019 | lobjs.remove(123) |
| 1020 | self.assertEqual(e.exception.message, "Only ManyToMany lists are writeable") |
| 1021 | |
| 1022 | def test_ORMLocalObjectManager_add(self): |
| 1023 | orm = self.make_coreapi() |
| 1024 | |
| 1025 | t = orm.TestModel() |
| 1026 | t.save() |
| 1027 | |
| 1028 | lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [], True) |
| 1029 | lobjs.add(t) |
| 1030 | self.assertEqual(lobjs.count(), 1) |
| 1031 | self.assertEqual(lobjs.first().id, t.id) |
| 1032 | |
| 1033 | def test_ORMLocalObjectManager_remove(self): |
| 1034 | orm = self.make_coreapi() |
| 1035 | |
| 1036 | t = orm.TestModel() |
| 1037 | t.save() |
| 1038 | |
| 1039 | lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], True) |
| 1040 | lobjs.remove(t) |
| 1041 | self.assertEqual(lobjs.count(), 0) |
Scott Baker | 5b7fba0 | 2018-10-17 08:46:46 -0700 | [diff] [blame] | 1042 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 1043 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1044 | def main(): |
| 1045 | global USE_FAKE_STUB |
| 1046 | global xos_grpc_client |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 1047 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1048 | # Command-line argument of -R will cause this test to use a real grpc server |
| 1049 | # rather than the fake stub. |
Scott Baker | ff104cc | 2017-08-14 15:24:41 -0700 | [diff] [blame] | 1050 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1051 | if "-R" in sys.argv: |
| 1052 | USE_FAKE_STUB = False |
| 1053 | sys.argv.remove("-R") |
| 1054 | # Note: will leave lots of litter (users, sites, etc) behind in the database |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 1055 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1056 | if USE_FAKE_STUB: |
| 1057 | unittest.main() |
| 1058 | else: |
| 1059 | # This assumes xos-client python library is installed, and a gRPC server |
| 1060 | # is available. |
Scott Baker | d194097 | 2017-05-01 15:45:32 -0700 | [diff] [blame] | 1061 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1062 | from xosapi import xos_grpc_client |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 1063 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 1064 | print("Using xos-client library and core server") |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 1065 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1066 | def test_callback(): |
| 1067 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 1068 | sys.argv = sys.argv[ |
| 1069 | :1 |
| 1070 | ] # unittest does not like xos_grpc_client's command line arguments (TODO: find a cooperative approach) |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1071 | unittest.main() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 1072 | except exceptions.SystemExit as e: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1073 | global exitStatus |
| 1074 | exitStatus = e.code |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 1075 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1076 | xos_grpc_client.start_api_parseargs(test_callback) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 1077 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1078 | sys.exit(exitStatus) |
Scott Baker | f0ee0dc | 2017-05-15 10:10:05 -0700 | [diff] [blame] | 1079 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 1080 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 1081 | if __name__ == "__main__": |
| 1082 | main() |