def test_precedence_of_negation(self): p = ~Q(first_name='a') q = Q(last_name='b') r = p & q self.assertEqual(r.to_sql(Person), "(last_name = 'b') AND (NOT (first_name = 'a'))") r = q & p self.assertEqual(r.to_sql(Person), "(last_name = 'b') AND (NOT (first_name = 'a'))") r = q | p self.assertEqual(r.to_sql(Person), "(last_name = 'b') OR (NOT (first_name = 'a'))") r = ~q & p self.assertEqual( r.to_sql(Person), "(NOT (last_name = 'b')) AND (NOT (first_name = 'a'))")
def test_mixed_filter(self): qs = Person.objects_in(self.database) qs = qs.filter(Q(first_name='a'), F('greater', Person.height, 1.7), last_name='b') self.assertEqual( qs.conditions_as_sql(), "(first_name = 'a') AND (greater(`height`, 1.7)) AND (last_name = 'b')" )
def test_count(self): self.database.insert(self._sample_data()) self.assertEqual(self.database.count(Person), 100) # Conditions as string self.assertEqual(self.database.count(Person, "first_name = 'Courtney'"), 2) self.assertEqual(self.database.count(Person, "birthday > '2000-01-01'"), 22) self.assertEqual(self.database.count(Person, "birthday < '1970-03-01'"), 0) # Conditions as expression self.assertEqual(self.database.count(Person, Person.birthday > datetime.date(2000, 1, 1)), 22) # Conditions as Q object self.assertEqual(self.database.count(Person, Q(birthday__gt=datetime.date(2000, 1, 1))), 22)
def test_pagination_with_conditions(self): self._insert_and_check(self._sample_data(), len(data)) # Conditions as string page = self.database.paginate(Person, 'first_name, last_name', 1, 100, conditions="first_name < 'Ava'") self.assertEqual(page.number_of_objects, 10) # Conditions as expression page = self.database.paginate(Person, 'first_name, last_name', 1, 100, conditions=Person.first_name < 'Ava') self.assertEqual(page.number_of_objects, 10) # Conditions as Q object page = self.database.paginate(Person, 'first_name, last_name', 1, 100, conditions=Q(first_name__lt='Ava')) self.assertEqual(page.number_of_objects, 10)
def test_filter_with_q_objects(self): qs = Person.objects_in(self.database) self._test_qs(qs.filter(Q(first_name='Ciaran')), 2) self._test_qs( qs.filter(Q(first_name='Ciaran') | Q(first_name='Chelsea')), 3) self._test_qs( qs.filter( Q(first_name__in=['Warren', 'Whilemina', 'Whitney']) & Q(height__gte=1.7)), 3) self._test_qs( qs.filter((Q(first_name__in=['Warren', 'Whilemina', 'Whitney']) & Q(height__gte=1.7) | (Q(first_name__in=['Victoria', 'Victor', 'Venus']) & Q(height__lt=1.7)))), 4) self._test_qs(qs.filter(Q(first_name='Elton') & ~Q(last_name='Smith')), 1) # Check operator precendence self._test_qs( qs.filter(first_name='Cassady').filter( Q(last_name='Knapp') | Q(last_name='Rogers') | Q(last_name='Gregory')), 2) self._test_qs( qs.filter( Q(first_name='Cassady') & Q(last_name='Knapp') | Q(first_name='Beatrice') & Q(last_name='Gregory')), 2) self._test_qs( qs.filter( Q(first_name='Courtney') | Q(first_name='Cassady') & Q(last_name='Knapp')), 3)
def test_filter_with_q_objects(self): qs = Person.objects_in(self.database) self._test_qs(qs.filter(Q(first_name='Ciaran')), 2) self._test_qs( qs.filter(Q(first_name='Ciaran') | Q(first_name='Chelsea')), 3) self._test_qs( qs.filter( Q(first_name__in=['Warren', 'Whilemina', 'Whitney']) & Q(height__gte=1.7)), 3) self._test_qs( qs.filter((Q(first_name__in=['Warren', 'Whilemina', 'Whitney']) & Q(height__gte=1.7) | (Q(first_name__in=['Victoria', 'Victor', 'Venus']) & Q(height__lt=1.7)))), 4) self._test_qs(qs.filter(Q(first_name='Elton') & ~Q(last_name='Smith')), 1)