1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from os.path import basename, exists
6 from unittest import skip
8 from django.core.files.base import ContentFile
10 from catalogue.test_utils import *
11 from catalogue import models, utils
14 class BookMediaTests(WLTestCase):
17 WLTestCase.setUp(self)
18 self.file = ContentFile('X')
19 self.file2 = ContentFile('Y')
20 self.book = models.Book.objects.create(slug='test-book', title='Test')
22 def set_title(self, title):
23 self.book.title = title
26 def test_diacritics(self):
27 bm = models.BookMedia(book=self.book, type="ogg", name=u"Zażółć gęślą jaźń")
28 self.set_title(bm.name)
29 bm.file.save(None, self.file)
30 self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
32 def test_long_name(self):
33 bm = models.BookMedia(
34 book=self.book, type="ogg",
35 name="Some very very very very very very very very very very very very very very very very long file name")
36 self.set_title(bm.name)
37 bm.file.save(bm.name, self.file)
39 # reload to see what was really saved
40 bm = models.BookMedia.objects.get(pk=bm.pk)
41 self.assertEqual(bm.file.size, 1)
43 def test_overwrite(self):
45 File gets overwritten with same filename on update.
48 bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
49 self.set_title(bm.name)
50 bm.file.save(None, self.file)
51 bm.file.save(None, self.file2)
53 self.assertEqual(bm.file.read(), 'Y')
54 self.assertEqual(basename(bm.file.name), 'some-media.ogg')
56 @skip('broken, but is it needed?')
57 def test_no_clobber(self):
59 File save doesn't clobber some other media with similar name.
62 bm = models.BookMedia(book=self.book, type='ogg', name=u"Tytul")
63 self.set_title(bm.name)
64 bm.file.save(None, self.file)
65 bm2 = models.BookMedia(book=self.book, type='ogg', name=u"Tytuł")
66 self.set_title(bm2.name)
67 bm2.file.save(None, self.file2)
68 self.assertEqual(basename(bm.file.name), 'tytul.ogg')
69 self.assertNotEqual(basename(bm2.file.name), 'tytul.ogg')
70 self.assertEqual(bm.file.read(), 'X')
71 self.assertEqual(bm2.file.read(), 'Y')
73 def test_change_name(self):
75 File name reflects name change.
78 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
79 self.set_title(bm.name)
80 bm.file.save(None, self.file)
81 self.set_title("Other Title")
83 self.assertEqual(basename(bm.file.name), 'other-title.ogg')
84 self.assertEqual(bm.file.read(), 'X')
86 @skip('broken, but is it needed?')
87 def test_change_name_no_clobber(self):
89 File name after change won't clobber some other file
93 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
94 self.set_title(bm.name)
95 bm.file.save(None, self.file)
96 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
97 self.set_title(bm2.name)
98 bm2.file.save(None, self.file2)
99 self.set_title("Title")
101 self.assertNotEqual(basename(bm2.file.name), 'title.ogg')
102 self.assertEqual(bm.file.read(), 'X')
103 self.assertEqual(bm2.file.read(), 'Y')
105 def test_zip_audiobooks(self):
107 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
108 (None, join(dirname(__file__), "files/fraszki.xml")),
111 url = utils.create_zip(paths, 'test-zip-slug')
112 self.assertEqual("zip/test-zip-slug.zip", url)
113 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
115 utils.remove_zip('test-zip-slug')
116 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
118 def test_remove_zip_on_media_change(self):
119 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
120 self.set_title(bm.name)
121 bm.file.save(None, self.file)
124 zip_url = self.book.zip_audiobooks('ogg')
125 self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
126 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
128 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
129 self.set_title(bm2.name)
130 bm2.file.save(None, self.file2)
131 self.set_title("Title")
133 # was the audiobook zip deleted?
134 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))