1 # -*- coding: utf-8 -*-
3 from datetime import datetime
5 from django.test import TestCase
6 from django.utils import simplejson as json
7 from django.conf import settings
9 from api.helpers import timestamp
10 from catalogue.models import Book, Tag
13 class ApiTest(TestCase):
16 self.old_api_wait = settings.API_WAIT
17 settings.API_WAIT = -1
20 settings.API_WAIT = self.old_api_wait
23 class ChangesTest(ApiTest):
26 book = Book(title='A Book')
28 tag = Tag.objects.create(category='author', name='Author')
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')
41 class BookChangesTests(ApiTest):
44 super(BookChangesTests, self).setUp()
45 self.book = Book.objects.create(slug='slug')
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']),
52 'Added book not in book_changes.updated')
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.')
62 changed_at = self.book.changed_at
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,
70 class TagChangesTests(ApiTest):
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]
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']),
84 'Added tag not in tag_changes.updated')
86 def test_empty_disappears(self):
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.')
95 class BookTests(TestCase):
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()
104 def test_book_list(self):
105 books = json.loads(self.client.get('/api/books/').content)
106 self.assertEqual(len(books), 2,
109 def test_tagged_books(self):
110 books = json.loads(self.client.get('/api/authors/joe/books/').content)
112 self.assertEqual([b['title'] for b in books], [self.book_tagged.title],
113 'Wrong tagged book list.')
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.')
121 class TagTests(TestCase):
124 self.tag = Tag.objects.create(category='author', slug='joe', name='Joe')
126 def test_tag_list(self):
127 tags = json.loads(self.client.get('/api/authors/').content)
128 self.assertEqual(len(tags), 1,
131 def test_tag_detail(self):
132 tag = json.loads(self.client.get('/api/authors/joe/').content)
133 self.assertEqual(tag['name'], self.tag.name,
134 'Wrong tag details.')