1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from django.core.files.uploadedfile import SimpleUploadedFile
8 from django.test import TestCase
9 from django.test.utils import override_settings
12 from catalogue.models import Book, Tag
13 from picture.forms import PictureImportForm
14 from picture.models import Picture
21 'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}},
22 SSIFY_CACHE_ALIASES=['default'],
25 class ApiTest(TestCase):
26 def load_json(self, url):
27 content = self.client.get(url).content
29 data = json.loads(content)
31 self.fail('No JSON could be decoded:', content)
35 class BookTests(ApiTest):
38 self.tag = Tag.objects.create(category='author', slug='joe')
39 self.book = Book.objects.create(title='A Book', slug='a-book')
40 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
41 self.book_tagged.tags = [self.tag]
42 self.book_tagged.save()
44 def test_book_list(self):
45 books = self.load_json('/api/books/')
46 self.assertEqual(len(books), 2,
49 def test_tagged_books(self):
50 books = self.load_json('/api/authors/joe/books/')
52 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
53 'Wrong tagged book list.')
55 def test_detail(self):
56 book = self.load_json('/api/books/a-book/')
57 self.assertEqual(book['title'], self.book.title,
58 'Wrong book details.')
61 class TagTests(ApiTest):
64 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
65 self.book = Book.objects.create(title='A Book', slug='a-book')
66 self.book.tags = [self.tag]
69 def test_tag_list(self):
70 tags = self.load_json('/api/authors/')
71 self.assertEqual(len(tags), 1,
74 def test_tag_detail(self):
75 tag = self.load_json('/api/authors/joe/')
76 self.assertEqual(tag['name'], self.tag.name,
80 class PictureTests(ApiTest):
81 def test_publish(self):
82 slug = "kandinsky-composition-viii"
83 xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
84 img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
86 import_form = PictureImportForm({}, {
87 'picture_xml_file': xml,
88 'picture_image_file': img
91 assert import_form.is_valid()
92 if import_form.is_valid():
95 Picture.objects.get(slug=slug)