Esempio n. 1
0
    def test_relations(self, mock_get_relation_name):
        model = mock.MagicMock()
        relations = get_relations(model)
        expect = {
            'version': '0.1',
            'abbreviation': 'REL',
            'description': 'GOB Relations',
            'collections': {}
        }
        self.assertEqual(relations, expect)

        mock_get_relation_name.return_value = 'name'
        model._data = {
            "catalog": {
                "abbreviation": "cat",
                "collections": {
                    "collection": {
                        "abbreviation": "col",
                        "attributes": {}
                    }
                }
            }
        }
        model._extract_references.return_value = {
            "reference": {
                "type": "GOB.Reference",
                "ref": "dst_cat:dst_col"
            }
        }
        relations = get_relations(model)
        self.assertIsNotNone(relations['collections']['name'])
        self.assertEqual(len(relations['collections'].items()), 1)
def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    model = GOBModel()

    connection = op.get_bind()

    for relation_name, relation in get_relations(model)['collections'].items():
        table_name = f"rel_{relation_name}"

        try:
            # The code uses the current GOBModel
            # This model is normally more recent that the database
            # because of the migrations that have still to be processes after this migration
            # So do not fail on any missing tables
            res = connection.execute(
                f"SELECT * FROM {table_name} WHERE bronwaarde IS NULL LIMIT 1")

            if res.fetchall():
                print(
                    f"{table_name} contains NULL values for bronwaarde. Clearing table and events."
                )

                op.execute(
                    f"DELETE FROM events WHERE entity='{relation_name}'")
                op.execute(f"TRUNCATE {table_name}")
        except Exception:
            connection.execute(f"ROLLBACK")
Esempio n. 3
0
    def get_all(self):
        """Returns definitions of materialized views

        :return:
        """
        definitions = []

        for relation_name in model_relations.get_relations(self.model)['collections'].keys():
            definitions.append(MaterializedView(relation_name))

        return definitions
Esempio n. 4
0
    def __init__(self):
        if GOBModel._data is not None:
            # Model is already initialised
            return

        path = os.path.join(os.path.dirname(__file__), 'gobmodel.json')
        with open(path) as file:
            data = json.load(file)

        if os.getenv('DISABLE_TEST_CATALOGUE', False):
            # Default is to include the test catalogue
            # By setting the DISABLE_TEST_CATALOGUE environment variable
            # the test catalogue can be removed
            del data["test_catalogue"]

        GOBModel._data = data
        self._load_schemas()
        data[QUALITY_CATALOG] = get_quality_assurances(self)
        data["rel"] = get_relations(self)

        global_attributes = {
            **PRIVATE_META_FIELDS,
            **PUBLIC_META_FIELDS,
            **FIXED_FIELDS
        }

        # Extract references for easy access in API. Add catalog and collection names to catalog and collection objects
        for catalog_name, catalog in self._data.items():
            catalog['name'] = catalog_name

            for entity_name, model in catalog['collections'].items():
                model['name'] = entity_name
                model['references'] = self._extract_references(
                    model['attributes'])
                model[
                    'very_many_references'] = self._extract_very_many_references(
                        model['attributes'])

                model_attributes = model['attributes']
                state_attributes = STATE_FIELDS if self.has_states(
                    catalog_name, entity_name) else {}
                all_attributes = {**state_attributes, **model_attributes}

                # Add fields to the GOBModel to be used in database creation and lookups
                model['fields'] = all_attributes

                # Include complete definition, including all global fields
                model['all_fields'] = {**all_attributes, **global_attributes}
Esempio n. 5
0
    def __init__(self):
        path = os.path.join(os.path.dirname(__file__), 'gobmodel.json')
        with open(path) as file:
            data = json.load(file)

        if os.getenv('DISABLE_TEST_CATALOGUE', False):
            # Default is to include the test catalogue
            # By setting the DISABLE_TEST_CATALOGUE environment variable
            # the test catalogue can be removed
            del data["test_catalogue"]

        self._data = data
        self._data["rel"] = get_relations(self)

        global_attributes = {
            **PRIVATE_META_FIELDS,
            **PUBLIC_META_FIELDS,
            **FIXED_FIELDS
        }

        # Extract references for easy access in API
        for catalog_name, catalog in self._data.items():
            for entity_name, model in catalog['collections'].items():
                model['references'] = self._extract_references(model['attributes'])

                self._set_api(catalog_name, entity_name, model)

                model_attributes = model['attributes']
                state_attributes = STATE_FIELDS if self.has_states(catalog_name, entity_name) else {}
                all_attributes = {
                    **state_attributes,
                    **model_attributes
                }

                # Add fields to the GOBModel to be used in database creation and lookups
                model['fields'] = all_attributes

                # Include complete definition, including all global fields
                model['all_fields'] = {
                    **all_attributes,
                    **global_attributes
                }