blob: cfb4c8beb02413ab1277bddd51e3cde2af32a0fd [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# 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
Matteo Scandolo67654fa2017-06-09 09:33:17 -070016import unittest
17from xosgenx.jinja2_extensions import FieldNotFound
Scott Baker1f7791d2018-10-04 13:21:20 -070018from helpers import XProtoTestHelpers
19from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Zack Williams045b63d2019-01-22 16:30:57 -070020from functools import reduce
21
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070022
Matteo Scandolo67654fa2017-06-09 09:33:17 -070023class XProtoFieldGraphTest(unittest.TestCase):
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070024 def _test_field_graph(self):
Zack Williams045b63d2019-01-22 16:30:57 -070025 xproto = """
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070026message 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 Scandoloa17e6e42018-05-25 10:28:25 -070041 target = XProtoTestHelpers.write_tmp_target(
Zack Williams045b63d2019-01-22 16:30:57 -070042 """
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070043{{ xproto_field_graph_components(proto.messages.0.fields, proto.messages.0) }}
Zack Williams045b63d2019-01-22 16:30:57 -070044"""
45 )
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070046
Zack Williams045b63d2019-01-22 16:30:57 -070047 args = XOSProcessorArgs(inputs=xproto, target=target)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080048 output = XOSProcessor.process(args)
Zack Williams045b63d2019-01-22 16:30:57 -070049 output = eval(output)
50 self.assertIn({"A", "B", "C"}, output)
51 self.assertIn({"openflow_id", "name"}, output)
52 self.assertIn({"config_key", "vrouter_service", "driver"}, output)
53 self.assertIn({"E", "F", "G"}, output)
54
55 union = reduce(lambda acc, x: acc | x, output)
56 self.assertNotIn("D", union)
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070057
58 def test_missing_field(self):
Zack Williams045b63d2019-01-22 16:30:57 -070059 xproto = """
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070060message Foo (PlCoreBase){
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070061 required string A = 6 [unique_with="B"];
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070062}
63"""
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070064 target = XProtoTestHelpers.write_tmp_target(
Zack Williams045b63d2019-01-22 16:30:57 -070065 """
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070066{{ xproto_field_graph_components(proto.messages.0.fields, proto.messages.0) }}
Zack Williams045b63d2019-01-22 16:30:57 -070067"""
68 )
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070069
70 def generate():
Zack Williams045b63d2019-01-22 16:30:57 -070071 args = XOSProcessorArgs(inputs=xproto, target=target)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080072 output = XOSProcessor.process(args)
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070073
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070074 with self.assertRaises(FieldNotFound) as e:
Zack Williams045b63d2019-01-22 16:30:57 -070075 generate()
Matteo Scandoloa17e6e42018-05-25 10:28:25 -070076
Zack Williams045b63d2019-01-22 16:30:57 -070077 self.assertEqual(
78 e.exception.message,
79 'Field "B" not found in model "Foo", referenced from field "A" by option "unique_with"',
80 )
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070081
Zack Williams045b63d2019-01-22 16:30:57 -070082
83if __name__ == "__main__":
Sapan Bhatiaf7934b52017-06-12 05:04:23 -070084 unittest.main()