Tag test fixture
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
from webapp.models import Tag
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
|
||||||
|
def createTagIcon():
|
||||||
|
return tempfile.NamedTemporaryFile(suffix=".jpg").name
|
||||||
|
|
||||||
|
|
||||||
|
def tagBuilder(slug="Tag1", icon=createTagIcon()):
|
||||||
|
return Tag.objects.create(
|
||||||
|
slug=slug,
|
||||||
|
name_fi=slug + " name_fi",
|
||||||
|
name_en=slug + " name_en",
|
||||||
|
icon=icon
|
||||||
|
)
|
||||||
@@ -4,22 +4,17 @@ from django.utils import timezone
|
|||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.test import APITestCase, APIRequestFactory, force_authenticate
|
from rest_framework.test import APITestCase, APIRequestFactory, force_authenticate
|
||||||
|
|
||||||
from webapp.models import Tag, Event
|
from webapp.models import Event
|
||||||
from webapp.serializers import EventSerializer
|
from webapp.serializers import EventSerializer
|
||||||
|
from webapp.tests.tag_fixture import tagBuilder, createTagIcon
|
||||||
import tempfile
|
|
||||||
|
|
||||||
|
|
||||||
class EventTestCase(APITestCase):
|
class EventTestCase(APITestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.icon = tempfile.NamedTemporaryFile(suffix=".jpg").name
|
tag1 = tagBuilder()
|
||||||
Tag.objects.create(slug='testtag1', name='test1', icon=self.icon)
|
tag2 = tagBuilder("testtag2")
|
||||||
tag1 = Tag.objects.get(slug="testtag1")
|
self.testTagId = tag1.id
|
||||||
Tag.objects.create(slug="testtag2", name='test2', icon=self.icon)
|
|
||||||
tag2 = Tag.objects.get(slug="testtag2")
|
|
||||||
self.assertEqual(Tag.objects.count(), 2)
|
|
||||||
self.testTagId = Tag.objects.get(slug="testtag1").id
|
|
||||||
|
|
||||||
# Visible and relevant
|
# Visible and relevant
|
||||||
Event.objects.create(
|
Event.objects.create(
|
||||||
|
|||||||
+27
-27
@@ -3,33 +3,28 @@ from django.contrib.auth.models import User
|
|||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.test import APITestCase, APIRequestFactory, force_authenticate
|
from rest_framework.test import APITestCase, APIRequestFactory, force_authenticate
|
||||||
|
|
||||||
from webapp.models import Tag, Feed
|
from webapp.models import Feed
|
||||||
from webapp.serializers import FeedSerializer
|
from webapp.serializers import FeedSerializer
|
||||||
|
from webapp.tests.tag_fixture import tagBuilder
|
||||||
import tempfile
|
|
||||||
|
|
||||||
|
|
||||||
class FeedTestCase(APITestCase):
|
class FeedTestCase(APITestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.icon = tempfile.NamedTemporaryFile(suffix=".jpg").name
|
tag1 = tagBuilder()
|
||||||
Tag.objects.create(slug='testtag1', name='test1', icon=self.icon)
|
tag2 = tagBuilder("testtag2")
|
||||||
tag1 = Tag.objects.get(slug="testtag1")
|
|
||||||
Tag.objects.create(slug="testtag2", name='test2', icon=self.icon)
|
|
||||||
tag2 = Tag.objects.get(slug="testtag2")
|
|
||||||
self.assertEqual(Tag.objects.count(), 2)
|
|
||||||
|
|
||||||
Feed.objects.create(title="TestFeed", visible=True, description="diidadaapa", content="lorem ipsum")
|
feed = Feed.objects.create(title="TestFeed", visible=True, description="diidadaapa", content="lorem ipsum")
|
||||||
Feed.objects.get(title="TestFeed").tags.add(tag1)
|
feed.tags.add(tag1)
|
||||||
Feed.objects.get(title="TestFeed").tags.add(tag2)
|
feed.tags.add(tag2)
|
||||||
self.assertEqual(Feed.objects.count(), 1)
|
self.assertEqual(Feed.objects.count(), 1)
|
||||||
self.assertEqual(Feed.objects.all()[0].tags.count(), 2)
|
self.assertEqual(Feed.objects.all()[0].tags.count(), 2)
|
||||||
|
|
||||||
username, password = 'test_admin', 'password123'
|
username, password = "test_admin", "password123"
|
||||||
self.authClient = User.objects.create_superuser(username, 'myemail@test.com', password)
|
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||||
|
|
||||||
def test_get_feed(self):
|
def test_get_feed(self):
|
||||||
response = self.client.get('/api/feed/', format='json')
|
response = self.client.get("/api/feed/", format="json")
|
||||||
self.assertTrue(status.is_success(response.status_code))
|
self.assertTrue(status.is_success(response.status_code))
|
||||||
|
|
||||||
feeds = Feed.objects.all()
|
feeds = Feed.objects.all()
|
||||||
@@ -38,26 +33,31 @@ class FeedTestCase(APITestCase):
|
|||||||
context={
|
context={
|
||||||
"request": APIRequestFactory().get(r"http://testserver/api/events/")
|
"request": APIRequestFactory().get(r"http://testserver/api/events/")
|
||||||
})
|
})
|
||||||
self.assertEqual(response.data['results'], serializer.data)
|
self.assertEqual(response.data["results"], serializer.data)
|
||||||
|
|
||||||
def test_post_feed(self):
|
def test_post_feed(self):
|
||||||
Tag.objects.create(slug="test1", name="testsds")
|
tag1_id = tagBuilder("test1").id
|
||||||
Tag.objects.create(slug="test2", name="testsdsd")
|
tag2_id = tagBuilder("test2").id
|
||||||
tag1_id = Tag.objects.get(slug="test1").id
|
|
||||||
tag2_id = Tag.objects.get(slug="test2").id
|
|
||||||
|
|
||||||
data = {'tags': [tag1_id, tag2_id], 'title_fi': 'testtitle', 'title_en': 'testtitle', 'visible': 'True', 'description_fi': 'liirumlaarum', 'description_en': 'liirumlaarum', 'content_fi': 'lorem ipsum', 'content_en': 'lorem ipsum'}
|
data = {
|
||||||
|
"tag_id": [tag1_id, tag2_id],
|
||||||
|
"title_fi": "testtitle",
|
||||||
|
"title_en": "testtitle",
|
||||||
|
"visible": "True",
|
||||||
|
"description_fi": "liirumlaarum", "description_en": "liirumlaarum",
|
||||||
|
"content_fi": "lorem ipsum",
|
||||||
|
"content_en": "lorem ipsum"
|
||||||
|
}
|
||||||
# Try post without authentication
|
# Try post without authentication
|
||||||
response = self.client.post('/api/feed/', data, format='multipart')
|
response = self.client.post("/api/feed/", data, format="multipart")
|
||||||
self.assertTrue(status.is_client_error(response.status_code))
|
self.assertTrue(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
self.assertEqual(Feed.objects.count(), 1)
|
self.assertEqual(Feed.objects.count(), 1)
|
||||||
# Authenticate
|
# Authenticate
|
||||||
self.client.force_authenticate(user=self.authClient)
|
self.client.force_authenticate(user=self.authClient)
|
||||||
response = self.client.post('/api/feed/', data, format='multipart')
|
response = self.client.post("/api/feed/", data, format="multipart")
|
||||||
# Return success and check object was created
|
# Return success and check object was created
|
||||||
self.assertTrue(status.is_success(response.status_code))
|
self.assertTrue(response.status_code, status.HTTP_201_CREATED)
|
||||||
self.assertEqual(Feed.objects.count(), 2)
|
self.assertEqual(Feed.objects.count(), 2)
|
||||||
|
|
||||||
created = Feed.objects.get(title_fi="testtitle")
|
created = Feed.objects.get(title_fi="testtitle")
|
||||||
print(created.tags)
|
self.assertEqual(created.tags.count(), 2)
|
||||||
# self.assertEqual(created.tags.count(), 2)
|
|
||||||
|
|||||||
@@ -8,23 +8,26 @@ from webapp.serializers import TagSerializer
|
|||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
import tempfile
|
|
||||||
|
from webapp.tests.tag_fixture import tagBuilder, createTagIcon
|
||||||
|
|
||||||
|
|
||||||
class TagsTestCase(APITestCase):
|
class TagsTestCase(APITestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.icon = tempfile.NamedTemporaryFile(suffix=".jpg").name
|
self.icon = createTagIcon()
|
||||||
self.tag_id = Tag.objects.create(slug='Party', name_fi='Bileet', name_en="Partytime", icon=self.icon).id
|
tag = tagBuilder("Party", icon=self.icon)
|
||||||
Tag.objects.create(slug='OFF', name_fi='Virallinen', name_en="Official", icon=self.icon)
|
self.tag_id = tag.id
|
||||||
|
|
||||||
|
tagBuilder("Off")
|
||||||
self.assertEqual(Tag.objects.count(), 2)
|
self.assertEqual(Tag.objects.count(), 2)
|
||||||
|
|
||||||
username, password = 'test_admin', 'password123'
|
username, password = 'test_admin', 'password123'
|
||||||
self.authClient = User.objects.create_superuser(username, 'myemail@test.com', password)
|
self.authClient = User.objects.create_superuser(username, 'myemail@test.com', password)
|
||||||
|
|
||||||
def test_get_multiple_tags(self):
|
def test_get_multiple_tags(self):
|
||||||
Tag.objects.create(slug='Freshmen', name_fi='Fuksit', icon=self.icon)
|
tagBuilder("Fuksi")
|
||||||
Tag.objects.create(slug='International', name_fi='Ulkkarit', icon=self.icon)
|
tagBuilder("Inter")
|
||||||
|
|
||||||
expected = TagSerializer(
|
expected = TagSerializer(
|
||||||
Tag.objects.all(), many=True,
|
Tag.objects.all(), many=True,
|
||||||
|
|||||||
Reference in New Issue
Block a user