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
65 # putting on a shelf should not update changed_at
66 shelf = Tag.objects.create(category='set', slug='shelf')
67 self.book.tags = [shelf]
68 self.assertEqual(self.book.changed_at,
71 class TagChangesTests(ApiTest):
74 super(TagChangesTests, self).setUp()
75 self.tag = Tag.objects.create(category='author')
76 self.book = Book.objects.create()
77 self.book.tags = [self.tag]
81 # test tag in tag_changes.added
82 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
83 self.assertEqual(len(changes['updated']),
85 'Added tag not in tag_changes.updated')
87 def test_empty_disappears(self):
90 changes = json.loads(self.client.get('/api/tag_changes/0.json').content)
91 self.assertEqual(len(changes), 1,
92 'Empty or deleted tag should disappear.')