Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | from xosgenx.jinja2_extensions import FieldNotFound |
| 19 | from helpers import FakeArgs, OUTPUT_DIR, XProtoTestHelpers |
| 20 | from xosgenx.generator import XOSGenerator |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 21 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 22 | class XProtoFieldGraphTest(unittest.TestCase): |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 23 | def test_field_graph(self): |
| 24 | xproto = \ |
| 25 | """ |
| 26 | message VRouterDevice (PlCoreBase){ |
| 27 | optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True, unique_with="openflow_id"]; |
| 28 | required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False, unique_with="name"]; |
| 29 | required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False, unique_with="driver"]; |
| 30 | required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False, unique_with="vrouter_service"]; |
| 31 | required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False]; |
| 32 | required string A = 6 [unique_with="B"]; |
| 33 | required string B = 7 [unique_with="C"]; |
| 34 | required string C = 8 [unique_with="A"]; |
| 35 | required string D = 9; |
| 36 | required string E = 10 [unique_with="F,G"]; |
| 37 | required string F = 11; |
| 38 | required string G = 12; |
| 39 | } |
| 40 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 41 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 42 | """ |
| 43 | {{ xproto_field_graph_components(proto.messages.0.fields) }} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 44 | """) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 45 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 46 | args = FakeArgs() |
| 47 | args.inputs = xproto |
| 48 | args.target = target |
| 49 | output = XOSGenerator.generate(args) |
| 50 | output = eval(output) |
| 51 | self.assertIn({'A','B','C'}, output) |
| 52 | self.assertIn({'openflow_id','name'}, output) |
| 53 | self.assertIn({'config_key','vrouter_service','driver'}, output) |
| 54 | self.assertIn({'E','F','G'}, output) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 55 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 56 | union = reduce(lambda acc,x: acc | x, output) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 57 | self.assertNotIn('D', union) |
| 58 | |
| 59 | def test_missing_field(self): |
| 60 | xproto = \ |
| 61 | """ |
| 62 | message VRouterDevice (PlCoreBase){ |
| 63 | optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True, unique_with="hamburger"]; |
| 64 | required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False, unique_with="name"]; |
| 65 | required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False, unique_with="driver"]; |
| 66 | required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False, unique_with="vrouter_service"]; |
| 67 | required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False]; |
| 68 | required string A = 6 [unique_with="B"]; |
| 69 | required string B = 7 [unique_with="C"]; |
| 70 | required string C = 8 [unique_with="A"]; |
| 71 | required string D = 9; |
| 72 | } |
| 73 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 74 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 75 | """ |
| 76 | {{ xproto_field_graph_components(proto.messages.0.fields) }} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 77 | """) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 78 | |
| 79 | def generate(): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 80 | args = FakeArgs() |
| 81 | args.inputs = xproto |
| 82 | args.target = target |
| 83 | output = XOSGenerator.generate(args) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 84 | |
| 85 | # The following call generates some output, which should disappear |
| 86 | # when Matteo merges his refactoring of XOSGenerator. |
| 87 | self.assertRaises(FieldNotFound, generate) |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | unittest.main() |
| 91 | |
| 92 | |