Ticket #17600: exclude-tests-ticket17600.diff

File exclude-tests-ticket17600.diff, 4.5 KB (added by gabejackson, 3 years ago)

Test Cases for Q, ~Q and exclude bugs

  • new file tests/modeltests/excludes/models.py

    diff --git a/tests/modeltests/excludes/__init__.py b/tests/modeltests/excludes/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/modeltests/excludes/models.py b/tests/modeltests/excludes/models.py
    new file mode 100644
    index 0000000..56d2d09
    - +  
     1"""
     27. The lookup API
     3
     4This demonstrates features of the database API.
     5"""
     6
     7from __future__ import unicode_literals
     8
     9from django.db import models
     10
     11class Order(models.Model):
     12    class Meta:
     13        ordering = ('pk', )
     14
     15    def __unicode__(self):
     16        return '%s' % self.pk
     17
     18class OrderItem(models.Model):
     19    order = models.ForeignKey(Order, related_name='items')
     20    status = models.IntegerField()
     21   
     22    class Meta:
     23        ordering = ('pk', )
     24
     25    def __unicode__(self):
     26        return '%s' % self.pk
  • new file tests/modeltests/excludes/tests.py

    diff --git a/tests/modeltests/excludes/tests.py b/tests/modeltests/excludes/tests.py
    new file mode 100644
    index 0000000..b30f932
    - +  
     1from __future__ import absolute_import, unicode_literals
     2
     3from django.test import TestCase
     4from django.db.models import Q
     5
     6from .models import Order, OrderItem
     7
     8
     9class 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>'])       
Back to Top