ac9298cdda1a1d4dccd7f957835df85d75f2e2a4
[redakcja.git] / apps / catalogue / tests / __init__.py
1 from os.path import abspath, dirname, join, basename, exists
2 from os import makedirs, listdir
3 from nose.tools import *
4 from mock import patch
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
10
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')
15
16
17 class PublishTests(TestCase):
18
19     def setUp(self):
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')
23
24     @patch('apiclient.api_call')
25     def test_unpublishable(self, api_call):
26         with self.assertRaises(AssertionError):
27             self.book.publish(self.user)
28
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})
34
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')})
43
44
45 class ManipulationTests(TestCase):
46
47     def setUp(self):
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')
51
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)
56
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)
62
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')
68
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')
75
76     def test_split_book(self):
77         self.book1.chunk_set.create(number=2, title='Second chunk',
78                 slug='book3')
79         self.book1[1].commit('I survived!')
80         self.assertEqual(len(self.book1), 2)
81         self.book1.split()
82         self.assertEqual(set([b.slug for b in Book.objects.all()]),
83                 set(['book2', '1', 'book3']))
84         self.assertEqual(
85                 Book.objects.get(slug='book3').materialize(),
86                 'I survived!')
87
88
89 class GalleryAppendTests(TestCase):
90     def setUp(self):
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',
94                 slug='book 1 / 2')
95         c=self.book1[0]
96         c.gallery_start=0
97         c=self.book1[1]
98         c.gallery_start=2
99         
100         self.scandir = join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
101         if not exists(self.scandir):
102             makedirs(self.scandir)
103
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')
108             f.write(cont)
109             f.close()
110         book.gallery = basename(d)
111
112
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',
116                 slug='book 2 / 2')
117         c = self.book2[0]
118         c.gallery_start = 0
119         c = self.book2[1]
120         c.gallery_start = 2
121
122         self.make_gallery(self.book1, {
123             '1-0001_1l' : 'aa',
124             '1-0001_2r' : 'bb',
125             '1-0002_1l' : 'cc',
126             '1-0002_2r' : 'dd',
127             })
128
129         self.make_gallery(self.book2, {
130             '1-0001_1l' : 'dd', # the same, should not be moved
131             '1-0001_2r' : 'ff',
132             '2-0002_1l' : 'gg',
133             '2-0002_2r' : 'hh',
134             })
135
136         self.book1.append(self.book2)
137
138         files = listdir(join(self.scandir, self.book1.gallery))
139         self.assertEqual(files, [
140             '1-0001_1l',
141             '1-0001_2r',
142             '1-0002_1l',
143             '1-0002_2r',
144             #            '2-0001_1l',
145             '2-0001_2r',
146             '3-0002_1l',
147             '3-0002_2r',
148             ])        
149
150         self.assertEqual((3, 5), (self.book1[2].gallery_start, self.book1[3].gallery_start))
151         
152         
153     def test_none_indexed(self):
154         self.book2 = Book.create(self.user, 'book 2', slug='book2')
155         self.make_gallery(self.book1, {
156             '0001_1l' : 'aa',
157             '0001_2r' : 'bb',
158             '0002_1l' : 'cc',
159             '0002_2r' : 'dd',
160             })
161
162         self.make_gallery(self.book2, {
163             '0001_1l' : 'ee',
164             '0001_2r' : 'ff',
165             '0002_1l' : 'gg',
166             '0002_2r' : 'hh',
167             })
168
169         self.book1.append(self.book2)
170
171         files = listdir(join(self.scandir, self.book1.gallery))
172         print files
173         self.assertEqual(files, [
174             '0-0001_1l',
175             '0-0001_2r',
176             '0-0002_1l',
177             '0-0002_2r',
178             '1-0001_1l',
179             '1-0001_2r',
180             '1-0002_1l',
181             '1-0002_2r',
182             ])        
183
184
185     def test_none_indexed(self):
186         import nose.tools
187         self.book2 = Book.create(self.user, 'book 2', slug='book2')
188         self.make_gallery(self.book1, {
189             '1-0001_1l' : 'aa',
190             '1-0001_2r' : 'bb',
191             '1002_1l' : 'cc',
192             '1002_2r' : 'dd',
193             })
194
195         self.make_gallery(self.book2, {
196             '0001_1l' : 'ee',
197             '0001_2r' : 'ff',
198             '0002_1l' : 'gg',
199             '0002_2r' : 'hh',
200             })
201
202         self.book1.append(self.book2)
203
204         files = listdir(join(self.scandir, self.book1.gallery))
205         print files
206         self.assertEqual(files, [
207             '0-1-0001_1l',
208             '0-1-0001_2r',
209             '0-1002_1l',
210             '0-1002_2r',
211             '1-0001_1l',
212             '1-0001_2r',
213             '1-0002_1l',
214             '1-0002_2r',
215             ])