File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,12 @@ def __add__(self, other):
108
108
def __sub__ (self , other ):
109
109
return self | Q ._make_must_not (self )
110
110
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
+
111
117
def __str__ (self ):
112
118
rv = ''
113
119
if hasattr (self , 'field' ):
Original file line number Diff line number Diff line change 4
4
"""
5
5
6
6
from lucenequerybuilder import Q
7
- import re
8
7
9
8
def test_general ():
10
9
a = 'a'
@@ -38,6 +37,23 @@ def test_simple_phrase():
38
37
query_string = str (Q ('abc 123' ))
39
38
assert query_string == '"abc 123"' , query_string
40
39
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
+
41
57
#this test doesn't work, but might be worth rewriting
42
58
#
43
59
#def test_escaping():
You can’t perform that action at this time.
0 commit comments