diff --git a/src/linkahead/apiutils.py b/src/linkahead/apiutils.py index 88687716d16343fd3d7fb2d10de32f75513bdab9..39f97fcd49b88fa727102facd01a1579b5b36404 100644 --- a/src/linkahead/apiutils.py +++ b/src/linkahead/apiutils.py @@ -623,22 +623,3 @@ def _same_id_as_resolved_entity(this, that): if not isinstance(this, Entity) and isinstance(that, Entity): return that.id is not None and that.id == this return this == that - - -def escape_quoted_text(text: str) -> str: - """The characters `\\` and `*` need to be escaped if used in quoted expressions in the query - language. - - This function return the given string where the characters `\\` and `*` are escaped by a `\\`. - - Parameters - ---------- - text : str - The text to be escaped - - Returns - ------- - str - The escaped text - """ - return text.replace('\\', r'\\').replace('*', r'\*') diff --git a/src/linkahead/utils/get_entity.py b/src/linkahead/utils/get_entity.py index ea9f3228bfc32f223979846623fccdec45752e5d..9fee45826b46c5a5ec9ca8c1e0bec134f5d7ce77 100644 --- a/src/linkahead/utils/get_entity.py +++ b/src/linkahead/utils/get_entity.py @@ -22,7 +22,9 @@ """Convenience functions to retrieve a specific entity.""" from typing import Union -from ..common.models import execute_query, Entity + +from ..common.models import Entity, execute_query +from .escape import escape_quoted_text def get_entity_by_name(name: str) -> Entity: @@ -30,6 +32,7 @@ def get_entity_by_name(name: str) -> Entity: Submits the query "FIND ENTITY WITH name='{name}'". """ + name = escape_quoted_text(name) return execute_query(f"FIND ENTITY WITH name='{name}'", unique=True) diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 47d8e3e9f02bb010190213616195ecc91647dcf4..3ed719f7b00a4c5bf25583ac6729738d3d872f57 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -31,8 +31,7 @@ import linkahead.apiutils import pytest from linkahead.apiutils import (EntityMergeConflictError, apply_to_ids, compare_entities, create_id_query, empty_diff, - escape_quoted_text, merge_entities, - resolve_reference) + merge_entities, resolve_reference) from linkahead.common.models import SPECIAL_ATTRIBUTES @@ -633,10 +632,3 @@ def test_merge_id_with_resolved_entity(): merge_entities(recA, recB, merge_id_with_resolved_entity=True) assert recA.get_property(rtname).value == [ref_id, ref_id*2] assert recA.get_property(rtname).value == recB.get_property(rtname).value - - -def test_escape_quoted_text(): - assert escape_quoted_text("bla") == "bla" - assert escape_quoted_text("bl\\a") == "bl\\\\a" - assert escape_quoted_text("bl*a") == "bl\\*a" - assert escape_quoted_text("bl*ab\\\\lab\\*labla") == "bl\\*ab\\\\\\\\lab\\\\\\*labla" diff --git a/unittests/test_utils.py b/unittests/test_utils.py index 3d8e2896247f66c98f1461c1a1e91baca5f01cb6..aa3ec8745ef5f47bb4f85106e1df4c66d8b962de 100644 --- a/unittests/test_utils.py +++ b/unittests/test_utils.py @@ -23,8 +23,10 @@ # """Tests for linkahead.common.utils.""" from __future__ import unicode_literals -from lxml.etree import Element + from linkahead.common.utils import xml2str +from linkahead.utils.escape import escape_quoted_text +from lxml.etree import Element def test_xml2str(): @@ -32,3 +34,11 @@ def test_xml2str(): element = Element(name) serialized = xml2str(element) assert serialized == "<Björn/>\n" + + + +def test_escape_quoted_text(): + assert escape_quoted_text("bla") == "bla" + assert escape_quoted_text("bl\\a") == "bl\\\\a" + assert escape_quoted_text("bl*a") == "bl\\*a" + assert escape_quoted_text("bl*ab\\\\lab\\*labla") == "bl\\*ab\\\\\\\\lab\\\\\\*labla"