diff --git a/src/caoscrawler/identifiable_adapters.py b/src/caoscrawler/identifiable_adapters.py index 653951f404f2ccbd71f6ff998e6713a4f449dade..7a27dfeb6df8128ab825924b26279aff8df4f442 100644 --- a/src/caoscrawler/identifiable_adapters.py +++ b/src/caoscrawler/identifiable_adapters.py @@ -42,7 +42,7 @@ def convert_value(value): Parameters ---------- - value : The property of which the value shall be returned. + value : the value that shall be returned and potentially converted. Returns ------- @@ -54,11 +54,13 @@ def convert_value(value): return str(value.id) elif isinstance(value, datetime): return value.isoformat() - elif type(value) == str: + elif isinstance(value, bool): + return str(value).upper() + elif isinstance(value, str): # replace single quotes, otherwise they may break the queries return value.replace("\'", "\\'") else: - return f"{value}" + return str(value).strip() class IdentifiableAdapter(metaclass=ABCMeta): @@ -97,7 +99,7 @@ class IdentifiableAdapter(metaclass=ABCMeta): whether the required record already exists. """ - query_string = "FIND Record " + query_string = "FIND RECORD " if ident.record_type is not None: query_string += ident.record_type for ref in ident.backrefs: diff --git a/unittests/test_identifiable_adapters.py b/unittests/test_identifiable_adapters.py index 6817b9e6993c0ec509354b68ff60d9a9caf534ae..c9c9e86aee5dbebf12a83dcaa26eef2f9668bca5 100644 --- a/unittests/test_identifiable_adapters.py +++ b/unittests/test_identifiable_adapters.py @@ -50,9 +50,9 @@ def test_create_query_for_identifiable(): "h": db.Record(id=1111), "i": db.File(id=1112), "j": [2222, db.Record(id=3333)]})) - assert (query.lower() == "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' ") + 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( @@ -71,6 +71,13 @@ def test_create_query_for_identifiable(): 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"})) + print("find record person which is referenced by 14433 and which is " + "referenced by 333 and with 'last_name'='B\\'Or' ") + assert query == ("FIND RECORD Person WITH 'last_name'='B\\'Or' ") + def test_load_from_yaml_file(): ident = CaosDBIdentifiableAdapter()