blob: 022a64057edd3fafab57ed9096954650179ab0b4 [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
Zack Williams9a42f872019-02-15 17:56:04 -070016from __future__ import absolute_import
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
Zack Williams045b63d2019-01-22 16:30:57 -070021VROUTER_XPROTO = os.path.abspath(
22 os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto"
23)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070024
25# Generate Protobuf from Xproto and then parse the resulting Protobuf
Zack Williams045b63d2019-01-22 16:30:57 -070026
27
Matteo Scandolo67654fa2017-06-09 09:33:17 -070028class XProtoProtobufGeneratorTest(unittest.TestCase):
29 def test_proto_generator(self):
30 """
31 [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys
32 """
Zack Williams045b63d2019-01-22 16:30:57 -070033 args = XOSProcessorArgs(files=[VROUTER_XPROTO], target="django.xtarget")
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080034 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070035
Zack Williams9a42f872019-02-15 17:56:04 -070036 fields = [s for s in output.splitlines() if "Field(" in s]
Matteo Scandolo67654fa2017-06-09 09:33:17 -070037 self.assertEqual(len(fields), 2)
Zack Williams9a42f872019-02-15 17:56:04 -070038 links = [s for s in output.splitlines() if "Key(" in s]
Matteo Scandolo67654fa2017-06-09 09:33:17 -070039 self.assertEqual(len(links), 2)
40
Matteo Scandolodfceafa2018-01-30 17:44:23 -080041 def test_optional_relations(self):
42 """
43 [XOS-GenX] Generate DJANGO models, verify relations
44 """
Zack Williams045b63d2019-01-22 16:30:57 -070045 xproto = """
Matteo Scandolodfceafa2018-01-30 17:44:23 -080046 option app_label = "test";
47
48 message ENodeB {
49 }
Zack Williams045b63d2019-01-22 16:30:57 -070050
Matteo Scandolodfceafa2018-01-30 17:44:23 -080051 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
Zack Williams045b63d2019-01-22 16:30:57 -070060 args = XOSProcessorArgs(inputs=xproto, target="django.xtarget")
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080061 output = XOSProcessor.process(args)
Matteo Scandolodfceafa2018-01-30 17:44:23 -080062
Zack Williams9a42f872019-02-15 17:56:04 -070063 null_true = [s for s in output.splitlines() if "null = True" in s]
64 null_false = [s for s in output.splitlines() if "null = False" in s]
Matteo Scandolodfceafa2018-01-30 17:44:23 -080065
Zack Williams9a42f872019-02-15 17:56:04 -070066 blank_true = [s for s in output.splitlines() if "blank = True" in s]
67 blank_false = [s for s in output.splitlines() if "blank = False" in s]
Matteo Scandolodfceafa2018-01-30 17:44:23 -080068
69 self.assertEqual(len(null_true), 1)
70 self.assertEqual(len(null_false), 1)
71 self.assertEqual(len(blank_true), 1)
72 self.assertEqual(len(blank_false), 1)
73
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080074 def test_feedback_state(self):
75 """
76 [XOS-GenX] Generate DJANGO models, verify feedback_state fields
77 """
Zack Williams045b63d2019-01-22 16:30:57 -070078 xproto = """
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080079 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
Zack Williams045b63d2019-01-22 16:30:57 -070090 args = XOSProcessorArgs(inputs=xproto, target="django.xtarget")
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080091 output = XOSProcessor.process(args)
92
Matteo Scandolo23cf15f2018-03-06 18:12:36 -080093 self.assertIn("feedback_state_fields = ['parent_name', 'name']", output)
94
Matteo Scandolo61a9f202018-08-01 08:58:13 -040095 def test_min_max_validators(self):
96 """
97 [XOS-GenX] Use django validors for min and max values
98 """
Zack Williams045b63d2019-01-22 16:30:57 -070099 xproto = """
Matteo Scandolo61a9f202018-08-01 08:58:13 -0400100 option app_label = "test";
101
102 message Foo (ParentFoo) {
103 required int32 val = 1 [min_value = 1, max_value = 10];
104 }
105 """
106
Zack Williams045b63d2019-01-22 16:30:57 -0700107 args = XOSProcessorArgs(inputs=xproto, target="django.xtarget")
Matteo Scandolo61a9f202018-08-01 08:58:13 -0400108 output = XOSProcessor.process(args)
109
110 self.assertIn("validators=[", output)
111 self.assertIn("MinValueValidator(1)", output)
112 self.assertIn("MaxValueValidator(10)", output)
113
Zack Williams045b63d2019-01-22 16:30:57 -0700114
115if __name__ == "__main__":
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700116 unittest.main()