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
15 from django.core.files.uploadedfile import SimpleUploadedFile
20 class ApiTest(TestCase):
23 self.old_api_wait = settings.API_WAIT
24 settings.API_WAIT = -1
27 settings.API_WAIT = self.old_api_wait
30 class ChangesTest(ApiTest):
33 book = Book(title='A Book')
35 tag = Tag.objects.create(category='author', name='Author')
39 changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
40 self.assertEqual(changes['updated']['books'],
41 [{'id': book.id, 'title': book.title}],
42 'Invalid book format in changes')
43 self.assertEqual(changes['updated']['tags'],
44 [{'id': tag.id, 'name': tag.name}],
45 'Invalid tag format in changes')
48 class BookChangesTests(ApiTest):
51 super(BookChangesTests, self).setUp()
52 self.book = Book.objects.create(slug='slug')
55 # test book in book_changes.added
56 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
57 self.assertEqual(len(changes['updated']),
59 'Added book not in book_changes.updated')
61 def test_deleted_disappears(self):
62 # test deleted book disappears
63 Book.objects.all().delete()
64 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
65 self.assertEqual(len(changes), 1,
66 'Deleted book should disappear.')
69 changed_at = self.book.changed_at
71 # putting on a shelf should not update changed_at
72 shelf = Tag.objects.create(category='set', slug='shelf')
73 self.book.tags = [shelf]
74 self.assertEqual(self.book.changed_at,
77 class TagChangesTests(ApiTest):
80 super(TagChangesTests, self).setUp()
81 self.tag = Tag.objects.create(category='author')
82 self.book = Book.objects.create()
83 self.book.tags = [self.tag]
87 # test tag in tag_changes.added
88 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
89 self.assertEqual(len(changes['updated']),
91 'Added tag not in tag_changes.updated')
93 def test_empty_disappears(self):
96 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
97 self.assertEqual(len(changes), 1,
98 'Empty or deleted tag should disappear.')
102 class BookTests(TestCase):
105 self.tag = Tag.objects.create(category='author', slug='joe')
106 self.book = Book.objects.create(title='A Book', slug='a-book')
107 self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
108 self.book_tagged.tags = [self.tag]
109 self.book_tagged.save()
111 def test_book_list(self):
112 books = json.loads(self.client.get('/api/books/').content)
113 self.assertEqual(len(books), 2,
116 def test_tagged_books(self):
117 books = json.loads(self.client.get('/api/authors/joe/books/').content)
119 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
120 'Wrong tagged book list.')
122 def test_detail(self):
123 book = json.loads(self.client.get('/api/books/a-book/').content)
124 self.assertEqual(book['title'], self.book.title,
125 'Wrong book details.')
128 class TagTests(TestCase):
131 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
132 self.book = Book.objects.create(title='A Book', slug='a-book')
133 self.book.tags = [self.tag]
136 def test_tag_list(self):
137 tags = json.loads(self.client.get('/api/authors/').content)
138 self.assertEqual(len(tags), 1,
141 def test_tag_detail(self):
142 tag = json.loads(self.client.get('/api/authors/joe/').content)
143 self.assertEqual(tag['name'], self.tag.name,
144 'Wrong tag details.')
147 class PictureTests(ApiTest):
148 def test_publish(self):
149 slug = "kandinsky-composition-viii"
150 xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
151 img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
153 import_form = PictureImportForm({}, {
154 'picture_xml_file': xml,
155 'picture_image_file': img
158 assert import_form.is_valid()
159 if import_form.is_valid():
162 pic = Picture.objects.get(slug=slug)