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',
26 self.scandir = join(settings.MEDIA_ROOT, settings.IMAGE_DIR)
27 if not exists(self.scandir):
28 makedirs(self.scandir)
30 def make_gallery(self, book, files):
31 d = mkdtemp('gallery', dir=self.scandir)
32 for named, cont in files.items():
33 f = open(join(d, named), 'w')
36 book.gallery = basename(d)
39 def test_both_indexed(self):
40 self.book2 = Book.create(self.user, 'book 2', slug='book2')
41 self.book2.chunk_set.create(number=2, title='Second chunk of second book',
48 print "gallery starts:",self.book2[0].gallery_start, self.book2[1].gallery_start
50 self.make_gallery(self.book1, {
57 self.make_gallery(self.book2, {
58 '1-0001_1l' : 'dd', # the same, should not be moved
64 self.book1.append(self.book2)
66 files = listdir(join(self.scandir, self.book1.gallery))
69 self.assertEqual(files, [
80 self.assertEqual((4, 6), (self.book1[2].gallery_start, self.book1[3].gallery_start))
83 def test_none_indexed(self):
84 self.book2 = Book.create(self.user, 'book 2', slug='book2')
85 self.make_gallery(self.book1, {
92 self.make_gallery(self.book2, {
99 self.book1.append(self.book2)
101 files = listdir(join(self.scandir, self.book1.gallery))
104 self.assertEqual(files, [
116 def test_none_indexed(self):
118 self.book2 = Book.create(self.user, 'book 2', slug='book2')
119 self.make_gallery(self.book1, {
126 self.make_gallery(self.book2, {
133 self.book1.append(self.book2)
135 files = listdir(join(self.scandir, self.book1.gallery))
138 self.assertEqual(files, [