ae7bef7dd4a3bdb8fffb290774197d19b05ce0b7
[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=1
97         c=self.book1[1]
98         c.gallery_start=3
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
118         c = self.book2[0]
119         c.gallery_start = 1
120         c.save()
121         c = self.book2[1]
122         c.gallery_start = 3
123         c.save()
124         
125         print "gallery starts:",self.book2[0].gallery_start, self.book2[1].gallery_start
126
127         self.make_gallery(self.book1, {
128             '1-0001_1l' : 'aa',
129             '1-0001_2r' : 'bb',
130             '1-0002_1l' : 'cc',
131             '1-0002_2r' : 'dd',
132             })
133
134         self.make_gallery(self.book2, {
135             '1-0001_1l' : 'dd', # the same, should not be moved
136             '1-0001_2r' : 'ff',
137             '2-0002_1l' : 'gg',
138             '2-0002_2r' : 'hh',
139             })
140
141         self.book1.append(self.book2)
142
143         files = listdir(join(self.scandir, self.book1.gallery))
144         files.sort()
145         print files
146         self.assertEqual(files, [
147             '1-0001_1l',
148             '1-0001_2r',
149             '1-0002_1l',
150             '1-0002_2r',
151             #            '2-0001_1l',
152             '2-0001_2r',
153             '3-0002_1l',
154             '3-0002_2r',
155             ])        
156
157         self.assertEqual((4, 6), (self.book1[2].gallery_start, self.book1[3].gallery_start))
158         
159         
160     def test_none_indexed(self):
161         self.book2 = Book.create(self.user, 'book 2', slug='book2')
162         self.make_gallery(self.book1, {
163             '0001_1l' : 'aa',
164             '0001_2r' : 'bb',
165             '0002_1l' : 'cc',
166             '0002_2r' : 'dd',
167             })
168
169         self.make_gallery(self.book2, {
170             '0001_1l' : 'ee',
171             '0001_2r' : 'ff',
172             '0002_1l' : 'gg',
173             '0002_2r' : 'hh',
174             })
175
176         self.book1.append(self.book2)
177
178         files = listdir(join(self.scandir, self.book1.gallery))
179         files.sort()
180         print files
181         self.assertEqual(files, [
182             '0-0001_1l',
183             '0-0001_2r',
184             '0-0002_1l',
185             '0-0002_2r',
186             '1-0001_1l',
187             '1-0001_2r',
188             '1-0002_1l',
189             '1-0002_2r',
190             ])        
191
192
193     def test_none_indexed(self):
194         import nose.tools
195         self.book2 = Book.create(self.user, 'book 2', slug='book2')
196         self.make_gallery(self.book1, {
197             '1-0001_1l' : 'aa',
198             '1-0001_2r' : 'bb',
199             '1002_1l' : 'cc',
200             '1002_2r' : 'dd',
201             })
202
203         self.make_gallery(self.book2, {
204             '0001_1l' : 'ee',
205             '0001_2r' : 'ff',
206             '0002_1l' : 'gg',
207             '0002_2r' : 'hh',
208             })
209
210         self.book1.append(self.book2)
211
212         files = listdir(join(self.scandir, self.book1.gallery))
213         files.sort()
214         print files
215         self.assertEqual(files, [
216             '0-1-0001_1l',
217             '0-1-0001_2r',
218             '0-1002_1l',
219             '0-1002_2r',
220             '1-0001_1l',
221             '1-0001_2r',
222             '1-0002_1l',
223             '1-0002_2r',
224             ])