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 helpers import OUTPUT_DIR |
| 20 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 21 | |
| 22 | TEST_EXPECTED_OUTPUT = """ |
| 23 | name: XOSModel |
| 24 | fields: |
| 25 | name: |
| 26 | type: string |
| 27 | description: "Help Name" |
| 28 | files: |
| 29 | type: string |
| 30 | description: "Help Files" |
| 31 | """ |
| 32 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 33 | BASE_XPROTO = os.path.abspath( |
| 34 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/base.xproto" |
| 35 | ) |
| 36 | TEST_XPROTO = os.path.abspath( |
| 37 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/test.xproto" |
| 38 | ) |
| 39 | FIELDTEST_XPROTO = os.path.abspath( |
| 40 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/fieldtest.xproto" |
| 41 | ) |
| 42 | REVERSEFIELDTEST_XPROTO = os.path.abspath( |
| 43 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/reversefieldtest.xproto" |
| 44 | ) |
| 45 | FILTERTEST_XPROTO = os.path.abspath( |
| 46 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/filtertest.xproto" |
| 47 | ) |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 48 | CUSTOM_TEST1_XPROTO = os.path.abspath( |
| 49 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/custom_test1.xproto" |
| 50 | ) |
| 51 | CUSTOM_TEST2_XPROTO = os.path.abspath( |
| 52 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/custom_test2.xproto" |
| 53 | ) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 54 | SKIP_DJANGO_XPROTO = os.path.abspath( |
| 55 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto" |
| 56 | ) |
| 57 | VROUTER_XPROTO = os.path.abspath( |
| 58 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto" |
| 59 | ) |
| 60 | TEST_TARGET = os.path.abspath( |
| 61 | os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget" |
| 62 | ) |
| 63 | FIELDTEST_TARGET = os.path.abspath( |
| 64 | os.path.dirname(os.path.realpath(__file__)) + "/xtarget/fieldtest.xtarget" |
| 65 | ) |
| 66 | FILTERTEST_TARGET = os.path.abspath( |
| 67 | os.path.dirname(os.path.realpath(__file__)) + "/xtarget/filtertest.xtarget" |
| 68 | ) |
| 69 | SPLIT_TARGET = os.path.abspath( |
| 70 | os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget" |
| 71 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 72 | |
| 73 | TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/") |
| 74 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 75 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 76 | class XOSProcessorTest(unittest.TestCase): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 77 | """ |
| 78 | Testing the XOS Generative Toolchain |
| 79 | """ |
| 80 | |
| 81 | def setUp(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | os.chdir( |
| 83 | os.path.join( |
| 84 | os.path.abspath(os.path.dirname(os.path.realpath(__file__))), ".." |
| 85 | ) |
| 86 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 87 | filesToRemove = [f for f in os.listdir(OUTPUT_DIR)] |
| 88 | for f in filesToRemove: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | if not f.startswith("."): |
| 90 | os.remove(OUTPUT_DIR + "/" + f) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 91 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 92 | def test_generator_custom_target_from_file(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 93 | """ |
| 94 | [XOS-GenX] Generate output from base.xproto |
| 95 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 96 | args = XOSProcessorArgs(files=[TEST_XPROTO], target=TEST_TARGET) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 97 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 98 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 99 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 100 | def test_generator_custom_target_from_inputs(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 101 | """ |
| 102 | [XOS-GenX] Generate output from base.xproto |
| 103 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 104 | args = XOSProcessorArgs(inputs=open(TEST_XPROTO).read(), target=TEST_TARGET) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 105 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 106 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 107 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 108 | def test_django_with_attic(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 109 | """ |
| 110 | [XOS-GenX] Generate django output from test.xproto |
| 111 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 112 | args = XOSProcessorArgs( |
| 113 | files=[TEST_XPROTO, VROUTER_XPROTO], |
| 114 | target="django.xtarget", |
| 115 | attic=TEST_ATTICS, |
| 116 | output=OUTPUT_DIR, |
| 117 | dest_extension="py", |
| 118 | write_to_file="model", |
| 119 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 120 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 121 | |
| 122 | # xosmodel has custom header attic |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 123 | self.assertIn("from core.models.xosbase import *", output["XOSModel"]) |
| 124 | self.assertIn("class XOSModel_decl(XOSBase):", output["XOSModel"]) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 125 | |
| 126 | # vrouter port use the default header |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 127 | self.assertIn("from core.models.xosbase import *", output["VRouterPort"]) |
| 128 | self.assertIn("class VRouterPort_decl(XOSBase):", output["VRouterPort"]) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 129 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | # verify files |
| 131 | xosmodel = OUTPUT_DIR + "/xosmodel.py" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 132 | self.assertTrue(os.path.isfile(xosmodel)) |
| 133 | xmf = open(xosmodel).read() |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 134 | self.assertIn("from core.models.xosbase import *", xmf) |
| 135 | self.assertIn("class XOSModel_decl(XOSBase):", xmf) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 136 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 137 | vrouterport = OUTPUT_DIR + "/vrouterport.py" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 138 | self.assertTrue(os.path.isfile(vrouterport)) |
| 139 | vrpf = open(vrouterport).read() |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 140 | self.assertIn("from core.models.xosbase import *", vrpf) |
| 141 | self.assertIn("class VRouterPort_decl(XOSBase):", vrpf) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 142 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 143 | def test_django_with_base(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 144 | args = XOSProcessorArgs( |
| 145 | files=[TEST_XPROTO, BASE_XPROTO], |
| 146 | target="django.xtarget", |
| 147 | attic=TEST_ATTICS, |
| 148 | output=OUTPUT_DIR, |
| 149 | dest_extension="py", |
| 150 | write_to_file="model", |
| 151 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 152 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 153 | |
| 154 | # verify files |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 155 | xosmodel = OUTPUT_DIR + "/xosmodel.py" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 156 | self.assertTrue(os.path.isfile(xosmodel)) |
| 157 | xmf = open(xosmodel).read() |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 158 | self.assertIn("from core.models.xosbase import *", xmf) |
| 159 | self.assertIn("class XOSModel_decl(XOSBase):", xmf) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 160 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 161 | xosbase = OUTPUT_DIR + "/xosbase.py" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 162 | self.assertTrue(os.path.isfile(xosbase)) |
| 163 | xbf = open(xosbase).read() |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 164 | self.assertIn("from core.models.xosbase import *", xbf) |
| 165 | self.assertIn("class XOSBase_decl(models.Model, PlModelMixIn):", xbf) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 166 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 167 | def test_write_multiple_files(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 168 | """ |
| 169 | [XOS-GenX] read multiple models as input, print one file per model |
| 170 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 171 | args = XOSProcessorArgs( |
| 172 | files=[TEST_XPROTO, VROUTER_XPROTO], |
| 173 | target=TEST_TARGET, |
| 174 | output=OUTPUT_DIR, |
| 175 | dest_extension="txt", |
| 176 | write_to_file="model", |
| 177 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 178 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 179 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 180 | generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith(".")] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 181 | self.assertEqual(len(generated_files), 2) |
| 182 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 183 | xosmodel = open(os.path.join(OUTPUT_DIR, "xosmodel.txt"), "r").read() |
| 184 | vrouterport = open(os.path.join(OUTPUT_DIR, "vrouterport.txt"), "r").read() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 185 | |
| 186 | self.assertIn("name: XOSModel", xosmodel) |
| 187 | self.assertIn("name: VRouterPort", vrouterport) |
| 188 | |
| 189 | def test_write_multiple_files_from_xtarget(self): |
| 190 | """ |
| 191 | [XOS-GenX] read multiple models as input, print separate files based on +++ |
| 192 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 193 | args = XOSProcessorArgs( |
| 194 | files=[TEST_XPROTO, VROUTER_XPROTO], |
| 195 | target=SPLIT_TARGET, |
| 196 | output=OUTPUT_DIR, |
| 197 | write_to_file="target", |
| 198 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 199 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 200 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 201 | generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith(".")] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 202 | self.assertEqual(len(generated_files), 2) |
| 203 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 204 | xosmodel = open(os.path.join(OUTPUT_DIR, "xosmodel.txt"), "r").read() |
| 205 | vrouterport = open(os.path.join(OUTPUT_DIR, "vrouterport.txt"), "r").read() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 206 | |
| 207 | self.assertIn("name: XOSModel", xosmodel) |
| 208 | self.assertIn("name: VRouterPort", vrouterport) |
| 209 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 210 | def test_skip_django(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 211 | args = XOSProcessorArgs( |
| 212 | files=[SKIP_DJANGO_XPROTO], |
| 213 | target="django.xtarget", |
| 214 | output=OUTPUT_DIR, |
| 215 | dest_extension="py", |
| 216 | write_to_file="model", |
| 217 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 218 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 219 | |
| 220 | # should not print a file if options.skip_django = True |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 221 | file = OUTPUT_DIR + "/user.py" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 222 | self.assertFalse(os.path.isfile(file)) |
| 223 | |
| 224 | def test_service_order(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 225 | args = XOSProcessorArgs( |
| 226 | files=[BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO], |
| 227 | target="service.xtarget", |
| 228 | output=OUTPUT_DIR, |
| 229 | write_to_file="target", |
| 230 | ) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 231 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 232 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 233 | model = OUTPUT_DIR + "/models.py" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 234 | self.assertTrue(os.path.isfile(model)) |
| 235 | line_num = 0 |
| 236 | |
| 237 | for line in open(model).readlines(): |
| 238 | line_num += 1 |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 239 | if line.find("class XOSBase(models.Model, PlModelMixIn):") >= 0: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 240 | base_line = line_num |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 241 | if line.find("XOSModel(XOSBase):") >= 0: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 242 | xosmodel_line = line_num |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 243 | if line.find("class VRouterPort(XOSBase):") >= 0: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 244 | vrouter_line = line_num |
| 245 | self.assertLess(base_line, xosmodel_line) |
| 246 | self.assertLess(xosmodel_line, vrouter_line) |
| 247 | |
Scott Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 248 | def test_field_numbers(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 249 | args = XOSProcessorArgs(files=[FIELDTEST_XPROTO], target=FIELDTEST_TARGET) |
Scott Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 250 | output = XOSProcessor.process(args) |
| 251 | |
| 252 | def _assert_field(modelname, fieldname, id): |
| 253 | self.assertIn("%s,%s,%s" % (modelname, fieldname, id), output) |
| 254 | |
| 255 | _assert_field("Site", "id", 1) |
| 256 | _assert_field("Site", "base_field", 2) |
| 257 | _assert_field("Site", "base_field2", 3) |
| 258 | _assert_field("Site", "otherstuff_field", 102) |
| 259 | _assert_field("Site", "slice_field", 201) |
| 260 | _assert_field("Site", "slices_ids", 1002) |
| 261 | |
| 262 | _assert_field("Slice", "id", 1) |
| 263 | _assert_field("Slice", "base_field", 2) |
| 264 | _assert_field("Slice", "base_field2", 3) |
| 265 | _assert_field("Slice", "slice_field", 101) |
| 266 | _assert_field("Slice", "site", 102) |
| 267 | |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 268 | def test_field_numbers(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 269 | args = XOSProcessorArgs( |
| 270 | files=[REVERSEFIELDTEST_XPROTO], target=FIELDTEST_TARGET |
| 271 | ) |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 272 | output = XOSProcessor.process(args) |
| 273 | |
| 274 | def _assert_field(modelname, fieldname, id): |
| 275 | self.assertIn("%s,%s,%s" % (modelname, fieldname, id), output) |
| 276 | |
| 277 | # rel_int1s_ids is the reverse link from RelatedToIntermediate1. It gets the related id with no offset, so it |
| 278 | # will be assigned 1001. rel_leaf1as_ids inherits from Intermediate1, so its reverse links will all be offset |
| 279 | # by 100 |
| 280 | _assert_field("Leaf1a", "rel_int1s_ids", 1001) |
| 281 | _assert_field("Leaf1a", "rel_leaf1as_ids", 1101) |
| 282 | |
| 283 | # rel_int2s_ids is the reverse link from RelatedToIntermediate1. It gets the related id with no offset, so it |
| 284 | # will be assigned 1001. rel_leaf1bs_ids inherits from Intermediate1, so its reverse links will all be offset |
| 285 | # by 100 |
| 286 | _assert_field("Leaf1b", "rel_int1s_ids", 1001) |
| 287 | _assert_field("Leaf1b", "rel_leaf1bs_ids", 1101) |
| 288 | |
| 289 | # There are no reverse numbers specified for Intermediate2 or Leaf2, so xproto will fall back to automatic |
| 290 | # numbering starting at 1900. |
| 291 | _assert_field("Leaf2", "rel_int2s_ids", 1900) |
| 292 | _assert_field("Leaf2", "rel_leaf2s_ids", 1901) |
| 293 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 294 | def test_unfiltered(self): |
| 295 | """ With no include_* args, should get all models """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 296 | args = XOSProcessorArgs(files=[FILTERTEST_XPROTO], target=FILTERTEST_TARGET) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 297 | output = XOSProcessor.process(args) |
| 298 | |
| 299 | self.assertEqual(output, "Model1,Model2,Model3,") |
| 300 | |
| 301 | def test_filter_models(self): |
| 302 | """ Should only get models specified by include_models """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 303 | args = XOSProcessorArgs( |
| 304 | files=[FILTERTEST_XPROTO], |
| 305 | target=FILTERTEST_TARGET, |
| 306 | include_models=["Model1", "Model3"], |
| 307 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 308 | output = XOSProcessor.process(args) |
| 309 | |
| 310 | self.assertEqual(output, "Model1,Model3,") |
| 311 | |
| 312 | def test_filter_apps(self): |
| 313 | """ Should only get models whose apps are specified by include_apps """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 314 | args = XOSProcessorArgs( |
| 315 | files=[FILTERTEST_XPROTO], target=FILTERTEST_TARGET, include_apps=["core"] |
| 316 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 317 | output = XOSProcessor.process(args) |
| 318 | |
| 319 | self.assertEqual(output, "Model1,Model2,") |
| 320 | |
Scott Baker | be2a517 | 2019-04-10 18:02:50 -0700 | [diff] [blame] | 321 | def test_django_custom_test1(self): |
| 322 | args = XOSProcessorArgs( |
| 323 | files=[CUSTOM_TEST1_XPROTO, BASE_XPROTO], |
| 324 | target="django.xtarget", |
| 325 | attic=TEST_ATTICS, |
| 326 | output=OUTPUT_DIR, |
| 327 | dest_extension="py", |
| 328 | write_to_file="model", |
| 329 | ) |
| 330 | output = XOSProcessor.process(args) |
| 331 | |
| 332 | # verify files |
| 333 | xosmodel = OUTPUT_DIR + "/xosmodel_decl.py" |
| 334 | self.assertTrue(os.path.isfile(xosmodel)) |
| 335 | xmf = open(xosmodel).read() |
| 336 | self.assertIn("class XOSModel_decl(XOSBase):", xmf) |
| 337 | |
| 338 | xosmodel = OUTPUT_DIR + "/xosmodel2_decl.py" |
| 339 | self.assertTrue(os.path.isfile(xosmodel)) |
| 340 | xmf = open(xosmodel).read() |
| 341 | self.assertIn("class XOSModel2_decl(XOSBase):", xmf) |
| 342 | |
| 343 | def test_django_custom_test2(self): |
| 344 | args = XOSProcessorArgs( |
| 345 | files=[CUSTOM_TEST2_XPROTO, BASE_XPROTO], |
| 346 | target="django.xtarget", |
| 347 | attic=TEST_ATTICS, |
| 348 | output=OUTPUT_DIR, |
| 349 | dest_extension="py", |
| 350 | write_to_file="model", |
| 351 | ) |
| 352 | output = XOSProcessor.process(args) |
| 353 | |
| 354 | # verify files |
| 355 | xosmodel = OUTPUT_DIR + "/xosmodel_decl.py" |
| 356 | self.assertTrue(os.path.isfile(xosmodel)) |
| 357 | xmf = open(xosmodel).read() |
| 358 | self.assertIn("class XOSModel_decl(XOSBase):", xmf) |
| 359 | self.assertNotIn("class XOSModel(XOSModel_decl):", xmf) |
| 360 | |
| 361 | xosmodel = OUTPUT_DIR + "/xosmodel2.py" |
| 362 | self.assertTrue(os.path.isfile(xosmodel)) |
| 363 | xmf = open(xosmodel).read() |
| 364 | self.assertIn("class XOSModel2_decl(XOSBase):", xmf) |
| 365 | self.assertIn("class XOSModel2(XOSModel2_decl):", xmf) |
| 366 | |
| 367 | def test_service_custom_test1(self): |
| 368 | args = XOSProcessorArgs( |
| 369 | files=[CUSTOM_TEST1_XPROTO, BASE_XPROTO], |
| 370 | target="service.xtarget", |
| 371 | attic=TEST_ATTICS, |
| 372 | output=OUTPUT_DIR, |
| 373 | dest_extension="py", |
| 374 | write_to_file="target", |
| 375 | ) |
| 376 | output = XOSProcessor.process(args) |
| 377 | |
| 378 | # verify files |
| 379 | xosmodel = OUTPUT_DIR + "/models_decl.py" |
| 380 | self.assertTrue(os.path.isfile(xosmodel)) |
| 381 | xmf = open(xosmodel).read() |
| 382 | self.assertIn("class XOSModel_decl(XOSBase_decl):", xmf) |
| 383 | |
| 384 | xosmodel = OUTPUT_DIR + "/models_decl.py" |
| 385 | self.assertTrue(os.path.isfile(xosmodel)) |
| 386 | xmf = open(xosmodel).read() |
| 387 | self.assertIn("class XOSModel2_decl(XOSBase_decl):", xmf) |
| 388 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 389 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 390 | if __name__ == "__main__": |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 391 | unittest.main() |