From 7408f5098ba0a40488c4201a8c9f0ae05d3b7f00 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 16 Oct 2010 17:00:11 +0000 Subject: [PATCH] Converted or_lookups tests from doctests to unittests. We have always been at war with doctests. Thanks to Paul Tax for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14236 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/or_lookups/models.py | 109 ------------ tests/modeltests/or_lookups/tests.py | 232 ++++++++++++++++++++++++++ 2 files changed, 232 insertions(+), 109 deletions(-) create mode 100644 tests/modeltests/or_lookups/tests.py diff --git a/tests/modeltests/or_lookups/models.py b/tests/modeltests/or_lookups/models.py index 1179c6d2dc5..7f14ba50eb6 100644 --- a/tests/modeltests/or_lookups/models.py +++ b/tests/modeltests/or_lookups/models.py @@ -20,112 +20,3 @@ class Article(models.Model): def __unicode__(self): return self.headline - -__test__ = {'API_TESTS':""" ->>> from datetime import datetime ->>> from django.db.models import Q - ->>> a1 = Article(headline='Hello', pub_date=datetime(2005, 11, 27)) ->>> a1.save() - ->>> a2 = Article(headline='Goodbye', pub_date=datetime(2005, 11, 28)) ->>> a2.save() - ->>> a3 = Article(headline='Hello and goodbye', pub_date=datetime(2005, 11, 29)) ->>> a3.save() - ->>> Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye') -[, , ] - ->>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')) -[, , ] - ->>> Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')) -[] - -# You can shorten this syntax with code like the following, -# which is especially useful if building the query in stages: ->>> articles = Article.objects.all() ->>> articles.filter(headline__startswith='Hello') & articles.filter(headline__startswith='Goodbye') -[] - ->>> articles.filter(headline__startswith='Hello') & articles.filter(headline__contains='bye') -[] - ->>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello') -[] - ->>> Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye') -[, , ] - ->>> Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood') -[, , ] - ->>> Article.objects.filter(Q(pk=1) | Q(pk=2)) -[, ] - ->>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) -[, , ] - -# You could also use "in" to accomplish the same as above. ->>> Article.objects.filter(pk__in=[1,2,3]) -[, , ] ->>> Article.objects.filter(pk__in=(1,2,3)) -[, , ] - ->>> Article.objects.filter(pk__in=[1,2,3,4]) -[, , ] - -# Passing "in" an empty list returns no results ... ->>> Article.objects.filter(pk__in=[]) -[] - -# ... but can return results if we OR it with another query. ->>> Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye')) -[, ] - -# Q arg objects are ANDed ->>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')) -[] - -# Q arg AND order is irrelevant ->>> Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello') -[] - -# Q objects can be negated ->>> Article.objects.filter(Q(pk=1) | ~Q(pk=2)) -[, ] ->>> Article.objects.filter(~Q(pk=1) & ~Q(pk=2)) -[] - -# This allows for more complex queries than filter() and exclude() alone would -# allow ->>> Article.objects.filter(Q(pk=1) & (~Q(pk=2) | Q(pk=3))) -[] - -# Try some arg queries with operations other than filter. ->>> Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye')) - - ->>> Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count() -3 - ->>> dicts = list(Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values()) ->>> [sorted(d.items()) for d in dicts] -[[('headline', u'Hello and goodbye'), ('id', 3), ('pub_date', datetime.datetime(2005, 11, 29, 0, 0))]] - ->>> Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([1,2]) -{1: } - -# Demonstrating exclude with a Q object ->>> Article.objects.exclude(Q(headline__startswith='Hello')) -[] - -# The 'complex_filter' method supports framework features such as -# 'limit_choices_to' which normally take a single dictionary of lookup arguments -# but need to support arbitrary queries via Q objects too. ->>> Article.objects.complex_filter({'pk': 1}) -[] ->>> Article.objects.complex_filter(Q(pk=1) | Q(pk=2)) -[, ] -"""} diff --git a/tests/modeltests/or_lookups/tests.py b/tests/modeltests/or_lookups/tests.py new file mode 100644 index 00000000000..ad218cd0b21 --- /dev/null +++ b/tests/modeltests/or_lookups/tests.py @@ -0,0 +1,232 @@ +from datetime import datetime +from operator import attrgetter + +from django.db.models import Q +from django.test import TestCase + +from models import Article + + +class OrLookupsTests(TestCase): + + def setUp(self): + self.a1 = Article.objects.create( + headline='Hello', pub_date=datetime(2005, 11, 27) + ).pk + self.a2 = Article.objects.create( + headline='Goodbye', pub_date=datetime(2005, 11, 28) + ).pk + self.a3 = Article.objects.create( + headline='Hello and goodbye', pub_date=datetime(2005, 11, 29) + ).pk + + def test_filter_or(self): + self.assertQuerysetEqual( + Article.objects.filter(headline__startswith='Hello') | Article.objects.filter(headline__startswith='Goodbye'), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline") + ) + + self.assertQuerysetEqual( + Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye'), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline") + ) + + self.assertQuerysetEqual( + Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood'), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline") + ) + + self.assertQuerysetEqual( + Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline") + ) + + + def test_stages(self): + # You can shorten this syntax with code like the following, which is + # especially useful if building the query in stages: + articles = Article.objects.all() + self.assertQuerysetEqual( + articles.filter(headline__startswith='Hello') & articles.filter(headline__startswith='Goodbye'), + [] + ) + self.assertQuerysetEqual( + articles.filter(headline__startswith='Hello') & articles.filter(headline__contains='bye'), [ + 'Hello and goodbye' + ], + attrgetter("headline") + ) + + def test_pk_q(self): + self.assertQuerysetEqual( + Article.objects.filter(Q(pk=self.a1) | Q(pk=self.a2)), [ + 'Hello', + 'Goodbye' + ], + attrgetter("headline") + ) + + self.assertQuerysetEqual( + Article.objects.filter(Q(pk=self.a1) | Q(pk=self.a2) | Q(pk=self.a3)), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + + def test_pk_in(self): + self.assertQuerysetEqual( + Article.objects.filter(pk__in=[self.a1, self.a2, self.a3]), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + + self.assertQuerysetEqual( + Article.objects.filter(pk__in=(self.a1, self.a2, self.a3)), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + + self.assertQuerysetEqual( + Article.objects.filter(pk__in=[self.a1, self.a2, self.a3, 40000]), [ + 'Hello', + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + + def test_q_negated(self): + # Q objects can be negated + self.assertQuerysetEqual( + Article.objects.filter(Q(pk=self.a1) | ~Q(pk=self.a2)), [ + 'Hello', + 'Hello and goodbye' + ], + attrgetter("headline") + ) + + self.assertQuerysetEqual( + Article.objects.filter(~Q(pk=self.a1) & ~Q(pk=self.a2)), [ + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + # This allows for more complex queries than filter() and exclude() + # alone would allow + self.assertQuerysetEqual( + Article.objects.filter(Q(pk=self.a1) & (~Q(pk=self.a2) | Q(pk=self.a3))), [ + 'Hello' + ], + attrgetter("headline"), + ) + + def test_complex_filter(self): + # The 'complex_filter' method supports framework features such as + # 'limit_choices_to' which normally take a single dictionary of lookup + # arguments but need to support arbitrary queries via Q objects too. + self.assertQuerysetEqual( + Article.objects.complex_filter({'pk': self.a1}), [ + 'Hello' + ], + attrgetter("headline"), + ) + + self.assertQuerysetEqual( + Article.objects.complex_filter(Q(pk=self.a1) | Q(pk=self.a2)), [ + 'Hello', + 'Goodbye' + ], + attrgetter("headline"), + ) + + def test_empty_in(self): + # Passing "in" an empty list returns no results ... + self.assertQuerysetEqual( + Article.objects.filter(pk__in=[]), + [] + ) + # ... but can return results if we OR it with another query. + self.assertQuerysetEqual( + Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye')), [ + 'Goodbye', + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + + def test_q_and(self): + # Q arg objects are ANDed + self.assertQuerysetEqual( + Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')), [ + 'Hello and goodbye' + ], + attrgetter("headline") + ) + # Q arg AND order is irrelevant + self.assertQuerysetEqual( + Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello'), [ + 'Hello and goodbye' + ], + attrgetter("headline"), + ) + + self.assertQuerysetEqual( + Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')), + [] + ) + + def test_q_exclude(self): + self.assertQuerysetEqual( + Article.objects.exclude(Q(headline__startswith='Hello')), [ + 'Goodbye' + ], + attrgetter("headline") + ) + + def test_other_arg_queries(self): + # Try some arg queries with operations other than filter. + self.assertEqual( + Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye')).headline, + 'Hello and goodbye' + ) + + self.assertEqual( + Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count(), + 3 + ) + + self.assertQuerysetEqual( + Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values(), [ + {"headline": "Hello and goodbye", "id": self.a3, "pub_date": datetime(2005, 11, 29)}, + ], + lambda o: o, + ) + + self.assertEqual( + Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([self.a1, self.a2]), + {self.a1: Article.objects.get(pk=self.a1)} + )