new librarian sub-module: custom pdfs
[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
12
13 class ApiTest(TestCase):
14
15     def setUp(self):
16         self.old_api_wait = settings.API_WAIT
17         settings.API_WAIT = -1
18
19     def tearDown(self):
20         settings.API_WAIT = self.old_api_wait
21
22
23 class ChangesTest(ApiTest):
24
25     def test_basic(self):
26         book = Book(title='A Book')
27         book.save()
28         tag = Tag.objects.create(category='author', name='Author')
29         book.tags = [tag]
30         book.save()
31
32         changes = json.loads(self.client.get('/api/changes/0.json?book_fields=title&tag_fields=name').content)
33         self.assertEqual(changes['updated']['books'], 
34                          [{'id': book.id, 'title': book.title}],
35                          'Invalid book format in changes')
36         self.assertEqual(changes['updated']['tags'], 
37                          [{'id': tag.id, 'name': tag.name}],
38                          'Invalid tag format in changes')
39
40
41 class BookChangesTests(ApiTest):
42
43     def setUp(self):
44         super(BookChangesTests, self).setUp()
45         self.book = Book.objects.create(slug='slug')
46
47     def test_basic(self):
48         # test book in book_changes.added
49         changes = json.loads(self.client.get('/api/book_changes/0.json').content)
50         self.assertEqual(len(changes['updated']),
51                          1,
52                          'Added book not in book_changes.updated')
53
54     def test_deleted_disappears(self):
55         # test deleted book disappears
56         Book.objects.all().delete()
57         changes = json.loads(self.client.get('/api/book_changes/0.json').content)
58         self.assertEqual(len(changes), 1,
59                          'Deleted book should disappear.')
60
61     def test_shelf(self):
62         changed_at = self.book.changed_at
63
64         # putting on a shelf should not update changed_at
65         shelf = Tag.objects.create(category='set', slug='shelf')
66         self.book.tags = [shelf]
67         self.assertEqual(self.book.changed_at,
68                          changed_at)
69
70 class TagChangesTests(ApiTest):
71
72     def setUp(self):
73         super(TagChangesTests, self).setUp()
74         self.tag = Tag.objects.create(category='author')
75         self.book = Book.objects.create()
76         self.book.tags = [self.tag]
77         self.book.save()
78
79     def test_added(self):
80         # test tag in tag_changes.added
81         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
82         self.assertEqual(len(changes['updated']),
83                          1,
84                          'Added tag not in tag_changes.updated')
85
86     def test_empty_disappears(self):
87         self.book.tags = []
88         self.book.save()
89         changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
90         self.assertEqual(len(changes), 1,
91                          'Empty or deleted tag should disappear.')
92
93
94
95 class BookTests(TestCase):
96
97     def setUp(self):
98         self.tag = Tag.objects.create(category='author', slug='joe')
99         self.book = Book.objects.create(title='A Book', slug='a-book')
100         self.book_tagged = Book.objects.create(title='Tagged Book', slug='tagged-book')
101         self.book_tagged.tags = [self.tag]
102         self.book_tagged.save()
103
104     def test_book_list(self):
105         books = json.loads(self.client.get('/api/books/').content)
106         self.assertEqual(len(books), 2,
107                          'Wrong book list.')
108
109     def test_tagged_books(self):
110         books = json.loads(self.client.get('/api/authors/joe/books/').content)
111
112         self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
113                         'Wrong tagged book list.')
114
115     def test_detail(self):
116         book = json.loads(self.client.get('/api/books/a-book/').content)
117         self.assertEqual(book['title'], self.book.title,
118                         'Wrong book details.')
119
120
121 class TagTests(TestCase):
122
123     def setUp(self):
124         self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
125         self.book = Book.objects.create(title='A Book', slug='a-book')
126         self.book.tags = [self.tag]
127         self.book.save()
128
129     def test_tag_list(self):
130         tags = json.loads(self.client.get('/api/authors/').content)
131         self.assertEqual(len(tags), 1,
132                         'Wrong tag list.')
133
134     def test_tag_detail(self):
135         tag = json.loads(self.client.get('/api/authors/joe/').content)
136         self.assertEqual(tag['name'], self.tag.name,
137                         'Wrong tag details.')