1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 """Tests for galleries of scans."""
6 from os.path import join, basename, exists
7 from os import makedirs, listdir
8 from django.test import TestCase
9 from django.contrib.auth.models import User
10 from documents.models import Book
11 from tempfile import mkdtemp
12 from django.conf import settings
15 class GalleryAppendTests(TestCase):
17 self.user = User.objects.create(username='tester')
18 self.book1 = Book.create(self.user, 'book 1', slug='book1')
19 self.book1.chunk_set.create(number=2, title='Second chunk',
24 self.scandir = join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
25 if not exists(self.scandir):
26 makedirs(self.scandir)
28 def make_gallery(self, book, files):
29 d = mkdtemp('gallery', dir=self.scandir)
30 for named, cont in files.items():
31 f = open(join(d, named), 'w')
34 book.gallery = basename(d)
37 def test_both_indexed(self):
38 self.book2 = Book.create(self.user, 'book 2', slug='book2')
39 self.book2.chunk_set.create(number=2, title='Second chunk of second book',
46 self.make_gallery(self.book1, {
53 self.make_gallery(self.book2, {
54 '1-0001_1l' : 'dd', # the same, should not be moved
60 self.book1.append(self.book2)
62 files = listdir(join(self.scandir, self.book1.gallery))
64 self.assertEqual(files, [
75 self.assertEqual((4, 6), (self.book1[2].gallery_start, self.book1[3].gallery_start))
78 def test_none_indexed(self):
79 self.book2 = Book.create(self.user, 'book 2', slug='book2')
80 self.make_gallery(self.book1, {
87 self.make_gallery(self.book2, {
94 self.book1.append(self.book2)
96 files = listdir(join(self.scandir, self.book1.gallery))
98 self.assertEqual(files, [
110 def test_none_indexed(self):
111 self.book2 = Book.create(self.user, 'book 2', slug='book2')
112 self.make_gallery(self.book1, {
119 self.make_gallery(self.book2, {
126 self.book1.append(self.book2)
128 files = listdir(join(self.scandir, self.book1.gallery))
130 self.assertEqual(files, [