Django 1.6 and some cleaning.
[wolnelektury.git] / apps / api / tests.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from os import path
6
7 from django.core.files.uploadedfile import SimpleUploadedFile
8 from django.test import TestCase
9 from django.test.utils import override_settings
10 import json
11
12 from catalogue.models import Book, Tag
13 from picture.forms import PictureImportForm
14 from picture.models import Picture
15 import picture.tests
16
17
18 @override_settings(
19     API_WAIT=-1, 
20     CACHES = {'api': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
21               'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
22               'permanent': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'}}
23 )
24 class ApiTest(TestCase):
25     pass
26
27
28 class ChangesTest(ApiTest):
29
30     def test_basic(self):
31         book = Book(title='A Book')
32         book.save()
33         tag = Tag.objects.create(category='author', name='Author')
34         book.tags = [tag]
35         book.save()
36
37         changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
38         self.assertEqual(changes['updated']['books'], 
39                          [{'id': book.id, 'title': book.title}],
40                          'Invalid book format in changes')
41         self.assertEqual(changes['updated']['tags'], 
42                          [{'id': tag.id, 'name': tag.name}],
43                          'Invalid tag format in changes')
44
45
46 class BookChangesTests(ApiTest):
47
48     def setUp(self):
49         super(BookChangesTests, self).setUp()
50         self.book = Book.objects.create(slug='slug')
51
52     def test_basic(self):
53         # test book in book_changes.added
54         changes = json.loads(self.client.get('/api/book_changes/0.json').content)
55         self.assertEqual(len(changes['updated']),
56                          1,
57                          'Added book not in book_changes.updated')
58
59     def test_deleted_disappears(self):
60         # test deleted book disappears
61         Book.objects.all().delete()
62         changes = json.loads(self.client.get('/api/book_changes/0.json').content)
63         self.assertEqual(len(changes), 1,
64                          'Deleted book should disappear.')
65
66     def test_shelf(self):
67         changed_at = self.book.changed_at
68
69         # putting on a shelf should not update changed_at
70         shelf = Tag.objects.create(category='set', slug='shelf')
71         self.book.tags = [shelf]
72         self.assertEqual(self.book.changed_at,
73                          changed_at)
74
75 class TagChangesTests(ApiTest):
76
77     def setUp(self):
78         super(TagChangesTests, self).setUp()
79         self.tag = Tag.objects.create(category='author')
80         self.book = Book.objects.create()
81         self.book.tags = [self.tag]
82         self.book.save()
83
84     def test_added(self):
85         # test tag in tag_changes.added
86         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
87         self.assertEqual(len(changes['updated']),
88                          1,
89                          'Added tag not in tag_changes.updated')
90
91     def test_empty_disappears(self):
92         self.book.tags = []
93         self.book.save()
94         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
95         self.assertEqual(len(changes), 1,
96                          'Empty or deleted tag should disappear.')
97
98
99
100 class BookTests(TestCase):
101
102     def setUp(self):
103         self.tag = Tag.objects.create(category='author', slug='joe')
104         self.book = Book.objects.create(title='A Book', slug='a-book')
105         self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
106         self.book_tagged.tags = [self.tag]
107         self.book_tagged.save()
108
109     def test_book_list(self):
110         books = json.loads(self.client.get('/api/books/').content)
111         self.assertEqual(len(books), 2,
112                          'Wrong book list.')
113
114     def test_tagged_books(self):
115         books = json.loads(self.client.get('/api/authors/joe/books/').content)
116
117         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
118                         'Wrong tagged book list.')
119
120     def test_detail(self):
121         book = json.loads(self.client.get('/api/books/a-book/').content)
122         self.assertEqual(book['title'], self.book.title,
123                         'Wrong book details.')
124
125
126 class TagTests(TestCase):
127
128     def setUp(self):
129         self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
130         self.book = Book.objects.create(title='A Book', slug='a-book')
131         self.book.tags = [self.tag]
132         self.book.save()
133
134     def test_tag_list(self):
135         tags = json.loads(self.client.get('/api/authors/').content)
136         self.assertEqual(len(tags), 1,
137                         'Wrong tag list.')
138
139     def test_tag_detail(self):
140         tag = json.loads(self.client.get('/api/authors/joe/').content)
141         self.assertEqual(tag['name'], self.tag.name,
142                         'Wrong tag details.')
143
144
145 class PictureTests(ApiTest):
146     def test_publish(self):
147         slug = "kandinsky-composition-viii"
148         xml = SimpleUploadedFile('composition8.xml', open(path.join(picture.tests.__path__[0], "files", slug + ".xml")).read())
149         img = SimpleUploadedFile('kompozycja-8.png', open(path.join(picture.tests.__path__[0], "files", slug + ".png")).read())
150
151         import_form = PictureImportForm({}, {
152             'picture_xml_file': xml,
153             'picture_image_file': img
154             })
155
156         assert import_form.is_valid()
157         if import_form.is_valid():
158             import_form.save()
159
160         pic = Picture.objects.get(slug=slug)
161