From e9518a0d2dc7ae44f4c39b0d58c9a1ad48744e4f Mon Sep 17 00:00:00 2001 From: Alfred Rubin Date: Fri, 29 Sep 2023 16:41:10 +0200 Subject: [PATCH] now multival props behave as sets during the ingestion and added test to check the behaviour --- .../query_composers/NodeQueryComposer.py | 3 +- test/integration/multival_test.py | 41 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/rdflib_neo4j/query_composers/NodeQueryComposer.py b/rdflib_neo4j/query_composers/NodeQueryComposer.py index 7a4e912..b7414e6 100644 --- a/rdflib_neo4j/query_composers/NodeQueryComposer.py +++ b/rdflib_neo4j/query_composers/NodeQueryComposer.py @@ -4,7 +4,8 @@ def prop_query_append(prop): - return f"""n.`{prop}` = CASE WHEN COALESCE(param["{prop}"], NULL) IS NULL THEN n.{prop} ELSE COALESCE(n.`{prop}`,[]) + param["{prop}"] END""" + return f"""n.`{prop}` = CASE WHEN COALESCE(param["{prop}"], NULL) IS NULL THEN n.{prop} ELSE REDUCE(i=COALESCE(n.{prop},[]), val IN param["{prop}"] | CASE WHEN val IN i THEN i ELSE i+val END) END """ + def prop_query_single(prop): diff --git a/test/integration/multival_test.py b/test/integration/multival_test.py index 2077bbd..fbbd2cf 100644 --- a/test/integration/multival_test.py +++ b/test/integration/multival_test.py @@ -1,4 +1,4 @@ -from test.integration.constants import LOCAL +from test.integration.constants import LOCAL, RDFLIB_DB from rdflib import Graph, Namespace from rdflib_neo4j.Neo4jStore import Neo4jStore from rdflib_neo4j.config.Neo4jStoreConfig import Neo4jStoreConfig @@ -41,7 +41,6 @@ def test_read_file_multival_with_strategy_no_predicates(neo4j_container, neo4j_d def test_read_file_multival_with_strategy_and_predicates(neo4j_container, neo4j_driver): - """Compare data imported with n10s procs and n10s + rdflib in single add mode for multivalues""" """Compare data imported with n10s procs and n10s + rdflib in single add mode for multivalues""" auth_data = get_credentials(LOCAL, neo4j_container) @@ -101,3 +100,41 @@ def test_read_file_multival_with_no_strategy_and_predicates(neo4j_container, neo assert len(records_from_rdf_lib) == len(records) for i in range(len(records)): assert records_equal(records[i], records_from_rdf_lib[i]) + +def test_read_file_multival_array_as_set_behavior(neo4j_container, neo4j_driver): + """When importing the data, if a triple will add the same value to a multivalued property it won't be added""" + auth_data = get_credentials(LOCAL, neo4j_container) + + prefixes = {'music': Namespace('neo4j://graph.schema#')} + + custom_mappings = [] + + multival_props = [("rdfs", "label")] + config = Neo4jStoreConfig(auth_data=auth_data, + custom_prefixes=prefixes, + custom_mappings=custom_mappings, + multival_props_names=multival_props, + handle_vocab_uri_strategy=HANDLE_VOCAB_URI_STRATEGY.IGNORE, + handle_multival_strategy=HANDLE_MULTIVAL_STRATEGY.ARRAY, + batching=False) + + graph_store = Graph(store=Neo4jStore(config=config)) + + payload1 = """ "Sparklight"@en .\ + "Donald Ernest. Graham II" .\ + .\ + "3.04391E8"^^ .\ + """ + payload2 = """ "Donald Ernest. Graham II" . """ + + payload3 = """ "Donald Ernest. Graham II" . """ + + for p in [payload1,payload2,payload3]: + graph_store.parse(data=p, format="ttl") + + records, summary, keys = neo4j_driver.execute_query("MATCH (n) WHERE size(n.label) > 1 RETURN n", + database_=RDFLIB_DB) + + assert len(records) == 0 + + x = 1 \ No newline at end of file