fix
[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         print changed_at
64
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,
69                          changed_at)
70
71 class TagChangesTests(ApiTest):
72
73     def setUp(self):
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]
78         self.book.save()
79
80     def test_added(self):
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']),
84                          1,
85                          'Added tag not in tag_changes.updated')
86
87     def test_empty_disappears(self):
88         self.book.tags = []
89         self.book.save()
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.')