from django.test import TestCase
from django.urls import reverse
from .models import Article, Category
from .services import get_published_articles

class ArticleModelTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.category = Category.objects.create(name="Новости", slug="news")
        cls.article = Article.objects.create(
            title="Тест",
            slug="test",
            category=cls.category,
            content="Контент"
        )

    def test_article_creation(self):
        self.assertEqual(self.article.title, "Тест")
        self.assertTrue(self.article.is_published)

    def test_get_absolute_url(self):
        url = self.article.get_absolute_url()
        self.assertEqual(url, reverse('blog:article_detail', kwargs={'slug': 'test'}))

class ArticleListViewTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.category = Category.objects.create(name="Новости", slug="news")
        for i in range(15):
            Article.objects.create(
                title=f"Статья {i}",
                slug=f"statya-{i}",
                category=cls.category,
                content="Контент"
            )

    def test_view_url_exists(self):
        resp = self.client.get('/blog/')
        self.assertEqual(resp.status_code, 200)

    def test_pagination_is_ten(self):
        resp = self.client.get(reverse('blog:article_list'))
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('is_paginated' in resp.context)
        self.assertTrue(resp.context['is_paginated'])
        self.assertEqual(len(resp.context['articles']), 10)

    def test_service_returns_published(self):
        articles = get_published_articles()
        self.assertEqual(len(articles), 15)

