blob: a81f80c47694046f0f47dc20d94e2530fcd595ad [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
17import os
Scott Baker1f7791d2018-10-04 13:21:20 -070018from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Matteo Scandolo67654fa2017-06-09 09:33:17 -070019
Zack Williams045b63d2019-01-22 16:30:57 -070020VROUTER_XPROTO = os.path.abspath(
21 os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto"
22)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070023
24# Generate Protobuf from Xproto and then parse the resulting Protobuf
Zack Williams045b63d2019-01-22 16:30:57 -070025
26
Matteo Scandolo67654fa2017-06-09 09:33:17 -070027class XProtoProtobufGeneratorTest(unittest.TestCase):
28 def test_proto_generator(self):
29 """
30 [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys
31 """
Zack Williams045b63d2019-01-22 16:30:57 -070032 args = XOSProcessorArgs(files=[VROUTER_XPROTO], target="django.xtarget")
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080033 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070034
Zack Williams045b63d2019-01-22 16:30:57 -070035 fields = filter(lambda s: "Field(" in s, output.splitlines())
Matteo Scandolo67654fa2017-06-09 09:33:17 -070036 self.assertEqual(len(fields), 2)
Zack Williams045b63d2019-01-22 16:30:57 -070037 links = filter(lambda s: "Key(" in s, output.splitlines())
Matteo Scandolo67654fa2017-06-09 09:33:17 -070038 self.assertEqual(len(links), 2)
39
Matteo Scandolodfceafa2018-01-30 17:44:23 -080040 def test_optional_relations(self):
41 """
42 [XOS-GenX] Generate DJANGO models, verify relations
43 """
Zack Williams045b63d2019-01-22 16:30:57 -070044 xproto = """
Matteo Scandolodfceafa2018-01-30 17:44:23 -080045 option app_label = "test";
46
47 message ENodeB {
48 }
Zack Williams045b63d2019-01-22 16:30:57 -070049
Matteo Scandolodfceafa2018-01-30 17:44:23 -080050 message Handover {
51 }
52
53 message Foo {
54 optional manytoone enodeb->ENodeB:profiles = 1 [null = True, blank = True];
55 required manytoone handover->Handover:profiles = 2 [null = False, blank = False];
56 }
57 """
58
Zack Williams045b63d2019-01-22 16:30:57 -070059 args = XOSProcessorArgs(inputs=xproto, target="django.xtarget")
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080060 output = XOSProcessor.process(args)
Matteo Scandolodfceafa2018-01-30 17:44:23 -080061
Zack Williams045b63d2019-01-22 16:30:57 -070062 null_true = filter(lambda s: "null = True" in s, output.splitlines())
63 null_false = filter(lambda s: "null = False" in s, output.splitlines())
Matteo Scandolodfceafa2018-01-30 17:44:23 -080064
Zack Williams045b63d2019-01-22 16:30:57 -070065 blank_true = filter(lambda s: "blank = True" in s, output.splitlines())
66 blank_false = filter(lambda s: "blank = False" in s, output.splitlines())
Matteo Scandolodfceafa2018-01-30 17:44:23 -080067
68 self.assertEqual(len(null_true), 1)
69 self.assertEqual(len(null_false), 1)
70 self.assertEqual(len(blank_true), 1)
71 self.assertEqual(len(blank_false), 1)
72
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080073 def test_feedback_state(self):
74 """
75 [XOS-GenX] Generate DJANGO models, verify feedback_state fields
76 """
Zack Williams045b63d2019-01-22 16:30:57 -070077 xproto = """
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080078 option app_label = "test";
79
80 message ParentFoo {
81 required string parent_name = 1 [null = False, blank = False, feedback_state = True];
82 }
83
84 message Foo (ParentFoo) {
85 required string name = 1 [null = False, blank = False, feedback_state = True];
86 }
87 """
88
Zack Williams045b63d2019-01-22 16:30:57 -070089 args = XOSProcessorArgs(inputs=xproto, target="django.xtarget")
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080090 output = XOSProcessor.process(args)
91
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080092 self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
93
Matteo Scandolo61a9f202018-08-01 08:58:13 -040094 def test_min_max_validators(self):
95 """
96 [XOS-GenX] Use django validors for min and max values
97 """
Zack Williams045b63d2019-01-22 16:30:57 -070098 xproto = """
Matteo Scandolo61a9f202018-08-01 08:58:13 -040099 option app_label = "test";
100
101 message Foo (ParentFoo) {
102 required int32 val = 1 [min_value = 1, max_value = 10];
103 }
104 """
105
Zack Williams045b63d2019-01-22 16:30:57 -0700106 args = XOSProcessorArgs(inputs=xproto, target="django.xtarget")
Matteo Scandolo61a9f202018-08-01 08:58:13 -0400107 output = XOSProcessor.process(args)
108
109 self.assertIn("validators=[", output)
110 self.assertIn("MinValueValidator(1)", output)
111 self.assertIn("MaxValueValidator(10)", output)
112
Zack Williams045b63d2019-01-22 16:30:57 -0700113
114if __name__ == "__main__":
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700115 unittest.main()