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

MAITN: move function

parent 4dcd8e8b
No related branches found
No related tags found
2 merge requests!130Release v0.14.0,!125F escape
Pipeline #46643 failed
......@@ -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'\*')
......@@ -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)
......
......@@ -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"
......@@ -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"
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