Commit f3338348 authored by okhin's avatar okhin 🚴

Merge branch '41-test-everything' into rp2

parents d65d8df5 87160362
Pipeline #2569 failed with stages
in 47 seconds
from django.shortcuts import render
# Create your views here.
......@@ -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 rest_framework.test import APIClient
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 +18,107 @@ 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
class TestArticleApi(TestCase):
def setUp(self):
self.client = APIClient()
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_api_get(self):
r = self.client.get('/api/articles/1/', {})
assert r.status_code == 200
assert r.data['id'] == 1
def test_api_list(self):
r = self.client.get('/api/articles/', {})
assert r.status_code == 200
assert r.data['count'] == 2 * ArticleList.paginate_by
def test_api_filter_tag(self):
tag = self.articles[0].tags.all()[1]
# All articles have Tag2 as a tag
r = self.client.get('/api/articles-by-tag/{}/'.format(tag.name))
assert r.status_code == 200
assert r.data['count'] == 2 * ArticleList.paginate_by
# Case sensitivity checking - tags are sensitive to case
r = self.client.get('/api/articles-by-tag/{}/'.format(tag.name.lower()))
assert r.status_code == 200
assert r.data['count'] == 0
# No article should have the ZogZog tag
r = self.client.get('/api/articles-by-tag/zogzog/')
assert r.status_code == 200
assert r.data['count'] == 0
def test_api_filter_search(self):
text = '+'.join(self.articles[0].extracts.split(' ')[:10])
r = self.client.get('/api/articles-search/{}/'.format(text))
print(r.data)
assert r.status_code == 200
assert r.data['count'] == 1
assert r.data['results'][0]['id'] == self.articles[0].id
......@@ -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
......
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from userprofile.apps import UserprofileConfig
from userprofile.models import Profile
from userprofile.factories import ProfileFactory
def test_init():
assert UserprofileConfig.name == "userprofile"
def test_profile():
profile = ProfileFactory()
assert type(profile.user) == User
assert type(profile) == Profile
assert profile.slug == slugify(profile.user.username)
assert str(profile) == profile.user.username
profile.delete()
from django.contrib.auth.models import User, Permission
from django.template.defaultfilters import slugify
from django.test import Client, TestCase
from userprofile.apps import UserprofileConfig
from userprofile.models import Profile
from userprofile.factories import ProfileFactory
class ProfileTestCase(TestCase):
def setUp(self):
self.client = Client()
p = Permission.objects.get(codename='can_edit_users')
user = User.objects.create_user('test', 'test@example.org', 'test')
user.user_permissions.add(p)
self.user = user
def test_init(self):
assert UserprofileConfig.name == "userprofile"
def test_profile(self):
profile = ProfileFactory()
assert type(profile.user) == User
assert type(profile) == Profile
assert profile.slug == slugify(profile.user.username)
assert str(profile) == profile.user.username
profile.delete()
def test_list(self):
self.client.force_login(self.user)
r = self.client.get('/users/list/')
assert r.status_code == 200
assert self.user.email in str(r.content)
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