Scott Baker | c59f1bc | 2017-12-04 16:55:05 -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 | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 19 | import sys |
| 20 | import unittest |
| 21 | |
| 22 | orm = None |
| 23 | |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 24 | |
| 25 | TEST_NODE_LABEL_1_NAME = "test_node_label_1" |
| 26 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 27 | |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 28 | class TestORM(unittest.TestCase): |
| 29 | def setUp(self): |
Scott Baker | d2c2e5b | 2017-12-07 13:46:08 -0800 | [diff] [blame] | 30 | self.test_node_label_1_name = TEST_NODE_LABEL_1_NAME |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 31 | |
| 32 | nodes1 = orm.Node.objects.filter(name="test_node_1") |
| 33 | if nodes1: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 34 | self.node1 = nodes1[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 35 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | self.node1 = orm.Node( |
| 37 | name="test_node_1", site_deployment=orm.SiteDeployment.objects.first() |
| 38 | ) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 39 | self.node1.save() |
| 40 | |
| 41 | nodes2 = orm.Node.objects.filter(name="test_node_2") |
| 42 | if nodes2: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 43 | self.node2 = nodes2[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 44 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 45 | self.node2 = orm.Node( |
| 46 | name="test_node_2", site_deployment=orm.SiteDeployment.objects.first() |
| 47 | ) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 48 | self.node2.save() |
| 49 | |
| 50 | def tearDown(self): |
Scott Baker | d2c2e5b | 2017-12-07 13:46:08 -0800 | [diff] [blame] | 51 | node_labels1 = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 52 | for node_label in node_labels1: |
| 53 | node_label.delete() |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 54 | |
Scott Baker | d2c2e5b | 2017-12-07 13:46:08 -0800 | [diff] [blame] | 55 | nodes1 = orm.Node.objects.filter(name="test_node_1") |
| 56 | for node in nodes1: |
| 57 | node.delete() |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 58 | |
Scott Baker | d2c2e5b | 2017-12-07 13:46:08 -0800 | [diff] [blame] | 59 | nodes2 = orm.Node.objects.filter(name="test_node_2") |
| 60 | for node in nodes2: |
| 61 | node.delete() |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 62 | |
| 63 | pass |
| 64 | |
| 65 | def test_create_empty_node_label(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | n = orm.NodeLabel(name=self.test_node_label_1_name) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 67 | n.save() |
| 68 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 69 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 70 | self.assertEqual(len(labels), 1) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 71 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 72 | n = labels[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 73 | self.assertNotEqual(n, None) |
| 74 | self.assertEqual(len(n.node.all()), 0) |
| 75 | |
| 76 | def test_create_node_label_one_node(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 77 | n = orm.NodeLabel(name=self.test_node_label_1_name) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 78 | n.node.add(self.node1) |
| 79 | n.save() |
| 80 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 81 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 82 | self.assertEqual(len(labels), 1) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 83 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 84 | n = labels[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 85 | self.assertNotEqual(n, None) |
| 86 | self.assertEqual(len(n.node.all()), 1) |
| 87 | |
| 88 | def test_create_node_label_two_nodes(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | n = orm.NodeLabel(name=self.test_node_label_1_name) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 90 | n.node.add(self.node1) |
| 91 | n.node.add(self.node2) |
| 92 | n.save() |
| 93 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 94 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 95 | self.assertEqual(len(labels), 1) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 96 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 97 | n = labels[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 98 | self.assertNotEqual(n, None) |
| 99 | self.assertEqual(len(n.node.all()), 2) |
| 100 | |
| 101 | def test_add_node_to_label(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 102 | n = orm.NodeLabel(name=self.test_node_label_1_name) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 103 | n.save() |
| 104 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 105 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 106 | self.assertEqual(len(labels), 1) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 107 | n = labels[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 108 | n.node.add(self.node1) |
| 109 | n.save() |
| 110 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 111 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 112 | self.assertEqual(len(labels), 1) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 113 | n = labels[0] |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 114 | self.assertEqual(len(n.node.all()), 1) |
| 115 | |
| 116 | def test_remove_node_from_label(self): |
| 117 | n = orm.NodeLabel(name=self.test_node_label_1_name) |
| 118 | n.node.add(self.node1) |
| 119 | n.node.add(self.node2) |
| 120 | n.save() |
| 121 | |
| 122 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 123 | self.assertEqual(len(labels), 1) |
| 124 | n = labels[0] |
| 125 | self.assertEqual(len(n.node.all()), 2) |
| 126 | n.node.remove(self.node1) |
| 127 | n.save() |
| 128 | |
| 129 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 130 | self.assertEqual(len(labels), 1) |
| 131 | n = labels[0] |
| 132 | self.assertEqual(len(n.node.all()), 1) |
| 133 | |
| 134 | def test_remove_last_node_from_label(self): |
| 135 | n = orm.NodeLabel(name=self.test_node_label_1_name) |
| 136 | n.node.add(self.node1) |
| 137 | n.save() |
| 138 | |
| 139 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 140 | self.assertEqual(len(labels), 1) |
| 141 | n = labels[0] |
| 142 | self.assertEqual(len(n.node.all()), 1) |
| 143 | n.node.remove(self.node1) |
| 144 | n.save(update_fields=["node_ids"]) |
| 145 | |
| 146 | labels = orm.NodeLabel.objects.filter(name=self.test_node_label_1_name) |
| 147 | self.assertEqual(len(labels), 1) |
| 148 | n = labels[0] |
| 149 | self.assertEqual(len(n.node.all()), 0) |
| 150 | |
| 151 | |
| 152 | def test_callback(): |
| 153 | global orm |
| 154 | |
| 155 | orm = xos_grpc_client.coreclient.xos_orm |
| 156 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 157 | sys.argv = sys.argv[:1] # unittest gets mad about the orm command line arguments |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 158 | unittest.main() |
| 159 | |
Scott Baker | c59f1bc | 2017-12-04 16:55:05 -0800 | [diff] [blame] | 160 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 161 | xos_grpc_client.start_api_parseargs(test_callback) |