1 # -*- coding: utf-8 -*-
2 from os.path import basename
3 from django.core.files.base import ContentFile
5 from catalogue.test_utils import *
6 from catalogue import models
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')