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
20 CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
21 'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
22 'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
24 class ApiTest(TestCase):
28 class ChangesTest(ApiTest):
31 book = Book(title='A Book')
33 tag = Tag.objects.create(category='author', name='Author')
37 changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
38 self.assertEqual(changes['updated']['books'],
39 [{'id': book.id, 'title': book.title}],
40 'Invalid book format in changes')
41 self.assertEqual(changes['updated']['tags'],
42 [{'id': tag.id, 'name': tag.name}],
43 'Invalid tag format in changes')
46 class BookChangesTests(ApiTest):
49 super(BookChangesTests, self).setUp()
50 self.book = Book.objects.create(slug='slug')
53 # test book in book_changes.added
54 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
55 self.assertEqual(len(changes['updated']),
57 'Added book not in book_changes.updated')
59 def test_deleted_disappears(self):
60 # test deleted book disappears
61 Book.objects.all().delete()
62 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
63 self.assertEqual(len(changes), 1,
64 'Deleted book should disappear.')
67 changed_at = self.book.changed_at
69 # putting on a shelf should not update changed_at
70 shelf = Tag.objects.create(category='set', slug='shelf')
71 self.book.tags = [shelf]
72 self.assertEqual(self.book.changed_at,
75 class TagChangesTests(ApiTest):
78 super(TagChangesTests, self).setUp()
79 self.tag = Tag.objects.create(category='author')
80 self.book = Book.objects.create()
81 self.book.tags = [self.tag]
85 # test tag in tag_changes.added
86 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
87 self.assertEqual(len(changes['updated']),
89 'Added tag not in tag_changes.updated')
91 def test_empty_disappears(self):
94 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
95 self.assertEqual(len(changes), 1,
96 'Empty or deleted tag should disappear.')
100 class BookTests(TestCase):
103 self.tag = Tag.objects.create(category='author', slug='joe')
104 self.book = Book.objects.create(title='A Book', slug='a-book')
105 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
106 self.book_tagged.tags = [self.tag]
107 self.book_tagged.save()
109 def test_book_list(self):
110 books = json.loads(self.client.get('/api/books/').content)
111 self.assertEqual(len(books), 2,
114 def test_tagged_books(self):
115 books = json.loads(self.client.get('/api/authors/joe/books/').content)
117 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
118 'Wrong tagged book list.')
120 def test_detail(self):
121 book = json.loads(self.client.get('/api/books/a-book/').content)
122 self.assertEqual(book['title'], self.book.title,
123 'Wrong book details.')
126 class TagTests(TestCase):
129 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
130 self.book = Book.objects.create(title='A Book', slug='a-book')
131 self.book.tags = [self.tag]
134 def test_tag_list(self):
135 tags = json.loads(self.client.get('/api/authors/').content)
136 self.assertEqual(len(tags), 1,
139 def test_tag_detail(self):
140 tag = json.loads(self.client.get('/api/authors/joe/').content)
141 self.assertEqual(tag['name'], self.tag.name,
142 'Wrong tag details.')
145 class PictureTests(ApiTest):
146 def test_publish(self):
147 slug = "kandinsky-composition-viii"
148 xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
149 img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
151 import_form = PictureImportForm({}, {
152 'picture_xml_file': xml,
153 'picture_image_file': img
156 assert import_form.is_valid()
157 if import_form.is_valid():
160 pic = Picture.objects.get(slug=slug)