1 # -*- coding: utf-8 -*-
5 from django.core.files.uploadedfile import SimpleUploadedFile
6 from django.test import TestCase
7 from django.test.utils import override_settings
8 from django.utils import simplejson as json
10 from catalogue.models import Book, Tag
11 from picture.forms import PictureImportForm
12 from picture.models import Picture
18 CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
19 'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
20 'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
22 class ApiTest(TestCase):
26 class ChangesTest(ApiTest):
29 book = Book(title='A Book')
31 tag = Tag.objects.create(category='author', name='Author')
35 changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
36 self.assertEqual(changes['updated']['books'],
37 [{'id': book.id, 'title': book.title}],
38 'Invalid book format in changes')
39 self.assertEqual(changes['updated']['tags'],
40 [{'id': tag.id, 'name': tag.name}],
41 'Invalid tag format in changes')
44 class BookChangesTests(ApiTest):
47 super(BookChangesTests, self).setUp()
48 self.book = Book.objects.create(slug='slug')
51 # test book in book_changes.added
52 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
53 self.assertEqual(len(changes['updated']),
55 'Added book not in book_changes.updated')
57 def test_deleted_disappears(self):
58 # test deleted book disappears
59 Book.objects.all().delete()
60 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
61 self.assertEqual(len(changes), 1,
62 'Deleted book should disappear.')
65 changed_at = self.book.changed_at
67 # putting on a shelf should not update changed_at
68 shelf = Tag.objects.create(category='set', slug='shelf')
69 self.book.tags = [shelf]
70 self.assertEqual(self.book.changed_at,
73 class TagChangesTests(ApiTest):
76 super(TagChangesTests, self).setUp()
77 self.tag = Tag.objects.create(category='author')
78 self.book = Book.objects.create()
79 self.book.tags = [self.tag]
83 # test tag in tag_changes.added
84 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
85 self.assertEqual(len(changes['updated']),
87 'Added tag not in tag_changes.updated')
89 def test_empty_disappears(self):
92 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
93 self.assertEqual(len(changes), 1,
94 'Empty or deleted tag should disappear.')
98 class BookTests(TestCase):
101 self.tag = Tag.objects.create(category='author', slug='joe')
102 self.book = Book.objects.create(title='A Book', slug='a-book')
103 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
104 self.book_tagged.tags = [self.tag]
105 self.book_tagged.save()
107 def test_book_list(self):
108 books = json.loads(self.client.get('/api/books/').content)
109 self.assertEqual(len(books), 2,
112 def test_tagged_books(self):
113 books = json.loads(self.client.get('/api/authors/joe/books/').content)
115 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
116 'Wrong tagged book list.')
118 def test_detail(self):
119 book = json.loads(self.client.get('/api/books/a-book/').content)
120 self.assertEqual(book['title'], self.book.title,
121 'Wrong book details.')
124 class TagTests(TestCase):
127 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
128 self.book = Book.objects.create(title='A Book', slug='a-book')
129 self.book.tags = [self.tag]
132 def test_tag_list(self):
133 tags = json.loads(self.client.get('/api/authors/').content)
134 self.assertEqual(len(tags), 1,
137 def test_tag_detail(self):
138 tag = json.loads(self.client.get('/api/authors/joe/').content)
139 self.assertEqual(tag['name'], self.tag.name,
140 'Wrong tag details.')
143 class PictureTests(ApiTest):
144 def test_publish(self):
145 slug = "kandinsky-composition-viii"
146 xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
147 img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
149 import_form = PictureImportForm({}, {
150 'picture_xml_file': xml,
151 'picture_image_file': img
154 assert import_form.is_valid()
155 if import_form.is_valid():
158 pic = Picture.objects.get(slug=slug)