Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for issue 613 including test cases; combining complex queries was…

… broken.
  • Loading branch information...
commit 9192dbd9a30c15b1b847261c1e53a60b8b626a28 1 parent 769f5a8
Martin J. Laubach authored
Showing with 26 additions and 1 deletion.
  1. +1 −1  haystack/backends/__init__.py
  2. +25 −0 tests/core/tests/query.py
View
2  haystack/backends/__init__.py
@@ -599,7 +599,7 @@ def add_filter(self, query_filter, use_or=False):
else:
connector = SQ.AND
- if self.query_filter and query_filter.connector != SQ.AND and len(query_filter) > 1:
+ if self.query_filter and query_filter.connector != connector and len(query_filter) > 1:
self.query_filter.start_subtree(connector)
subtree = True
else:
View
25 tests/core/tests/query.py
@@ -726,6 +726,31 @@ def test___or__(self):
self.assertTrue(isinstance(sqs, SearchQuerySet))
self.assertEqual(len(sqs.query.query_filter), 2)
+ def test_and_or(self):
+ """
+ Combining AND queries with OR should give
+ AND(OR(a, b), OR(c, d))
+ """
+ sqs1 = self.msqs.filter(content='foo').filter(content='oof')
+ sqs2 = self.msqs.filter(content='bar').filter(content='rab')
+ sqs = sqs1 | sqs2
+
+ self.assertEqual(sqs.query.query_filter.connector, 'OR')
+ self.assertEqual(repr(sqs.query.query_filter.children[0]), repr(sqs1.query.query_filter))
+ self.assertEqual(repr(sqs.query.query_filter.children[1]), repr(sqs2.query.query_filter))
+
+ def test_or_and(self):
+ """
+ Combining OR queries with AND should give
+ OR(AND(a, b), AND(c, d))
+ """
+ sqs1 = self.msqs.filter(content='foo').filter_or(content='oof')
+ sqs2 = self.msqs.filter(content='bar').filter_or(content='rab')
+ sqs = sqs1 & sqs2
+
+ self.assertEqual(sqs.query.query_filter.connector, 'AND')
+ self.assertEqual(repr(sqs.query.query_filter.children[0]), repr(sqs1.query.query_filter))
+ self.assertEqual(repr(sqs.query.query_filter.children[1]), repr(sqs2.query.query_filter))
class ValuesQuerySetTestCase(SearchQuerySetTestCase):
def test_values_sqs(self):
Please sign in to comment.
Something went wrong with that request. Please try again.