blob: 943a7fb651b27db1e177f575ec93937f2bdf2d50 [file] [log] [blame]
Scott Bakerfb0f4192018-01-30 12:06:40 -08001# 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 Williams045b63d2019-01-22 16:30:57 -070018from xosapi import xos_grpc_client
Scott Bakerfb0f4192018-01-30 12:06:40 -080019import sys
20import unittest
21
22orm = None
23
Scott Bakerfb0f4192018-01-30 12:06:40 -080024
25SERVICE_1_NAME = "test_service_1"
26SERVICEINSTANCE_1_NAME = "test_service_instance_1"
27
28SERVICE_2_NAME = "test_service_2"
29SERVICEINSTANCE_2_NAME = "test_service_instance_2"
30
Zack Williams045b63d2019-01-22 16:30:57 -070031
Scott Bakerfb0f4192018-01-30 12:06:40 -080032class 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 Bakerfb0f4192018-01-30 12:06:40 -080041 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 Williams045b63d2019-01-22 16:30:57 -070051 serviceinstance1 = orm.ServiceInstance(
52 name=SERVICEINSTANCE_1_NAME, owner=service1
53 )
Scott Bakerfb0f4192018-01-30 12:06:40 -080054 serviceinstance1.save()
55
56 service2 = orm.Service(name=SERVICE_2_NAME)
57 service2.save()
58
Zack Williams045b63d2019-01-22 16:30:57 -070059 serviceinstance2 = orm.ServiceInstance(
60 name=SERVICEINSTANCE_2_NAME, owner=service2
61 )
Scott Bakerfb0f4192018-01-30 12:06:40 -080062 serviceinstance2.save()
63
Zack Williams045b63d2019-01-22 16:30:57 -070064 link = orm.ServiceInstanceLink(
65 provider_service_instance=serviceinstance1,
66 subscriber_service_instance=serviceinstance2,
67 )
Scott Bakerfb0f4192018-01-30 12:06:40 -080068 link.save()
69
Zack Williams045b63d2019-01-22 16:30:57 -070070 si1_readback = orm.ServiceInstance.objects.get(id=serviceinstance1.id)
71 si2_readback = orm.ServiceInstance.objects.get(id=serviceinstance2.id)
Scott Bakerfb0f4192018-01-30 12:06:40 -080072
73 self.assertEqual(si1_readback.provided_links.count(), 1)
74 self.assertEqual(si2_readback.subscribed_links.count(), 1)
75
Zack Williams045b63d2019-01-22 16:30:57 -070076
Scott Bakerfb0f4192018-01-30 12:06:40 -080077def test_callback():
78 global orm
79
80 orm = xos_grpc_client.coreclient.xos_orm
81
Zack Williams045b63d2019-01-22 16:30:57 -070082 sys.argv = sys.argv[:1] # unittest gets mad about the orm command line arguments
Scott Bakerfb0f4192018-01-30 12:06:40 -080083 unittest.main()
84
Scott Bakerfb0f4192018-01-30 12:06:40 -080085
Zack Williams045b63d2019-01-22 16:30:57 -070086xos_grpc_client.start_api_parseargs(test_callback)