Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 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 | |
| 21 | VROUTER_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 |
| 24 | class XProtoProtobufGeneratorTest(unittest.TestCase): |
| 25 | def test_proto_generator(self): |
| 26 | """ |
| 27 | [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys |
| 28 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 29 | args = XOSProcessorArgs(files = [VROUTER_XPROTO], |
| 30 | target = 'django.xtarget') |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 31 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 32 | |
| 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 Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 38 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 58 | args = XOSProcessorArgs(inputs = xproto, |
| 59 | target = 'django.xtarget') |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 60 | output = XOSProcessor.process(args) |
Matteo Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 61 | |
| 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 Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 73 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 90 | args = XOSProcessorArgs(inputs = xproto, |
| 91 | target = 'django.xtarget') |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 92 | output = XOSProcessor.process(args) |
| 93 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 94 | self.assertIn("feedback_state_fields = ['parent_name', 'name']", output) |
| 95 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 96 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 109 | args = XOSProcessorArgs(inputs = xproto, |
| 110 | target = 'django.xtarget') |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 111 | output = XOSProcessor.process(args) |
| 112 | |
| 113 | self.assertIn("validators=[", output) |
| 114 | self.assertIn("MinValueValidator(1)", output) |
| 115 | self.assertIn("MaxValueValidator(10)", output) |
| 116 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 117 | if __name__ == '__main__': |
| 118 | unittest.main() |
| 119 | |
| 120 | |