3421d333a2bd46041542330514a9c190ac12dc85
[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         files.sort()
140         self.assertEqual(files, [
141             '1-0001_1l',
142             '1-0001_2r',
143             '1-0002_1l',
144             '1-0002_2r',
145             #            '2-0001_1l',
146             '2-0001_2r',
147             '3-0002_1l',
148             '3-0002_2r',
149             ])        
150
151         self.assertEqual((3, 5), (self.book1[2].gallery_start, self.book1[3].gallery_start))
152         
153         
154     def test_none_indexed(self):
155         self.book2 = Book.create(self.user, 'book 2', slug='book2')
156         self.make_gallery(self.book1, {
157             '0001_1l' : 'aa',
158             '0001_2r' : 'bb',
159             '0002_1l' : 'cc',
160             '0002_2r' : 'dd',
161             })
162
163         self.make_gallery(self.book2, {
164             '0001_1l' : 'ee',
165             '0001_2r' : 'ff',
166             '0002_1l' : 'gg',
167             '0002_2r' : 'hh',
168             })
169
170         self.book1.append(self.book2)
171
172         files = listdir(join(self.scandir, self.book1.gallery))
173         files.sort()
174         print files
175         self.assertEqual(files, [
176             '0-0001_1l',
177             '0-0001_2r',
178             '0-0002_1l',
179             '0-0002_2r',
180             '1-0001_1l',
181             '1-0001_2r',
182             '1-0002_1l',
183             '1-0002_2r',
184             ])        
185
186
187     def test_none_indexed(self):
188         import nose.tools
189         self.book2 = Book.create(self.user, 'book 2', slug='book2')
190         self.make_gallery(self.book1, {
191             '1-0001_1l' : 'aa',
192             '1-0001_2r' : 'bb',
193             '1002_1l' : 'cc',
194             '1002_2r' : 'dd',
195             })
196
197         self.make_gallery(self.book2, {
198             '0001_1l' : 'ee',
199             '0001_2r' : 'ff',
200             '0002_1l' : 'gg',
201             '0002_2r' : 'hh',
202             })
203
204         self.book1.append(self.book2)
205
206         files = listdir(join(self.scandir, self.book1.gallery))
207         files.sort()
208         print files
209         self.assertEqual(files, [
210             '0-1-0001_1l',
211             '0-1-0001_2r',
212             '0-1002_1l',
213             '0-1002_2r',
214             '1-0001_1l',
215             '1-0001_2r',
216             '1-0002_1l',
217             '1-0002_2r',
218             ])