b03701f8c7dc80a64ef0d2e44a993b1fe912060f
[redakcja.git] / apps / catalogue / tests / __init__.py
1 from os.path import abspath, dirname, join
2 from nose.tools import *
3 from mock import patch
4 from django.test import TestCase
5 from django.contrib.auth.models import User
6 from catalogue.models import Book, BookPublishRecord
7
8
9 def get_fixture(path):
10     f_path = join(dirname(abspath(__file__)), 'files', path)
11     with open(f_path) as f:
12         return unicode(f.read(), 'utf-8')
13
14
15 class PublishTests(TestCase):
16
17     def setUp(self):
18         self.user = User.objects.create(username='tester')
19         self.text1 = get_fixture('chunk1.xml')
20         self.book = Book.create(self.user, self.text1, slug='test-book')
21
22     @patch('apiclient.api_call')
23     def test_unpublishable(self, api_call):
24         with self.assertRaises(AssertionError):
25             self.book.publish(self.user)
26
27     @patch('apiclient.api_call')
28     def test_publish(self, api_call):
29         self.book[0].head.set_publishable(True)
30         self.book.publish(self.user)
31         api_call.assert_called_with(self.user, 'books/', {"book_xml": self.text1})
32
33     @patch('apiclient.api_call')
34     def test_publish_multiple(self, api_call):
35         self.book[0].head.set_publishable(True)
36         self.book[0].split(slug='part-2')
37         self.book[1].commit(get_fixture('chunk2.xml'))
38         self.book[1].head.set_publishable(True)
39         self.book.publish(self.user)
40         api_call.assert_called_with(self.user, 'books/', {"book_xml": get_fixture('expected.xml')})
41
42
43 class ManipulationTests(TestCase):
44
45     def setUp(self):
46         self.user = User.objects.create(username='tester')
47         self.book1 = Book.create(self.user, 'book 1', slug='book1')
48         self.book2 = Book.create(self.user, 'book 2', slug='book2')
49
50     def test_append(self):
51         self.book1.append(self.book2)
52         self.assertEqual(Book.objects.all().count(), 1)
53         self.assertEqual(len(self.book1), 2)
54
55     def test_append_to_self(self):
56         with self.assertRaises(AssertionError):
57             self.book1.append(Book.objects.get(pk=self.book1.pk))
58         self.assertEqual(Book.objects.all().count(), 2)
59         self.assertEqual(len(self.book1), 1)
60
61     def test_prepend_history(self):
62         self.book1.prepend_history(self.book2)
63         self.assertEqual(Book.objects.all().count(), 1)
64         self.assertEqual(len(self.book1), 1)
65         self.assertEqual(self.book1.materialize(), 'book 1')
66
67     def test_prepend_history_to_self(self):
68         with self.assertRaises(AssertionError):
69             self.book1.prepend_history(self.book1)
70         self.assertEqual(Book.objects.all().count(), 2)
71         self.assertEqual(self.book1.materialize(), 'book 1')
72         self.assertEqual(self.book2.materialize(), 'book 2')