blob: 0ba90b55e2b6c4e6d73488884282a64cd040af8b [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 Scandolo67654fa2017-06-09 09:33:17 -070076if __name__ == '__main__':
77 unittest.main()
78
79