1 # -*- coding: utf-8 -*-
2 from os.path import basename, exists, join, dirname
3 from django.core.files.base import ContentFile, File
5 from catalogue.test_utils import *
6 from catalogue import models, utils
8 class BookMediaTests(WLTestCase):
11 WLTestCase.setUp(self)
12 self.file = ContentFile('X')
13 self.file2 = ContentFile('Y')
14 self.book = models.Book.objects.create(slug='test-book')
16 def test_diacritics(self):
17 bm = models.BookMedia(book=self.book, type="ogg",
18 name=u"Zażółć gęślą jaźń")
19 bm.file.save(None, self.file)
20 self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
22 def test_long_name(self):
23 bm = models.BookMedia(book=self.book, type="ogg",
24 name="Some very very very very very very very very very very very very very very very very long file name")
25 bm.file.save(bm.name, self.file)
27 # reload to see what was really saved
28 bm = models.BookMedia.objects.get(pk=bm.pk)
29 self.assertEqual(bm.file.size, 1)
31 def test_overwrite(self):
33 File gets overwritten with same filename on update.
36 bm = models.BookMedia(book=self.book, type='ogg',
38 bm.file.save(None, self.file)
39 bm.file.save(None, self.file2)
41 self.assertEqual(bm.file.read(), 'Y')
42 self.assertEqual(basename(bm.file.name), 'some-media.ogg')
44 def test_no_clobber(self):
46 File save doesn't clobber some other media with similar name.
49 bm = models.BookMedia(book=self.book, type='ogg',
51 bm.file.save(None, self.file)
52 bm2 = models.BookMedia(book=self.book, type='ogg',
54 bm2.file.save(None, self.file2)
55 self.assertEqual(basename(bm.file.name), 'tytul.ogg')
56 self.assertNotEqual(basename(bm2.file.name), 'tytul.ogg')
57 self.assertEqual(bm.file.read(), 'X')
58 self.assertEqual(bm2.file.read(), 'Y')
60 def test_change_name(self):
62 File name reflects name change.
65 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
66 bm.file.save(None, self.file)
67 bm.name = "Other Title"
69 self.assertEqual(basename(bm.file.name), 'other-title.ogg')
70 self.assertEqual(bm.file.read(), 'X')
72 def test_change_name_no_clobber(self):
74 File name after change won't clobber some other file
78 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
79 bm.file.save(None, self.file)
80 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
81 bm2.file.save(None, self.file2)
84 self.assertNotEqual(basename(bm2.file.name), 'title.ogg')
85 self.assertEqual(bm.file.read(), 'X')
86 self.assertEqual(bm2.file.read(), 'Y')
88 def test_zip_audiobooks(self):
90 (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
91 (None, join(dirname(__file__), "files/fraszki.xml")),
94 url = utils.create_zip(paths, 'test-zip-slug')
95 self.assertEqual("zip/test-zip-slug.zip", url)
96 self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
98 utils.remove_zip('test-zip-slug')
99 self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
101 def test_remove_zip_on_media_change(self):
102 bm = models.BookMedia(book=self.book, type='ogg', name="Title")
103 bm.file.save(None, self.file)
106 zip_url = self.book.zip_audiobooks()
107 self.assertEqual('zip/'+self.book.slug+'.zip', zip_url)
108 self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
110 bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
111 bm2.file.save(None, self.file2)
114 # was the audiobook zip deleted?
115 self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))