fix dictionary for ptrad
[wolnelektury.git] / src / catalogue / tests / test_bookmedia.py
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.
3 #
4 from os.path import basename, exists
5 from unittest import skip
6
7 from django.core.files.base import ContentFile, File
8
9 from catalogue.test_utils import *
10 from catalogue import models, utils
11
12
13 class BookMediaTests(WLTestCase):
14
15     def setUp(self):
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))
22
23     def set_title(self, title):
24         self.book.title = title
25         self.book.save()
26
27     def test_overwrite(self):
28         """
29             File gets overwritten with same filename on update.
30         """
31
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)
36
37         self.assertEqual(bm.file.read(), b'Y')
38         self.assertEqual(basename(bm.file.name), 'test-book.ogg')
39
40     def test_zip_audiobooks(self):
41         paths = [
42             (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
43             (None, join(dirname(__file__), "files/fraszki.xml")),
44             ]
45
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)))
49
50         utils.remove_zip('test-zip-slug')
51         self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
52
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)
57         bm.save()
58
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)))
62
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")
67         bm2.save()
68         # was the audiobook zip deleted?
69         self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))