blob: 86be969cea7d687ab3494f9c0c7db50e14c08c58 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# 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 Williams045b63d2019-01-22 16:30:57 -070015from __future__ import print_function
Zack Williams5c2ea232019-01-30 15:23:01 -070016
Scott Baker7dddd512017-10-24 10:13:34 -070017import os
Scott Bakerff104cc2017-08-14 15:24:41 -070018import random
Scott Bakerff104cc2017-08-14 15:24:41 -070019import string
Scott Bakerd1940972017-05-01 15:45:32 -070020import sys
21import unittest
Zack Williams5c2ea232019-01-30 15:23:01 -070022
23try: # python 3
24 from io import StringIO
25 from unittest.mock import patch
26except ImportError: # python 2
27 from StringIO import StringIO
28 from mock import patch
29
Scott Bakerd1940972017-05-01 15:45:32 -070030
Scott Baker7dddd512017-10-24 10:13:34 -070031# by default, use fake stub rather than real core
Zack Williams045b63d2019-01-22 16:30:57 -070032USE_FAKE_STUB = True
Scott Bakerd1940972017-05-01 15:45:32 -070033
Zack Williams045b63d2019-01-22 16:30:57 -070034PARENT_DIR = os.path.join(os.path.dirname(__file__), "..")
35
Scott Bakerd1940972017-05-01 15:45:32 -070036
37class TestORM(unittest.TestCase):
Scott Baker7dddd512017-10-24 10:13:34 -070038 def setUp(self):
Scott Bakere5f0f682018-06-18 10:21:35 -070039 from xosconfig import Config
Zack Williams045b63d2019-01-22 16:30:57 -070040
Scott Bakere5f0f682018-06-18 10:21:35 -070041 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 Williams045b63d2019-01-22 16:30:57 -070044 Config.init(config, "synchronizer-config-schema.yaml")
45 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070046 sys.path.append(PARENT_DIR)
47
Scott Baker9e477252019-01-07 11:49:45 -080048 # Import these after config, in case they depend on config
49 from xosapi.orm import ORMQuerySet, ORMLocalObjectManager
Zack Williams045b63d2019-01-22 16:30:57 -070050
Scott Baker9e477252019-01-07 11:49:45 -080051 self.ORMQuerySet = ORMQuerySet
52 self.ORMLocalObjectManager = ORMLocalObjectManager
53
Scott Baker7dddd512017-10-24 10:13:34 -070054 def tearDown(self):
Zack Williams045b63d2019-01-22 16:30:57 -070055 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070056 sys.path.remove(PARENT_DIR)
57
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070058 def make_coreapi(self):
59 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070060 import xosapi.orm
Scott Bakerb96ba432018-02-26 09:53:48 -080061 from fake_stub import FakeStub, FakeProtos, FakeObj
Scott Baker7dddd512017-10-24 10:13:34 -070062
Scott Bakerc0017032018-05-01 10:54:26 -070063 xosapi.orm.import_convenience_methods()
64
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070065 stub = FakeStub()
Zack Williams045b63d2019-01-22 16:30:57 -070066 api = xosapi.orm.ORMStub(
67 stub=stub,
68 package_name="xos",
69 protos=FakeProtos(),
70 empty=FakeObj,
71 enable_backoff=False,
72 )
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070073 return api
74 else:
75 return xos_grpc_client.coreapi
76
Scott Bakerd1940972017-05-01 15:45:32 -070077 def test_repr_name(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070078 orm = self.make_coreapi()
79 s = orm.Slice(name="foo")
Scott Bakerd1940972017-05-01 15:45:32 -070080 self.assertNotEqual(s, None)
81 self.assertEqual(repr(s), "<Slice: foo>")
82
83 def test_str_name(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070084 orm = self.make_coreapi()
85 s = orm.Slice(name="foo")
Scott Bakerd1940972017-05-01 15:45:32 -070086 self.assertNotEqual(s, None)
87 self.assertEqual(str(s), "foo")
88
89 def test_dumpstr_name(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070090 orm = self.make_coreapi()
91 s = orm.Slice(name="foo")
Scott Bakerd1940972017-05-01 15:45:32 -070092 self.assertNotEqual(s, None)
93 self.assertEqual(s.dumpstr(), 'name: "foo"\n')
94
95 def test_repr_noname(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070096 orm = self.make_coreapi()
97 s = orm.Slice()
Scott Bakerd1940972017-05-01 15:45:32 -070098 self.assertNotEqual(s, None)
99 self.assertEqual(repr(s), "<Slice: id-0>")
100
101 def test_str_noname(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700102 orm = self.make_coreapi()
103 s = orm.Slice()
Scott Bakerd1940972017-05-01 15:45:32 -0700104 self.assertNotEqual(s, None)
105 self.assertEqual(str(s), "Slice-0")
106
107 def test_dumpstr_noname(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -0700108 orm = self.make_coreapi()
109 s = orm.Slice()
Scott Bakerd1940972017-05-01 15:45:32 -0700110 self.assertNotEqual(s, None)
Zack Williams045b63d2019-01-22 16:30:57 -0700111 self.assertEqual(s.dumpstr(), "")
Scott Bakerd1940972017-05-01 15:45:32 -0700112
Scott Baker9e477252019-01-07 11:49:45 -0800113 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 Baker500f8c72017-05-19 09:41:50 -0700123 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 Williams045b63d2019-01-22 16:30:57 -0700134 got_site = orm.Site.objects.get(id=site.id)
Scott Baker500f8c72017-05-19 09:41:50 -0700135 self.assertNotEqual(got_site, None)
136 self.assertEqual(got_site.id, site.id)
137
Scott Baker9e477252019-01-07 11:49:45 -0800138 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 Baker9e477252019-01-07 11:49:45 -0800153 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 Williams045b63d2019-01-22 16:30:57 -0700165 self.assertEqual(len(orm.Site.objects.all()), orig_len_sites + 1)
Scott Baker9e477252019-01-07 11:49:45 -0800166
167 # retrieve the site, and update it
168 created_site_id = site.id
169 site = orm.Site.objects.get(id=created_site_id)
Zack Williams045b63d2019-01-22 16:30:57 -0700170 site.name = "mysitetwo"
Scott Baker9e477252019-01-07 11:49:45 -0800171 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 Baker500f8c72017-05-19 09:41:50 -0700182 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 Williams045b63d2019-01-22 16:30:57 -0700198 self.assertEqual(len(sites), orig_len_sites + 1)
Scott Baker500f8c72017-05-19 09:41:50 -0700199
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 Williams045b63d2019-01-22 16:30:57 -0700209 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 Baker500f8c72017-05-19 09:41:50 -0700212
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 Williams045b63d2019-01-22 16:30:57 -0700218 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 Bakerff104cc2017-08-14 15:24:41 -0700225 user.save()
226 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700227 slice = orm.Slice(name="mysite_foo", site_id=site.id, creator_id=user.id)
Scott Baker500f8c72017-05-19 09:41:50 -0700228 slice.save()
229 self.assertTrue(slice.id > 0)
230 self.assertNotEqual(slice.site, None)
231 self.assertEqual(slice.site.id, site.id)
232
Scott Bakerc4156c32017-12-08 10:58:21 -0800233 def test_foreign_key_set_with_invalidate(self):
Scott Baker500f8c72017-05-19 09:41:50 -0700234 orm = self.make_coreapi()
235 site = orm.Site(name="mysite")
236 site.save()
237 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700238 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 Bakerff104cc2017-08-14 15:24:41 -0700245 user.save()
246 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700247 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Baker500f8c72017-05-19 09:41:50 -0700248 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 Bakerc4156c32017-12-08 10:58:21 -0800253 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 Williams045b63d2019-01-22 16:30:57 -0700261 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 Bakerc4156c32017-12-08 10:58:21 -0800268 user.save()
269 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700270 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800271 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 Williams045b63d2019-01-22 16:30:57 -0700285 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 Bakerc4156c32017-12-08 10:58:21 -0800292 user.save()
293 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700294 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800295 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 Williams045b63d2019-01-22 16:30:57 -0700324 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 Bakerc4156c32017-12-08 10:58:21 -0800331 user.save()
332 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700333 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800334 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 Williams045b63d2019-01-22 16:30:57 -0700362 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 Bakerc4156c32017-12-08 10:58:21 -0800369 user.save()
370 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700371 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800372 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 Baker500f8c72017-05-19 09:41:50 -0700395
Scott Bakera1eae7a2017-06-06 09:20:15 -0700396 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 Williams045b63d2019-01-22 16:30:57 -0700401 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 Bakerff104cc2017-08-14 15:24:41 -0700408 user.save()
409 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700410 slice = orm.Slice(
411 name="mysite_foo", site=site, service=None, creator_id=user.id
412 )
Scott Bakera1eae7a2017-06-06 09:20:15 -0700413 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 Williams045b63d2019-01-22 16:30:57 -0700423 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 Bakerff104cc2017-08-14 15:24:41 -0700430 user.save()
431 self.assertTrue(user.id > 0)
Scott Bakera1eae7a2017-06-06 09:20:15 -0700432 service = orm.Service(name="myservice")
433 service.save()
434 self.assertTrue(service.id > 0)
435 # start out slice.service is non-None
Zack Williams045b63d2019-01-22 16:30:57 -0700436 slice = orm.Slice(
437 name="mysite_foo", site=site, service=service, creator_id=user.id
438 )
Scott Bakera1eae7a2017-06-06 09:20:15 -0700439 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 Baker500f8c72017-05-19 09:41:50 -0700450 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 Williams045b63d2019-01-22 16:30:57 -0700457 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 Baker500f8c72017-05-19 09:41:50 -0700464 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 Bakerd0f1dc12018-04-23 12:05:32 -0700469 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 Williams045b63d2019-01-22 16:30:57 -0700476 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 Bakerd0f1dc12018-04-23 12:05:32 -0700483 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 Williams045b63d2019-01-22 16:30:57 -0700495 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 Bakerd0f1dc12018-04-23 12:05:32 -0700502 tag.save()
503 self.assertTrue(tag.id > 0)
504 with self.assertRaises(Exception) as e:
Zack Williams045b63d2019-01-22 16:30:57 -0700505
506 self.assertEqual(
Zack Williams5c2ea232019-01-30 15:23:01 -0700507 str(e.exception),
Zack Williams045b63d2019-01-22 16:30:57 -0700508 "Content_type does_not_exist not found in self.content_type_map",
509 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700510
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 Williams045b63d2019-01-22 16:30:57 -0700518 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 Bakerd0f1dc12018-04-23 12:05:32 -0700525 tag.save()
526 self.assertTrue(tag.id > 0)
527 with self.assertRaises(Exception) as e:
Zack Williams045b63d2019-01-22 16:30:57 -0700528 self.assertEqual(
Zack Williams5c2ea232019-01-30 15:23:01 -0700529 str(e.exception), "Object 4567 of model Site was not found"
Zack Williams045b63d2019-01-22 16:30:57 -0700530 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700531
Scott Baker500f8c72017-05-19 09:41:50 -0700532 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 Bakerff104cc2017-08-14 15:24:41 -0700549 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 Baker37cf9e22018-08-20 14:39:33 -0700572 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 Williams045b63d2019-01-22 16:30:57 -0700608 self.assertEqual(
Zack Williams5c2ea232019-01-30 15:23:01 -0700609 str(e.exception),
Zack Williams045b63d2019-01-22 16:30:57 -0700610 "Setting a non-foreignkey field to None is not supported",
611 )
Scott Baker37cf9e22018-08-20 14:39:33 -0700612
Scott Bakere1607b82018-09-20 14:10:59 -0700613 def test_query_iexact(self):
614 orm = self.make_coreapi()
615 with patch.object(orm.grpc_stub, "FilterTestModel", autospec=True) as filter:
Zack Williams045b63d2019-01-22 16:30:57 -0700616 orm.TestModel.objects.filter(name__iexact="foo")
Scott Bakere1607b82018-09-20 14:10:59 -0700617 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 Williams045b63d2019-01-22 16:30:57 -0700628 orm.TestModel.objects.filter(name="foo")
Scott Bakere1607b82018-09-20 14:10:59 -0700629 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 Baker9e477252019-01-07 11:49:45 -0800637 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 Baker5b7fba02018-10-17 08:46:46 -0700644 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 Williams045b63d2019-01-22 16:30:57 -0700657 self.assertEqual(site._dict, {"login_base": "bar", "name": "mysite"})
658 self.assertEqual(site.diff, {"login_base": (None, "bar")})
Scott Baker5b7fba02018-10-17 08:46:46 -0700659 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 Williams045b63d2019-01-22 16:30:57 -0700680 self.assertEqual(site._dict, {"id": 1, "login_base": "bar", "name": "mysite"})
681 self.assertEqual(site.diff, {"login_base": ("foo", "bar")})
Scott Baker5b7fba02018-10-17 08:46:46 -0700682 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 Williams045b63d2019-01-22 16:30:57 -0700696 self.assertEqual(site.diff, {"login_base": ("foo", "bar")})
Scott Baker5b7fba02018-10-17 08:46:46 -0700697
698 site.save()
699
700 self.assertEqual(site.diff, {})
701
Scott Baker9e477252019-01-07 11:49:45 -0800702 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 Williams045b63d2019-01-22 16:30:57 -0700731 testModel.intfield = 9
Scott Baker9e477252019-01-07 11:49:45 -0800732
Zack Williams045b63d2019-01-22 16:30:57 -0700733 with patch.object(
734 orm.grpc_stub, "UpdateTestModel", wraps=orm.grpc_stub.UpdateTestModel
735 ) as update:
Scott Baker9e477252019-01-07 11:49:45 -0800736 testModel.save_changed_fields()
Scott Baker5dc7cc52019-01-18 15:05:27 -0800737
738 self.assertEqual(update.call_count, 1)
739 self.assertIn("metadata", update.call_args[1])
Zack Williams045b63d2019-01-22 16:30:57 -0700740 update_fields_arg = [
741 x[1] for x in update.call_args[1]["metadata"] if x[0] == "update_fields"
742 ]
Scott Baker5dc7cc52019-01-18 15:05:27 -0800743 self.assertEqual(update_fields_arg, ["intfield"])
Scott Baker9e477252019-01-07 11:49:45 -0800744
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 Williams045b63d2019-01-22 16:30:57 -0700760 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 Baker9e477252019-01-07 11:49:45 -0800771
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 Williams045b63d2019-01-22 16:30:57 -0700781 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 Baker9e477252019-01-07 11:49:45 -0800791
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 Baker7ab456b2019-01-08 14:58:13 -0800807 # the cache should have been populated
808 self.assertIn(("testmodel", testModel_resolved), testModelTwo.cache.items())
809
Scott Baker9e477252019-01-07 11:49:45 -0800810 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 Baker7ab456b2019-01-08 14:58:13 -0800829 # the reverse_cache should have been populated
Zack Williams045b63d2019-01-22 16:30:57 -0700830 self.assertIn(
831 ("testmodeltwos", testModelTwos_resolved), testModel.reverse_cache.items()
832 )
Scott Baker7ab456b2019-01-08 14:58:13 -0800833
Scott Baker9e477252019-01-07 11:49:45 -0800834 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 Williams045b63d2019-01-22 16:30:57 -0700861 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 Baker9e477252019-01-07 11:49:45 -0800870
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 Williams045b63d2019-01-22 16:30:57 -0700891 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 Baker9e477252019-01-07 11:49:45 -0800900
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 Baker9e477252019-01-07 11:49:45 -0800906 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 Williams045b63d2019-01-22 16:30:57 -0700912 self.assertDictEqual(
913 testModel.tologdict(), {"model_name": "TestModel", "pk": 0}
914 )
Scott Baker9e477252019-01-07 11:49:45 -0800915
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 Bakerea1f4d02018-12-17 10:21:50 -0800925 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 Williams045b63d2019-01-22 16:30:57 -0700935 self.assertEqual(len(deleted_sites), orig_len_deleted_sites + 1)
Scott Bakerea1f4d02018-12-17 10:21:50 -0800936
937 def test_deleted_objects_filter(self):
938 orm = self.make_coreapi()
Zack Williams045b63d2019-01-22 16:30:57 -0700939 with patch.object(
940 orm.grpc_stub, "FilterTestModel", wraps=orm.grpc_stub.FilterTestModel
941 ) as filter:
Scott Bakerea1f4d02018-12-17 10:21:50 -0800942 foo = orm.TestModel(name="foo")
943 foo.save()
944 foo.delete()
945
946 # There should be no live objects
Zack Williams045b63d2019-01-22 16:30:57 -0700947 objs = orm.TestModel.objects.filter(name="foo")
Scott Bakerea1f4d02018-12-17 10:21:50 -0800948 self.assertEqual(len(objs), 0)
949
950 # There should be one deleted object
Zack Williams045b63d2019-01-22 16:30:57 -0700951 deleted_objs = orm.TestModel.deleted_objects.filter(name="foo")
Scott Bakerea1f4d02018-12-17 10:21:50 -0800952 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 Baker9e477252019-01-07 11:49:45 -0800964 def test_ORMQuerySet_first_nonempty(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700965 qs = self.ORMQuerySet([1, 2, 3])
Scott Baker9e477252019-01-07 11:49:45 -0800966 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 Williams045b63d2019-01-22 16:30:57 -0700973 qs = self.ORMQuerySet([1, 2, 3])
Scott Baker9e477252019-01-07 11:49:45 -0800974 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 Williams5c2ea232019-01-30 15:23:01 -07001022 self.assertEqual(str(e.exception), "Only ManyToMany lists are writeable")
Scott Baker9e477252019-01-07 11:49:45 -08001023
1024 with self.assertRaises(Exception) as e:
1025 lobjs.remove(123)
Zack Williams5c2ea232019-01-30 15:23:01 -07001026 self.assertEqual(str(e.exception), "Only ManyToMany lists are writeable")
Scott Baker9e477252019-01-07 11:49:45 -08001027
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 Baker5b7fba02018-10-17 08:46:46 -07001048
Zack Williams045b63d2019-01-22 16:30:57 -07001049
Scott Baker7dddd512017-10-24 10:13:34 -07001050def main():
1051 global USE_FAKE_STUB
1052 global xos_grpc_client
Scott Bakerff104cc2017-08-14 15:24:41 -07001053
Scott Baker7dddd512017-10-24 10:13:34 -07001054 # Command-line argument of -R will cause this test to use a real grpc server
1055 # rather than the fake stub.
Scott Bakerff104cc2017-08-14 15:24:41 -07001056
Scott Baker7dddd512017-10-24 10:13:34 -07001057 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 Bakerd1940972017-05-01 15:45:32 -07001061
Scott Baker7dddd512017-10-24 10:13:34 -07001062 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 Bakerd1940972017-05-01 15:45:32 -07001067
Scott Baker7dddd512017-10-24 10:13:34 -07001068 from xosapi import xos_grpc_client
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001069
Zack Williams045b63d2019-01-22 16:30:57 -07001070 print("Using xos-client library and core server")
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001071
Scott Baker7dddd512017-10-24 10:13:34 -07001072 def test_callback():
1073 try:
Zack Williams045b63d2019-01-22 16:30:57 -07001074 sys.argv = sys.argv[
1075 :1
Zack Williams5c2ea232019-01-30 15:23:01 -07001076 ]
1077 # unittest does not like xos_grpc_client's command line
1078 # arguments (TODO: find a cooperative approach)
Scott Baker7dddd512017-10-24 10:13:34 -07001079 unittest.main()
Zack Williams5c2ea232019-01-30 15:23:01 -07001080 except SystemExit as e:
Scott Baker7dddd512017-10-24 10:13:34 -07001081 global exitStatus
1082 exitStatus = e.code
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001083
Scott Baker7dddd512017-10-24 10:13:34 -07001084 xos_grpc_client.start_api_parseargs(test_callback)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001085
Scott Baker7dddd512017-10-24 10:13:34 -07001086 sys.exit(exitStatus)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001087
Zack Williams045b63d2019-01-22 16:30:57 -07001088
Scott Baker7dddd512017-10-24 10:13:34 -07001089if __name__ == "__main__":
1090 main()