from django.test import TestCase

from queryset_sequence import QuerySetSequence

from .models import Article, Author, Book

class TestQuerySetSequence(TestCase):
    @classmethod
    def setUpClass(cls):
        """Set-up some data to be tested against."""
        alice = Author.objects.create(name="Alice")
        bob = Author.objects.create(name="Bob")

        # Alice wrote some articles.
        Article.objects.create(title="Django Rocks", author=alice)
        Article.objects.create(title="Alice in Django-land", author=alice)

        # Bob wrote a couple of books and an article.
        Book.objects.create(title="Fiction", author=bob)
        Book.objects.create(title="Biography", author=bob)
        Article.objects.create(title="Some Article", author=bob)

        # Save the authors for later.
        cls.alice = alice
        cls.bob = bob

    @classmethod
    def tearDownClass(cls):
        del cls.alice
        del cls.bob

        # Clear the database.
        Author.objects.all().delete()
        Article.objects.all().delete()
        Book.objects.all().delete()

    def test_length(self):
        qss = QuerySetSequence(Book.objects.filter(author=self.bob),
                               Article.objects.filter(author=self.bob))

        # The proper length should be returned via database queries.
        self.assertEqual(qss.count(), 3)
        self.assertIsNone(qss._result_cache)

        # Calling len() evaluates the QuerySet.
        self.assertEqual(len(qss), 3)
        self.assertIsNotNone(qss._result_cache)

        # Count should still work (and not hit the database).
        qss.query = None
        self.assertEqual(qss.count(), 3)

    def test_filter(self):
        qss = QuerySetSequence(Book.objects.all(), Article.objects.all())

        # Check that everything is in the current list.
        self.assertEqual(qss.count(), 5)

        # Now filter to just Bob's work.
        bob_qss = qss.filter(author=self.bob)
        self.assertEqual(bob_qss.count(), 3)

@@ -1,12 +1,50 @@
+from django.db.models import QuerySet%0A
 from django.
@@ -64,16 +64,16 @@
 estCase%0A
-
 %0Afrom qu
@@ -1144,32 +1144,151 @@
 t_length(self):%0A
+        %22%22%22%0A        Ensure that count() and len() are properly summed over the children%0A        QuerySets.%0A        %22%22%22%0A
         qss = Qu
@@ -1852,32 +1852,193 @@
 t_filter(self):%0A
+        %22%22%22%0A        Ensure that filter() properly filters the children QuerySets, note that%0A        no QuerySets are actually evaluated during this.%0A        %22%22%22%0A
         qss = Qu
@@ -2277,24 +2277,24 @@
 r=self.bob)%0A
-
         self
@@ -2310,24 +2310,1444 @@
 ual(bob_qss.count(), 3)%0A
+        self.assertIsNone(qss._result_cache)%0A%0A        # Now filter to just Alice's work.%0A        alice_qss = qss.filter(author=self.alice)%0A        self.assertEqual(alice_qss.count(), 2)%0A        self.assertIsNone(qss._result_cache)%0A        # Since we've now filtered down to a single QuerySet, we shouldn't be a%0A        # QuerySetSequence any longer.%0A        self.assertIsInstance(alice_qss, QuerySet)%0A%0A    def test_exclude(self):%0A        %22%22%22%0A        Ensure that exclude() properly excludes the children QuerySets, note%0A        that no QuerySets are actually evaluated during this.%0A%0A        Note that this is the same test as test_filter, but we exclude the other%0A        author instead of filtering.%0A        %22%22%22%0A        qss = QuerySetSequence(Book.objects.all(), Article.objects.all())%0A%0A        # Check that everything is in the current list.%0A        self.assertEqual(qss.count(), 5)%0A%0A        # Now filter to just Bob's work.%0A        bob_qss = qss.exclude(author=self.alice)%0A        self.assertEqual(bob_qss.count(), 3)%0A        self.assertIsNone(qss._result_cache)%0A%0A        # Now filter to just Alice's work.%0A        alice_qss = qss.exclude(author=self.bob)%0A        self.assertEqual(alice_qss.count(), 2)%0A        self.assertIsNone(qss._result_cache)%0A        # Since we've now filtered down to a single QuerySet, we shouldn't be a%0A        # QuerySetSequence any longer.%0A        self.assertIsInstance(alice_qss, QuerySet)%0A

Add exclude() test.