Scott Baker | 425bd06 | 2019-05-31 17:14:39 -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 | from __future__ import absolute_import |
| 16 | import unittest |
| 17 | import os |
| 18 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
| 19 | from helpers import OUTPUT_DIR |
| 20 | |
| 21 | TEST_INPUT = """option name = "test_custom_python"; |
| 22 | option app_label = "test_custom_python"; |
| 23 | |
| 24 | message ModelOne (XOSBase) { |
| 25 | option custom_python = "True"; |
| 26 | optional manytoone model_two->ModelTwo:model_ones = 1:1001 [ |
| 27 | help_text = "FK from model_one to model_two", |
| 28 | db_index = True]; |
| 29 | } |
| 30 | |
| 31 | message ModelTwo (XOSBase) { |
| 32 | optional manytoone model_three->ModelThree:model_twos = 1:1001 [ |
| 33 | help_text = "FK from model_two to model_three", |
| 34 | db_index = True]; |
| 35 | } |
| 36 | |
| 37 | message ModelThree (XOSBase) { |
| 38 | option custom_python = "True"; |
| 39 | } |
| 40 | """ |
| 41 | |
| 42 | |
| 43 | class XProtoCustomPythonTests(unittest.TestCase): |
| 44 | def setUp(self): |
| 45 | # Make sure the environment is clean before running tests |
| 46 | self.cleanup() |
| 47 | |
| 48 | def tearDown(self): |
| 49 | self.cleanup() |
| 50 | |
| 51 | def cleanup(self): |
| 52 | for fn in [os.path.join(OUTPUT_DIR, "models_decl.py"), |
| 53 | os.path.join(OUTPUT_DIR, "modelthree_decl.py"), |
| 54 | os.path.join(OUTPUT_DIR, "modeltwo.py"), |
| 55 | os.path.join(OUTPUT_DIR, "modelone_decl.py")]: |
| 56 | if os.path.exists(fn): |
| 57 | os.remove(fn) |
| 58 | |
| 59 | def test_core(self): |
| 60 | args = XOSProcessorArgs() |
| 61 | args.inputs = TEST_INPUT |
| 62 | args.write_to_file = "model" |
| 63 | args.output = OUTPUT_DIR |
| 64 | args.target = "django.xtarget" |
| 65 | args.dest_extension = "py" |
| 66 | XOSProcessor.process(args) |
| 67 | |
| 68 | modelone_decl_fn = os.path.join(OUTPUT_DIR, "modelone_decl.py") |
| 69 | self.assertTrue(os.path.exists(modelone_decl_fn)) |
| 70 | m1output = open(modelone_decl_fn).read() |
| 71 | |
| 72 | modeltwo_fn = os.path.join(OUTPUT_DIR, "modeltwo.py") |
| 73 | self.assertTrue(os.path.exists(modeltwo_fn)) |
| 74 | m2output = open(modeltwo_fn).read() |
| 75 | |
| 76 | modelthree_decl_fn = os.path.join(OUTPUT_DIR, "modelthree_decl.py") |
| 77 | self.assertTrue(os.path.exists(modelthree_decl_fn)) |
| 78 | m3output = open(modelthree_decl_fn).read() |
| 79 | |
| 80 | # Every model should get a _decl |
| 81 | self.assertIn("class ModelOne_decl(XOSBase):", m1output) |
| 82 | self.assertIn("class ModelTwo_decl(XOSBase):", m2output) |
| 83 | self.assertIn("class ModelThree_decl(XOSBase):", m3output) |
| 84 | |
| 85 | # ModelTwo has no custom_python so it should get a stub automatically |
| 86 | self.assertIn("class ModelTwo(ModelTwo_decl):", m2output) |
| 87 | |
| 88 | # Foreign Keys always go to the derived models |
| 89 | self.assertIn("model_two = ForeignKey(ModelTwo,", m1output) |
| 90 | self.assertIn("model_three = ForeignKey(ModelThree,", m2output) |
| 91 | |
| 92 | def test_services(self): |
| 93 | output_fn = os.path.join(OUTPUT_DIR, "models_decl.py") |
| 94 | |
| 95 | args = XOSProcessorArgs() |
| 96 | args.inputs = TEST_INPUT |
| 97 | args.write_to_file = "target" |
| 98 | args.output = OUTPUT_DIR |
| 99 | args.target = "service.xtarget" |
| 100 | XOSProcessor.process(args) |
| 101 | |
| 102 | self.assertTrue(os.path.exists(output_fn)) |
| 103 | |
| 104 | output = open(output_fn).read() |
| 105 | |
| 106 | # Every model should get a _decl |
| 107 | self.assertIn("class ModelOne_decl(XOSBase):", output) |
| 108 | self.assertIn("class ModelTwo_decl(XOSBase):", output) |
| 109 | self.assertIn("class ModelThree_decl(XOSBase):", output) |
| 110 | |
| 111 | # ModelTwo has no custom_python so it should get a stub automatically |
| 112 | self.assertIn("class ModelTwo(ModelTwo_decl):", output) |
| 113 | |
| 114 | # Foreign Keys always go to the decl models |
| 115 | self.assertIn("model_two = ForeignKey(ModelTwo_decl,", output) |
| 116 | self.assertIn("model_three = ForeignKey(ModelThree_decl,", output) |
| 117 | |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | unittest.main() |