1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 """Tests for galleries of scans."""
8 from os.path import join, basename, exists
9 from os import makedirs, listdir
10 from django.test import TestCase
11 from django.contrib.auth.models import User
12 from catalogue.models import Book
13 from tempfile import mkdtemp
14 from django.conf import settings
17 class GalleryAppendTests(TestCase):
19 self.user = User.objects.create(username='tester')
20 self.book1 = Book.create(self.user, 'book 1', slug='book1')
21 self.book1.chunk_set.create(number=2, title='Second chunk',
28 self.scandir = join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
29 if not exists(self.scandir):
30 makedirs(self.scandir)
32 def make_gallery(self, book, files):
33 d = mkdtemp('gallery', dir=self.scandir)
34 for named, cont in files.items():
35 f = open(join(d, named), 'w')
38 book.gallery = basename(d)
41 def test_both_indexed(self):
42 self.book2 = Book.create(self.user, 'book 2', slug='book2')
43 self.book2.chunk_set.create(number=2, title='Second chunk of second book',
53 print "gallery starts:",self.book2[0].gallery_start, self.book2[1].gallery_start
55 self.make_gallery(self.book1, {
62 self.make_gallery(self.book2, {
63 '1-0001_1l' : 'dd', # the same, should not be moved
69 self.book1.append(self.book2)
71 files = listdir(join(self.scandir, self.book1.gallery))
74 self.assertEqual(files, [
85 self.assertEqual((4, 6), (self.book1[2].gallery_start, self.book1[3].gallery_start))
88 def test_none_indexed(self):
89 self.book2 = Book.create(self.user, 'book 2', slug='book2')
90 self.make_gallery(self.book1, {
97 self.make_gallery(self.book2, {
104 self.book1.append(self.book2)
106 files = listdir(join(self.scandir, self.book1.gallery))
109 self.assertEqual(files, [
121 def test_none_indexed(self):
123 self.book2 = Book.create(self.user, 'book 2', slug='book2')
124 self.make_gallery(self.book1, {
131 self.make_gallery(self.book2, {
138 self.book1.append(self.book2)
140 files = listdir(join(self.scandir, self.book1.gallery))
143 self.assertEqual(files, [