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 NO_SEARCH_INDEX = True,
21 CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
22 'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
23 'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
25 class ApiTest(TestCase):
29 class ChangesTest(ApiTest):
32 book = Book(title='A Book')
34 tag = Tag.objects.create(category='author', name='Author')
38 changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
39 self.assertEqual(changes['updated']['books'],
40 [{'id': book.id, 'title': book.title}],
41 'Invalid book format in changes')
42 self.assertEqual(changes['updated']['tags'],
43 [{'id': tag.id, 'name': tag.name}],
44 'Invalid tag format in changes')
47 class BookChangesTests(ApiTest):
50 super(BookChangesTests, self).setUp()
51 self.book = Book.objects.create(slug='slug')
54 # test book in book_changes.added
55 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
56 self.assertEqual(len(changes['updated']),
58 'Added book not in book_changes.updated')
60 def test_deleted_disappears(self):
61 # test deleted book disappears
62 Book.objects.all().delete()
63 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
64 self.assertEqual(len(changes), 1,
65 'Deleted book should disappear.')
68 changed_at = self.book.changed_at
70 # putting on a shelf should not update changed_at
71 shelf = Tag.objects.create(category='set', slug='shelf')
72 self.book.tags = [shelf]
73 self.assertEqual(self.book.changed_at,
76 class TagChangesTests(ApiTest):
79 super(TagChangesTests, self).setUp()
80 self.tag = Tag.objects.create(category='author')
81 self.book = Book.objects.create()
82 self.book.tags = [self.tag]
86 # test tag in tag_changes.added
87 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
88 self.assertEqual(len(changes['updated']),
90 'Added tag not in tag_changes.updated')
92 def test_empty_disappears(self):
95 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
96 self.assertEqual(len(changes), 1,
97 'Empty or deleted tag should disappear.')
101 class BookTests(TestCase):
104 self.tag = Tag.objects.create(category='author', slug='joe')
105 self.book = Book.objects.create(title='A Book', slug='a-book')
106 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
107 self.book_tagged.tags = [self.tag]
108 self.book_tagged.save()
110 def test_book_list(self):
111 books = json.loads(self.client.get('/api/books/').content)
112 self.assertEqual(len(books), 2,
115 def test_tagged_books(self):
116 books = json.loads(self.client.get('/api/authors/joe/books/').content)
118 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
119 'Wrong tagged book list.')
121 def test_detail(self):
122 book = json.loads(self.client.get('/api/books/a-book/').content)
123 self.assertEqual(book['title'], self.book.title,
124 'Wrong book details.')
127 class TagTests(TestCase):
130 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
131 self.book = Book.objects.create(title='A Book', slug='a-book')
132 self.book.tags = [self.tag]
135 def test_tag_list(self):
136 tags = json.loads(self.client.get('/api/authors/').content)
137 self.assertEqual(len(tags), 1,
140 def test_tag_detail(self):
141 tag = json.loads(self.client.get('/api/authors/joe/').content)
142 self.assertEqual(tag['name'], self.tag.name,
143 'Wrong tag details.')
146 class PictureTests(ApiTest):
147 def test_publish(self):
148 slug = "kandinsky-composition-viii"
149 xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
150 img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
152 import_form = PictureImportForm({}, {
153 'picture_xml_file': xml,
154 'picture_image_file': img
157 assert import_form.is_valid()
158 if import_form.is_valid():
161 Picture.objects.get(slug=slug)