Skip to content
Snippets Groups Projects
Commit 609a0062 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

ENH: add a function that checks the existence of ids

parent 4f6c5669
No related branches found
No related tags found
1 merge request!23F reference field
Pipeline #15793 canceled
......@@ -31,6 +31,7 @@ import logging
import pathlib
from datetime import datetime
import caosdb as db
import numpy as np
import pandas as pd
from xlrd import XLRDError
......@@ -56,6 +57,21 @@ def assure_name_format(name):
return name
def check_reference_field(ent_id, recordtype):
if 1 != db.execute_query("COUNT {} WIHT id={}".format(
recordtype,
ent_id),
unique=True):
raise ValueError(
"No {} with the supplied id={} exists. \n"
"Please supply a valid ID.".format(
recordtype,
ent_id
))
return ent_id
def yes_no_converter(val):
"""
converts a string to True or False if possible.
......
......@@ -74,6 +74,9 @@ class H5CFoodTest(unittest.TestCase):
self.assertEqual(i.name, "group_level2_aa")
def test_collect_existing_structure(self):
# TODO this does probably break the code: The function will not be
# restored correctly.
# Change it to use the BaseMockUpTest
real_retrieve = caosdb.apiutils.retrieve_entity_with_id
caosdb.apiutils.retrieve_entity_with_id = dummy_get
......
......@@ -23,22 +23,25 @@ import unittest
from functools import partial
from tempfile import NamedTemporaryFile
import caosdb as db
import numpy as np
import pandas as pd
import pytest
from caosadvancedtools.datainconsistency import DataInconsistencyError
from caosadvancedtools.table_importer import (XLSImporter, assure_name_format,
from caosadvancedtools.table_importer import (CSVImporter, TableImporter,
TSVImporter, XLSImporter,
assure_name_format,
check_reference_field,
date_converter,
datetime_converter,
TableImporter,
TSVImporter,
CSVImporter,
incomplete_date_converter,
string_in_list,
win_path_converter,
win_path_list_converter,
string_in_list,
yes_no_converter)
from test_utils import BaseMockUpTest
class ConverterTest(unittest.TestCase):
def test_yes_no(self):
......@@ -211,3 +214,34 @@ class TSVImporterTest(TableImporterTest):
self.valid_df.to_csv(tmp.name, sep="\t")
importer = TSVImporter(**self.importer_kwargs)
importer.read_file(tmp.name)
class CountQueryNoneConverterTest(BaseMockUpTest):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# simulate that 0 entity exists
self.entities = (
'<Response count="0">'
'<Query string="count record" results="0">'
'</Query>'
'</Response>'
)
def test_check_reference_field(self):
self.assertRaises(ValueError, check_reference_field, "1232", "Max")
class CountQuerySingleConverterTest(BaseMockUpTest):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# simulate that 1 entity exists
self.entities = (
'<Response count="1">'
'<Query string="count record" results="1">'
'</Query>'
'</Response>'
)
def test_check_reference_field(self):
self.assertEqual(check_reference_field("1232", "Max"),
"1232")
......@@ -32,14 +32,7 @@ from caosdb.connection.mockup import MockUpResponse, MockUpServerConnection
from caosdb.exceptions import TransactionError
class ReferencesBaseTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.entities = (
'<Response><File name="test.npy" path="/some/path/test.npy'
'" id="1234"/><Query string="find record" results="1">'
'</Query></Response>')
class BaseMockUpTest(unittest.TestCase):
def setUp(self):
conlogger = logging.getLogger("connection")
conlogger.setLevel(level=logging.ERROR)
......@@ -70,6 +63,15 @@ class ReferencesBaseTest(unittest.TestCase):
return log
class ReferencesBaseTest(BaseMockUpTest):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.entities = (
'<Response><File name="test.npy" path="/some/path/test.npy'
'" id="1234"/><Query string="find record" results="1">'
'</Query></Response>')
def test_ref(self):
self.clear_log()
files = get_referenced_files("test.npy", prefix=None, filename=None,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment