blob: 88bf6d43bbfb80294a71a25541a0aebc825e315e [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
Scott Bakerd1940972017-05-01 15:45:32 -070016import exceptions
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 Williams045b63d2019-01-22 16:30:57 -070022from mock import patch
Scott Baker9e477252019-01-07 11:49:45 -080023from StringIO import StringIO
Scott Bakerd1940972017-05-01 15:45:32 -070024
Scott Baker7dddd512017-10-24 10:13:34 -070025# by default, use fake stub rather than real core
Zack Williams045b63d2019-01-22 16:30:57 -070026USE_FAKE_STUB = True
Scott Bakerd1940972017-05-01 15:45:32 -070027
Zack Williams045b63d2019-01-22 16:30:57 -070028PARENT_DIR = os.path.join(os.path.dirname(__file__), "..")
29
Scott Bakerd1940972017-05-01 15:45:32 -070030
31class TestORM(unittest.TestCase):
Scott Baker7dddd512017-10-24 10:13:34 -070032 def setUp(self):
Scott Bakere5f0f682018-06-18 10:21:35 -070033 from xosconfig import Config
Zack Williams045b63d2019-01-22 16:30:57 -070034
Scott Bakere5f0f682018-06-18 10:21:35 -070035 test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
36 config = os.path.join(test_path, "test_config.yaml")
37 Config.clear()
Zack Williams045b63d2019-01-22 16:30:57 -070038 Config.init(config, "synchronizer-config-schema.yaml")
39 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070040 sys.path.append(PARENT_DIR)
41
Scott Baker9e477252019-01-07 11:49:45 -080042 # Import these after config, in case they depend on config
43 from xosapi.orm import ORMQuerySet, ORMLocalObjectManager
Zack Williams045b63d2019-01-22 16:30:57 -070044
Scott Baker9e477252019-01-07 11:49:45 -080045 self.ORMQuerySet = ORMQuerySet
46 self.ORMLocalObjectManager = ORMLocalObjectManager
47
Scott Baker7dddd512017-10-24 10:13:34 -070048 def tearDown(self):
Zack Williams045b63d2019-01-22 16:30:57 -070049 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070050 sys.path.remove(PARENT_DIR)
51
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070052 def make_coreapi(self):
53 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070054 import xosapi.orm
Scott Bakerb96ba432018-02-26 09:53:48 -080055 from fake_stub import FakeStub, FakeProtos, FakeObj
Scott Baker7dddd512017-10-24 10:13:34 -070056
Scott Bakerc0017032018-05-01 10:54:26 -070057 xosapi.orm.import_convenience_methods()
58
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070059 stub = FakeStub()
Zack Williams045b63d2019-01-22 16:30:57 -070060 api = xosapi.orm.ORMStub(
61 stub=stub,
62 package_name="xos",
63 protos=FakeProtos(),
64 empty=FakeObj,
65 enable_backoff=False,
66 )
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070067 return api
68 else:
69 return xos_grpc_client.coreapi
70
Scott Bakerd1940972017-05-01 15:45:32 -070071 def test_repr_name(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070072 orm = self.make_coreapi()
73 s = orm.Slice(name="foo")
Scott Bakerd1940972017-05-01 15:45:32 -070074 self.assertNotEqual(s, None)
75 self.assertEqual(repr(s), "<Slice: foo>")
76
77 def test_str_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(str(s), "foo")
82
83 def test_dumpstr_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(s.dumpstr(), 'name: "foo"\n')
88
89 def test_repr_noname(self):
Scott Bakerf0ee0dc2017-05-15 10:10:05 -070090 orm = self.make_coreapi()
91 s = orm.Slice()
Scott Bakerd1940972017-05-01 15:45:32 -070092 self.assertNotEqual(s, None)
93 self.assertEqual(repr(s), "<Slice: id-0>")
94
95 def test_str_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(str(s), "Slice-0")
100
101 def test_dumpstr_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)
Zack Williams045b63d2019-01-22 16:30:57 -0700105 self.assertEqual(s.dumpstr(), "")
Scott Bakerd1940972017-05-01 15:45:32 -0700106
Scott Baker9e477252019-01-07 11:49:45 -0800107 def test_dump(self):
108 """ dump() is like dumpstr() but prints to stdout. Mock stdout by using a stringIO. """
109
110 orm = self.make_coreapi()
111 s = orm.Slice(name="foo")
112 self.assertNotEqual(s, None)
113 with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
114 s.dump()
115 self.assertEqual(mock_stdout.getvalue(), 'name: "foo"\n\n')
116
Scott Baker500f8c72017-05-19 09:41:50 -0700117 def test_create(self):
118 orm = self.make_coreapi()
119 site = orm.Site(name="mysite")
120 site.save()
121 self.assertTrue(site.id > 0)
122
123 def test_get(self):
124 orm = self.make_coreapi()
125 site = orm.Site(name="mysite")
126 site.save()
127 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700128 got_site = orm.Site.objects.get(id=site.id)
Scott Baker500f8c72017-05-19 09:41:50 -0700129 self.assertNotEqual(got_site, None)
130 self.assertEqual(got_site.id, site.id)
131
Scott Baker9e477252019-01-07 11:49:45 -0800132 def test_invalidate_cache(self):
133 orm = self.make_coreapi()
134 testModel = orm.TestModel()
135
136 # populate the caches with some placeholders we can test for
137 testModel.cache = {"a": 1}
138 testModel.reverse_cache = {"b": 2}
139
140 testModel.invalidate_cache()
141
142 self.assertEqual(testModel.cache, {})
143 self.assertEqual(testModel.reverse_cache, {})
144
145 def test_save_new(self):
146 orm = self.make_coreapi()
Scott Baker9e477252019-01-07 11:49:45 -0800147 site = orm.Site(name="mysite")
148 site.save()
149 self.assertTrue(site.id > 0)
150
151 def test_save_existing(self):
152 orm = self.make_coreapi()
153 orig_len_sites = len(orm.Site.objects.all())
154 site = orm.Site(name="mysite")
155 site.save()
156 self.assertTrue(site.id > 0)
157
158 # there should be one new site
Zack Williams045b63d2019-01-22 16:30:57 -0700159 self.assertEqual(len(orm.Site.objects.all()), orig_len_sites + 1)
Scott Baker9e477252019-01-07 11:49:45 -0800160
161 # retrieve the site, and update it
162 created_site_id = site.id
163 site = orm.Site.objects.get(id=created_site_id)
Zack Williams045b63d2019-01-22 16:30:57 -0700164 site.name = "mysitetwo"
Scott Baker9e477252019-01-07 11:49:45 -0800165 site.save()
166
167 # the site_id should not have changed
168 self.assertEqual(site.id, created_site_id)
169
170 # there should still be only one new site
171 self.assertEqual(len(orm.Site.objects.all()), orig_len_sites + 1)
172
173 # the name should have changed
174 self.assertEqual(orm.Site.objects.get(id=created_site_id).name, "mysitetwo")
175
Scott Baker500f8c72017-05-19 09:41:50 -0700176 def test_delete(self):
177 orm = self.make_coreapi()
178 orig_len_sites = len(orm.Site.objects.all())
179 site = orm.Site(name="mysite")
180 site.save()
181 self.assertTrue(site.id > 0)
182 site.delete()
183 sites = orm.Site.objects.all()
184 self.assertEqual(len(sites), orig_len_sites)
185
186 def test_objects_all(self):
187 orm = self.make_coreapi()
188 orig_len_sites = len(orm.Site.objects.all())
189 site = orm.Site(name="mysite")
190 site.save()
191 sites = orm.Site.objects.all()
Zack Williams045b63d2019-01-22 16:30:57 -0700192 self.assertEqual(len(sites), orig_len_sites + 1)
Scott Baker500f8c72017-05-19 09:41:50 -0700193
194 def test_objects_first(self):
195 orm = self.make_coreapi()
196 site = orm.Site(name="mysite")
197 site.save()
198 site = orm.Site.objects.first()
199 self.assertNotEqual(site, None)
200
201 def test_content_type_map(self):
202 orm = self.make_coreapi()
Zack Williams045b63d2019-01-22 16:30:57 -0700203 self.assertTrue("Slice" in orm.content_type_map.values())
204 self.assertTrue("Site" in orm.content_type_map.values())
205 self.assertTrue("Tag" in orm.content_type_map.values())
Scott Baker500f8c72017-05-19 09:41:50 -0700206
207 def test_foreign_key_get(self):
208 orm = self.make_coreapi()
209 site = orm.Site(name="mysite")
210 site.save()
211 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700212 user = orm.User(
213 email="fake_"
214 + "".join(
215 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
216 ),
217 site_id=site.id,
218 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700219 user.save()
220 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700221 slice = orm.Slice(name="mysite_foo", site_id=site.id, creator_id=user.id)
Scott Baker500f8c72017-05-19 09:41:50 -0700222 slice.save()
223 self.assertTrue(slice.id > 0)
224 self.assertNotEqual(slice.site, None)
225 self.assertEqual(slice.site.id, site.id)
226
Scott Bakerc4156c32017-12-08 10:58:21 -0800227 def test_foreign_key_set_with_invalidate(self):
Scott Baker500f8c72017-05-19 09:41:50 -0700228 orm = self.make_coreapi()
229 site = orm.Site(name="mysite")
230 site.save()
231 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700232 user = orm.User(
233 email="fake_"
234 + "".join(
235 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
236 ),
237 site_id=site.id,
238 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700239 user.save()
240 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700241 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Baker500f8c72017-05-19 09:41:50 -0700242 slice.save()
243 slice.invalidate_cache()
244 self.assertTrue(slice.id > 0)
245 self.assertNotEqual(slice.site, None)
246 self.assertEqual(slice.site.id, site.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800247 if not USE_FAKE_STUB:
248 self.assertTrue(slice.id in slice.site.slices_ids)
249
250 def test_foreign_key_set_without_invalidate(self):
251 orm = self.make_coreapi()
252 site = orm.Site(name="mysite")
253 site.save()
254 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700255 user = orm.User(
256 email="fake_"
257 + "".join(
258 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
259 ),
260 site_id=site.id,
261 )
Scott Bakerc4156c32017-12-08 10:58:21 -0800262 user.save()
263 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700264 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800265 slice.save()
266 self.assertTrue(slice.id > 0)
267 self.assertNotEqual(slice.site, None)
268 self.assertEqual(slice.site.id, site.id)
269 if not USE_FAKE_STUB:
270 self.assertTrue(slice.id in slice.site.slices_ids)
271 ids_from_models = [x.id for x in slice.site.slices.all()]
272 self.assertTrue(slice.id in ids_from_models)
273
274 def test_foreign_key_reset(self):
275 orm = self.make_coreapi()
276 site = orm.Site(name="mysite")
277 site.save()
278 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700279 user = orm.User(
280 email="fake_"
281 + "".join(
282 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
283 ),
284 site_id=site.id,
285 )
Scott Bakerc4156c32017-12-08 10:58:21 -0800286 user.save()
287 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700288 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800289 slice.save()
290 self.assertTrue(slice.id > 0)
291 self.assertNotEqual(slice.site, None)
292 self.assertEqual(slice.site.id, site.id)
293 if not USE_FAKE_STUB:
294 self.assertTrue(slice.id in site.slices_ids)
295 self.assertTrue(slice.id in slice.site.slices_ids)
296
297 site2 = orm.Site(name="mysite2")
298 site2.save()
299 slice.name = "mysite2_foo"
300 slice.site = site2
301 slice.save()
302 self.assertNotEqual(slice.site, None)
303 self.assertEqual(slice.site.id, site2.id)
304 if not USE_FAKE_STUB:
305 self.assertTrue(slice.id not in site.slices_ids)
306 self.assertTrue(slice.id in site2.slices_ids)
307 self.assertTrue(slice.id in slice.site.slices_ids)
308 ids_from_models1 = [x.id for x in site.slices.all()]
309 self.assertTrue(slice.id not in ids_from_models1)
310 ids_from_models2 = [x.id for x in site2.slices.all()]
311 self.assertTrue(slice.id in ids_from_models2)
312
313 def test_foreign_key_back_and_forth_even(self):
314 orm = self.make_coreapi()
315 site = orm.Site(name="mysite")
316 site.save()
317 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700318 user = orm.User(
319 email="fake_"
320 + "".join(
321 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
322 ),
323 site_id=site.id,
324 )
Scott Bakerc4156c32017-12-08 10:58:21 -0800325 user.save()
326 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700327 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800328 slice.save()
329 self.assertTrue(slice.id > 0)
330 self.assertNotEqual(slice.site, None)
331 self.assertEqual(slice.site.id, site.id)
332 if not USE_FAKE_STUB:
333 self.assertTrue(slice.id in site.slices_ids)
334 self.assertTrue(slice.id in slice.site.slices_ids)
335
336 site2 = orm.Site(name="mysite2")
337 site2.save()
338 slice.name = "mysite2_foo"
339 slice.site = site2
340 slice.site = site
341 slice.site = site2
342 slice.site = site
343 slice.save()
344 self.assertNotEqual(slice.site, None)
345 self.assertEqual(slice.site.id, site.id)
346 if not USE_FAKE_STUB:
347 self.assertTrue(slice.id not in site2.slices_ids)
348 self.assertTrue(slice.id in site.slices_ids)
349 self.assertTrue(slice.id in slice.site.slices_ids)
350
351 def test_foreign_key_back_and_forth_odd(self):
352 orm = self.make_coreapi()
353 site = orm.Site(name="mysite")
354 site.save()
355 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700356 user = orm.User(
357 email="fake_"
358 + "".join(
359 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
360 ),
361 site_id=site.id,
362 )
Scott Bakerc4156c32017-12-08 10:58:21 -0800363 user.save()
364 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700365 slice = orm.Slice(name="mysite_foo", site=site, creator_id=user.id)
Scott Bakerc4156c32017-12-08 10:58:21 -0800366 slice.save()
367 self.assertTrue(slice.id > 0)
368 self.assertNotEqual(slice.site, None)
369 self.assertEqual(slice.site.id, site.id)
370 if not USE_FAKE_STUB:
371 self.assertTrue(slice.id in site.slices_ids)
372 self.assertTrue(slice.id in slice.site.slices_ids)
373
374 site2 = orm.Site(name="mysite2")
375 site2.save()
376 slice.name = "mysite2_foo"
377 slice.site = site2
378 slice.site = site
379 slice.site = site2
380 slice.site = site
381 slice.site = site2
382 slice.save()
383 self.assertNotEqual(slice.site, None)
384 self.assertEqual(slice.site.id, site2.id)
385 if not USE_FAKE_STUB:
386 self.assertTrue(slice.id not in site.slices_ids)
387 self.assertTrue(slice.id in site2.slices_ids)
388 self.assertTrue(slice.id in slice.site.slices_ids)
Scott Baker500f8c72017-05-19 09:41:50 -0700389
Scott Bakera1eae7a2017-06-06 09:20:15 -0700390 def test_foreign_key_create_null(self):
391 orm = self.make_coreapi()
392 site = orm.Site(name="mysite")
393 site.save()
394 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700395 user = orm.User(
396 email="fake_"
397 + "".join(
398 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
399 ),
400 site_id=site.id,
401 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700402 user.save()
403 self.assertTrue(user.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700404 slice = orm.Slice(
405 name="mysite_foo", site=site, service=None, creator_id=user.id
406 )
Scott Bakera1eae7a2017-06-06 09:20:15 -0700407 slice.save()
408 slice.invalidate_cache()
409 self.assertTrue(slice.id > 0)
410 self.assertEqual(slice.service, None)
411
412 def test_foreign_key_set_null(self):
413 orm = self.make_coreapi()
414 site = orm.Site(name="mysite")
415 site.save()
416 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700417 user = orm.User(
418 email="fake_"
419 + "".join(
420 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
421 ),
422 site_id=site.id,
423 )
Scott Bakerff104cc2017-08-14 15:24:41 -0700424 user.save()
425 self.assertTrue(user.id > 0)
Scott Bakera1eae7a2017-06-06 09:20:15 -0700426 service = orm.Service(name="myservice")
427 service.save()
428 self.assertTrue(service.id > 0)
429 # start out slice.service is non-None
Zack Williams045b63d2019-01-22 16:30:57 -0700430 slice = orm.Slice(
431 name="mysite_foo", site=site, service=service, creator_id=user.id
432 )
Scott Bakera1eae7a2017-06-06 09:20:15 -0700433 slice.save()
434 slice.invalidate_cache()
435 self.assertTrue(slice.id > 0)
436 self.assertNotEqual(slice.service, None)
437 self.assertEqual(slice.service.id, service.id)
438 # now set it to None
439 slice.service = None
440 slice.save()
441 slice.invalidate_cache()
442 self.assertEqual(slice.service, None)
443
Scott Baker500f8c72017-05-19 09:41:50 -0700444 def test_generic_foreign_key_get(self):
445 orm = self.make_coreapi()
446 service = orm.Service(name="myservice")
447 service.save()
448 site = orm.Site(name="mysite")
449 site.save()
450 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700451 tag = orm.Tag(
452 service=service,
453 name="mytag",
454 value="somevalue",
455 content_type=site.self_content_type_id,
456 object_id=site.id,
457 )
Scott Baker500f8c72017-05-19 09:41:50 -0700458 tag.save()
459 self.assertTrue(tag.id > 0)
460 self.assertNotEqual(tag.content_object, None)
461 self.assertEqual(tag.content_object.id, site.id)
462
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700463 def test_generic_foreign_key_get_decl(self):
464 orm = self.make_coreapi()
465 service = orm.Service(name="myservice")
466 service.save()
467 site = orm.Site(name="mysite")
468 site.save()
469 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700470 tag = orm.Tag(
471 service=service,
472 name="mytag",
473 value="somevalue",
474 content_type=site.self_content_type_id + "_decl",
475 object_id=site.id,
476 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700477 tag.save()
478 self.assertTrue(tag.id > 0)
479 self.assertNotEqual(tag.content_object, None)
480 self.assertEqual(tag.content_object.id, site.id)
481
482 def test_generic_foreign_key_get_bad_contenttype(self):
483 orm = self.make_coreapi()
484 service = orm.Service(name="myservice")
485 service.save()
486 site = orm.Site(name="mysite")
487 site.save()
488 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700489 tag = orm.Tag(
490 service=service,
491 name="mytag",
492 value="somevalue",
493 content_type="does_not_exist",
494 object_id=site.id,
495 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700496 tag.save()
497 self.assertTrue(tag.id > 0)
498 with self.assertRaises(Exception) as e:
Zack Williams045b63d2019-01-22 16:30:57 -0700499
500 self.assertEqual(
501 e.exception.message,
502 "Content_type does_not_exist not found in self.content_type_map",
503 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700504
505 def test_generic_foreign_key_get_bad_id(self):
506 orm = self.make_coreapi()
507 service = orm.Service(name="myservice")
508 service.save()
509 site = orm.Site(name="mysite")
510 site.save()
511 self.assertTrue(site.id > 0)
Zack Williams045b63d2019-01-22 16:30:57 -0700512 tag = orm.Tag(
513 service=service,
514 name="mytag",
515 value="somevalue",
516 content_type=site.self_content_type_id,
517 object_id=4567,
518 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700519 tag.save()
520 self.assertTrue(tag.id > 0)
521 with self.assertRaises(Exception) as e:
Zack Williams045b63d2019-01-22 16:30:57 -0700522 self.assertEqual(
523 e.exception.message, "Object 4567 of model Site was not found"
524 )
Scott Bakerd0f1dc12018-04-23 12:05:32 -0700525
Scott Baker500f8c72017-05-19 09:41:50 -0700526 def test_generic_foreign_key_set(self):
527 orm = self.make_coreapi()
528 service = orm.Service(name="myservice")
529 service.save()
530 site = orm.Site(name="mysite")
531 site.save()
532 self.assertTrue(site.id > 0)
533 tag = orm.Tag(service=service, name="mytag", value="somevalue")
534 tag.content_object = site
535 tag.invalidate_cache()
536 self.assertEqual(tag.content_type, site.self_content_type_id)
537 self.assertEqual(tag.object_id, site.id)
538 tag.save()
539 self.assertTrue(tag.id > 0)
540 self.assertNotEqual(tag.content_object, None)
541 self.assertEqual(tag.content_object.id, site.id)
542
Scott Bakerff104cc2017-08-14 15:24:41 -0700543 def test_leaf_model_trivial(self):
544 orm = self.make_coreapi()
545 service = orm.Service(name="myservice")
546 service.save()
547 self.assertEqual(service.leaf_model_name, "Service")
548
549 def test_leaf_model_descendant(self):
550 orm = self.make_coreapi()
551 onos_service = orm.ONOSService(name="myservice")
552 onos_service.save()
553 self.assertEqual(onos_service.model_name, "ONOSService")
554 self.assertEqual(onos_service.leaf_model_name, "ONOSService")
555
556 service = orm.Service.objects.get(id=onos_service.id)
557 self.assertEqual(service.id, onos_service.id)
558 self.assertEqual(service.model_name, "Service")
559 self.assertEqual(service.leaf_model_name, "ONOSService")
560
561 onos_service_cast = service.leaf_model
562 self.assertEqual(onos_service_cast.model_name, "ONOSService")
563 self.assertEqual(onos_service_cast.leaf_model_name, "ONOSService")
564 self.assertEqual(onos_service_cast.id, onos_service.id)
565
Scott Baker37cf9e22018-08-20 14:39:33 -0700566 def test_field_null(self):
567 """ In a saved object, if a nullable field is left set to None, make sure the ORM returns None """
568
569 orm = self.make_coreapi()
570 tm = orm.TestModel()
571 tm.save()
572
573 tm = orm.TestModel.objects.all()[0]
574 self.assertFalse(tm._wrapped_class.HasField("intfield"))
575 self.assertEqual(tm.intfield, None)
576
577 def test_field_null_new(self):
578 """ For models that haven't been saved yet, reading the field should return the gRPC default """
579
580 orm = self.make_coreapi()
581 tm = orm.TestModel()
582
583 self.assertEqual(tm.intfield, 0)
584
585 def test_field_non_null(self):
586 """ In a saved object, if a nullable field is set to a value, then make sure the ORM returns the value """
587
588 orm = self.make_coreapi()
589 tm = orm.TestModel(intfield=3)
590 tm.save()
591
592 tm = orm.TestModel.objects.all()[0]
593 self.assertEqual(tm.intfield, 3)
594
595 def test_field_set_null(self):
596 """ Setting a field to None is not allowed """
597
598 orm = self.make_coreapi()
599 tm = orm.TestModel()
600 with self.assertRaises(Exception) as e:
601 tm.intfile = None
Zack Williams045b63d2019-01-22 16:30:57 -0700602 self.assertEqual(
603 e.exception.message,
604 "Setting a non-foreignkey field to None is not supported",
605 )
Scott Baker37cf9e22018-08-20 14:39:33 -0700606
Scott Bakere1607b82018-09-20 14:10:59 -0700607 def test_query_iexact(self):
608 orm = self.make_coreapi()
609 with patch.object(orm.grpc_stub, "FilterTestModel", autospec=True) as filter:
Zack Williams045b63d2019-01-22 16:30:57 -0700610 orm.TestModel.objects.filter(name__iexact="foo")
Scott Bakere1607b82018-09-20 14:10:59 -0700611 self.assertEqual(filter.call_count, 1)
612 q = filter.call_args[0][0]
613
614 self.assertEqual(q.kind, q.DEFAULT)
615 self.assertEqual(len(q.elements), 1)
616 self.assertEqual(q.elements[0].operator, q.elements[0].IEXACT)
617 self.assertEqual(q.elements[0].sValue, "foo")
618
619 def test_query_equal(self):
620 orm = self.make_coreapi()
621 with patch.object(orm.grpc_stub, "FilterTestModel", autospec=True) as filter:
Zack Williams045b63d2019-01-22 16:30:57 -0700622 orm.TestModel.objects.filter(name="foo")
Scott Bakere1607b82018-09-20 14:10:59 -0700623 self.assertEqual(filter.call_count, 1)
624 q = filter.call_args[0][0]
625
626 self.assertEqual(q.kind, q.DEFAULT)
627 self.assertEqual(len(q.elements), 1)
628 self.assertEqual(q.elements[0].operator, q.elements[0].EQUAL)
629 self.assertEqual(q.elements[0].sValue, "foo")
630
Scott Baker9e477252019-01-07 11:49:45 -0800631 def test_ORMWrapper_dict(self):
632 orm = self.make_coreapi()
633
634 testModel = orm.TestModel(intfield=7, stringfield="foo")
635
636 self.assertDictEqual(testModel._dict, {"intfield": 7, "stringfield": "foo"})
637
Scott Baker5b7fba02018-10-17 08:46:46 -0700638 def test_ORMWrapper_new_diff(self):
639 orm = self.make_coreapi()
640 site = orm.Site(name="mysite")
641
642 self.assertEqual(site.is_new, True)
643 self.assertEqual(site._dict, {"name": "mysite"})
644 self.assertEqual(site.diff, {})
645 self.assertEqual(site.changed_fields, ["name"])
646 self.assertEqual(site.has_field_changed("name"), False)
647 self.assertEqual(site.has_field_changed("login_base"), False)
648
649 site.login_base = "bar"
650
Zack Williams045b63d2019-01-22 16:30:57 -0700651 self.assertEqual(site._dict, {"login_base": "bar", "name": "mysite"})
652 self.assertEqual(site.diff, {"login_base": (None, "bar")})
Scott Baker5b7fba02018-10-17 08:46:46 -0700653 self.assertIn("name", site.changed_fields)
654 self.assertIn("login_base", site.changed_fields)
655 self.assertEqual(site.has_field_changed("name"), False)
656 self.assertEqual(site.has_field_changed("login_base"), True)
657 self.assertEqual(site.get_field_diff("login_base"), (None, "bar"))
658
659 def test_ORMWrapper_existing_diff(self):
660 orm = self.make_coreapi()
661 site = orm.Site(name="mysite", login_base="foo")
662 site.save()
663 site = orm.Site.objects.first()
664
665 self.assertEqual(site.is_new, False)
666 self.assertEqual(site._dict, {"id": 1, "name": "mysite", "login_base": "foo"})
667 self.assertEqual(site.diff, {})
668 self.assertEqual(site.changed_fields, [])
669 self.assertEqual(site.has_field_changed("name"), False)
670 self.assertEqual(site.has_field_changed("login_base"), False)
671
672 site.login_base = "bar"
673
Zack Williams045b63d2019-01-22 16:30:57 -0700674 self.assertEqual(site._dict, {"id": 1, "login_base": "bar", "name": "mysite"})
675 self.assertEqual(site.diff, {"login_base": ("foo", "bar")})
Scott Baker5b7fba02018-10-17 08:46:46 -0700676 self.assertIn("login_base", site.changed_fields)
677 self.assertEqual(site.has_field_changed("name"), False)
678 self.assertEqual(site.has_field_changed("login_base"), True)
679
680 def test_ORMWrapper_diff_after_save(self):
681 orm = self.make_coreapi()
682 site = orm.Site(name="mysite", login_base="foo")
683 site.save()
684 site = orm.Site.objects.first()
685
686 self.assertEqual(site.diff, {})
687
688 site.login_base = "bar"
689
Zack Williams045b63d2019-01-22 16:30:57 -0700690 self.assertEqual(site.diff, {"login_base": ("foo", "bar")})
Scott Baker5b7fba02018-10-17 08:46:46 -0700691
692 site.save()
693
694 self.assertEqual(site.diff, {})
695
Scott Baker9e477252019-01-07 11:49:45 -0800696 def test_ORMWrapper_recompute_initial(self):
697 """ For saved models, Recompute_initial should take recompute the set of initial values, removing all items
698 from the diff set.
699 """
700
701 orm = self.make_coreapi()
702
703 testModel = orm.TestModel()
704 testModel.save()
705
706 testModel.intfield = 9
707 self.assertEqual(testModel.changed_fields, ["intfield"])
708
709 testModel.recompute_initial()
710 self.assertEqual(testModel.changed_fields, [])
711
712 def test_ORMWrapper_create_attr(self):
713 orm = self.make_coreapi()
714
715 testModel = orm.TestModel()
716 testModel.create_attr("some_new_attribute", "foo")
717 self.assertEqual(testModel.some_new_attribute, "foo")
718
719 def test_ORMWrapper_save_changed_fields(self):
720 orm = self.make_coreapi()
721
722 testModel = orm.TestModel(intfield=7, stringfield="foo")
723 testModel.save()
724
Zack Williams045b63d2019-01-22 16:30:57 -0700725 testModel.intfield = 9
Scott Baker9e477252019-01-07 11:49:45 -0800726
Zack Williams045b63d2019-01-22 16:30:57 -0700727 with patch.object(
728 orm.grpc_stub, "UpdateTestModel", wraps=orm.grpc_stub.UpdateTestModel
729 ) as update:
Scott Baker9e477252019-01-07 11:49:45 -0800730 testModel.save_changed_fields()
Scott Baker5dc7cc52019-01-18 15:05:27 -0800731
732 self.assertEqual(update.call_count, 1)
733 self.assertIn("metadata", update.call_args[1])
Zack Williams045b63d2019-01-22 16:30:57 -0700734 update_fields_arg = [
735 x[1] for x in update.call_args[1]["metadata"] if x[0] == "update_fields"
736 ]
Scott Baker5dc7cc52019-01-18 15:05:27 -0800737 self.assertEqual(update_fields_arg, ["intfield"])
Scott Baker9e477252019-01-07 11:49:45 -0800738
739 def test_ORMWrapper_get_generic_foreignkeys(self):
740 """ Currently this is a placeholder that returns an empty list """
741
742 orm = self.make_coreapi()
743
744 testModel = orm.TestModel()
745 self.assertEqual(testModel.get_generic_foreignkeys(), [])
746
747 def test_ORMWrapper_gen_fkmap(self):
748 """ TestModelTwo includes a foreignkey relation to TestModel, and the fkmap should contain that relation """
749
750 orm = self.make_coreapi()
751
752 testModelTwo = orm.TestModelTwo()
753
Zack Williams045b63d2019-01-22 16:30:57 -0700754 self.assertDictEqual(
755 testModelTwo.gen_fkmap(),
756 {
757 "testmodel": {
758 "kind": "fk",
759 "modelName": "TestModel",
760 "reverse_fieldName": "testmodeltwos",
761 "src_fieldName": "testmodel_id",
762 }
763 },
764 )
Scott Baker9e477252019-01-07 11:49:45 -0800765
766 def test_ORMWrapper_gen_reverse_fkmap(self):
767 """ TestModel includes a reverse relation back to TestModelTwo, and the reverse_fkmap should contain that
768 relation.
769 """
770
771 orm = self.make_coreapi()
772
773 testModel = orm.TestModel()
774
Zack Williams045b63d2019-01-22 16:30:57 -0700775 self.assertDictEqual(
776 testModel.gen_reverse_fkmap(),
777 {
778 "testmodeltwos": {
779 "modelName": "TestModelTwo",
780 "src_fieldName": "testmodeltwos_ids",
781 "writeable": False,
782 }
783 },
784 )
Scott Baker9e477252019-01-07 11:49:45 -0800785
786 def test_ORMWrapper_fk_resolve(self):
787 """ If we create a TestModelTwo that has a foreign key reference to a TestModel, then calling fk_resolve should
788 return that model.
789 """
790
791 orm = self.make_coreapi()
792
793 testModel = orm.TestModel()
794 testModel.save()
795
796 testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)
797
798 testModel_resolved = testModelTwo.fk_resolve("testmodel")
799 self.assertEqual(testModel_resolved.id, testModel.id)
800
Scott Baker7ab456b2019-01-08 14:58:13 -0800801 # the cache should have been populated
802 self.assertIn(("testmodel", testModel_resolved), testModelTwo.cache.items())
803
Scott Baker9e477252019-01-07 11:49:45 -0800804 def test_ORMWrapper_reverse_fk_resolve(self):
805 """ If a TestModelTwo has a relation to TestModel, then TestModel's reverse_fk should be resolvable to a list
806 of TestModelTwo objects.
807 """
808
809 orm = self.make_coreapi()
810
811 testModel = orm.TestModel()
812 testModel.save()
813
814 testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)
815 testModelTwo.save()
816
817 # fake_stub.py doesn't populate the reverse relations for us, so force what the server would have done...
818 testModel._wrapped_class.testmodeltwos_ids = [testModelTwo.id]
819
820 testModelTwos_resolved = testModel.reverse_fk_resolve("testmodeltwos")
821 self.assertEqual(testModelTwos_resolved.count(), 1)
822
Scott Baker7ab456b2019-01-08 14:58:13 -0800823 # the reverse_cache should have been populated
Zack Williams045b63d2019-01-22 16:30:57 -0700824 self.assertIn(
825 ("testmodeltwos", testModelTwos_resolved), testModel.reverse_cache.items()
826 )
Scott Baker7ab456b2019-01-08 14:58:13 -0800827
Scott Baker9e477252019-01-07 11:49:45 -0800828 def test_ORMWrapper_fk_set(self):
829 """ fk_set will set the testmodel field on TesTModelTwo to point to the TestModel. """
830
831 orm = self.make_coreapi()
832
833 testModel = orm.TestModel()
834 testModel.save()
835
836 testModelTwo = orm.TestModelTwo()
837
838 testModelTwo.fk_set("testmodel", testModel)
839
840 self.assertEqual(testModelTwo.testmodel_id, testModel.id)
841
842 def test_ORMWrapper_post_save_fixups_remove(self):
843 """ Apply a post_save_fixup that removes a reverse foreign key """
844
845 orm = self.make_coreapi()
846
847 testModel = orm.TestModel()
848 testModel.save()
849
850 testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)
851
852 # fake_stub.py doesn't populate the reverse relations for us, so force what the server would have done...
853 testModel._wrapped_class.testmodeltwos_ids = [testModelTwo.id]
854
Zack Williams045b63d2019-01-22 16:30:57 -0700855 post_save_fixups = [
856 {
857 "src_fieldName": "testmodel",
858 "dest_id": None, # this field appears to not be used...
859 "dest_model": testModel,
860 "remove": True,
861 "reverse_fieldName": "testmodeltwos",
862 }
863 ]
Scott Baker9e477252019-01-07 11:49:45 -0800864
865 testModelTwo.post_save_fixups = post_save_fixups
866 testModelTwo.do_post_save_fixups()
867
868 self.assertEqual(testModel._wrapped_class.testmodeltwos_ids, [])
869
870 def test_ORMWrapper_post_save_fixups_add(self):
871 """ Apply a post_save_fixup that adds a reverse foreign key """
872
873 orm = self.make_coreapi()
874
875 testModel = orm.TestModel()
876 testModel.save()
877
878 testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)
879 testModelTwo.save()
880
881 # Make sure the reverse_relation is unpopulated. This should be the case, as fake_stub.py() doesn't populate
882 # the reverse relation. But let's be sure, in case someone fixes that.
883 testModel._wrapped_class.testmodeltwos_ids = []
884
Zack Williams045b63d2019-01-22 16:30:57 -0700885 post_save_fixups = [
886 {
887 "src_fieldName": "testmodel",
888 "dest_id": None, # this field appears to not be used...
889 "dest_model": testModel,
890 "remove": False,
891 "reverse_fieldName": "testmodeltwos",
892 }
893 ]
Scott Baker9e477252019-01-07 11:49:45 -0800894
895 testModelTwo.post_save_fixups = post_save_fixups
896 testModelTwo.do_post_save_fixups()
897
898 self.assertEqual(testModel._wrapped_class.testmodeltwos_ids, [testModelTwo.id])
899
Scott Baker9e477252019-01-07 11:49:45 -0800900 def test_ORMWrapper_tologdict(self):
901 """ Tologdict contains the model name and id, used for structured logging """
902 orm = self.make_coreapi()
903
904 testModel = orm.TestModel(intfield=7, stringfile="foo")
905
Zack Williams045b63d2019-01-22 16:30:57 -0700906 self.assertDictEqual(
907 testModel.tologdict(), {"model_name": "TestModel", "pk": 0}
908 )
Scott Baker9e477252019-01-07 11:49:45 -0800909
910 def test_ORMWrapper_ansible_tag(self):
911 """ Ansible_tag is used by old-style synchronizers. Deprecated. """
912
913 orm = self.make_coreapi()
914
915 testModel = orm.TestModel(id=7)
916
917 self.assertEqual(testModel.ansible_tag, "TestModel_7")
918
Scott Bakerea1f4d02018-12-17 10:21:50 -0800919 def test_deleted_objects_all(self):
920 orm = self.make_coreapi()
921 orig_len_sites = len(orm.Site.objects.all())
922 orig_len_deleted_sites = len(orm.Site.deleted_objects.all())
923 site = orm.Site(name="mysite")
924 site.save()
925 site.delete()
926 sites = orm.Site.objects.all()
927 self.assertEqual(len(sites), orig_len_sites)
928 deleted_sites = orm.Site.deleted_objects.all()
Zack Williams045b63d2019-01-22 16:30:57 -0700929 self.assertEqual(len(deleted_sites), orig_len_deleted_sites + 1)
Scott Bakerea1f4d02018-12-17 10:21:50 -0800930
931 def test_deleted_objects_filter(self):
932 orm = self.make_coreapi()
Zack Williams045b63d2019-01-22 16:30:57 -0700933 with patch.object(
934 orm.grpc_stub, "FilterTestModel", wraps=orm.grpc_stub.FilterTestModel
935 ) as filter:
Scott Bakerea1f4d02018-12-17 10:21:50 -0800936 foo = orm.TestModel(name="foo")
937 foo.save()
938 foo.delete()
939
940 # There should be no live objects
Zack Williams045b63d2019-01-22 16:30:57 -0700941 objs = orm.TestModel.objects.filter(name="foo")
Scott Bakerea1f4d02018-12-17 10:21:50 -0800942 self.assertEqual(len(objs), 0)
943
944 # There should be one deleted object
Zack Williams045b63d2019-01-22 16:30:57 -0700945 deleted_objs = orm.TestModel.deleted_objects.filter(name="foo")
Scott Bakerea1f4d02018-12-17 10:21:50 -0800946 self.assertEqual(len(deleted_objs), 1)
947
948 # Two calls, one for when we checked live objects, the other for when we checked deleted objects
949 self.assertEqual(filter.call_count, 2)
950 q = filter.call_args[0][0]
951
952 # Now spy on the query that was generated, to make sure it looks like we expect
953 self.assertEqual(q.kind, q.SYNCHRONIZER_DELETED_OBJECTS)
954 self.assertEqual(len(q.elements), 1)
955 self.assertEqual(q.elements[0].operator, q.elements[0].EQUAL)
956 self.assertEqual(q.elements[0].sValue, "foo")
957
Scott Baker9e477252019-01-07 11:49:45 -0800958 def test_ORMQuerySet_first_nonempty(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700959 qs = self.ORMQuerySet([1, 2, 3])
Scott Baker9e477252019-01-07 11:49:45 -0800960 self.assertEqual(qs.first(), 1)
961
962 def test_ORMQuerySet_first_empty(self):
963 qs = self.ORMQuerySet([])
964 self.assertEqual(qs.first(), None)
965
966 def test_ORMQuerySet_exists_nonempty(self):
Zack Williams045b63d2019-01-22 16:30:57 -0700967 qs = self.ORMQuerySet([1, 2, 3])
Scott Baker9e477252019-01-07 11:49:45 -0800968 self.assertEqual(qs.exists(), True)
969
970 def test_ORMQuerySet_exists_empty(self):
971 qs = self.ORMQuerySet()
972 self.assertEqual(qs.exists(), False)
973
974 def test_ORMLocalObjectManager_nonempty(self):
975 """ Test all(), first(), exists(), and count() together since they're all closely related. Use a nonempty
976 list.
977 """
978 orm = self.make_coreapi()
979
980 t = orm.TestModel()
981 t.save()
982
983 lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], False)
984 self.assertEqual(len(lobjs.all()), 1)
985 self.assertEqual(lobjs.all()[0].id, t.id)
986 self.assertEqual(lobjs.exists(), True)
987 self.assertEqual(lobjs.count(), 1)
988 self.assertEqual(lobjs.first().id, t.id)
989
990 def test_ORMLocalObjectManager_empty(self):
991 """ Test all(), first(), exists(), and count() together since they're all closely related. Use an empty
992 list.
993 """
994 orm = self.make_coreapi()
995
996 t = orm.TestModel()
997 t.save()
998
999 lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [], False)
1000 self.assertEqual(len(lobjs.all()), 0)
1001 self.assertEqual(lobjs.exists(), False)
1002 self.assertEqual(lobjs.count(), 0)
1003 self.assertEqual(lobjs.first(), None)
1004
1005 def test_ORMLocalObjectManager_not_writeable(self):
1006 """ An ORMLocalObjectManager that is not writeable should throw exceptions on add() and remove() """
1007 orm = self.make_coreapi()
1008
1009 t = orm.TestModel()
1010 t.save()
1011
1012 lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], False)
1013
1014 with self.assertRaises(Exception) as e:
1015 lobjs.add(123)
1016 self.assertEqual(e.exception.message, "Only ManyToMany lists are writeable")
1017
1018 with self.assertRaises(Exception) as e:
1019 lobjs.remove(123)
1020 self.assertEqual(e.exception.message, "Only ManyToMany lists are writeable")
1021
1022 def test_ORMLocalObjectManager_add(self):
1023 orm = self.make_coreapi()
1024
1025 t = orm.TestModel()
1026 t.save()
1027
1028 lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [], True)
1029 lobjs.add(t)
1030 self.assertEqual(lobjs.count(), 1)
1031 self.assertEqual(lobjs.first().id, t.id)
1032
1033 def test_ORMLocalObjectManager_remove(self):
1034 orm = self.make_coreapi()
1035
1036 t = orm.TestModel()
1037 t.save()
1038
1039 lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], True)
1040 lobjs.remove(t)
1041 self.assertEqual(lobjs.count(), 0)
Scott Baker5b7fba02018-10-17 08:46:46 -07001042
Zack Williams045b63d2019-01-22 16:30:57 -07001043
Scott Baker7dddd512017-10-24 10:13:34 -07001044def main():
1045 global USE_FAKE_STUB
1046 global xos_grpc_client
Scott Bakerff104cc2017-08-14 15:24:41 -07001047
Scott Baker7dddd512017-10-24 10:13:34 -07001048 # Command-line argument of -R will cause this test to use a real grpc server
1049 # rather than the fake stub.
Scott Bakerff104cc2017-08-14 15:24:41 -07001050
Scott Baker7dddd512017-10-24 10:13:34 -07001051 if "-R" in sys.argv:
1052 USE_FAKE_STUB = False
1053 sys.argv.remove("-R")
1054 # Note: will leave lots of litter (users, sites, etc) behind in the database
Scott Bakerd1940972017-05-01 15:45:32 -07001055
Scott Baker7dddd512017-10-24 10:13:34 -07001056 if USE_FAKE_STUB:
1057 unittest.main()
1058 else:
1059 # This assumes xos-client python library is installed, and a gRPC server
1060 # is available.
Scott Bakerd1940972017-05-01 15:45:32 -07001061
Scott Baker7dddd512017-10-24 10:13:34 -07001062 from xosapi import xos_grpc_client
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001063
Zack Williams045b63d2019-01-22 16:30:57 -07001064 print("Using xos-client library and core server")
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001065
Scott Baker7dddd512017-10-24 10:13:34 -07001066 def test_callback():
1067 try:
Zack Williams045b63d2019-01-22 16:30:57 -07001068 sys.argv = sys.argv[
1069 :1
1070 ] # unittest does not like xos_grpc_client's command line arguments (TODO: find a cooperative approach)
Scott Baker7dddd512017-10-24 10:13:34 -07001071 unittest.main()
Zack Williams045b63d2019-01-22 16:30:57 -07001072 except exceptions.SystemExit as e:
Scott Baker7dddd512017-10-24 10:13:34 -07001073 global exitStatus
1074 exitStatus = e.code
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001075
Scott Baker7dddd512017-10-24 10:13:34 -07001076 xos_grpc_client.start_api_parseargs(test_callback)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001077
Scott Baker7dddd512017-10-24 10:13:34 -07001078 sys.exit(exitStatus)
Scott Bakerf0ee0dc2017-05-15 10:10:05 -07001079
Zack Williams045b63d2019-01-22 16:30:57 -07001080
Scott Baker7dddd512017-10-24 10:13:34 -07001081if __name__ == "__main__":
1082 main()