def test_load_schema_error(self, mock_print, mock_load_schema): mock_load_schema.side_effect = SchemaException model = GOBModel() model._data = { 'cat_a': { 'collections': { 'coll_a': { 'attributes': { 'some': 'attribute', }, }, 'coll_b': { '_attributes': { 'some': 'attribute', }, 'schema': 'schema_b', } } }, } expected = copy.deepcopy(model._data) expected['cat_a']['collections']['coll_b']['attributes'] = \ expected['cat_a']['collections']['coll_b']['_attributes'] model._load_schemas() self.assertEqual(expected, model._data) mock_print.assert_called_once() self.assertTrue(mock_print.call_args[0][0].startswith( 'ERROR: failed to load schema'))
def test_catalog_collection_from_abbr(self): model = GOBModel() model._data = { 'cat_a': { 'abbreviation': 'ca', 'collections': { 'col_a': { 'abbreviation': 'coa', 'some other': 'data', }, 'col_b': { 'abbreviation': 'cob', 'some other': 'data', } } } } self.assertEqual(({ 'abbreviation': 'ca', 'collections': { 'col_a': { 'abbreviation': 'coa', 'some other': 'data', }, 'col_b': { 'abbreviation': 'cob', 'some other': 'data', } } }, { 'abbreviation': 'cob', 'some other': 'data', }), model.get_catalog_collection_from_abbr('ca', 'cob'))
def test_load_schemas(self, mock_load_schema): mock_load_schema.return_value = 'loaded_attributes' model = GOBModel() model._data = { 'cat_a': { 'collections': { 'coll_a': { 'attributes': { 'some': 'attribute', }, }, 'coll_b': { '_attributes': { 'some': 'attribute', }, 'schema': 'schema_b', } } }, } expected = copy.deepcopy(model._data) expected['cat_a']['collections']['coll_b'][ 'attributes'] = 'loaded_attributes' model._load_schemas() self.assertEqual(expected, model._data) mock_load_schema.assert_called_with('schema_b', 'cat_a', 'coll_b')
def test_get_collection_by_name(self): model = GOBModel() model._data = { 'cat_a': { 'collections': { 'coll_a': 'collection a', 'coll_b': 'collection b' } }, 'cat_b': { 'collections': { 'coll_a': 'second collection a' } } } # Success res = model.get_collection_by_name('coll_b') self.assertEqual(('cat_a', 'collection b'), res) # Not found self.assertIsNone(model.get_collection_by_name('nonexistent')) # Multiple found with self.assertRaises(GOBException): model.get_collection_by_name('coll_a')
def test_catalog_from_abbr(self): model = GOBModel() model._data = { 'cat_a': { 'abbreviation': 'ca', 'collections': { 'col_a': {} } } } self.assertEqual(model._data['cat_a'], model.get_catalog_from_abbr('ca')) with self.assertRaises(NoSuchCatalogException): model.get_catalog_from_abbr('nonexistent')
def test_catalog_collection_from_abbr(self): model = GOBModel() model._data = { 'cat_a': { 'abbreviation': 'ca', 'collections': { 'col_a': { 'abbreviation': 'coa', 'some other': 'data', }, 'col_b': { 'abbreviation': 'cob', 'some other': 'data', } } } } self.assertEqual(({ 'abbreviation': 'ca', 'collections': { 'col_a': { 'abbreviation': 'coa', 'some other': 'data', }, 'col_b': { 'abbreviation': 'cob', 'some other': 'data', } } }, { 'abbreviation': 'cob', 'some other': 'data', }), model.get_catalog_collection_from_abbr('ca', 'cob')) with self.assertRaises(NoSuchCatalogException): model.get_catalog_collection_from_abbr('cc', 'cob') with self.assertRaises(NoSuchCollectionException): model.get_catalog_collection_from_abbr('ca', 'coc')
def test_add_resolve_attrs_to_columns(self): class MockColumn: def __init__(self, name): self.name = name model = GOBModel() model._data = { 'the_catalog': { 'abbreviation': 'cat', 'name': 'the_catalog', 'collections': { 'the_collection': { 'name': 'the_collection', 'abbreviation': 'col', 'attributes': { 'ignored_column': { 'column': 'spec', 'of': 'ignored_column', }, 'resolved_column': { 'column': 'spec', 'of': 'resolved_column', } } }, 'the_collection2': { 'name': 'the_collection2', 'abbreviation': 'col2', 'attributes': { 'other_resolved_column': { 'column': 'spec', 'of': 'other_resolved_column' } } } } } } columns = [ MockColumn('ignored_column'), MockColumn('cat:col:resolved_column'), MockColumn('cat:col2:other_resolved_column'), MockColumn('cat:col2:nonexisting'), ] with mock.patch("gobapi.storage.GOBModel", return_value=model): _add_resolve_attrs_to_columns(columns) self.assertFalse(hasattr(columns[0], 'attribute')) self.assertFalse(hasattr(columns[0], 'public_name')) self.assertEqual({ 'column': 'spec', 'of': 'resolved_column', }, getattr(columns[1], 'attribute')) self.assertEqual('resolved_column', getattr(columns[1], 'public_name')) self.assertEqual({ 'column': 'spec', 'of': 'other_resolved_column', }, getattr(columns[2], 'attribute')) self.assertEqual('other_resolved_column', getattr(columns[2], 'public_name')) self.assertFalse(hasattr(columns[3], 'attribute')) self.assertFalse(hasattr(columns[3], 'public_name'))