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 |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 19 | from xosgenx.generator import XOSProcessor |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | from helpers import FakeArgs |
| 21 | |
| 22 | VROUTER_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 |
| 25 | class 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 33 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 34 | |
| 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 Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 40 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 63 | output = XOSProcessor.process(args) |
Matteo Scandolo | dfceafa | 2018-01-30 17:44:23 -0800 | [diff] [blame] | 64 | |
| 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 76 | if __name__ == '__main__': |
| 77 | unittest.main() |
| 78 | |
| 79 | |