1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. 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
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')
21 def set_title(self, title):
22 self.book.title = title
25 def test_overwrite(self):
27 File gets overwritten with same filename on update.
30 bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
31 self.set_title(bm.name)
32 bm.file.save(None, self.file)
33 bm.file.save(None, self.file2)
35 self.assertEqual(bm.file.read(), b'Y')
36 self.assertEqual(basename(bm.file.name), 'test-book.ogg')
38 def test_zip_audiobooks(self):
40 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
41 (None, join(dirname(__file__), "files/fraszki.xml")),
44 url = utils.create_zip(paths, 'test-zip-slug')
45 self.assertEqual("zip/test-zip-slug.zip", url)
46 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
48 utils.remove_zip('test-zip-slug')
49 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
51 def test_remove_zip_on_media_change(self):
52 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
53 self.set_title(bm.name)
54 bm.file.save(None, self.file)
57 zip_url = self.book.zip_audiobooks('ogg')
58 self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
59 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
61 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
62 self.set_title(bm2.name)
63 bm2.file.save(None, self.file2)
64 self.set_title("Title")
66 # was the audiobook zip deleted?
67 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))