sha1 in test xml file
[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
14 import picture.tests
15 from django.core.files.uploadedfile import SimpleUploadedFile
16  
17 from os import path
18
19
20 class ApiTest(TestCase):
21
22     def setUp(self):
23         self.old_api_wait = settings.API_WAIT
24         settings.API_WAIT = -1
25
26     def tearDown(self):
27         settings.API_WAIT = self.old_api_wait
28
29
30 class ChangesTest(ApiTest):
31
32     def test_basic(self):
33         book = Book(title='A Book')
34         book.save()
35         tag = Tag.objects.create(category='author', name='Author')
36         book.tags = [tag]
37         book.save()
38
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')
46
47
48 class BookChangesTests(ApiTest):
49
50     def setUp(self):
51         super(BookChangesTests, self).setUp()
52         self.book = Book.objects.create(slug='slug')
53
54     def test_basic(self):
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']),
58                          1,
59                          'Added book not in book_changes.updated')
60
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.')
67
68     def test_shelf(self):
69         changed_at = self.book.changed_at
70
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,
75                          changed_at)
76
77 class TagChangesTests(ApiTest):
78
79     def setUp(self):
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]
84         self.book.save()
85
86     def test_added(self):
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']),
90                          1,
91                          'Added tag not in tag_changes.updated')
92
93     def test_empty_disappears(self):
94         self.book.tags = []
95         self.book.save()
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.')
99
100
101
102 class BookTests(TestCase):
103
104     def setUp(self):
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()
110
111     def test_book_list(self):
112         books = json.loads(self.client.get('/api/books/').content)
113         self.assertEqual(len(books), 2,
114                          'Wrong book list.')
115
116     def test_tagged_books(self):
117         books = json.loads(self.client.get('/api/authors/joe/books/').content)
118
119         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
120                         'Wrong tagged book list.')
121
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.')
126
127
128 class TagTests(TestCase):
129
130     def setUp(self):
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]
134         self.book.save()
135
136     def test_tag_list(self):
137         tags = json.loads(self.client.get('/api/authors/').content)
138         self.assertEqual(len(tags), 1,
139                         'Wrong tag list.')
140
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.')
145
146
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())
152
153         import_form = PictureImportForm({}, {
154             'picture_xml_file': xml,
155             'picture_image_file': img
156             })
157
158         assert import_form.is_valid()
159         if import_form.is_valid():
160             import_form.save()
161
162         pic = Picture.objects.get(slug=slug)