"""
Tests that the custom queryset methods work, this is important
because the pattern of building them is different from Django
1.5 to 1.6 to 1.7 so there will be 3 patterns in play at the
same time.
"""
from wiki.models import Article
from wiki.models import URLPath
from wiki.plugins.attachments.models import Attachment

from ..base import ArticleTestBase


class ArticleManagerTests(ArticleTestBase):
    def test_queryset_methods_directly_on_manager(self):
        self.assertEqual(Article.objects.can_read(self.superuser1).count(), 1)
        self.assertEqual(Article.objects.can_write(self.superuser1).count(), 1)
        self.assertEqual(Article.objects.active().count(), 1)

    def test_mass_deletion(self):
        """
        https://github.com/django-wiki/django-wiki/issues/857
        """
        Article.objects.all().delete()
        self.assertEqual(Article.objects.all().count(), 0)

    def test_queryset_methods_on_querysets(self):
        self.assertEqual(
            Article.objects.all().can_read(self.superuser1).count(), 1
        )
        self.assertEqual(
            Article.objects.all().can_write(self.superuser1).count(), 1
        )
        self.assertEqual(Article.objects.all().active().count(), 1)

    # See: https://code.djangoproject.com/ticket/22817
    def test_queryset_empty_querysets(self):
        self.assertEqual(
            Article.objects.none().can_read(self.superuser1).count(), 0
        )
        self.assertEqual(
            Article.objects.none().can_write(self.superuser1).count(), 0
        )
        self.assertEqual(Article.objects.none().active().count(), 0)


class AttachmentManagerTests(ArticleTestBase):
    def test_queryset_methods_directly_on_manager(self):
        # Do the same for Attachment which uses ArtickeFkManager
        self.assertEqual(
            Attachment.objects.can_read(self.superuser1).count(), 0
        )
        self.assertEqual(
            Attachment.objects.can_write(self.superuser1).count(), 0
        )
        self.assertEqual(Attachment.objects.active().count(), 0)

    def test_queryset_methods_on_querysets(self):
        self.assertEqual(
            Attachment.objects.all().can_read(self.superuser1).count(), 0
        )
        self.assertEqual(
            Attachment.objects.all().can_write(self.superuser1).count(), 0
        )
        self.assertEqual(Attachment.objects.all().active().count(), 0)

    # See: https://code.djangoproject.com/ticket/22817
    def test_queryset_empty_query_sets(self):
        self.assertEqual(
            Attachment.objects.none().can_read(self.superuser1).count(), 0
        )
        self.assertEqual(
            Attachment.objects.none().can_write(self.superuser1).count(), 0
        )
        self.assertEqual(Attachment.objects.none().active().count(), 0)


class URLPathManagerTests(ArticleTestBase):
    def test_related_manager_works_with_filters(self):
        root = URLPath.root()
        self.assertNotIn(root.id, [p.id for p in root.children.active()])
