1 from os.path import abspath, dirname, join, basename, exists
2 from os import makedirs, listdir
3 from nose.tools import *
5 from django.test import TestCase
6 from django.contrib.auth.models import User
7 from catalogue.models import Book, BookPublishRecord
8 from tempfile import mkdtemp
9 from django.conf import settings
11 def get_fixture(path):
12 f_path = join(dirname(abspath(__file__)), 'files', path)
13 with open(f_path) as f:
14 return unicode(f.read(), 'utf-8')
17 class PublishTests(TestCase):
20 self.user = User.objects.create(username='tester')
21 self.text1 = get_fixture('chunk1.xml')
22 self.book = Book.create(self.user, self.text1, slug='test-book')
24 @patch('apiclient.api_call')
25 def test_unpublishable(self, api_call):
26 with self.assertRaises(AssertionError):
27 self.book.publish(self.user)
29 @patch('apiclient.api_call')
30 def test_publish(self, api_call):
31 self.book[0].head.set_publishable(True)
32 self.book.publish(self.user)
33 api_call.assert_called_with(self.user, 'books/', {"book_xml": self.text1})
35 @patch('apiclient.api_call')
36 def test_publish_multiple(self, api_call):
37 self.book[0].head.set_publishable(True)
38 self.book[0].split(slug='part-2')
39 self.book[1].commit(get_fixture('chunk2.xml'))
40 self.book[1].head.set_publishable(True)
41 self.book.publish(self.user)
42 api_call.assert_called_with(self.user, 'books/', {"book_xml": get_fixture('expected.xml')})
45 class ManipulationTests(TestCase):
48 self.user = User.objects.create(username='tester')
49 self.book1 = Book.create(self.user, 'book 1', slug='book1')
50 self.book2 = Book.create(self.user, 'book 2', slug='book2')
52 def test_append(self):
53 self.book1.append(self.book2)
54 self.assertEqual(Book.objects.all().count(), 1)
55 self.assertEqual(len(self.book1), 2)
57 def test_append_to_self(self):
58 with self.assertRaises(AssertionError):
59 self.book1.append(Book.objects.get(pk=self.book1.pk))
60 self.assertEqual(Book.objects.all().count(), 2)
61 self.assertEqual(len(self.book1), 1)
63 def test_prepend_history(self):
64 self.book1.prepend_history(self.book2)
65 self.assertEqual(Book.objects.all().count(), 1)
66 self.assertEqual(len(self.book1), 1)
67 self.assertEqual(self.book1.materialize(), 'book 1')
69 def test_prepend_history_to_self(self):
70 with self.assertRaises(AssertionError):
71 self.book1.prepend_history(self.book1)
72 self.assertEqual(Book.objects.all().count(), 2)
73 self.assertEqual(self.book1.materialize(), 'book 1')
74 self.assertEqual(self.book2.materialize(), 'book 2')
76 def test_split_book(self):
77 self.book1.chunk_set.create(number=2, title='Second chunk',
79 self.book1[1].commit('I survived!')
80 self.assertEqual(len(self.book1), 2)
82 self.assertEqual(set([b.slug for b in Book.objects.all()]),
83 set(['book2', '1', 'book3']))
85 Book.objects.get(slug='book3').materialize(),
89 class GalleryAppendTests(TestCase):
91 self.user = User.objects.create(username='tester')
92 self.book1 = Book.create(self.user, 'book 1', slug='book1')
93 self.book1.chunk_set.create(number=2, title='Second chunk',
100 self.scandir = join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
101 if not exists(self.scandir):
102 makedirs(self.scandir)
104 def make_gallery(self, book, files):
105 d = mkdtemp('gallery', dir=self.scandir)
106 for named, cont in files.items():
107 f = open(join(d, named), 'w')
110 book.gallery = basename(d)
113 def test_both_indexed(self):
114 self.book2 = Book.create(self.user, 'book 2', slug='book2')
115 self.book2.chunk_set.create(number=2, title='Second chunk of second book',
121 import nose.tools; nose.tools.set_trace()
122 self.make_gallery(self.book1, {
129 self.make_gallery(self.book2, {
136 self.book1.append(self.book2)
138 files = listdir(join(self.scandir, self.book1.gallery))
139 self.assertEqual(files, [
150 self.assertEqual((4, 6), (self.book1[2].gallery_start, self.book1[3].gallery_start))
153 def test_none_indexed(self):
154 self.book2 = Book.create(self.user, 'book 2', slug='book2')
155 self.make_gallery(self.book1, {
162 self.make_gallery(self.book2, {
169 self.book1.append(self.book2)
171 files = listdir(join(self.scandir, self.book1.gallery))
173 self.assertEqual(files, [
185 def test_none_indexed(self):
186 self.book2 = Book.create(self.user, 'book 2', slug='book2')
187 self.make_gallery(self.book1, {
194 self.make_gallery(self.book2, {
201 self.book1.append(self.book2)
203 files = listdir(join(self.scandir, self.book1.gallery))
205 self.assertEqual(files, [