| 1 | from __future__ import absolute_import, unicode_literals |
| 2 | |
| 3 | from django.test import TestCase |
| 4 | from django.db.models import Q |
| 5 | |
| 6 | from .models import Order, OrderItem |
| 7 | |
| 8 | |
| 9 | class LookupTests(TestCase): |
| 10 | |
| 11 | def setUp(self): |
| 12 | # Create a few Orders. |
| 13 | self.o1 = Order.objects.create() |
| 14 | self.o2 = Order.objects.create() |
| 15 | self.o3 = Order.objects.create() |
| 16 | |
| 17 | # Create some OrderItems for the first order with homogeneous |
| 18 | # status_id values |
| 19 | self.oi1 = OrderItem.objects.create(order=self.o1, status=1) |
| 20 | self.oi2 = OrderItem.objects.create(order=self.o1, status=1) |
| 21 | self.oi3 = OrderItem.objects.create(order=self.o1, status=1) |
| 22 | |
| 23 | # Create some OrderItems for the second order with heterogeneous |
| 24 | # status_id values |
| 25 | self.oi4 = OrderItem.objects.create(order=self.o2, status=1) |
| 26 | self.oi5 = OrderItem.objects.create(order=self.o2, status=2) |
| 27 | self.oi6 = OrderItem.objects.create(order=self.o2, status=3) |
| 28 | |
| 29 | # Create some OrderItems for the second order with heterogeneous |
| 30 | # status_id values |
| 31 | self.oi7 = OrderItem.objects.create(order=self.o3, status=2) |
| 32 | self.oi8 = OrderItem.objects.create(order=self.o3, status=3) |
| 33 | self.oi9 = OrderItem.objects.create(order=self.o3, status=4) |
| 34 | |
| 35 | def test_exclude_plain(self): |
| 36 | """ |
| 37 | This should exclude Orders which have some items with status 1 |
| 38 | |
| 39 | """ |
| 40 | self.assertQuerysetEqual( |
| 41 | Order.objects.exclude(items__status=1), |
| 42 | [u'<Order: 3>']) |
| 43 | |
| 44 | def test_exclude_plain_distinct(self): |
| 45 | """ |
| 46 | This should exclude Orders which have some items with status 1 |
| 47 | |
| 48 | """ |
| 49 | self.assertQuerysetEqual( |
| 50 | Order.objects.exclude(items__status=1).distinct(), |
| 51 | [u'<Order: 3>']) |
| 52 | |
| 53 | def test_exclude_with_q_object_distinct(self): |
| 54 | """ |
| 55 | This should exclude Orders which have some items with status 1 |
| 56 | |
| 57 | """ |
| 58 | self.assertQuerysetEqual( |
| 59 | Order.objects.exclude(Q(items__status=1)).distinct(), |
| 60 | [u'<Order: 3>']) |
| 61 | |
| 62 | def test_exclude_with_q_object_no_distinct(self): |
| 63 | """ |
| 64 | This should exclude Orders which have some items with status 1 |
| 65 | |
| 66 | """ |
| 67 | self.assertQuerysetEqual( |
| 68 | Order.objects.exclude(Q(items__status=1)), |
| 69 | [u'<Order: 3>']) |
| 70 | |
| 71 | def test_exclude_with_q_is_equal_to_plain_exclude(self): |
| 72 | """ |
| 73 | Using exclude(condition) and exclude(Q(condition)) should |
| 74 | yield the same QuerySet |
| 75 | |
| 76 | """ |
| 77 | self.assertEquals( |
| 78 | Order.objects.exclude(items__status=1).distinct(), |
| 79 | Order.objects.exclude(Q(items__status=1)).distinct()) |
| 80 | |
| 81 | def test_exclude_with_q_is_equal_to_plain_exclude_variation(self): |
| 82 | """ |
| 83 | Using exclude(condition) and exclude(Q(condition)) should |
| 84 | yield the same QuerySet |
| 85 | |
| 86 | """ |
| 87 | self.assertEquals( |
| 88 | Order.objects.exclude(items__status=1), |
| 89 | Order.objects.exclude(Q(items__status=1)).distinct()) |
| 90 | |
| 91 | def test_only_orders_with_all_items_having_status_1(self): |
| 92 | """ |
| 93 | According to the output of test_exclude_with_q_object_*, |
| 94 | this should only return orders having ALL items set to status 1 |
| 95 | |
| 96 | """ |
| 97 | self.assertQuerysetEqual( |
| 98 | Order.objects.exclude(~Q(items__status=1)).distinct(), |
| 99 | [u'<Order: 1>']) |