Commit 87160362 authored by okhin's avatar okhin 🚴

Finsihing some API tests

parent 4e7004d3
Pipeline #2568 failed with stages
in 48 seconds
from django.shortcuts import render
# Create your views here.
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
......@@ -22,8 +23,11 @@ class TestArticle(TestCase):
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")
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()
......@@ -70,3 +74,51 @@ class TestArticleViews(TestCase):
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
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