Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [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 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 16 | from __future__ import absolute_import |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | import os |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 19 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 20 | import yaml |
| 21 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 22 | PROTO_EXPECTED_OUTPUT = """ |
| 23 | message VRouterPort { |
| 24 | option bases = "XOSBase"; |
| 25 | optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = ""port friendly name"", modifier = "optional", db_index = "False" ]; |
| 26 | required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = ""port identifier in ONOS"", modifier = "required", db_index = "False" ]; |
| 27 | required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", link_type = "manytoone", db_index = "True" ]; |
| 28 | required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", link_type = "manytoone", db_index = "True" ]; |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 29 | } |
| 30 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | VROUTER_XPROTO = os.path.abspath( |
| 32 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto" |
| 33 | ) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 34 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | # Generate other formats from xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | |
| 37 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 38 | class XProtoTranslatorTest(unittest.TestCase): |
| 39 | def _test_proto_generator(self): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 40 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 41 | args.files = [VROUTER_XPROTO] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 42 | args.target = "proto.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 43 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 44 | self.assertEqual(output, PROTO_EXPECTED_OUTPUT) |
| 45 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 46 | def test_yaml_generator(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | xproto = """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 48 | option app_label = "test"; |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 49 | |
| 50 | message Port (PlCoreBase,ParameterMixin){ |
| 51 | required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False]; |
| 52 | optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True]; |
| 53 | optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 54 | optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True]; |
| 55 | optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True]; |
| 56 | required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True]; |
| 57 | } |
| 58 | |
| 59 | message Instance (PlCoreBase){ |
| 60 | optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; |
| 61 | optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; |
| 62 | required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; |
| 63 | optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; |
| 64 | optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 65 | required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; |
| 66 | optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; |
| 67 | required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; |
| 68 | required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; |
| 69 | required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; |
| 70 | required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; |
| 71 | required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False]; |
| 72 | optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True]; |
| 73 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 74 | optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; |
| 75 | optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; |
| 76 | required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True]; |
| 77 | } |
| 78 | |
| 79 | message Network (PlCoreBase,ParameterMixin) { |
| 80 | required string name = 1 [db_index = False, max_length = 32, null = False, blank = False]; |
| 81 | required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False]; |
| 82 | required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True]; |
| 83 | required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True]; |
| 84 | required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True]; |
| 85 | optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 86 | optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 87 | required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False]; |
| 88 | required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False]; |
| 89 | required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True]; |
| 90 | optional string topology_parameters = 11 [db_index = False, null = True, blank = True]; |
| 91 | optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 92 | optional string controller_parameters = 13 [db_index = False, null = True, blank = True]; |
| 93 | optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True]; |
| 94 | optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True]; |
| 95 | optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True]; |
| 96 | required bool autoconnect = 17 [help_text = "This network can be autoconnected to the slice that owns it", default = True, null = False, db_index = False, blank = True]; |
| 97 | required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True]; |
| 98 | required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True]; |
| 99 | required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True]; |
| 100 | } |
| 101 | |
| 102 | message Slice (PlCoreBase){ |
| 103 | required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False]; |
| 104 | required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True]; |
| 105 | required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True]; |
| 106 | required string description = 4 [help_text = "High level description of the slice and expected activities", max_length = 1024, null = False, db_index = False, blank = True]; |
| 107 | required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True]; |
| 108 | required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False]; |
| 109 | required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False]; |
| 110 | optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True]; |
| 111 | optional string network = 9 [blank = True, max_length = 256, null = True, db_index = False, choices = "((None, 'Default'), ('host', 'Host'), ('bridged', 'Bridged'), ('noauto', 'No Automatic Networks'))"]; |
| 112 | optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True]; |
| 113 | optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True]; |
| 114 | optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True]; |
| 115 | optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True]; |
| 116 | optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True]; |
| 117 | optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True]; |
| 118 | optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False]; |
| 119 | required string default_isolation = 17 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 120 | required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True]; |
| 121 | } |
| 122 | """ |
| 123 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 124 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 125 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 126 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 127 | output = XOSProcessor.process(args) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 128 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 129 | yaml_ir = yaml.load(output) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | self.assertEqual(len(yaml_ir["items"]), 4) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 131 | |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 132 | def test_gui_hidden_models(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 133 | xproto = """ |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 134 | option app_label = "test"; |
| 135 | |
| 136 | message Foo { |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 137 | option gui_hidden = True; |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 138 | required string name = 1 [ null = "False", blank="False"]; |
| 139 | } |
| 140 | |
| 141 | message Bar { |
| 142 | option gui_hidden = "False"; |
| 143 | required string name = 1 [ null = "False", blank="False"]; |
| 144 | } |
| 145 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 146 | args = XOSProcessorArgs() |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 147 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 148 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 149 | output = XOSProcessor.process(args) |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 150 | yaml_ir = yaml.load(output) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 151 | self.assertEqual(len(yaml_ir["items"]), 1) |
| 152 | self.assertIn("Bar", output) |
| 153 | self.assertNotIn("Foo", output) |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 154 | |
| 155 | def test_gui_hidden_model_fields(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 156 | xproto = """ |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 157 | option app_label = "test"; |
| 158 | |
| 159 | message Foo { |
| 160 | required string name = 1 [ null = "False", blank="False"]; |
| 161 | required string secret = 1 [ null = "False", blank="False", gui_hidden = "True"]; |
| 162 | } |
| 163 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 164 | args = XOSProcessorArgs() |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 165 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 166 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 167 | output = XOSProcessor.process(args) |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 168 | yaml_ir = yaml.load(output) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 169 | self.assertEqual(len(yaml_ir["items"]), 1) |
| 170 | self.assertIn("name", output) |
| 171 | self.assertNotIn("secret", output) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 172 | |
| 173 | def test_static_options(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 174 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 175 | option app_label = "test"; |
| 176 | |
| 177 | message Foo { |
| 178 | required string name = 1 [ null = "False", blank="False"]; |
| 179 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 180 | } |
| 181 | """ |
| 182 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 183 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 184 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 185 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 186 | output = XOSProcessor.process(args) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 187 | self.assertIn("options:", output) |
| 188 | self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output) |
| 189 | |
| 190 | def test_not_static_options(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 191 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 192 | option app_label = "test"; |
| 193 | |
| 194 | message Foo { |
| 195 | required string name = 1 [ null = "False", blank="False"]; |
| 196 | } |
| 197 | """ |
| 198 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 199 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 200 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 201 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 202 | output = XOSProcessor.process(args) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 203 | self.assertNotIn("options:", output) |
| 204 | |
| 205 | def test_default_value_in_modeldef(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 206 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 207 | option app_label = "test"; |
| 208 | |
| 209 | message Foo { |
| 210 | required string name = 1 [ null = "False", blank="False", default = "bar"]; |
| 211 | required string falsetrue = 1 [ null = "False", blank="False", default = False]; |
| 212 | required string truefalse = 1 [ null = "False", blank="False", default = True]; |
| 213 | required string some = 1 [ null = "False", blank="False", default = None]; |
| 214 | required string zero = 1 [ null = "False", blank="False", default = 0]; |
| 215 | } |
| 216 | """ |
| 217 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 218 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 219 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 220 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 221 | output = XOSProcessor.process(args) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 222 | self.assertIn('default: "bar"', output) |
| 223 | self.assertIn('default: "false"', output) |
| 224 | self.assertIn('default: "true"', output) |
| 225 | self.assertIn('default: "null"', output) |
| 226 | self.assertIn('default: "0"', output) |
| 227 | |
| 228 | def test_not_default_value_in_modeldef(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 229 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 230 | option app_label = "test"; |
| 231 | |
| 232 | message Foo { |
| 233 | required string name = 1 [ null = "False", blank="False"]; |
| 234 | } |
| 235 | """ |
| 236 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 237 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 238 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 239 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 240 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 241 | self.assertNotIn("default:", output) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 242 | |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 243 | def test_one_to_many_in_modeldef(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 244 | xproto = """ |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 245 | option app_label = "test"; |
| 246 | |
| 247 | message ServiceDependency { |
| 248 | required manytoone provider_service->Service:provided_dependencies = 1; |
| 249 | required manytoone subscriber_service->Service:subscribed_dependencies = 2; |
| 250 | } |
| 251 | |
| 252 | message Service { |
| 253 | required string name = 1; |
| 254 | } |
| 255 | """ |
| 256 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 257 | args = XOSProcessorArgs() |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 258 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 259 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 260 | output = XOSProcessor.process(args) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 261 | # Service deps model |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 262 | self.assertIn( |
| 263 | "{model: Service, type: manytoone, on_field: provider_service}", output |
| 264 | ) |
| 265 | self.assertIn( |
| 266 | "{model: Service, type: manytoone, on_field: provider_service}", output |
| 267 | ) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 268 | |
| 269 | # Service model |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 270 | self.assertIn( |
| 271 | "{model: ServiceDependency, type: onetomany, on_field: provider_service}", |
| 272 | output, |
| 273 | ) |
| 274 | self.assertIn( |
| 275 | "{model: ServiceDependency, type: onetomany, on_field: provider_service}", |
| 276 | output, |
| 277 | ) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 278 | |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 279 | def test_model_description(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 280 | xproto = """ |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 281 | option app_label = "test"; |
| 282 | |
| 283 | message Foo { |
| 284 | option description="This is the Foo model"; |
| 285 | required string name = 1 [ null = "False", blank="False"]; |
| 286 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 287 | } |
| 288 | |
| 289 | message Bar { |
| 290 | required string name = 1; |
| 291 | } |
| 292 | """ |
| 293 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 294 | args = XOSProcessorArgs() |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 295 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 296 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 297 | output = XOSProcessor.process(args) |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 298 | self.assertIn('description: "This is the Foo model"', output) |
| 299 | |
| 300 | def test_model_verbose_name(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 301 | xproto = """ |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 302 | option app_label = "test"; |
| 303 | |
| 304 | message Foo { |
| 305 | option verbose_name="Verbose Foo Name"; |
| 306 | required string name = 1 [ null = "False", blank="False"]; |
| 307 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 308 | } |
| 309 | |
| 310 | message Bar { |
| 311 | required string name = 1; |
| 312 | } |
| 313 | """ |
| 314 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 315 | args = XOSProcessorArgs() |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 316 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 317 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 318 | output = XOSProcessor.process(args) |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 319 | self.assertIn('verbose_name: "Verbose Foo Name"', output) |
| 320 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 321 | def test_feedback_field(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 322 | xproto = """ |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 323 | option app_label = "test"; |
| 324 | |
| 325 | message ParentFoo { |
| 326 | required string parent_name = 1 [null = False, blank = False, feedback_state = True]; |
| 327 | } |
| 328 | |
| 329 | message Foo (ParentFoo) { |
| 330 | required string name = 1 [null = False, blank = False, feedback_state = True]; |
| 331 | } |
| 332 | """ |
| 333 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 334 | args = XOSProcessorArgs() |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 335 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 336 | args.target = "modeldefs.xtarget" |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 337 | output = XOSProcessor.process(args) |
| 338 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 339 | read_only = [s for s in output.splitlines() if "read_only: True" in s] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 340 | self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 341 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 342 | |
| 343 | if __name__ == "__main__": |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 344 | unittest.main() |