1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from os.path import basename, exists
5 from unittest import skip
7 from django.core.files.base import ContentFile, File
9 from catalogue.test_utils import *
10 from catalogue import models, utils
13 class BookMediaTests(WLTestCase):
16 WLTestCase.setUp(self)
17 self.file = ContentFile(b'X')
18 self.file2 = ContentFile(b'Y')
19 self.book = models.Book.objects.create(slug='test-book', title='Test')
20 with open(join(dirname(__file__), "files/fraszka-do-anusie.xml")) as f:
21 self.book.xml_file.save(None, File(f))
23 def set_title(self, title):
24 self.book.title = title
27 def test_overwrite(self):
29 File gets overwritten with same filename on update.
32 bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
33 self.set_title(bm.name)
34 bm.file.save(None, self.file)
35 bm.file.save(None, self.file2)
37 self.assertEqual(bm.file.read(), b'Y')
38 self.assertEqual(basename(bm.file.name), 'test-book.ogg')
40 def test_zip_audiobooks(self):
42 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
43 (None, join(dirname(__file__), "files/fraszki.xml")),
46 url = utils.create_zip(paths, 'test-zip-slug')
47 self.assertEqual("zip/test-zip-slug.zip", url)
48 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
50 utils.remove_zip('test-zip-slug')
51 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
53 def test_remove_zip_on_media_change(self):
54 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
55 self.set_title(bm.name)
56 bm.file.save(None, self.file)
59 zip_url = self.book.zip_audiobooks('ogg')
60 self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
61 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
63 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
64 self.set_title(bm2.name)
65 bm2.file.save(None, self.file2)
66 self.set_title("Title")
68 # was the audiobook zip deleted?
69 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))