Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [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 | |
| 15 | # These are functional tests of ManyToMany relations. These tests need to be conducted end-to-end with a real |
| 16 | # API to verify that the client and server ends of the API are working with each other. |
| 17 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 18 | from xosapi import xos_grpc_client |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 19 | import sys |
| 20 | import unittest |
| 21 | |
| 22 | orm = None |
| 23 | |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 24 | |
| 25 | SERVICE_1_NAME = "test_service_1" |
| 26 | SERVICEINSTANCE_1_NAME = "test_service_instance_1" |
| 27 | |
| 28 | SERVICE_2_NAME = "test_service_2" |
| 29 | SERVICEINSTANCE_2_NAME = "test_service_instance_2" |
| 30 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 32 | class TestORMReverseRelations(unittest.TestCase): |
| 33 | def setUp(self): |
| 34 | pass |
| 35 | |
| 36 | def cleanup_models(self, cls, name): |
| 37 | objs = cls.objects.filter(name=name) |
| 38 | for obj in objs: |
| 39 | obj.delete() |
| 40 | |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 41 | def tearDown(self): |
| 42 | self.cleanup_models(orm.ServiceInstance, SERVICEINSTANCE_1_NAME) |
| 43 | self.cleanup_models(orm.ServiceInstance, SERVICEINSTANCE_2_NAME) |
| 44 | self.cleanup_models(orm.Service, SERVICE_1_NAME) |
| 45 | self.cleanup_models(orm.Service, SERVICE_2_NAME) |
| 46 | |
| 47 | def test_reverse_relations(self): |
| 48 | service1 = orm.Service(name=SERVICE_1_NAME) |
| 49 | service1.save() |
| 50 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 51 | serviceinstance1 = orm.ServiceInstance( |
| 52 | name=SERVICEINSTANCE_1_NAME, owner=service1 |
| 53 | ) |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 54 | serviceinstance1.save() |
| 55 | |
| 56 | service2 = orm.Service(name=SERVICE_2_NAME) |
| 57 | service2.save() |
| 58 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 59 | serviceinstance2 = orm.ServiceInstance( |
| 60 | name=SERVICEINSTANCE_2_NAME, owner=service2 |
| 61 | ) |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 62 | serviceinstance2.save() |
| 63 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 64 | link = orm.ServiceInstanceLink( |
| 65 | provider_service_instance=serviceinstance1, |
| 66 | subscriber_service_instance=serviceinstance2, |
| 67 | ) |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 68 | link.save() |
| 69 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 70 | si1_readback = orm.ServiceInstance.objects.get(id=serviceinstance1.id) |
| 71 | si2_readback = orm.ServiceInstance.objects.get(id=serviceinstance2.id) |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 72 | |
| 73 | self.assertEqual(si1_readback.provided_links.count(), 1) |
| 74 | self.assertEqual(si2_readback.subscribed_links.count(), 1) |
| 75 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 76 | |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 77 | def test_callback(): |
| 78 | global orm |
| 79 | |
| 80 | orm = xos_grpc_client.coreclient.xos_orm |
| 81 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | sys.argv = sys.argv[:1] # unittest gets mad about the orm command line arguments |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 83 | unittest.main() |
| 84 | |
Scott Baker | fb0f419 | 2018-01-30 12:06:40 -0800 | [diff] [blame] | 85 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | xos_grpc_client.start_api_parseargs(test_callback) |