Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | import os |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 19 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 21 | VROUTER_XPROTO = os.path.abspath( |
| 22 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto" |
| 23 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 24 | |
| 25 | # Generate Protobuf from Xproto and then parse the resulting Protobuf |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 26 | |
| 27 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 28 | class XProtoProtobufGeneratorTest(unittest.TestCase): |
| 29 | def test_proto_generator(self): |
| 30 | """ |
| 31 | [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys |
| 32 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 33 | args = XOSProcessorArgs(files=[VROUTER_XPROTO], target="django.xtarget") |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 34 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 36 | fields = [s for s in output.splitlines() if "Field(" in s] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 37 | self.assertEqual(len(fields), 2) |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 38 | links = [s for s in output.splitlines() if "Key(" in s] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 39 | self.assertEqual(len(links), 2) |
| 40 | |
Matteo Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 41 | def test_optional_relations(self): |
| 42 | """ |
| 43 | [XOS-GenX] Generate DJANGO models, verify relations |
| 44 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 45 | xproto = """ |
Matteo Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 46 | option app_label = "test"; |
| 47 | |
| 48 | message ENodeB { |
| 49 | } |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 50 | |
Matteo Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 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 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 60 | args = XOSProcessorArgs(inputs=xproto, target="django.xtarget") |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 61 | output = XOSProcessor.process(args) |
Matteo Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 62 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 63 | 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 Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 65 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 66 | 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 Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 68 | |
| 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 Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 74 | def test_feedback_state(self): |
| 75 | """ |
| 76 | [XOS-GenX] Generate DJANGO models, verify feedback_state fields |
| 77 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 78 | xproto = """ |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 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 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 90 | args = XOSProcessorArgs(inputs=xproto, target="django.xtarget") |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 91 | output = XOSProcessor.process(args) |
| 92 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 93 | self.assertIn("feedback_state_fields = ['parent_name', 'name']", output) |
| 94 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 95 | def test_min_max_validators(self): |
| 96 | """ |
| 97 | [XOS-GenX] Use django validors for min and max values |
| 98 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 99 | xproto = """ |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 100 | option app_label = "test"; |
| 101 | |
| 102 | message Foo (ParentFoo) { |
| 103 | required int32 val = 1 [min_value = 1, max_value = 10]; |
| 104 | } |
| 105 | """ |
| 106 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 107 | args = XOSProcessorArgs(inputs=xproto, target="django.xtarget") |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 108 | output = XOSProcessor.process(args) |
| 109 | |
| 110 | self.assertIn("validators=[", output) |
| 111 | self.assertIn("MinValueValidator(1)", output) |
| 112 | self.assertIn("MaxValueValidator(10)", output) |
| 113 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 114 | |
| 115 | if __name__ == "__main__": |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 116 | unittest.main() |