1 # -*- coding: utf-8 -*-
3 from datetime import datetime
5 from django.test import TestCase
6 from django.utils import simplejson as json
7 from django.conf import settings
9 from api.helpers import timestamp
10 from catalogue.models import Book, Tag
11 from picture.tests.utils import RequestFactory
12 from picture.forms import PictureImportForm
13 from picture.models import Picture, picture_storage
15 from django.core.files.uploadedfile import SimpleUploadedFile
21 class ApiTest(TestCase):
24 self.old_api_wait = settings.API_WAIT
25 settings.API_WAIT = -1
28 settings.API_WAIT = self.old_api_wait
31 class ChangesTest(ApiTest):
34 book = Book(title='A Book')
36 tag = Tag.objects.create(category='author', name='Author')
40 changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
41 self.assertEqual(changes['updated']['books'],
42 [{'id': book.id, 'title': book.title}],
43 'Invalid book format in changes')
44 self.assertEqual(changes['updated']['tags'],
45 [{'id': tag.id, 'name': tag.name}],
46 'Invalid tag format in changes')
49 class BookChangesTests(ApiTest):
52 super(BookChangesTests, self).setUp()
53 self.book = Book.objects.create(slug='slug')
56 # test book in book_changes.added
57 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
58 self.assertEqual(len(changes['updated']),
60 'Added book not in book_changes.updated')
62 def test_deleted_disappears(self):
63 # test deleted book disappears
64 Book.objects.all().delete()
65 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
66 self.assertEqual(len(changes), 1,
67 'Deleted book should disappear.')
70 changed_at = self.book.changed_at
72 # putting on a shelf should not update changed_at
73 shelf = Tag.objects.create(category='set', slug='shelf')
74 self.book.tags = [shelf]
75 self.assertEqual(self.book.changed_at,
78 class TagChangesTests(ApiTest):
81 super(TagChangesTests, self).setUp()
82 self.tag = Tag.objects.create(category='author')
83 self.book = Book.objects.create()
84 self.book.tags = [self.tag]
88 # test tag in tag_changes.added
89 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
90 self.assertEqual(len(changes['updated']),
92 'Added tag not in tag_changes.updated')
94 def test_empty_disappears(self):
97 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
98 self.assertEqual(len(changes), 1,
99 'Empty or deleted tag should disappear.')
103 class BookTests(TestCase):
106 self.tag = Tag.objects.create(category='author', slug='joe')
107 self.book = Book.objects.create(title='A Book', slug='a-book')
108 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
109 self.book_tagged.tags = [self.tag]
110 self.book_tagged.save()
112 def test_book_list(self):
113 books = json.loads(self.client.get('/api/books/').content)
114 self.assertEqual(len(books), 2,
117 def test_tagged_books(self):
118 books = json.loads(self.client.get('/api/authors/joe/books/').content)
120 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
121 'Wrong tagged book list.')
123 def test_detail(self):
124 book = json.loads(self.client.get('/api/books/a-book/').content)
125 self.assertEqual(book['title'], self.book.title,
126 'Wrong book details.')
129 class TagTests(TestCase):
132 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
133 self.book = Book.objects.create(title='A Book', slug='a-book')
134 self.book.tags = [self.tag]
137 def test_tag_list(self):
138 tags = json.loads(self.client.get('/api/authors/').content)
139 self.assertEqual(len(tags), 1,
142 def test_tag_detail(self):
143 tag = json.loads(self.client.get('/api/authors/joe/').content)
144 self.assertEqual(tag['name'], self.tag.name,
145 'Wrong tag details.')
148 class PictureTests(ApiTest):
149 def test_publish(self):
150 slug = "kandinsky-composition-viii"
151 xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
152 img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
154 import_form = PictureImportForm({}, {
155 'picture_xml_file': xml,
156 'picture_image_file': img
159 assert import_form.is_valid()
160 if import_form.is_valid():
163 pic = Picture.objects.get(slug=slug)