def test_compare_columns(self): """ SphinxQL does not allow comparison between columns, see http://sphinxsearch.com/bugs/view.php?id=2015. """ q = self.query.filter(C('added_time') > C('date')) self.assertEqual(len(q), 1)
def test_filter_between_integer(self): self.query = self.query.filter(C('number') | Between | (1, 3)) self.assertEqual(len(self.query), 1) self.query = self.query.filter(C('number') | Between | (10, 20)) self.assertEqual(len(self.query), 0)
def test_filter(self): self.assertEqual(len(self.query), 1) q = self.query.filter(C('number') >= 2) self.assertEqual(len(q), 1) q = self.query.filter(C('number') < 2) self.assertEqual(len(q), 0)
def test_filter_between_datetimes(self): self.query = self.query.filter(C('added_time') |Between| (datetime.datetime(2014, 1, 1), datetime.datetime(2016, 1, 1))) self.assertEqual(len(self.query), 1) self.query = self.query.filter(C('added_time') |Between| (datetime.datetime(2000, 1, 1), datetime.datetime(2001, 1, 1))) self.assertEqual(len(self.query), 0)
def test_order_by(self): q = SphinxQuerySet(DocumentIndex).order_by(C('number')) self.assertEqual(q[0].number, 2) q = SphinxQuerySet(DocumentIndex).order_by(-C('number')) self.assertEqual(q[0].number, 200) q = SphinxQuerySet(DocumentIndex).search('@text What').order_by(C('@relevance')) # most relevance is last, because has the most occurrences. self.assertEqual(q[0].number, 200) # other ordering q = SphinxQuerySet(DocumentIndex).search('@text What').order_by(C('number')) self.assertEqual(q[0].number, 2)
def test_exclude(self): """ SphinxQL does not accept queries of the form "WHERE NOT condition", see http://sphinxsearch.com/bugs/view.php?id=2004. """ q = self.query.filter(~(C('number') == 2)) self.assertEqual(len(q), 0)
def test_django_filter(self): query = self.query.filter(number__gt=190) self.assertEqual(len(query), 5) self.assertEqual(query[0].number, 192) q = query.search('@text text. What') self.assertEqual(len(q), 5) self.assertEqual(q[0].number, 200) q = query.search('@text text. What').search_order_by(-C('@relevance')) self.assertEqual(len(q), 5) self.assertEqual(q[0].number, 192)
def test_django_len(self): query = self.query.filter(number__gte=95) # exclude id in [1,94] self.assertEqual(len(query), 910) self.assertEqual(query.count(), 910) # since search results are ordered by id, they are ids 1-1000, # thus, the first 94 (id 1-94) + last 5 (id 1000-1005) are discarded self.assertEqual(len(query.search('@text nice')), 906) self.assertEqual(query.search('nice').count(), 906) query = query.search_order_by(-C('@id')) self.assertEqual(len(query.search('@text nice')), 910) self.assertEqual(query.search('nice').count(), 910)
def test_override_order_by(self): # order by in Django means first number is 200 query = self.query.order_by('-id') self.assertEqual(query[0].number, 200) # don't override Django order means first number is 200 query.search_mode = True query = query.search_order_by(C('@id')) self.assertEqual(query[0].number, 200) # clear Django ordering overrides it; order becomes @id. query = query.order_by() self.assertEqual(query[0].number, 2)
def test_override_order_by(self): # order by in Django means first number is 200 query = self.query.order_by('-id') self.assertEqual(query[0].number, 200) query = query.search_order_by(C('@id')) with self.assertRaises(Exception) as context: self.assertEqual(query[0].number, 200) self.assertTrue('Can not order by both database and sphinx' in str( context.exception)) # clear Django ordering overrides it; order becomes @id. query = query.order_by() self.assertEqual(query[0].number, 2)
def test_filter_string(self): q = self.query.filter(C('summary') == 'This is a summary') self.assertEqual(len(q), 1) q = self.query.filter(C('summary') == 'This') self.assertEqual(len(q), 0)
def test_two_filters(self): q = self.query.filter(C('number') >= 2) q = q.filter(C('date') == datetime.date(2015, 2, 2)) self.assertEqual(len(q), 1)
def test_lookup_fail(self): self.assertRaises(KeyError, self.query.filter, C('numberERROR') >= 2)
def test_date_comparison(self): q = self.query.filter(C('date') < datetime.datetime(2016, 1, 1)) self.assertEqual(len(q), 1)
def test_or(self): """ OR not defined in SphinxQL. """ q = self.query.filter(Or(C('number') == 2, C('number') > 2)) self.assertEqual(len(q), 1)
def test_search_doesnt_override_ordering(self): q = self.query.search_order_by(C('number')) self.assertEqual(q[0].number, 2) q = q.search('@text What') self.assertEqual(q[0].number, 2)