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 django.core.files.base import ContentFile
8 from catalogue.test_utils import *
9 from catalogue import models, utils
12 class BookMediaTests(WLTestCase):
15 WLTestCase.setUp(self)
16 self.file = ContentFile('X')
17 self.file2 = ContentFile('Y')
18 self.book = models.Book.objects.create(slug='test-book')
20 def test_diacritics(self):
21 bm = models.BookMedia(book=self.book, type="ogg", name=u"Zażółć gęślą jaźń")
22 bm.file.save(None, self.file)
23 self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
25 def test_long_name(self):
26 bm = models.BookMedia(
27 book=self.book, type="ogg",
28 name="Some very very very very very very very very very very very very very very very very long file name")
29 bm.file.save(bm.name, self.file)
31 # reload to see what was really saved
32 bm = models.BookMedia.objects.get(pk=bm.pk)
33 self.assertEqual(bm.file.size, 1)
35 def test_overwrite(self):
37 File gets overwritten with same filename on update.
40 bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
41 bm.file.save(None, self.file)
42 bm.file.save(None, self.file2)
44 self.assertEqual(bm.file.read(), 'Y')
45 self.assertEqual(basename(bm.file.name), 'some-media.ogg')
47 def test_no_clobber(self):
49 File save doesn't clobber some other media with similar name.
52 bm = models.BookMedia(book=self.book, type='ogg', name=u"Tytul")
53 bm.file.save(None, self.file)
54 bm2 = models.BookMedia(book=self.book, type='ogg', name=u"Tytuł")
55 bm2.file.save(None, self.file2)
56 self.assertEqual(basename(bm.file.name), 'tytul.ogg')
57 self.assertNotEqual(basename(bm2.file.name), 'tytul.ogg')
58 self.assertEqual(bm.file.read(), 'X')
59 self.assertEqual(bm2.file.read(), 'Y')
61 def test_change_name(self):
63 File name reflects name change.
66 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
67 bm.file.save(None, self.file)
68 bm.name = "Other Title"
70 self.assertEqual(basename(bm.file.name), 'other-title.ogg')
71 self.assertEqual(bm.file.read(), 'X')
73 def test_change_name_no_clobber(self):
75 File name after change won't clobber some other file
79 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
80 bm.file.save(None, self.file)
81 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
82 bm2.file.save(None, self.file2)
85 self.assertNotEqual(basename(bm2.file.name), 'title.ogg')
86 self.assertEqual(bm.file.read(), 'X')
87 self.assertEqual(bm2.file.read(), 'Y')
89 def test_zip_audiobooks(self):
91 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
92 (None, join(dirname(__file__), "files/fraszki.xml")),
95 url = utils.create_zip(paths, 'test-zip-slug')
96 self.assertEqual("zip/test-zip-slug.zip", url)
97 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
99 utils.remove_zip('test-zip-slug')
100 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
102 def test_remove_zip_on_media_change(self):
103 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
104 bm.file.save(None, self.file)
107 zip_url = self.book.zip_audiobooks('ogg')
108 self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
109 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
111 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
112 bm2.file.save(None, self.file2)
115 # was the audiobook zip deleted?
116 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))