e3dd71ad08f14248655d5e361206c46a00ddb7d6
[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
8 from api.helpers import timestamp
9 from catalogue.models import Book, Tag
10
11
12 class ChangesTests(TestCase):
13
14     def test_basic(self):
15         book = Book.objects.create(slug='a-book', title='A Book')
16         tag = Tag.objects.create(category='author', slug='author', name='Author')
17
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')
26
27
28 class BookChangesTests(TestCase):
29
30     def setUp(self):
31         self.book = Book.objects.create()
32
33     def test_basic(self):
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']),
37                          1,
38                          'Added book not in book_changes.added')
39
40         # test changed book in changed
41         self.book.slug = 'a-book'
42         self.book.save()
43         changes = json.loads(self.client.get('/api/book_changes/%f.json' % timestamp(self.book.created_at)).content)
44         self.assertEqual(changes['added'],
45                          [],
46                          'Changed book in book_changes.added instead of book_changes.changed.')
47         self.assertEqual(len(changes['changed']),
48                          1,
49                          'Changed book not in book_changes.changed.')
50
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'],
55                          [],
56                          'Deleted book still in book_changes.added.')
57         self.assertEqual(changes['changed'],
58                          [],
59                          'Deleted book still in book_changes.changed.')
60         self.assertEqual(len(changes['deleted']),
61                          1,
62                          'Deleted book not in book_changes.deleted.')
63
64     def test_shelf(self):
65         changed_at = self.book.changed_at
66         print changed_at
67
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,
72                          changed_at)
73
74 class TagChangesTests(TestCase):
75
76     def setUp(self):
77         self.tag = Tag.objects.create()
78
79     def test_basic(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['added']),
83                          1,
84                          'Added tag not in tag_changes.added')
85
86         # test changed tag in changed
87         self.tag.slug = 'a-tag'
88         self.tag.save()
89         changes = json.loads(self.client.get('/api/tag_changes/%f.json' % timestamp(self.tag.created_at)).content)
90         self.assertEqual(changes['added'],
91                          [],
92                          'Changed tag in tag_changes.added instead of tag_changes.changed.')
93         self.assertEqual(len(changes['changed']),
94                          1,
95                          'Changed tag not in tag_changes.changed.')
96
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'],
101                          [],
102                          'Deleted tag still in tag_changes.added.')
103         self.assertEqual(changes['changed'],
104                          [],
105                          'Deleted tag still in tag_changes.changed.')
106         self.assertEqual(len(changes['deleted']),
107                          1,
108                          'Deleted tag not in tag_changes.deleted.')