catalogue style change
[wolnelektury.git] / apps / api / tests.py
1 # -*- coding: utf-8 -*-
2
3 from datetime import datetime
4
5 from django.test import TestCase
6 from django.utils import simplejson as json
7 from django.conf import settings
8
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
14 import picture.tests
15 from django.core.files.uploadedfile import SimpleUploadedFile
16
17  
18 from os import path
19
20
21 class ApiTest(TestCase):
22
23     def setUp(self):
24         self.old_api_wait = settings.API_WAIT
25         settings.API_WAIT = -1
26
27     def tearDown(self):
28         settings.API_WAIT = self.old_api_wait
29
30
31 class ChangesTest(ApiTest):
32
33     def test_basic(self):
34         book = Book(title='A Book')
35         book.save()
36         tag = Tag.objects.create(category='author', name='Author')
37         book.tags = [tag]
38         book.save()
39
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')
47
48
49 class BookChangesTests(ApiTest):
50
51     def setUp(self):
52         super(BookChangesTests, self).setUp()
53         self.book = Book.objects.create(slug='slug')
54
55     def test_basic(self):
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']),
59                          1,
60                          'Added book not in book_changes.updated')
61
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.')
68
69     def test_shelf(self):
70         changed_at = self.book.changed_at
71
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,
76                          changed_at)
77
78 class TagChangesTests(ApiTest):
79
80     def setUp(self):
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]
85         self.book.save()
86
87     def test_added(self):
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']),
91                          1,
92                          'Added tag not in tag_changes.updated')
93
94     def test_empty_disappears(self):
95         self.book.tags = []
96         self.book.save()
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.')
100
101
102
103 class BookTests(TestCase):
104
105     def setUp(self):
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()
111
112     def test_book_list(self):
113         books = json.loads(self.client.get('/api/books/').content)
114         self.assertEqual(len(books), 2,
115                          'Wrong book list.')
116
117     def test_tagged_books(self):
118         books = json.loads(self.client.get('/api/authors/joe/books/').content)
119
120         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
121                         'Wrong tagged book list.')
122
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.')
127
128
129 class TagTests(TestCase):
130
131     def setUp(self):
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]
135         self.book.save()
136
137     def test_tag_list(self):
138         tags = json.loads(self.client.get('/api/authors/').content)
139         self.assertEqual(len(tags), 1,
140                         'Wrong tag list.')
141
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.')
146
147
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())
153
154         import_form = PictureImportForm({}, {
155             'picture_xml_file': xml,
156             'picture_image_file': img
157             })
158
159         assert import_form.is_valid()
160         if import_form.is_valid():
161             import_form.save()
162
163         pic = Picture.objects.get(slug=slug)
164