def test_read_write(self): fixture_filename = os.path.join(os.path.dirname(__file__), 'fixtures', 'example-model.xlsx') model = Reader().run(fixture_filename)[Model][0] self.assertEqual(model.validate(), None) # compare excel files Writer().run(self.filename, model, data_repo_metadata=False) original = read_workbook(fixture_filename) copy = read_workbook(self.filename) remove_ws_metadata(original) remove_ws_metadata(copy) original.pop('!!' + obj_tables.core.TOC_SHEET_NAME) copy.pop('!!' + obj_tables.core.TOC_SHEET_NAME) # note that models must be sorted by id for this assertion to hold for sheet in original.keys(): for i_row, (copy_row, original_row) in enumerate(zip(copy[sheet], original[sheet])): self.assertEqual(copy_row, original_row, msg='Rows {} of {} sheets are not equal'.format(i_row, sheet)) self.assertEqual(copy[sheet], original[sheet], msg='{} sheets are not equal'.format(sheet)) self.assertEqual(copy, original) # compare models model2 = Reader().run(self.filename)[Model][0] self.assertTrue(model2.is_equal(model)) self.assertTrue(model.difference(model2) == '')
def test_convert_sloppy(self): filename_xls1 = os.path.join(self.tempdir, 'model1.xlsx') filename_xls2 = os.path.join(self.tempdir, 'model2.xlsx') filename_csv = os.path.join(self.tempdir, 'model-*.csv') Writer().run(filename_xls1, self.model, data_repo_metadata=False) wb = read_workbook(filename_xls1) row = wb['!!Model'].pop(3) wb['!!Model'].insert(4, row) write_workbook(filename_xls1, wb) with self.assertRaisesRegex(ValueError, "The rows of worksheet '!!Model' must be defined in this order"): convert(filename_xls1, filename_csv) env = EnvironmentVarGuard() env.set('CONFIG__DOT__wc_lang__DOT__io__DOT__strict', '0') with env: convert(filename_xls1, filename_csv) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'model-Model.csv'))) self.assertTrue(os.path.isfile(os.path.join(self.tempdir, 'model-Taxon.csv'))) model = Reader().run(filename_csv)[Model][0] self.assertTrue(model.is_equal(self.model)) convert(filename_csv, filename_xls2) model = Reader().run(filename_xls2)[Model][0] self.assertTrue(model.is_equal(self.model))
def test_read_without_validation(self): # write model to file filename = os.path.join(self.tempdir, 'model.xlsx') Writer().run(filename, self.model, data_repo_metadata=False) # read model and verify that it validates model = Reader().run(filename)[Model][0] self.assertEqual(model.validate(), None) # introduce error into model file wb = read_workbook(filename) wb['!!Model'][4][1] = '1000' write_workbook(filename, wb) # read model and verify that it doesn't validate with self.assertRaisesRegex( ValueError, 'The model cannot be loaded because it fails to validate'): Reader().run(filename) env = EnvironmentVarGuard() env.set('CONFIG__DOT__wc_lang__DOT__io__DOT__validate', '0') with env: model = Reader().run(filename)[Model][0] self.assertNotEqual(model.validate(), None)
def _default(self): args = self.app.pargs if args.compare_files: model1 = read_workbook(args.path_1) model2 = read_workbook(args.path_2) if not args.compare_metadata_in_files: self.remove_metadata(model1) self.remove_metadata(model2) diff = model1.difference(model2) else: model1 = Reader().run(args.path_1)[Model][0] model2 = Reader().run(args.path_2)[Model][0] diff = model1.difference(model2) if diff: print(diff) else: print('Models are identical')
def test_write_read_sloppy(self): filename = os.path.join(self.tempdir, 'model.xlsx') Writer().run(filename, self.model, data_repo_metadata=False) wb = read_workbook(filename) row = wb['!!Model'].pop(3) wb['!!Model'].insert(4, row) write_workbook(filename, wb) with self.assertRaisesRegex(ValueError, "The rows of worksheet '!!Model' must be defined in this order"): Reader().run(filename) env = EnvironmentVarGuard() env.set('CONFIG__DOT__wc_lang__DOT__io__DOT__strict', '0') with env: model = Reader().run(filename)[Model][0] self.assertEqual(model.validate(), None) self.assertTrue(model.is_equal(self.model)) self.assertEqual(self.model.difference(model), '')