Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | import os |
| 3 | from helpers import FakeArgs, OUTPUT_DIR |
| 4 | from xosgenx.generator import XOSGenerator |
| 5 | |
| 6 | TEST_EXPECTED_OUTPUT = """ |
| 7 | name: XOSModel |
| 8 | fields: |
| 9 | name: |
| 10 | type: string |
| 11 | description: "Help Name" |
| 12 | files: |
| 13 | type: string |
| 14 | description: "Help Files" |
| 15 | """ |
| 16 | |
| 17 | BASE_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/base.xproto") |
| 18 | TEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/test.xproto") |
| 19 | SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto") |
| 20 | VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto") |
| 21 | TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget") |
| 22 | SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget") |
| 23 | |
| 24 | TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/") |
| 25 | |
| 26 | class XOSGeneratorTest(unittest.TestCase): |
| 27 | """ |
| 28 | Testing the XOS Generative Toolchain |
| 29 | """ |
| 30 | |
| 31 | def setUp(self): |
| 32 | filesToRemove = [f for f in os.listdir(OUTPUT_DIR)] |
| 33 | for f in filesToRemove: |
| 34 | if not f.startswith('.'): |
| 35 | os.remove(OUTPUT_DIR + '/' + f) |
| 36 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 37 | def test_generator_custom_target_from_file(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 38 | """ |
| 39 | [XOS-GenX] Generate output from base.xproto |
| 40 | """ |
| 41 | args = FakeArgs() |
| 42 | args.files = [TEST_XPROTO] |
| 43 | args.target = TEST_TARGET |
| 44 | output = XOSGenerator.generate(args) |
| 45 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 46 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 47 | def test_generator_custom_target_from_inputs(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 48 | """ |
| 49 | [XOS-GenX] Generate output from base.xproto |
| 50 | """ |
| 51 | args = FakeArgs() |
| 52 | args.inputs = open(TEST_XPROTO).read() |
| 53 | args.target = TEST_TARGET |
| 54 | output = XOSGenerator.generate(args) |
| 55 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 56 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 57 | def test_django_with_attic(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 58 | """ |
| 59 | [XOS-GenX] Generate django output from test.xproto |
| 60 | """ |
| 61 | args = FakeArgs() |
| 62 | args.files = [TEST_XPROTO, VROUTER_XPROTO] |
| 63 | args.target = 'django.xtarget' |
| 64 | args.attic = TEST_ATTICS |
| 65 | args.output = OUTPUT_DIR |
| 66 | args.dest_extension = 'py' |
| 67 | args.write_to_file = 'model' |
| 68 | output = XOSGenerator.generate(args) |
| 69 | |
| 70 | # xosmodel has custom header attic |
| 71 | self.assertIn('from xosmodel_header import *', output['XOSModel']) |
| 72 | self.assertIn('class XOSModel(XOSBase):', output['XOSModel']) |
| 73 | |
| 74 | # vrouter port use the default header |
| 75 | self.assertIn('header import *', output['VRouterPort']) |
| 76 | self.assertIn('class VRouterPort(XOSBase):', output['VRouterPort']) |
| 77 | |
| 78 | #verify files |
| 79 | xosmodel = OUTPUT_DIR + '/xosmodel.py' |
| 80 | self.assertTrue(os.path.isfile(xosmodel)) |
| 81 | xmf = open(xosmodel).read() |
| 82 | self.assertIn('from xosmodel_header import *', xmf) |
| 83 | self.assertIn('class XOSModel(XOSBase):', xmf) |
| 84 | |
| 85 | vrouterport = OUTPUT_DIR + '/vrouterport.py' |
| 86 | self.assertTrue(os.path.isfile(vrouterport)) |
| 87 | vrpf = open(vrouterport).read() |
| 88 | self.assertIn('header import *', vrpf) |
| 89 | self.assertIn('class VRouterPort(XOSBase):', vrpf) |
| 90 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 91 | def test_django_with_base(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 92 | args = FakeArgs() |
| 93 | args.files = [TEST_XPROTO, BASE_XPROTO] |
| 94 | args.target = 'django.xtarget' |
| 95 | args.attic = TEST_ATTICS |
| 96 | args.output = OUTPUT_DIR |
| 97 | args.dest_extension = 'py' |
| 98 | args.write_to_file = 'model' |
| 99 | output = XOSGenerator.generate(args) |
| 100 | |
| 101 | # verify files |
| 102 | xosmodel = OUTPUT_DIR + '/xosmodel.py' |
| 103 | self.assertTrue(os.path.isfile(xosmodel)) |
| 104 | xmf = open(xosmodel).read() |
| 105 | self.assertIn('from xosmodel_header import *', xmf) |
| 106 | self.assertIn('class XOSModel(XOSBase):', xmf) |
| 107 | |
| 108 | xosbase = OUTPUT_DIR + '/xosbase.py' |
| 109 | self.assertTrue(os.path.isfile(xosbase)) |
| 110 | xbf = open(xosbase).read() |
| 111 | self.assertIn('header import *', xbf) |
| 112 | self.assertIn('class XOSBase(models.Model, PlModelMixIn):', xbf) |
| 113 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 114 | def test_write_multiple_files(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 115 | """ |
| 116 | [XOS-GenX] read multiple models as input, print one file per model |
| 117 | """ |
| 118 | args = FakeArgs() |
| 119 | args.files = [TEST_XPROTO, VROUTER_XPROTO] |
| 120 | args.target = TEST_TARGET |
| 121 | args.output = OUTPUT_DIR |
| 122 | args.dest_extension = 'txt' |
| 123 | args.write_to_file = 'model' |
| 124 | XOSGenerator.generate(args) |
| 125 | |
| 126 | generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')] |
| 127 | self.assertEqual(len(generated_files), 2) |
| 128 | |
| 129 | xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read() |
| 130 | vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read() |
| 131 | |
| 132 | self.assertIn("name: XOSModel", xosmodel) |
| 133 | self.assertIn("name: VRouterPort", vrouterport) |
| 134 | |
| 135 | def test_write_multiple_files_from_xtarget(self): |
| 136 | """ |
| 137 | [XOS-GenX] read multiple models as input, print separate files based on +++ |
| 138 | """ |
| 139 | args = FakeArgs() |
| 140 | args.files = [TEST_XPROTO, VROUTER_XPROTO] |
| 141 | args.target = SPLIT_TARGET |
| 142 | args.output = OUTPUT_DIR |
| 143 | args.write_to_file = 'target' |
| 144 | XOSGenerator.generate(args) |
| 145 | |
| 146 | generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')] |
| 147 | self.assertEqual(len(generated_files), 2) |
| 148 | |
| 149 | xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read() |
| 150 | vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read() |
| 151 | |
| 152 | self.assertIn("name: XOSModel", xosmodel) |
| 153 | self.assertIn("name: VRouterPort", vrouterport) |
| 154 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 155 | def test_skip_django(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 156 | args = FakeArgs() |
| 157 | args.files = [SKIP_DJANGO_XPROTO] |
| 158 | args.target = 'django.xtarget' |
| 159 | args.output = OUTPUT_DIR |
| 160 | args.dest_extension = 'py' |
| 161 | args.write_to_file = 'model' |
| 162 | output = XOSGenerator.generate(args) |
| 163 | |
| 164 | # should not print a file if options.skip_django = True |
| 165 | file = OUTPUT_DIR + '/user.py' |
| 166 | self.assertFalse(os.path.isfile(file)) |
| 167 | |
| 168 | def test_service_order(self): |
| 169 | args = FakeArgs() |
| 170 | args.files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO] |
| 171 | args.target = 'service.xtarget' |
| 172 | args.output = OUTPUT_DIR |
| 173 | args.write_to_file = 'target' |
| 174 | output = XOSGenerator.generate(args) |
| 175 | |
| 176 | model = OUTPUT_DIR + '/models.py' |
| 177 | self.assertTrue(os.path.isfile(model)) |
| 178 | line_num = 0 |
| 179 | |
| 180 | for line in open(model).readlines(): |
| 181 | line_num += 1 |
| 182 | if line.find('class XOSBase(models.Model, PlModelMixIn):') >= 0: |
| 183 | base_line = line_num |
| 184 | if line.find('XOSModel(XOSBase):') >= 0: |
| 185 | xosmodel_line = line_num |
| 186 | if line.find('class VRouterPort(XOSBase):') >= 0: |
| 187 | vrouter_line = line_num |
| 188 | self.assertLess(base_line, xosmodel_line) |
| 189 | self.assertLess(xosmodel_line, vrouter_line) |
| 190 | |
| 191 | |
| 192 | if __name__ == '__main__': |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 193 | unittest.main() |