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