Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [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 | import unittest |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 16 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 17 | from helpers import XProtoTestHelpers |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 18 | |
| 19 | |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 20 | class XProtoToscaTypeTest(unittest.TestCase): |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 21 | def setUp(self): |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 22 | self.target_tosca_type = XProtoTestHelpers.write_tmp_target( |
| 23 | """ |
| 24 | {%- for m in proto.messages %} |
| 25 | {% for f in m.fields %} |
| 26 | {{ xproto_tosca_field_type(f.type) }} |
| 27 | {% endfor -%} |
| 28 | {% endfor -%} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | """ |
| 30 | ) |
| 31 | |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 32 | def test_tosca_fields(self): |
| 33 | """ |
| 34 | [XOS-GenX] should convert xproto types to tosca know types |
| 35 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | xproto = """ |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 37 | option app_label = "test"; |
| 38 | |
| 39 | message Foo { |
| 40 | required string name = 1 [ null = "False", blank="False"]; |
| 41 | required bool active = 1 [ null = "False", blank="False"]; |
| 42 | required int32 quantity = 1 [ null = "False", blank="False"]; |
| 43 | } |
| 44 | """ |
| 45 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 46 | args = XOSProcessorArgs() |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 47 | args.inputs = xproto |
| 48 | args.target = self.target_tosca_type |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 49 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 50 | self.assertIn("string", output) |
| 51 | self.assertIn("boolean", output) |
| 52 | self.assertIn("integer", output) |
| 53 | |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 54 | |
| 55 | class XProtoToscaKeyTest(unittest.TestCase): |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 56 | def setUp(self): |
| 57 | self.target_tosca_keys = XProtoTestHelpers.write_tmp_target( |
| 58 | """ |
| 59 | {%- for m in proto.messages %} |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 60 | {{ xproto_fields_to_tosca_keys(m.fields, m) }} |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 61 | {% endfor -%} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 62 | """ |
| 63 | ) |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 64 | |
| 65 | def test_xproto_fields_to_tosca_keys_default(self): |
| 66 | """ |
| 67 | [XOS-GenX] if no "tosca_key" is specified, and a name attribute is present in the model, use that |
| 68 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 69 | xproto = """ |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 70 | option app_label = "test"; |
| 71 | |
| 72 | message Foo { |
| 73 | required string name = 1 [ null = "False", blank="False"]; |
| 74 | } |
| 75 | """ |
| 76 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 77 | args = XOSProcessorArgs() |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 78 | args.inputs = xproto |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 79 | args.target = self.target_tosca_keys |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 80 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 81 | self.assertIn("name", output) |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 82 | |
| 83 | def test_xproto_fields_to_tosca_keys_custom(self): |
| 84 | """ |
| 85 | [XOS-GenX] if "tosca_key" is specified, use it |
| 86 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 87 | xproto = """ |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 88 | option app_label = "test"; |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 90 | message Foo { |
| 91 | required string name = 1 [ null = "False", blank="False"]; |
| 92 | required string key_1 = 2 [ null = "False", blank="False", tosca_key=True]; |
| 93 | required string key_2 = 3 [ null = "False", blank="False", tosca_key=True]; |
| 94 | } |
| 95 | """ |
| 96 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 97 | args = XOSProcessorArgs() |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 98 | args.inputs = xproto |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 99 | args.target = self.target_tosca_keys |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 100 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 101 | self.assertNotIn("name", output) |
| 102 | self.assertIn("key_1", output) |
| 103 | self.assertIn("key_2", output) |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 104 | |
| 105 | def test_xproto_fields_link_to_tosca_keys_custom(self): |
| 106 | """ |
| 107 | [XOS-GenX] if "tosca_key" is specified, use it |
| 108 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 109 | xproto = """ |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 110 | option app_label = "test"; |
| 111 | |
| 112 | message Foo { |
| 113 | required string name = 1 [ null = "False", blank="False"]; |
| 114 | required manytoone provider_service_instance->ServiceInstance:provided_links = 1 [db_index = True, null = False, blank = False, tosca_key=True]; |
| 115 | } |
| 116 | """ |
| 117 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 118 | args = XOSProcessorArgs() |
Matteo Scandolo | c3c0f0a | 2017-10-18 09:53:30 +0200 | [diff] [blame] | 119 | args.inputs = xproto |
Matteo Scandolo | 727e19c | 2017-12-05 12:53:16 -0800 | [diff] [blame] | 120 | args.target = self.target_tosca_keys |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 121 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 122 | self.assertNotIn("name", output) |
| 123 | self.assertIn("provider_service_instance_id", output) |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 124 | |
| 125 | def test_xproto_model_to_oneof_key(self): |
| 126 | """ |
| 127 | [XOS-GenX] in some models we need to have a combine key on variable fields, for example, keys can be subscriber_service_id + oneof(provider_service_id, provider_network_id) |
| 128 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 129 | xproto = """ |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 130 | option app_label = "test"; |
| 131 | |
| 132 | message Foo { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 133 | |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 134 | option tosca_key = "key1, oneof(key_2, key_3)"; |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 135 | |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 136 | required string name = 1 [ null = "False", blank="False"]; |
| 137 | required string key_1 = 2 [ null = "False", blank="False", tosca_key_one_of = "key_2"]; |
| 138 | required string key_2 = 3 [ null = "False", blank="False", tosca_key_one_of = "key_1"]; |
| 139 | required string key_3 = 4 [ null = "False", blank="False", tosca_key_one_of = "key_4"]; |
| 140 | required string key_4 = 5 [ null = "False", blank="False", tosca_key_one_of = "key_3"]; |
| 141 | } |
| 142 | """ |
| 143 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 144 | args = XOSProcessorArgs() |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 145 | args.inputs = xproto |
| 146 | args.target = self.target_tosca_keys |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 147 | output = XOSProcessor.process(args) |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 148 | self.assertIn("['name', ['key_4', 'key_3'], ['key_1', 'key_2']]", output) |
| 149 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 150 | xproto = """ |
Matteo Scandolo | 68ab543 | 2017-12-06 15:38:13 -0800 | [diff] [blame] | 151 | option app_label = "test"; |
| 152 | |
| 153 | message Foo { |
| 154 | |
| 155 | option tosca_key = "key1, oneof(key_2, key_3)"; |
| 156 | |
| 157 | required string name = 1 [ null = "False", blank="False"]; |
| 158 | required manytoone key_1->Bar:key_1s = 2; |
| 159 | required manytoone key_2->Bar:key_2s = 3 [tosca_key_one_of = "key_1"]; |
| 160 | required manytoone key_3->Bar:key_3s = 4 [tosca_key_one_of = "key_1"]; |
| 161 | } |
| 162 | """ |
| 163 | |
| 164 | args.inputs = xproto |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 165 | output = XOSProcessor.process(args) |
| 166 | self.assertIn("['name', ['key_1_id', 'key_3_id', 'key_2_id']]", output) |