Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | from xosgenx.jinja2_extensions import FieldNotFound |
| 3 | from helpers import FakeArgs, OUTPUT_DIR, XProtoTestHelpers |
| 4 | from xosgenx.generator import XOSGenerator |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 5 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 6 | class XProtoFieldGraphTest(unittest.TestCase): |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 7 | def test_field_graph(self): |
| 8 | xproto = \ |
| 9 | """ |
| 10 | message VRouterDevice (PlCoreBase){ |
| 11 | optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True, unique_with="openflow_id"]; |
| 12 | required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False, unique_with="name"]; |
| 13 | required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False, unique_with="driver"]; |
| 14 | required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False, unique_with="vrouter_service"]; |
| 15 | required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False]; |
| 16 | required string A = 6 [unique_with="B"]; |
| 17 | required string B = 7 [unique_with="C"]; |
| 18 | required string C = 8 [unique_with="A"]; |
| 19 | required string D = 9; |
| 20 | required string E = 10 [unique_with="F,G"]; |
| 21 | required string F = 11; |
| 22 | required string G = 12; |
| 23 | } |
| 24 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 25 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 26 | """ |
| 27 | {{ xproto_field_graph_components(proto.messages.0.fields) }} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 28 | """) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 29 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 30 | args = FakeArgs() |
| 31 | args.inputs = xproto |
| 32 | args.target = target |
| 33 | output = XOSGenerator.generate(args) |
| 34 | output = eval(output) |
| 35 | self.assertIn({'A','B','C'}, output) |
| 36 | self.assertIn({'openflow_id','name'}, output) |
| 37 | self.assertIn({'config_key','vrouter_service','driver'}, output) |
| 38 | self.assertIn({'E','F','G'}, output) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 39 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 40 | union = reduce(lambda acc,x: acc | x, output) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 41 | self.assertNotIn('D', union) |
| 42 | |
| 43 | def test_missing_field(self): |
| 44 | xproto = \ |
| 45 | """ |
| 46 | message VRouterDevice (PlCoreBase){ |
| 47 | optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True, unique_with="hamburger"]; |
| 48 | required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False, unique_with="name"]; |
| 49 | required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False, unique_with="driver"]; |
| 50 | required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False, unique_with="vrouter_service"]; |
| 51 | required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False]; |
| 52 | required string A = 6 [unique_with="B"]; |
| 53 | required string B = 7 [unique_with="C"]; |
| 54 | required string C = 8 [unique_with="A"]; |
| 55 | required string D = 9; |
| 56 | } |
| 57 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 58 | target = XProtoTestHelpers.write_tmp_target( |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 59 | """ |
| 60 | {{ xproto_field_graph_components(proto.messages.0.fields) }} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 61 | """) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 62 | |
| 63 | def generate(): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 64 | args = FakeArgs() |
| 65 | args.inputs = xproto |
| 66 | args.target = target |
| 67 | output = XOSGenerator.generate(args) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 68 | |
| 69 | # The following call generates some output, which should disappear |
| 70 | # when Matteo merges his refactoring of XOSGenerator. |
| 71 | self.assertRaises(FieldNotFound, generate) |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | unittest.main() |
| 75 | |
| 76 | |