def test_get_models(self): out_filename = os.path.join(self.tmp_dirname, 'schema.py') schema, _, models = utils.init_schema( 'tests/fixtures/declarative_schema/schema.csv', out_filename=out_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) self.assertEqual(sorted(utils.get_models(schema).keys()), ['Child', 'Parent', 'Quantity']) schema = utils.get_schema(out_filename) self.assertEqual(sorted(utils.get_models(schema).keys()), ['Child', 'Parent', 'Quantity'])
def test_init_schema(self): client = web_service.app.test_client() schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') with open(schema_filename, 'rb') as schema_file: rv = client.post('/api/init-schema/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), }) self.assertEqual(rv.status_code, 200) py_file = os.path.join(self.tempdir, 'schema.py') with open(py_file, 'wb') as file: file.write(rv.data) schema = utils.get_schema(py_file) self.assertEqual(sorted(utils.get_models(schema)), ['Child', 'Parent', 'Quantity']) # invalid schema schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'invalid-schema.csv') with open(schema_filename, 'rb') as schema_file: rv = client.post('/api/init-schema/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), }) self.assertEqual(rv.status_code, 400)
def test_normalize(self): csv_file = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') py_file = os.path.join(self.tempdir, 'schema.py') with __main__.App(argv=['init-schema', csv_file, py_file]) as app: app.run() csv_schema, _, csv_models = utils.init_schema(csv_file) py_schema = utils.get_schema(py_file) py_models = list(utils.get_models(py_schema).values()) self.assertEqual(set(model.__name__ for model in csv_models), set(model.__name__ for model in py_models)) xl_file_1 = os.path.join(self.tempdir, 'file1.xlsx') p_0 = csv_schema.Parent(id='p_0') p_0.children.create(id='c_0') p_0.children.create(id='c_1') io.WorkbookWriter().run(xl_file_1, [p_0], models=csv_models) xl_file_2 = os.path.join(self.tempdir, 'file2.xlsx') with __main__.App(argv=['normalize', csv_file, 'Parent', xl_file_1, xl_file_2]) as app: app.run() p_0_b = io.WorkbookReader().run(xl_file_2, models=csv_models, ignore_missing_attributes=True)[csv_schema.Parent][0] self.assertTrue(p_0_b.is_equal(p_0)) with self.assertRaises(SystemExit): with __main__.App(argv=['normalize', csv_file, 'Parent2', xl_file_1, xl_file_2]) as app: app.run()
def test_gen_template(self): csv_file = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') xl_file = os.path.join(self.tempdir, 'file.xlsx') with __main__.App(argv=['gen-template', csv_file, xl_file]) as app: app.run() csv_schema, _, csv_models = utils.init_schema(csv_file) py_file = os.path.join(self.tempdir, 'schema.py') with __main__.App(argv=['init-schema', csv_file, py_file]) as app: app.run() py_schema = utils.get_schema(py_file) py_models = list(utils.get_models(py_schema).values()) self.assertEqual(set(model.__name__ for model in csv_models), set(model.__name__ for model in py_models)) objs = io.WorkbookReader().run(xl_file, models=csv_models) self.assertEqual(objs, { csv_schema.Parent: [], csv_schema.Child: [], csv_schema.Quantity: [], }) csv_file = os.path.join(self.tempdir, 'file-*.xlsx') with __main__.App(argv=['gen-template', py_file, csv_file]) as app: app.run() objs = io.WorkbookReader().run(csv_file, models=py_models, group_objects_by_model=False) self.assertEqual(objs, None)
def test_get_model(self): schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) with self.assertRaises(werkzeug.exceptions.BadRequest): web_service.get_model(models, 'Parent2')
def test_init_schema(self): csv_file = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') py_file = os.path.join(self.tempdir, 'schema.py') with __main__.App(argv=['init-schema', csv_file, py_file]) as app: app.run() schema = utils.get_schema(py_file) self.assertEqual(sorted(utils.get_models(schema)), ['Child', 'Parent', 'Quantity'])
def test_diff(self): csv_file = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') py_file = os.path.join(self.tempdir, 'schema.py') with __main__.App(argv=['init-schema', csv_file, py_file]) as app: app.run() schema = utils.get_schema(py_file) models = list(utils.get_models(schema).values()) xl_file_1 = os.path.join(self.tempdir, 'file1.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0', name='c_0') p_0.children.create(id='c_1', name='c_1') io.WorkbookWriter().run(xl_file_1, [p_0], models=models) xl_file_2 = os.path.join(self.tempdir, 'file2.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0', name='c_0') p_0.children.create(id='c_1', name='c_0') io.WorkbookWriter().run(xl_file_2, [p_0], models=models) xl_file_3 = os.path.join(self.tempdir, 'file3.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0', name='c_0') p_0.children.create(id='c_1', name='c_1') p_0.children.create(id='c_2', name='c_2') io.WorkbookWriter().run(xl_file_3, [p_0], models=models) with __main__.App(argv=['diff', csv_file, 'Parent', xl_file_1, xl_file_1]) as app: app.run() with self.assertRaises(SystemExit): with __main__.App(argv=['diff', csv_file, 'Parent2', xl_file_1, xl_file_1]) as app: app.run() with self.assertRaises(SystemExit): with __main__.App(argv=['diff', csv_file, 'Parent', xl_file_1, xl_file_2]) as app: app.run() with self.assertRaises(SystemExit): with __main__.App(argv=['diff', csv_file, 'Child', xl_file_1, xl_file_3]) as app: app.run() with self.assertRaises(SystemExit): with __main__.App(argv=['diff', csv_file, 'Child', xl_file_3, xl_file_1]) as app: app.run()
def test_convert(self): csv_file = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') py_file = os.path.join(self.tempdir, 'schema.py') with __main__.App(argv=['init-schema', csv_file, py_file]) as app: app.run() schema = utils.get_schema(py_file) models = list(utils.get_models(schema).values()) xl_file_1 = os.path.join(self.tempdir, 'file1.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0') p_0.children.create(id='c_1') io.WorkbookWriter().run(xl_file_1, [p_0], models=models) csv_file_2 = os.path.join(self.tempdir, 'file2-*.csv') with __main__.App(argv=['convert', csv_file, xl_file_1, csv_file_2]) as app: app.run() p_0_b = io.WorkbookReader().run(csv_file_2, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_b.is_equal(p_0))
def test_gen_template(self): schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) client = web_service.app.test_client() with open(schema_filename, 'rb') as schema_file: rv = client.post('/api/gen-template/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 200) workbook_filename = os.path.join(self.tempdir, 'file.xlsx') with open(workbook_filename, 'wb') as file: file.write(rv.data) objs = io.WorkbookReader().run(workbook_filename, models=models, group_objects_by_model=False) self.assertEqual(objs, None) # invalid schema schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'invalid-schema.csv') with open(schema_filename, 'rb') as schema_file: rv = client.post('/api/gen-template/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 400)
def test_validate(self): csv_file = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') py_file = os.path.join(self.tempdir, 'schema.py') with __main__.App(argv=['init-schema', csv_file, py_file]) as app: app.run() schema = utils.get_schema(py_file) models = list(utils.get_models(schema).values()) xl_file_1 = os.path.join(self.tempdir, 'file1.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0') p_0.children.create(id='c_1') io.WorkbookWriter().run(xl_file_1, [p_0], models=models) with __main__.App(argv=['validate', csv_file, xl_file_1]) as app: app.run() xl_file_2 = os.path.join(self.tempdir, 'file2.xlsx') wb = wc_utils.workbook.io.read(xl_file_1) wb['!!Child'][4][0] = 'c_0' wc_utils.workbook.io.write(xl_file_2, wb) with self.assertRaises(SystemExit): with __main__.App(argv=['validate', csv_file, xl_file_2]) as app: app.run()
def do_sbtab_sbml_examples(self, action): dirname = os.path.join('examples', 'sbtab') schema_filename = os.path.join(dirname, 'SBtab.tsv') # Initalize Python module which implements schema py_module_filename = os.path.join(dirname, 'SBtab.py') with __main__.App( argv=['init-schema', schema_filename, py_module_filename ]) as app: app.run() # Visualize schema diagram_filename = os.path.join(dirname, 'SBtab.svg') with __main__.App( argv=['viz-schema', schema_filename, diagram_filename]) as app: app.run() # Generate a template for the schema template_filename = os.path.join(dirname, 'template.xlsx') with __main__.App(argv=[ 'gen-template', schema_filename, template_filename, '--write-schema', '--write-toc' ]) as app: app.run() # Validate that documents adhere to the schema data_filenames = [ 'template.xlsx', 'hynne.multi.tsv', 'feed_forward_loop_relationship.multi.tsv', 'kegg_reactions_cc_ph70_quantity.multi.tsv', 'yeast_transcription_network_chang_2008_relationship.multi.tsv', 'simple_examples/1.multi.tsv', 'simple_examples/2.multi.tsv', 'simple_examples/3.multi.tsv', 'simple_examples/4.multi.tsv', 'simple_examples/5.multi.tsv', 'simple_examples/6.multi.tsv', 'simple_examples/7.multi.tsv', 'simple_examples/8.multi.tsv', 'simple_examples/9.multi.tsv', 'simple_examples/10.multi.tsv', 'teusink_data.multi.tsv', 'teusink_model.multi.tsv', 'jiang_data.multi.tsv', 'jiang_model.multi.tsv', 'ecoli_noor_2016_data.multi.tsv', 'ecoli_noor_2016_model.multi.tsv', 'ecoli_wortel_2018_data.multi.tsv', 'ecoli_wortel_2018_model.multi.tsv', 'sigurdsson_model.multi.tsv', 'layout_model.multi.tsv', ] schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) for data_filename in data_filenames: full_data_filename = os.path.join(dirname, data_filename) if action == 'validate': with __main__.App( argv=['validate', schema_filename, full_data_filename ]) as app: app.run() if not data_filename.endswith('.xlsx'): with __main__.App(argv=[ 'validate', schema_filename.replace('.tsv', '.csv'), full_data_filename.replace('.multi.tsv', '.multi.csv'), ]) as app: app.run() objs_tsv = io.Reader().run(full_data_filename, models=models, **DEFAULT_READER_ARGS) objs_csv = io.Reader().run(full_data_filename.replace( '.multi.tsv', '.multi.csv'), models=models, **DEFAULT_READER_ARGS) for cls in objs_tsv.keys(): for obj_tsv, obj_csv in zip(objs_tsv[cls], objs_csv[cls]): self.assertTrue(obj_tsv.is_equal(obj_csv)) if action == 'convert' and not data_filename.endswith('.xlsx'): convert_filename = data_filename \ .replace('.multi.tsv', '.xlsx') full_convert_filename = os.path.join('examples', 'sbtab', convert_filename) with __main__.App(argv=[ 'convert', schema_filename, full_data_filename, full_convert_filename, ]) as app: app.run() convert_filename = data_filename \ .replace('.multi.tsv', '.tsv/*.tsv') full_convert_filename = os.path.join('examples', 'sbtab', convert_filename) if not os.path.isdir(os.path.dirname(full_convert_filename)): os.mkdir(os.path.dirname(full_convert_filename)) with __main__.App(argv=[ 'convert', schema_filename, full_data_filename, full_convert_filename, ]) as app: app.run() convert_filename = data_filename \ .replace('.multi.tsv', '.csv/*.csv') full_convert_filename = os.path.join('examples', 'sbtab', convert_filename) if not os.path.isdir(os.path.dirname(full_convert_filename)): os.mkdir(os.path.dirname(full_convert_filename)) with __main__.App(argv=[ 'convert', schema_filename, full_data_filename, full_convert_filename, ]) as app: app.run() convert_filename = data_filename \ .replace('.multi.tsv', '.json') full_convert_filename = os.path.join('examples', 'sbtab', convert_filename) with __main__.App(argv=[ 'convert', schema_filename, full_data_filename, full_convert_filename, ]) as app: app.run() convert_filename = data_filename \ .replace('.multi.tsv', '.yml') full_convert_filename = os.path.join('examples', 'sbtab', convert_filename) with __main__.App(argv=[ 'convert', schema_filename, full_data_filename, full_convert_filename, ]) as app: app.run() if action == 'validate': with __main__.App(argv=[ 'validate', schema_filename.replace('.tsv', '.xlsx'), os.path.join(dirname, data_filenames[0]), ]) as app: app.run()
def test_validate(self): client = web_service.app.test_client() schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) # valid XLSX file wb_filename = os.path.join(self.tempdir, 'wb.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0') p_0.children.create(id='c_1') io.WorkbookWriter().run(wb_filename, [p_0], models=models) with open(schema_filename, 'rb') as schema_file: with open(wb_filename, 'rb') as wb_file: rv = client.post('/api/validate/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (wb_file, os.path.basename(wb_filename)), }) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.json, 'The dataset is valid') # invalid extension with open(schema_filename, 'rb') as schema_file: with open(wb_filename, 'rb') as wb_file: rv = client.post( '/api/validate/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (wb_file, os.path.basename(wb_filename) + '-invalid'), }) self.assertEqual(rv.status_code, 400) # valid csv files wb_filename_2 = os.path.join(self.tempdir, '*.csv') wb = wc_utils.workbook.io.read(wb_filename) wc_utils.workbook.io.write(wb_filename_2, wb) wb_filename_3 = os.path.join(self.tempdir, 'wb.zip') zip_file = zipfile.ZipFile(wb_filename_3, mode='w') for filename in glob.glob(wb_filename_2): zip_file.write(filename, arcname=os.path.basename(filename)) zip_file.close() with open(schema_filename, 'rb') as schema_file: with open(wb_filename_3, 'rb') as wb_file: rv = client.post('/api/validate/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (wb_file, os.path.basename(wb_filename_3)), }) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.json, 'The dataset is valid') # invalid tsv files wb_filename_4 = os.path.join(self.tempdir, '*.tsv') wb = wc_utils.workbook.io.read(wb_filename) wb['!!Child'][4][0] = 'c_0' wc_utils.workbook.io.write(wb_filename_4, wb) wb_filename_5 = os.path.join(self.tempdir, 'wb2.zip') zip_file = zipfile.ZipFile(wb_filename_5, mode='w') for filename in glob.glob(wb_filename_4): zip_file.write(filename, arcname=os.path.basename(filename)) zip_file.close() with open(schema_filename, 'rb') as schema_file: with open(wb_filename_5, 'rb') as wb_file: rv = client.post('/api/validate/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (wb_file, os.path.basename(wb_filename_5)), }) self.assertEqual(rv.status_code, 200) self.assertNotEqual(rv.json, 'The dataset is valid') # invalid csv and tsv files wb_filename_6 = os.path.join(self.tempdir, 'wb3.zip') zip_file = zipfile.ZipFile(wb_filename_6, mode='w') for filename in glob.glob(wb_filename_2): zip_file.write(filename, arcname=os.path.basename(filename)) for filename in glob.glob(wb_filename_4): zip_file.write(filename, arcname=os.path.basename(filename)) zip_file.close() with open(schema_filename, 'rb') as schema_file: with open(wb_filename_6, 'rb') as wb_file: rv = client.post('/api/validate/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (wb_file, os.path.basename(wb_filename_6)), }) self.assertEqual(rv.status_code, 400) # invalid schema schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'invalid-schema.csv') with open(schema_filename, 'rb') as schema_file: with open(wb_filename, 'rb') as wb_file: rv = client.post('/api/validate/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (wb_file, os.path.basename(wb_filename)), }) self.assertEqual(rv.status_code, 400)
def test_normalize(self): schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) in_workbook_filename = os.path.join(self.tempdir, 'file1.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0') p_0.children.create(id='c_1') io.WorkbookWriter().run(in_workbook_filename, [p_0], models=models) client = web_service.app.test_client() # to xlsx with open(schema_filename, 'rb') as schema_file: with open(in_workbook_filename, 'rb') as in_workbook_file: rv = client.post('/api/normalize/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (in_workbook_file, os.path.basename(in_workbook_filename)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 200) out_workbook_file = os.path.join(self.tempdir, 'file2.xlsx') with open(out_workbook_file, 'wb') as file: file.write(rv.data) p_0_b = io.WorkbookReader().run( out_workbook_file, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_b.is_equal(p_0)) # to tsv with open(schema_filename, 'rb') as schema_file: with open(in_workbook_filename, 'rb') as in_workbook_file: rv = client.post('/api/normalize/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (in_workbook_file, os.path.basename(in_workbook_filename)), 'format': 'tsv', }) self.assertEqual(rv.status_code, 200) out_workbook_file = os.path.join(self.tempdir, '*.tsv') with zipfile.ZipFile(BytesIO(rv.data)) as zip_file: zip_file.extractall(self.tempdir) p_0_b = io.WorkbookReader().run( out_workbook_file, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_b.is_equal(p_0)) # invalid workbook wb = wc_utils.workbook.io.read(in_workbook_filename) wb['!!Child2'] = wb.pop('!!Child') wb['!!Child2'][0][0] = wb['!!Child2'][0][0].replace( "'Child'", "'Child2'") wc_utils.workbook.io.write(in_workbook_filename, wb) with open(schema_filename, 'rb') as schema_file: with open(in_workbook_filename, 'rb') as in_workbook_file: rv = client.post('/api/normalize/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (in_workbook_file, os.path.basename(in_workbook_filename)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 400) # invalid schema schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'invalid-schema.csv') with open(schema_filename, 'rb') as schema_file: with open(in_workbook_filename, 'rb') as in_workbook_file: rv = client.post('/api/normalize/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (in_workbook_file, os.path.basename(in_workbook_filename)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 400)
def test_convert(self): schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) workbook_filename_1 = os.path.join(self.tempdir, 'file1.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0') p_0.children.create(id='c_1') io.WorkbookWriter().run(workbook_filename_1, [p_0], models=models) # XLSX -> XLSX client = web_service.app.test_client() with open(schema_filename, 'rb') as schema_file: with open(workbook_filename_1, 'rb') as workbook_file: rv = client.post('/api/convert/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (workbook_file, os.path.basename(workbook_filename_1)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 200) workbook_filename_2 = os.path.join(self.tempdir, 'file2.xlsx') with open(workbook_filename_2, 'wb') as file: file.write(rv.data) p_0_b = io.WorkbookReader().run( workbook_filename_2, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_b.is_equal(p_0)) # XLSX -> multi.csv client = web_service.app.test_client() with open(schema_filename, 'rb') as schema_file: with open(workbook_filename_1, 'rb') as workbook_file: rv = client.post('/api/convert/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (workbook_file, os.path.basename(workbook_filename_1)), 'format': 'multi.csv', }) self.assertEqual(rv.status_code, 200) workbook_filename_3 = os.path.join(self.tempdir, 'file3.csv') with open(workbook_filename_3, 'wb') as file: file.write(rv.data) p_0_c = io.MultiSeparatedValuesReader().run( workbook_filename_3, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_c.is_equal(p_0)) # XLSX -> JSON client = web_service.app.test_client() with open(schema_filename, 'rb') as schema_file: with open(workbook_filename_1, 'rb') as workbook_file: rv = client.post('/api/convert/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (workbook_file, os.path.basename(workbook_filename_1)), 'format': 'json', }) self.assertEqual(rv.status_code, 200) workbook_filename_4 = os.path.join(self.tempdir, 'file4.json') with open(workbook_filename_4, 'wb') as file: file.write(rv.data) p_0_d = io.JsonReader().run( workbook_filename_4, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_d.is_equal(p_0)) # JSON -> YAML client = web_service.app.test_client() with open(schema_filename, 'rb') as schema_file: with open(workbook_filename_4, 'rb') as workbook_file: rv = client.post('/api/convert/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (workbook_file, os.path.basename(workbook_filename_4)), 'format': 'yml', }) self.assertEqual(rv.status_code, 200) workbook_filename_5 = os.path.join(self.tempdir, 'file5.yml') with open(workbook_filename_5, 'wb') as file: file.write(rv.data) p_0_e = io.JsonReader().run( workbook_filename_5, models=models, ignore_missing_attributes=True)[schema.Parent][0] self.assertTrue(p_0_e.is_equal(p_0)) # invalid schema schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'invalid-schema.csv') with open(schema_filename, 'rb') as schema_file: with open(workbook_filename_1, 'rb') as workbook_file: rv = client.post('/api/convert/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'workbook': (workbook_file, os.path.basename(workbook_filename_1)), 'format': 'xlsx', }) self.assertEqual(rv.status_code, 400)
def test_diff(self): schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'schema.csv') schema, _, models = utils.init_schema(schema_filename) self.assertEqual(set(models), set(utils.get_models(schema).values())) xl_file_1 = os.path.join(self.tempdir, 'file1.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0', name='c_0') p_0.children.create(id='c_1', name='c_1') io.WorkbookWriter().run(xl_file_1, [p_0], models=models) xl_file_2 = os.path.join(self.tempdir, 'file2.xlsx') p_0 = schema.Parent(id='p_0') p_0.children.create(id='c_0', name='c_0') p_0.children.create(id='c_1', name='c_0') io.WorkbookWriter().run(xl_file_2, [p_0], models=models) client = web_service.app.test_client() with open(schema_filename, 'rb') as schema_file: with open(xl_file_1, 'rb') as wb_file_1: with open(xl_file_1, 'rb') as wb_file_2: rv = client.post( '/api/diff/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (wb_file_1, os.path.basename(xl_file_1)), 'workbook-2': (wb_file_2, os.path.basename(xl_file_1)), }) self.assertEqual(rv.status_code, 200) self.assertEqual(rv.json, []) with open(schema_filename, 'rb') as schema_file: with open(xl_file_1, 'rb') as wb_file_1: with open(xl_file_2, 'rb') as wb_file_2: rv = client.post( '/api/diff/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (wb_file_1, os.path.basename(xl_file_1)), 'workbook-2': (wb_file_2, os.path.basename(xl_file_2)), }) self.assertEqual(rv.status_code, 200) self.assertNotEqual(rv.json, []) # invalid workbook wb = wc_utils.workbook.io.read(xl_file_2) wb['!!Child2'] = wb.pop('!!Child') wb['!!Child2'][0][0] = wb['!!Child2'][0][0].replace( "'Child'", "'Child2'") wc_utils.workbook.io.write(xl_file_2, wb) with open(schema_filename, 'rb') as schema_file: with open(xl_file_1, 'rb') as wb_file_1: with open(xl_file_2, 'rb') as wb_file_2: rv = client.post( '/api/diff/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (wb_file_1, os.path.basename(xl_file_1)), 'workbook-2': (wb_file_2, os.path.basename(xl_file_2)), }) self.assertEqual(rv.status_code, 400) # invalid schema schema_filename = os.path.join('tests', 'fixtures', 'declarative_schema', 'invalid-schema.csv') with open(schema_filename, 'rb') as schema_file: with open(xl_file_1, 'rb') as wb_file_1: with open(xl_file_2, 'rb') as wb_file_2: rv = client.post( '/api/diff/', data={ 'schema': (schema_file, os.path.basename(schema_filename)), 'model': 'Parent', 'workbook': (wb_file_1, os.path.basename(xl_file_1)), 'workbook-2': (wb_file_2, os.path.basename(xl_file_2)), }) self.assertEqual(rv.status_code, 400)