contrib translations for es fr it ru uk
[wolnelektury.git] / apps / api / tests.py
1 # -*- coding: utf-8 -*-
2
3 from os import path
4
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
9
10 from catalogue.models import Book, Tag
11 from picture.forms import PictureImportForm
12 from picture.models import Picture
13 import picture.tests
14
15
16 @override_settings(
17     API_WAIT=-1, 
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'}}
21 )
22 class ApiTest(TestCase):
23     pass
24
25
26 class ChangesTest(ApiTest):
27
28     def test_basic(self):
29         book = Book(title='A Book')
30         book.save()
31         tag = Tag.objects.create(category='author', name='Author')
32         book.tags = [tag]
33         book.save()
34
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')
42
43
44 class BookChangesTests(ApiTest):
45
46     def setUp(self):
47         super(BookChangesTests, self).setUp()
48         self.book = Book.objects.create(slug='slug')
49
50     def test_basic(self):
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']),
54                          1,
55                          'Added book not in book_changes.updated')
56
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.')
63
64     def test_shelf(self):
65         changed_at = self.book.changed_at
66
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,
71                          changed_at)
72
73 class TagChangesTests(ApiTest):
74
75     def setUp(self):
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]
80         self.book.save()
81
82     def test_added(self):
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']),
86                          1,
87                          'Added tag not in tag_changes.updated')
88
89     def test_empty_disappears(self):
90         self.book.tags = []
91         self.book.save()
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.')
95
96
97
98 class BookTests(TestCase):
99
100     def setUp(self):
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()
106
107     def test_book_list(self):
108         books = json.loads(self.client.get('/api/books/').content)
109         self.assertEqual(len(books), 2,
110                          'Wrong book list.')
111
112     def test_tagged_books(self):
113         books = json.loads(self.client.get('/api/authors/joe/books/').content)
114
115         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
116                         'Wrong tagged book list.')
117
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.')
122
123
124 class TagTests(TestCase):
125
126     def setUp(self):
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]
130         self.book.save()
131
132     def test_tag_list(self):
133         tags = json.loads(self.client.get('/api/authors/').content)
134         self.assertEqual(len(tags), 1,
135                         'Wrong tag list.')
136
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.')
141
142
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())
148
149         import_form = PictureImportForm({}, {
150             'picture_xml_file': xml,
151             'picture_image_file': img
152             })
153
154         assert import_form.is_valid()
155         if import_form.is_valid():
156             import_form.save()
157
158         pic = Picture.objects.get(slug=slug)
159