Permalink
Browse files

Refactors and created test for new feature support

  • Loading branch information...
1 parent 1a37fa5 commit 462478a25672c36d3e0e0bed297800762dcb4f2c @flaviamissi committed Nov 6, 2011
Showing with 35 additions and 14 deletions.
  1. +17 −11 kami/__init__.py
  2. +18 −3 tests/test_search.py
View
@@ -3,27 +3,33 @@ class Kami(object):
def __init__(self):
self.raw_query = ''
- def filter(self, **params):
+ def filter(self, q=None, **params):
""" Simple key value search query """
- self._combine(Q(**params).to_query())
- return self
+ return self._filter_or_exclude(False, q, **params)
- def exclude(self, **params):
+ def exclude(self, q=None, **params):
""" Exclude a statement in the query """
- self._combine((~Q(**params)).query)
+ return self._filter_or_exclude(True, q, **params)
+
+ def _filter_or_exclude(self, negate, q=None, **params):
+ if q and isinstance(q, Q):
+ self.raw_query = q.query
+ else:
+ if negate:
+ self._combine((~Q(**params)))
+ else:
+ self._combine(Q(**params))
+
return self
- def _combine(self, query):
+ def _combine(self, q):
if self.raw_query:
- self.raw_query = "%s AND %s" % (self.raw_query, query)
+ self.raw_query = "%s AND %s" % (self.raw_query, q.query)
else:
- self.raw_query = query
+ self.raw_query = q.query
class Q(object):
- OR = 'OR'
- AND = 'AND'
-
def __init__(self, **params):
self.params = params
self.to_query()
View
@@ -1,6 +1,6 @@
from unittest import TestCase
-from kami import Kami
+from kami import Kami, Q
#TODO test format equality by regex
@@ -16,11 +16,23 @@ def test_should_pass_title(self):
def test_should_pass_artist_and_title(self):
query = self.kami.filter(title='Mein Teil', artist='Rammstein').raw_query
- self.assertEqual('title: "Mein Teil" AND artist: "Rammstein"', query)
+ self.assertEqual('artist: "Rammstein" AND title: "Mein Teil"', query)
def test_should_have_one_AND_only_between_a_pair_of_statements(self):
query = self.kami.filter(title='Mein Teil', artist='Rammstein', album='Reise, Reise').raw_query
- self.assertEqual('album: "Reise, Reise" AND title: "Mein Teil" AND artist: "Rammstein"', query)
+ self.assertEqual('album: "Reise, Reise" AND artist: "Rammstein" AND title: "Mein Teil"', query)
+
+ def test_should_support_calling_filter_method_with_Q_objects_as_params(self):
+ query = self.kami.filter(Q(title='Mein Teil') & Q(artist='Rammstein')).raw_query
+ self.assertEqual('title: "Mein Teil" AND artist: "Rammstein"', query)
+
+ def test_should_call_filter_with_Q_objects_separeted_with_OR(self):
+ query = self.kami.filter(Q(title='Mein Teil') | Q(artist='Rammstein')).raw_query
+ self.assertEqual('title: "Mein Teil" OR artist: "Rammstein"', query)
+
+ def test_should_call_filter_with_Q_objects_negating_an_parameter(self):
+ query = self.kami.filter(~Q(title='Mein Teil') | Q(artist='Rammstein')).raw_query
+ self.assertEqual('NOT title: "Mein Teil" OR artist: "Rammstein"', query)
class SimpleExcludeSearchQueryTestCase(TestCase):
@@ -36,6 +48,9 @@ def test_should_negate_and_concatenate_when_receive_two_fields(self):
query = self.kami.exclude(title='Amerika', album='Reise, Reise').raw_query
self.assertEqual('NOT album: "Reise, Reise" AND NOT title: "Amerika"', query)
+ def test_should_support_calling_exclude_with_Q_objects_as_params(self):
+ query = self.kami.exclude(Q(title='Mein Teil') | Q(artist='Rammstein')).raw_query
+ self.assertEqual('NOT title: "Mein Teil" OR NOT artist: "Rammstein"', query)
class ExcludeAndSimpleSearchMustWorkTogetherTestCase(TestCase):

0 comments on commit 462478a

Please sign in to comment.