Commit 4e7004d3 authored by okhin's avatar okhin 🚴

Adding a lot of tests for the /rp/ views

parent 8616f16a
Pipeline #2560 passed with stages
in 3 minutes and 6 seconds
......@@ -2,9 +2,9 @@ import datetime
import pytz
import factory
from factory.fuzzy import FuzzyDateTime
from factory.fuzzy import FuzzyDateTime, FuzzyChoice
from .models import Article
from .models import Article, STATUS_CHOICES
class ArticleFactory(factory.django.DjangoModelFactory):
......@@ -12,7 +12,7 @@ class ArticleFactory(factory.django.DjangoModelFactory):
model = Article
url = factory.Faker("url")
lang = "en"
lang = "EN"
title = factory.Faker("sentence", nb_words=4)
website = factory.Sequence(lambda n: "Website {}".format(n))
......@@ -25,4 +25,13 @@ class ArticleFactory(factory.django.DjangoModelFactory):
published_at = FuzzyDateTime(
datetime.datetime(2014, 1, 1, tzinfo=pytz.UTC))
status = "NEW"
status = FuzzyChoice(STATUS_CHOICES)
@factory.post_generation
def tags(self, create, extracted, **kwargs):
if not create:
return
if extracted:
for tag in extracted:
self.tags.add(tag)
from django.test import TestCase
from django.test import TestCase, Client
from django.contrib.auth.models import User
from rp.models import Article
from rp.factories import ArticleFactory
from rp.apps import RpConfig
from rp.views.articles import ArticleList
class TestVote(TestCase):
class TestArticle(TestCase):
def setUp(self):
self.article = ArticleFactory()
......@@ -15,3 +17,56 @@ class TestVote(TestCase):
def test_article(self):
assert type(self.article) == Article
assert str(self.article) == self.article.title
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
......@@ -8,8 +8,8 @@ from rp.models import Article
class ArticleListTestCase(TestCase):
def setUp(self):
self.article = ArticleFactory()
self.article2 = ArticleFactory()
self.article = ArticleFactory(status='NEW')
self.article2 = ArticleFactory(status='NEW')
self.user = ProfileFactory().user
self.user2 = ProfileFactory().user
......
......@@ -12,7 +12,7 @@ from rp.api.views import ArticleViewSet
class VoteViewTestCase(TestCase):
def setUp(self):
self.article = ArticleFactory()
self.article = ArticleFactory(status="NEW")
self.article_draft = ArticleFactory(status="DRAFT")
self.profile = ProfileFactory()
......
from django.conf.urls import url
from rp.views.articles import ArticleListFlux, ArticleEdit, ArticleDetailView, ArticleList
from rp.views.articles import (ArticleListFlux, ArticleEdit,
ArticleDetailView, ArticleList)
urlpatterns = [
url(
......
......@@ -8,7 +8,8 @@ from django.db.models import Count
from django.db.models import Q
from django import forms
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.auth.mixins import (LoginRequiredMixin,
PermissionRequiredMixin)
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Div, HTML
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment