Select Git revision
test_identifiable_adapters.py
-
Henrik tom Wörden authoredHenrik tom Wörden authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_identifiable_adapters.py 4.04 KiB
#!/usr/bin/env python3
# encoding: utf-8
#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2021 Indiscale GmbH <info@indiscale.com>
# Copyright (C) 2021 Henrik tom Wörden <h.tomwoerden@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/>.
#
# ** end header
#
"""
test identifiable_adapters module
"""
import os
from datetime import datetime
from caoscrawler.identifiable_adapters import (
CaosDBIdentifiableAdapter, IdentifiableAdapter)
from caoscrawler.identifiable import Identifiable
import caosdb as db
def test_create_query_for_identifiable():
query = IdentifiableAdapter.create_query_for_identifiable(
Identifiable(record_type="Person", properties={"first_name": "A", "last_name": "B"}))
assert query.lower() == "find record person with 'first_name'='a' and 'last_name'='b' "
query = IdentifiableAdapter.create_query_for_identifiable(
Identifiable(name="A", record_type="B", properties={
"c": "c",
"d": 5,
"e": 5.5,
"f": datetime(2020, 10, 10),
"g": True,
"h": db.Record(id=1111),
"i": db.File(id=1112),
"j": [2222, db.Record(id=3333)]}))
assert (query == "FIND RECORD B WITH name='A' AND 'c'='c' AND 'd'='5' AND 'e'='5.5'"
" AND 'f'='2020-10-10T00:00:00' AND 'g'='TRUE' AND 'h'='1111' AND 'i'='1112' AND "
"'j'='2222' AND 'j'='3333' ")
# The name can be the only identifiable
query = IdentifiableAdapter.create_query_for_identifiable(
Identifiable(name="TestRecord", record_type="TestType"))
assert query.lower() == "find record testtype with name='testrecord'"
# With referencing entity (backref)
query = IdentifiableAdapter.create_query_for_identifiable(
Identifiable(record_type="Person", backrefs=[14433], properties={'last_name': "B"}))
assert query.lower() == ("find record person which is referenced by 14433 and with "
"'last_name'='b' ")
# With two referencing entities (backref)
query = IdentifiableAdapter.create_query_for_identifiable(
Identifiable(record_type="Person", backrefs=[14433, 333], properties={'last_name': "B"}))
assert query.lower() == ("find record person which is referenced by 14433 and which is "
"referenced by 333 and with 'last_name'='b' ")
# With single quote in string
query = IdentifiableAdapter.create_query_for_identifiable(
Identifiable(record_type="Person", backrefs=[], properties={'last_name': "B'Or"}))
assert query == ("FIND RECORD Person WITH 'last_name'='B\\'Or' ")
def test_load_from_yaml_file():
ident = CaosDBIdentifiableAdapter()
ident.load_from_yaml_definition(
os.path.join(os.path.dirname(__file__), "test_directories",
"single_file_test_data", "identifiables.yml")
)
person_i = ident.get_registered_identifiable(
db.Record().add_parent("Person"))
assert person_i is not None
assert person_i.get_property("full_name") is not None
keyword_i = ident.get_registered_identifiable(
db.Record().add_parent("Keyword"))
assert keyword_i is not None
assert keyword_i.get_property("name") is not None
project_i = ident.get_registered_identifiable(
db.Record().add_parent("Project"))
assert project_i is not None
assert project_i.get_property("project_id") is not None
assert project_i.get_property("title") is not None