Skip to content
Snippets Groups Projects
test_json_schema_datamodel_parser.py 2.21 KiB
Newer Older
#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2022 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2022 Florian Spreckelsen <f.spreckelsen@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#

import os

import caosdb as db
from caosadvancedtools.models.parser import parse_model_from_json_schema


def _clear_db():
    ents = db.execute_query("FIND ENTITY WITH ID>99")
    if ents:
        ents.delete()


def setup_module():
    _clear_db()


def teardown_module():
    _clear_db()


def _load_and_sync(fname):
    """Load datamodel from json schema in fname and synchronize it without asking.

    """
    # @author Florian Spreckelsen
    # @date 2022-03-23
    fpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), fname)
    model = parse_model_from_json_schema(fpath)
    model.sync_data_model(noquestion=True)


def test_json_parsed_datamodel():
    # @author Florian Spreckelsen
    # @date 2022-03-23

    _load_and_sync("test_datamodel.schema.json")

    # RecordType with atomic properties
    rt1 = db.execute_query(
        "FIND RECORDTYPE TestTypeWithAtomicProps", unique=True)
    assert rt1.description == "RecordType with scalar atomic properties"
    assert rt1.get_property("simple_text_prop") is not None
    assert rt1.get_property("simple_text_prop").datatype == db.TEXT
    assert rt1.get_importance("simple_text_prop") == db.OBLIGATORY

    # RecordType with references and enums
    rt2 = db.execute_query(
        "FIND RECORDTYPE TestTypeWithReferencesAndEnum", unique=True)

    # Recordtype with lists
    rt3 = db.execute_query("FIND RECORDTYPE TestTypeWithLists", unique=True)