blob: c3ffef5109f69468259b440bb45adec4746ac883 [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
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080019from xosgenx.generator import XOSProcessor
Matteo Scandolo67654fa2017-06-09 09:33:17 -070020from helpers import FakeArgs
21
22VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
23
24# Generate Protobuf from Xproto and then parse the resulting Protobuf
25class XProtoProtobufGeneratorTest(unittest.TestCase):
26 def test_proto_generator(self):
27 """
28 [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys
29 """
30 args = FakeArgs()
31 args.files = [VROUTER_XPROTO]
32 args.target = 'django.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080033 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070034
35 fields = filter(lambda s:'Field(' in s, output.splitlines())
36 self.assertEqual(len(fields), 2)
37 links = filter(lambda s:'Key(' in s, output.splitlines())
38 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 """
44 xproto = \
45 """
46 option app_label = "test";
47
48 message ENodeB {
49 }
50
51 message Handover {
52 }
53
54 message Foo {
55 optional manytoone enodeb->ENodeB:profiles = 1 [null = True, blank = True];
56 required manytoone handover->Handover:profiles = 2 [null = False, blank = False];
57 }
58 """
59
60 args = FakeArgs()
61 args.inputs = xproto
62 args.target = 'django.xtarget'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080063 output = XOSProcessor.process(args)
Matteo Scandolodfceafa2018-01-30 17:44:23 -080064
65 null_true = filter(lambda s: 'null = True' in s, output.splitlines())
66 null_false = filter(lambda s: 'null = False' in s, output.splitlines())
67
68 blank_true = filter(lambda s: 'blank = True' in s, output.splitlines())
69 blank_false = filter(lambda s: 'blank = False' in s, output.splitlines())
70
71 self.assertEqual(len(null_true), 1)
72 self.assertEqual(len(null_false), 1)
73 self.assertEqual(len(blank_true), 1)
74 self.assertEqual(len(blank_false), 1)
75
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080076 def test_feedback_state(self):
77 """
78 [XOS-GenX] Generate DJANGO models, verify feedback_state fields
79 """
80 xproto = \
81 """
82 option app_label = "test";
83
84 message ParentFoo {
85 required string parent_name = 1 [null = False, blank = False, feedback_state = True];
86 }
87
88 message Foo (ParentFoo) {
89 required string name = 1 [null = False, blank = False, feedback_state = True];
90 }
91 """
92
93 args = FakeArgs()
94 args.inputs = xproto
95 args.target = 'django.xtarget'
96 output = XOSProcessor.process(args)
97
98 print output
99
100 self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
101
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700102if __name__ == '__main__':
103 unittest.main()
104
105