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 django.conf import settings
6 from os.path import basename, exists, join, dirname
7 from django.core.files.base import ContentFile
9 from catalogue.test_utils import *
10 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",
22 name=u"Zażółć gęślą jaźń")
23 bm.file.save(None, self.file)
24 self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
26 def test_long_name(self):
27 bm = models.BookMedia(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',
42 bm.file.save(None, self.file)
43 bm.file.save(None, self.file2)
45 self.assertEqual(bm.file.read(), 'Y')
46 self.assertEqual(basename(bm.file.name), 'some-media.ogg')
48 def test_no_clobber(self):
50 File save doesn't clobber some other media with similar name.
53 bm = models.BookMedia(book=self.book, type='ogg',
55 bm.file.save(None, self.file)
56 bm2 = models.BookMedia(book=self.book, type='ogg',
58 bm2.file.save(None, self.file2)
59 self.assertEqual(basename(bm.file.name), 'tytul.ogg')
60 self.assertNotEqual(basename(bm2.file.name), 'tytul.ogg')
61 self.assertEqual(bm.file.read(), 'X')
62 self.assertEqual(bm2.file.read(), 'Y')
64 def test_change_name(self):
66 File name reflects name change.
69 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
70 bm.file.save(None, self.file)
71 bm.name = "Other Title"
73 self.assertEqual(basename(bm.file.name), 'other-title.ogg')
74 self.assertEqual(bm.file.read(), 'X')
76 def test_change_name_no_clobber(self):
78 File name after change won't clobber some other file
82 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
83 bm.file.save(None, self.file)
84 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
85 bm2.file.save(None, self.file2)
88 self.assertNotEqual(basename(bm2.file.name), 'title.ogg')
89 self.assertEqual(bm.file.read(), 'X')
90 self.assertEqual(bm2.file.read(), 'Y')
92 def test_zip_audiobooks(self):
94 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
95 (None, join(dirname(__file__), "files/fraszki.xml")),
98 url = utils.create_zip(paths, 'test-zip-slug')
99 self.assertEqual("zip/test-zip-slug.zip", url)
100 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
102 utils.remove_zip('test-zip-slug')
103 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
105 def test_remove_zip_on_media_change(self):
106 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
107 bm.file.save(None, self.file)
110 zip_url = self.book.zip_audiobooks('ogg')
111 self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
112 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
114 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
115 bm2.file.save(None, self.file2)
118 # was the audiobook zip deleted?
119 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))