From c1fc9922f6b29429caf95c79e912a035cd7338c3 Mon Sep 17 00:00:00 2001 From: Matt Luongo Date: Wed, 29 Jun 2011 14:55:09 -0400 Subject: [PATCH] Fixed the test suite (there was git trash), query NameErrors and added a couple simple tests. --- lucenequerybuilder/query.py | 12 +++++++----- lucenequerybuilder/tests.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lucenequerybuilder/query.py b/lucenequerybuilder/query.py index 1e9b03e..6e5d2f0 100644 --- a/lucenequerybuilder/query.py +++ b/lucenequerybuilder/query.py @@ -14,9 +14,9 @@ def __init__(self, *args, **kwargs): self._child_has_field = False if len(args) == 1 and not kwargs: if Q._check_whitespace(args[0]): - self.should.append('"'+escape(args[0])+'"') + self.should.append('"'+self._escape(args[0])+'"') else: - self.should.append(escape(args[0])) + self.should.append(self._escape(args[0])) elif len(args) <= 1 and kwargs: if kwargs.get('inrange'): self.inrange = kwargs['inrange'] @@ -33,9 +33,9 @@ def __init__(self, *args, **kwargs): self.field = args[0] self._has_field = True if Q._check_whitespace(args[1]): - self.should.append('"'+_escape(args[1])+'"') + self.should.append('"'+self._escape(args[1])+'"') else: - self.should.append(_escape(args[1])) + self.should.append(self._escape(args[1])) if self._check_nested_fields(): raise ValueError('No nested fields allowed.') @@ -55,10 +55,12 @@ def _escape(cls, s): if isinstance(s, basestring): rv = '' for c in s: - if c in specialchars: + if c in cls.specialchars: rv += '\\' + c else: rv += c + return rv + return s def _make_and(q1, q2): q = Q() diff --git a/lucenequerybuilder/tests.py b/lucenequerybuilder/tests.py index b52e550..66bbb7f 100644 --- a/lucenequerybuilder/tests.py +++ b/lucenequerybuilder/tests.py @@ -1,10 +1,12 @@ -<<<<<<< Updated upstream:lucenequerybuilder/tests.py +""" +Simple tests for Q. In the future, comparing output to an actual Lucene index +would be a good idea. +""" + from lucenequerybuilder import Q -======= -from query import Q ->>>>>>> Stashed changes:querybuilder/tests.py +import re -def test_lol(): +def test_general(): a = 'a' b = 'b' c = 'c' @@ -28,4 +30,23 @@ def test_lol(): else: raise AssertionError("Shouldn't allow nested fields.") -test_lol() +def test_simple_term(): + query_string = str(Q('a')) + assert query_string == 'a', query_string + +def test_simple_phrase(): + query_string = str(Q('abc 123')) + assert query_string == '"abc 123"', query_string + +#this test doesn't work, but might be worth rewriting +# +#def test_escaping(): +# """ Tests basic character escaping. Doesn't test double char escape, eg &&, ||.""" +# special_lucene_chars = r'\+-!(){}[]^"~*?:' +# unescaped_regex = '|'.join([r'(([^\\]|^)%s)' % re.escape(c) for c in special_lucene_chars]) +# unescaped_regex = re.compile(unescaped_regex) +# #test the regex +# assert unescaped_regex.match(r'\ [ )') is not None +# query_string = str(Q(':') & Q('\\')) +# #this won't work, dur +# assert not unescaped_regex.match(query_string), query_string