test_article.py 2.49 KB
Newer Older
1
2
from django.test import TestCase, Client
from django.contrib.auth.models import User
luxcem's avatar
luxcem committed
3

luxcem's avatar
luxcem committed
4
5
6
from rp.models import Article
from rp.factories import ArticleFactory
from rp.apps import RpConfig
7
from rp.views.articles import ArticleList
luxcem's avatar
luxcem committed
8
9


10
class TestArticle(TestCase):
luxcem's avatar
luxcem committed
11
12
    def setUp(self):
        self.article = ArticleFactory()
luxcem's avatar
luxcem committed
13

luxcem's avatar
luxcem committed
14
15
    def test_init(self):
        assert RpConfig.name == "rp"
luxcem's avatar
luxcem committed
16

luxcem's avatar
luxcem committed
17
18
    def test_article(self):
        assert type(self.article) == Article
luxcem's avatar
luxcem committed
19
        assert str(self.article) == self.article.title
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72


class TestArticleViews(TestCase):
    def setUp(self):
        self.client = Client()
        self.articles = [ ArticleFactory(tags=['Tag 1', 'Tag2']) for i in range(0, 2 * ArticleList.paginate_by)]
        self.user = User.objects.create(username="test", email="test@example.org", password="test")
        for a in self.articles:
            a.save()

    def test_list_fr(self):
        # ArticleFactory use english by default, so we should have
        # no objects
        r = self.client.get('/rp/')
        assert len(r.context['object_list']) == 0

    def test_list_en(self):
        r = self.client.get('/rp/international')
        assert r.context['is_paginated']
        assert len(r.context['object_list']) == ArticleList.paginate_by

    def test_filter_tag(self):
        tag = self.articles[0].tags.all()[1]

        r = self.client.get('/rp/by-tag/{}'.format(tag.name))
        assert r.context['is_paginated']
        assert len(r.context['object_list']) == ArticleList.paginate_by
        r = self.client.get('/rp/by-tag/zogzog')
        assert len(r.context['object_list']) == 0

    def test_filter_view(self):
        new = [article for article in self.articles if article.status == 'NEW']

        r = self.client.get('/rp/by-tag/new')
        assert len(r.context['object_list']) == len(new)
        r = self.client.get('/rp/by-tag/nosuchtag')
        assert len(r.context['object_list']) == 0

    def test_search_view(self):
        article = ArticleFactory(title=u'Zog Zog chez les schtroumphs', lang='FR')
        article.save()

        r = self.client.get('/rp/', {'q': 'Zog Zog'})
        assert len(r.context['article_list']) == 1
        r = self.client.get('/rp/', {'q': 'Gargamel was here'})
        assert len(r.context['article_list']) == 0

    def test_detail_view(self):
        # Let's find a published article
        self.client.force_login(user=self.user)
        a = self.articles[0]
        r = self.client.get('/rp/article/view/{}'.format(a.pk))
        assert r.context['object'] == a