8b21f6d94492064336b9031f55616850e2804015
[redakcja.git] / apps / catalogue / tests.py
1 from nose.tools import *
2 from mock import patch
3 from django.test import TestCase
4 from django.contrib.auth.models import User
5 from catalogue.models import Book, BookPublishRecord
6
7 class PublishTests(TestCase):
8
9     def setUp(self):
10         self.user = User.objects.create(username='tester')
11         self.book = Book.create(self.user, 'publish me')
12
13     @patch('apiclient.api_call')
14     def test_unpublishable(self, api_call):
15         with self.assertRaises(Book.NoTextError):
16             self.book.publish(self.user)
17
18     @patch('apiclient.api_call')
19     def test_publish(self, api_call):
20         self.book[0].head.set_publishable(True)
21         self.book.publish(self.user)
22         api_call.assert_called_with(self.user, 'books', {"book_xml": 'publish me'})
23
24     @patch('apiclient.api_call')
25     def test_publish_multiple(self, api_call):
26         self.book[0].head.set_publishable(True)
27         self.book[0].split(slug='part-2')
28         self.book[1].commit('take me \n<!-- TRIM_BEGIN -->\n too')
29         self.book[1].head.set_publishable(True)
30         self.book.publish(self.user)
31         api_call.assert_called_with(self.user, 'books', {"book_xml": 'publish me\n too'})
32
33
34 class ManipulationTests(TestCase):
35
36     def setUp(self):
37         self.user = User.objects.create(username='tester')
38         self.book1 = Book.create(self.user, 'book 1')
39         self.book2 = Book.create(self.user, 'book 2')
40
41     def test_append(self):
42         self.book1.append(self.book2)
43         self.assertEqual(Book.objects.all().count(), 1)
44         self.assertEqual(len(self.book1), 2)
45
46     def test_append_to_self(self):
47         with self.assertRaises(AssertionError):
48             self.book1.append(Book.objects.get(pk=self.book1.pk))
49         self.assertEqual(Book.objects.all().count(), 2)
50         self.assertEqual(len(self.book1), 1)