blob: 4f4dae4dc5e2aa943c3bd0c6c1da037bffe1d489 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo67654fa2017-06-09 09:33:17 -070017import unittest
18import os
Scott Baker1f7791d2018-10-04 13:21:20 -070019from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Matteo Scandolo67654fa2017-06-09 09:33:17 -070020
21VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
22
23# Generate Protobuf from Xproto and then parse the resulting Protobuf
24class XProtoProtobufGeneratorTest(unittest.TestCase):
25 def test_proto_generator(self):
26 """
27 [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys
28 """
Scott Baker1f7791d2018-10-04 13:21:20 -070029 args = XOSProcessorArgs(files = [VROUTER_XPROTO],
30 target = 'django.xtarget')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080031 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070032
33 fields = filter(lambda s:'Field(' in s, output.splitlines())
34 self.assertEqual(len(fields), 2)
35 links = filter(lambda s:'Key(' in s, output.splitlines())
36 self.assertEqual(len(links), 2)
37
Matteo Scandolodfceafa2018-01-30 17:44:23 -080038 def test_optional_relations(self):
39 """
40 [XOS-GenX] Generate DJANGO models, verify relations
41 """
42 xproto = \
43 """
44 option app_label = "test";
45
46 message ENodeB {
47 }
48
49 message Handover {
50 }
51
52 message Foo {
53 optional manytoone enodeb->ENodeB:profiles = 1 [null = True, blank = True];
54 required manytoone handover->Handover:profiles = 2 [null = False, blank = False];
55 }
56 """
57
Scott Baker1f7791d2018-10-04 13:21:20 -070058 args = XOSProcessorArgs(inputs = xproto,
59 target = 'django.xtarget')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080060 output = XOSProcessor.process(args)
Matteo Scandolodfceafa2018-01-30 17:44:23 -080061
62 null_true = filter(lambda s: 'null = True' in s, output.splitlines())
63 null_false = filter(lambda s: 'null = False' in s, output.splitlines())
64
65 blank_true = filter(lambda s: 'blank = True' in s, output.splitlines())
66 blank_false = filter(lambda s: 'blank = False' in s, output.splitlines())
67
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 """
77 xproto = \
78 """
79 option app_label = "test";
80
81 message ParentFoo {
82 required string parent_name = 1 [null = False, blank = False, feedback_state = True];
83 }
84
85 message Foo (ParentFoo) {
86 required string name = 1 [null = False, blank = False, feedback_state = True];
87 }
88 """
89
Scott Baker1f7791d2018-10-04 13:21:20 -070090 args = XOSProcessorArgs(inputs = xproto,
91 target = 'django.xtarget')
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080092 output = XOSProcessor.process(args)
93
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080094 self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
95
Matteo Scandolo61a9f202018-08-01 08:58:13 -040096 def test_min_max_validators(self):
97 """
98 [XOS-GenX] Use django validors for min and max values
99 """
100 xproto = \
101 """
102 option app_label = "test";
103
104 message Foo (ParentFoo) {
105 required int32 val = 1 [min_value = 1, max_value = 10];
106 }
107 """
108
Scott Baker1f7791d2018-10-04 13:21:20 -0700109 args = XOSProcessorArgs(inputs = xproto,
110 target = 'django.xtarget')
Matteo Scandolo61a9f202018-08-01 08:58:13 -0400111 output = XOSProcessor.process(args)
112
113 self.assertIn("validators=[", output)
114 self.assertIn("MinValueValidator(1)", output)
115 self.assertIn("MaxValueValidator(10)", output)
116
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700117if __name__ == '__main__':
118 unittest.main()
119
120