1 # -*- coding: utf-8 -*-
3 from datetime import datetime
5 from django.test import TestCase
6 from django.utils import simplejson as json
8 from api.helpers import timestamp
9 from catalogue.models import Book, Tag
12 class ChangesTests(TestCase):
15 book = Book.objects.create(slug='a-book', title='A Book')
16 tag = Tag.objects.create(category='author', slug='author', name='Author')
18 print self.client.get('/api/changes/0.json?book_fields=slug&tag_fields=slug').content
19 changes = json.loads(self.client.get('/api/changes/0.json?book_fields=slug&tag_fields=slug').content)
20 self.assertEqual(changes['added']['books'],
21 [{'id': book.id, 'slug': book.slug}],
22 'Invalid book format in changes')
23 self.assertEqual(changes['added']['tags'],
24 [{'id': tag.id, 'slug': tag.slug}],
25 'Invalid tag format in changes')
28 class BookChangesTests(TestCase):
31 self.book = Book.objects.create()
34 # test book in book_changes.added
35 changes = json.loads(self.client.get('/api/book_changes/0.json').content)
36 self.assertEqual(len(changes['added']),
38 'Added book not in book_changes.added')
40 # test changed book in changed
41 self.book.slug = 'a-book'
43 changes = json.loads(self.client.get('/api/book_changes/%f.json' % timestamp(self.book.created_at)).content)
44 self.assertEqual(changes['added'],
46 'Changed book in book_changes.added instead of book_changes.changed.')
47 self.assertEqual(len(changes['changed']),
49 'Changed book not in book_changes.changed.')
51 # test deleted book in deleted
52 Book.objects.all().delete()
53 changes = json.loads(self.client.get('/api/book_changes/%f.json' % timestamp(self.book.changed_at)).content)
54 self.assertEqual(changes['added'],
56 'Deleted book still in book_changes.added.')
57 self.assertEqual(changes['changed'],
59 'Deleted book still in book_changes.changed.')
60 self.assertEqual(len(changes['deleted']),
62 'Deleted book not in book_changes.deleted.')
65 changed_at = self.book.changed_at
68 # putting on a shelf should not update changed_at
69 shelf = Tag.objects.create(category='set', slug='shelf')
70 self.book.tags = [shelf]
71 self.assertEqual(self.book.changed_at,
74 class TagChangesTests(TestCase):
77 self.tag = Tag.objects.create()
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['added']),
84 'Added tag not in tag_changes.added')
86 # test changed tag in changed
87 self.tag.slug = 'a-tag'
89 changes = json.loads(self.client.get('/api/tag_changes/%f.json' % timestamp(self.tag.created_at)).content)
90 self.assertEqual(changes['added'],
92 'Changed tag in tag_changes.added instead of tag_changes.changed.')
93 self.assertEqual(len(changes['changed']),
95 'Changed tag not in tag_changes.changed.')
97 # test deleted book in deleted
98 Tag.objects.all().delete()
99 changes = json.loads(self.client.get('/api/tag_changes/%f.json' % timestamp(self.tag.changed_at)).content)
100 self.assertEqual(changes['added'],
102 'Deleted tag still in tag_changes.added.')
103 self.assertEqual(changes['changed'],
105 'Deleted tag still in tag_changes.changed.')
106 self.assertEqual(len(changes['deleted']),
108 'Deleted tag not in tag_changes.deleted.')