1 # -*- coding: utf-8 -*-
2 from django.conf import settings
3 from os.path import basename, exists, join, dirname
4 from django.core.files.base import ContentFile, File
6 from catalogue.test_utils import *
7 from catalogue import models, utils
9 class BookMediaTests(WLTestCase):
12 WLTestCase.setUp(self)
13 self.file = ContentFile('X')
14 self.file2 = ContentFile('Y')
15 self.book = models.Book.objects.create(slug='test-book')
17 def test_diacritics(self):
18 bm = models.BookMedia(book=self.book, type="ogg",
19 name=u"Zażółć gęślą jaźń")
20 bm.file.save(None, self.file)
21 self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
23 def test_long_name(self):
24 bm = models.BookMedia(book=self.book, type="ogg",
25 name="Some very very very very very very very very very very very very very very very very long file name")
26 bm.file.save(bm.name, self.file)
28 # reload to see what was really saved
29 bm = models.BookMedia.objects.get(pk=bm.pk)
30 self.assertEqual(bm.file.size, 1)
32 def test_overwrite(self):
34 File gets overwritten with same filename on update.
37 bm = models.BookMedia(book=self.book, type='ogg',
39 bm.file.save(None, self.file)
40 bm.file.save(None, self.file2)
42 self.assertEqual(bm.file.read(), 'Y')
43 self.assertEqual(basename(bm.file.name), 'some-media.ogg')
45 def test_no_clobber(self):
47 File save doesn't clobber some other media with similar name.
50 bm = models.BookMedia(book=self.book, type='ogg',
52 bm.file.save(None, self.file)
53 bm2 = models.BookMedia(book=self.book, type='ogg',
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)))