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 | |
| 20 | class XProtoToscaTest(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.target = XProtoTestHelpers.write_tmp_target( |
| 24 | """ |
| 25 | {%- for m in proto.messages %} |
| 26 | {{ xproto_fields_to_tosca_keys(m.fields) }} |
| 27 | {% endfor -%} |
| 28 | """) |
| 29 | |
| 30 | def test_xproto_fields_to_tosca_keys_default(self): |
| 31 | """ |
| 32 | [XOS-GenX] if no "tosca_key" is specified, and a name attribute is present in the model, use that |
| 33 | """ |
| 34 | xproto = \ |
| 35 | """ |
| 36 | option app_label = "test"; |
| 37 | |
| 38 | message Foo { |
| 39 | required string name = 1 [ null = "False", blank="False"]; |
| 40 | } |
| 41 | """ |
| 42 | |
| 43 | args = FakeArgs() |
| 44 | args.inputs = xproto |
| 45 | args.target = self.target |
| 46 | output = XOSGenerator.generate(args) |
| 47 | self.assertIn('name', output) |
| 48 | |
| 49 | def test_xproto_fields_to_tosca_keys_custom(self): |
| 50 | """ |
| 51 | [XOS-GenX] if "tosca_key" is specified, use it |
| 52 | """ |
| 53 | xproto = \ |
| 54 | """ |
| 55 | option app_label = "test"; |
| 56 | |
| 57 | message Foo { |
| 58 | required string name = 1 [ null = "False", blank="False"]; |
| 59 | required string key_1 = 2 [ null = "False", blank="False", tosca_key=True]; |
| 60 | required string key_2 = 3 [ null = "False", blank="False", tosca_key=True]; |
| 61 | } |
| 62 | """ |
| 63 | |
| 64 | args = FakeArgs() |
| 65 | args.inputs = xproto |
| 66 | args.target = self.target |
| 67 | output = XOSGenerator.generate(args) |
| 68 | self.assertNotIn('name', output) |
| 69 | self.assertIn('key_1', output) |
| 70 | self.assertIn('key_2', output) |
| 71 | |
| 72 | def test_xproto_fields_link_to_tosca_keys_custom(self): |
| 73 | """ |
| 74 | [XOS-GenX] if "tosca_key" is specified, use it |
| 75 | """ |
| 76 | xproto = \ |
| 77 | """ |
| 78 | option app_label = "test"; |
| 79 | |
| 80 | message Foo { |
| 81 | required string name = 1 [ null = "False", blank="False"]; |
| 82 | required manytoone provider_service_instance->ServiceInstance:provided_links = 1 [db_index = True, null = False, blank = False, tosca_key=True]; |
| 83 | } |
| 84 | """ |
| 85 | |
| 86 | args = FakeArgs() |
| 87 | args.inputs = xproto |
| 88 | args.target = self.target |
| 89 | output = XOSGenerator.generate(args) |
| 90 | self.assertNotIn('name', output) |
| 91 | self.assertIn('provider_service_instance_id', output) |