Skip to content

Commit a7b11a7

Browse files
committed
Merge pull request #6 from mhluongo/master
Closed issue #5
2 parents f648b4d + cf3d0d8 commit a7b11a7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lucenequerybuilder/query.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ def __add__(self, other):
108108
def __sub__(self, other):
109109
return self | Q._make_must_not(self)
110110

111+
def __eq__(self, other):
112+
return hash(self) == hash(other)
113+
114+
def __hash__(self):
115+
return hash((tuple(self.should), tuple(self.must), tuple(self.must_not)))
116+
111117
def __str__(self):
112118
rv = ''
113119
if hasattr(self, 'field'):

lucenequerybuilder/tests.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
from lucenequerybuilder import Q
7-
import re
87

98
def test_general():
109
a = 'a'
@@ -38,6 +37,23 @@ def test_simple_phrase():
3837
query_string = str(Q('abc 123'))
3938
assert query_string == '"abc 123"', query_string
4039

40+
def test_hashing():
41+
q1 = Q('a') & Q('b') | Q('c')
42+
q2 = Q('a') & Q('b') | Q('c')
43+
q3 = q1 | Q('d')
44+
45+
assert q1 == q2, "Queries aren't being properly evaluated for equality."
46+
assert q2 != q3, "Queries aren't being properly evaluated for inequality."
47+
48+
d = {}
49+
try:
50+
d[q1] = 1
51+
d[q2] = 2
52+
except:
53+
raise AssertionError('There was an error using queries as dict keys.')
54+
assert d[q2] == 2, 'Got the wrong value back from the query dict!'
55+
56+
4157
#this test doesn't work, but might be worth rewriting
4258
#
4359
#def test_escaping():

0 commit comments

Comments
 (0)