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