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_diacritics(self):
26 bm = models.BookMedia(book=self.book, type="ogg", name="Zażółć gęślą jaźń")
27 self.set_title(bm.name)
28 bm.file.save(None, self.file)
29 self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
31 def test_long_name(self):
32 bm = models.BookMedia(
33 book=self.book, type="ogg",
34 name="Some very very very very very very very very very very very very very very very very long file name")
35 self.set_title(bm.name)
36 bm.file.save(bm.name, self.file)
38 # reload to see what was really saved
39 bm = models.BookMedia.objects.get(pk=bm.pk)
40 self.assertEqual(bm.file.size, 1)
42 def test_overwrite(self):
44 File gets overwritten with same filename on update.
47 bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
48 self.set_title(bm.name)
49 bm.file.save(None, self.file)
50 bm.file.save(None, self.file2)
52 self.assertEqual(bm.file.read(), b'Y')
53 self.assertEqual(basename(bm.file.name), 'some-media.ogg')
55 @skip('broken, but is it needed?')
56 def test_no_clobber(self):
58 File save doesn't clobber some other media with similar name.
61 bm = models.BookMedia(book=self.book, type='ogg', name="Tytul")
62 self.set_title(bm.name)
63 bm.file.save(None, self.file)
64 bm2 = models.BookMedia(book=self.book, type='ogg', name="Tytuł")
65 self.set_title(bm2.name)
66 bm2.file.save(None, self.file2)
67 self.assertEqual(basename(bm.file.name), 'tytul.ogg')
68 self.assertNotEqual(basename(bm2.file.name), 'tytul.ogg')
69 self.assertEqual(bm.file.read(), b'X')
70 self.assertEqual(bm2.file.read(), b'Y')
72 def test_change_name(self):
74 File name reflects name change.
77 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
78 self.set_title(bm.name)
79 bm.file.save(None, self.file)
80 self.set_title("Other Title")
82 self.assertEqual(basename(bm.file.name), 'other-title.ogg')
83 self.assertEqual(bm.file.read(), b'X')
85 @skip('broken, but is it needed?')
86 def test_change_name_no_clobber(self):
88 File name after change won't clobber some other file
92 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
93 self.set_title(bm.name)
94 bm.file.save(None, self.file)
95 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
96 self.set_title(bm2.name)
97 bm2.file.save(None, self.file2)
98 self.set_title("Title")
100 self.assertNotEqual(basename(bm2.file.name), 'title.ogg')
101 self.assertEqual(bm.file.read(), b'X')
102 self.assertEqual(bm2.file.read(), b'Y')
104 def test_zip_audiobooks(self):
106 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
107 (None, join(dirname(__file__), "files/fraszki.xml")),
110 url = utils.create_zip(paths, 'test-zip-slug')
111 self.assertEqual("zip/test-zip-slug.zip", url)
112 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
114 utils.remove_zip('test-zip-slug')
115 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
117 def test_remove_zip_on_media_change(self):
118 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
119 self.set_title(bm.name)
120 bm.file.save(None, self.file)
123 zip_url = self.book.zip_audiobooks('ogg')
124 self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
125 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
127 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
128 self.set_title(bm2.name)
129 bm2.file.save(None, self.file2)
130 self.set_title("Title")
132 # was the audiobook zip deleted?
133 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))