From 448cc19185c8c517780c468a5a2832ca5baf332a Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 4 Nov 2015 09:04:07 +0100 Subject: [PATCH] Fixing ts_query function --- taiga/base/utils/db.py | 1 + tests/unit/test_utils.py | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/taiga/base/utils/db.py b/taiga/base/utils/db.py index 2308840a..82694014 100644 --- a/taiga/base/utils/db.py +++ b/taiga/base/utils/db.py @@ -204,6 +204,7 @@ def to_tsquery(term): continue bit = bit.replace("'", "") + bit = bit.replace("\\", "") if not bit: continue diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 9521974b..ff6889ab 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -17,9 +17,10 @@ from unittest import mock import django_sites as sites +import re from taiga.base.utils.urls import get_absolute_url, is_absolute_url, build_url -from taiga.base.utils.db import save_in_bulk, update_in_bulk, update_in_bulk_with_ids +from taiga.base.utils.db import save_in_bulk, update_in_bulk, update_in_bulk_with_ids, to_tsquery def test_is_absolute_url(): @@ -89,3 +90,27 @@ def test_update_in_bulk_with_ids(): ] model.objects.filter.assert_has_calls(expected_calls) + + +TS_QUERY_TRANSFORMATIONS = [ + ("1 OR 2", "1 | 2"), + ("(1) 2", "( 1 ) & 2"), + ("&", "'&':*"), + ('"hello world"', "'hello world'"), + ("not 1", "! 1"), + ("1 and not (2 or 3)", "1 & ! ( 2 | 3 )"), + ("not and and 1) or ( 2 not", "! 1 | ( 2 )"), + ("() 1", "1"), + ("1 2 3", "1 & 2 & 3"), + ("'&' |", "'&':* & '|':*"), + (") and 1 (2 or", "1 & ( 2 )"), + ("it's '", "'its':*"), + ("(1)", "( 1 )"), + ("1((", "1"), + ("test\\", "'test':*"), +] +def test_to_tsquery(): + for (input, expected) in TS_QUERY_TRANSFORMATIONS: + expected = re.sub("([0-9])", r"'\1':*", expected) + actual = to_tsquery(input) + assert actual == expected