from django.utils import timezone
from django.test import TestCase
from django.core.urlresolvers import reverse

from .views import MainView
from article.models import Articles
from tag.models import Tags
from category.models import Categories


class ViewTest(TestCase):

    """
    Testing the views.
    """

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        MainView.page_dif = 1
        MainView.paginate_by = 1
        tags = [Tags.create_tag('tag1')]
        categories = [Categories.create_category('category1')]
        for i in range(7):
            Articles.create_article('title', 'html', 'html',
                                    timezone.now(), categories, tags)

    def test_pagination_1_345_7(self):
        """
        Testing pagination with a page in the middle.
        """

        response = self.client.get(reverse('main')+'?page=4')
        should = ["1", "'...'", "3", "4", "5", "'...'", "7"]
        self.assertQuerysetEqual(response.context['pages_list'], should)

    def test_pagination_12_7(self):
        """
        Testing pagination with a page in the start.
        """

        response = self.client.get(reverse('main'))
        should = ["1", "2", "'...'", "7"]
        self.assertQuerysetEqual(response.context['pages_list'], should)

    def test_pagination_1_67(self):
        """
        Testing pagination with a page in the finish.
        """

        response = self.client.get(reverse('main')+'?page=7')
        should = ["1", "'...'", "6", "7"]
        self.assertQuerysetEqual(response.context['pages_list'], should)
