Skip to content

Commit

Permalink
Fixed the test suite (there was git trash), query NameErrors and adde…
Browse files Browse the repository at this point in the history
…d a couple simple tests.
  • Loading branch information
mhluongo committed Jun 29, 2011
1 parent 46501ec commit c1fc992
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
12 changes: 7 additions & 5 deletions lucenequerybuilder/query.py
Expand Up @@ -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']
Expand All @@ -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.')

Expand All @@ -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()
Expand Down
33 changes: 27 additions & 6 deletions 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'
Expand All @@ -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

0 comments on commit c1fc992

Please sign in to comment.