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