blob: ea3d52d59d09b62cd327c150b93a8b8aa1c9d2fc [file] [log] [blame]
Matteo Scandolo67654fa2017-06-09 09:33:17 -07001import unittest
2from xosgenx.jinja2_extensions import FieldNotFound
3from helpers import FakeArgs, OUTPUT_DIR, XProtoTestHelpers
4from xosgenx.generator import XOSGenerator
Sapan Bhatiaf7934b52017-06-12 05:04:23 -07005
Matteo Scandolo67654fa2017-06-09 09:33:17 -07006class XProtoFieldGraphTest(unittest.TestCase):
Sapan Bhatiaf7934b52017-06-12 05:04:23 -07007 def test_field_graph(self):
8 xproto = \
9"""
10message 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 Scandolo67654fa2017-06-09 09:33:17 -070025 target = XProtoTestHelpers.write_tmp_target(
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070026"""
27{{ xproto_field_graph_components(proto.messages.0.fields) }}
Matteo Scandolo67654fa2017-06-09 09:33:17 -070028""")
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070029
Matteo Scandolo67654fa2017-06-09 09:33:17 -070030 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 Bhatiaf7934b52017-06-12 05:04:23 -070039
Matteo Scandolo67654fa2017-06-09 09:33:17 -070040 union = reduce(lambda acc,x: acc | x, output)
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070041 self.assertNotIn('D', union)
42
43 def test_missing_field(self):
44 xproto = \
45"""
46message 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 Scandolo67654fa2017-06-09 09:33:17 -070058 target = XProtoTestHelpers.write_tmp_target(
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070059"""
60{{ xproto_field_graph_components(proto.messages.0.fields) }}
Matteo Scandolo67654fa2017-06-09 09:33:17 -070061""")
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070062
63 def generate():
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064 args = FakeArgs()
65 args.inputs = xproto
66 args.target = target
67 output = XOSGenerator.generate(args)
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070068
69 # The following call generates some output, which should disappear
70 # when Matteo merges his refactoring of XOSGenerator.
71 self.assertRaises(FieldNotFound, generate)
72
73if __name__ == '__main__':
74 unittest.main()
75
76