6bc6aa32cdb30b84f1b3d10361cfc446335d4269
[wolnelektury.git] / src / catalogue / tests / test_bookmedia.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from os.path import basename, exists
5 from unittest import skip
6
7 from django.core.files.base import ContentFile
8
9 from catalogue.test_utils import *
10 from catalogue import models, utils
11
12
13 class BookMediaTests(WLTestCase):
14
15     def setUp(self):
16         WLTestCase.setUp(self)
17         self.file = ContentFile(b'X')
18         self.file2 = ContentFile(b'Y')
19         self.book = models.Book.objects.create(slug='test-book', title='Test')
20
21     def set_title(self, title):
22         self.book.title = title
23         self.book.save()
24
25     def test_diacritics(self):
26         bm = models.BookMedia(book=self.book, type="ogg", name="Zażółć gęślą jaźń")
27         self.set_title(bm.name)
28         bm.file.save(None, self.file)
29         self.assertEqual(basename(bm.file.name), 'zazolc-gesla-jazn.ogg')
30
31     def test_long_name(self):
32         bm = models.BookMedia(
33             book=self.book, type="ogg",
34             name="Some very very very very very very very very very very very very very very very very long file name")
35         self.set_title(bm.name)
36         bm.file.save(bm.name, self.file)
37
38         # reload to see what was really saved
39         bm = models.BookMedia.objects.get(pk=bm.pk)
40         self.assertEqual(bm.file.size, 1)
41
42     def test_overwrite(self):
43         """
44             File gets overwritten with same filename on update.
45         """
46
47         bm = models.BookMedia(book=self.book, type='ogg', name="Some media")
48         self.set_title(bm.name)
49         bm.file.save(None, self.file)
50         bm.file.save(None, self.file2)
51
52         self.assertEqual(bm.file.read(), b'Y')
53         self.assertEqual(basename(bm.file.name), 'some-media.ogg')
54
55     @skip('broken, but is it needed?')
56     def test_no_clobber(self):
57         """
58             File save doesn't clobber some other media with similar name.
59         """
60
61         bm = models.BookMedia(book=self.book, type='ogg', name="Tytul")
62         self.set_title(bm.name)
63         bm.file.save(None, self.file)
64         bm2 = models.BookMedia(book=self.book, type='ogg', name="Tytuł")
65         self.set_title(bm2.name)
66         bm2.file.save(None, self.file2)
67         self.assertEqual(basename(bm.file.name), 'tytul.ogg')
68         self.assertNotEqual(basename(bm2.file.name), 'tytul.ogg')
69         self.assertEqual(bm.file.read(), b'X')
70         self.assertEqual(bm2.file.read(), b'Y')
71
72     def test_change_name(self):
73         """
74             File name reflects name change.
75         """
76
77         bm = models.BookMedia(book=self.book, type='ogg', name="Title")
78         self.set_title(bm.name)
79         bm.file.save(None, self.file)
80         self.set_title("Other Title")
81         bm.save()
82         self.assertEqual(basename(bm.file.name), 'other-title.ogg')
83         self.assertEqual(bm.file.read(), b'X')
84
85     @skip('broken, but is it needed?')
86     def test_change_name_no_clobber(self):
87         """
88             File name after change won't clobber some other file
89             with similar name.
90         """
91
92         bm = models.BookMedia(book=self.book, type='ogg', name="Title")
93         self.set_title(bm.name)
94         bm.file.save(None, self.file)
95         bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
96         self.set_title(bm2.name)
97         bm2.file.save(None, self.file2)
98         self.set_title("Title")
99         bm2.save()
100         self.assertNotEqual(basename(bm2.file.name), 'title.ogg')
101         self.assertEqual(bm.file.read(), b'X')
102         self.assertEqual(bm2.file.read(), b'Y')
103
104     def test_zip_audiobooks(self):
105         paths = [
106             (None, join(dirname(__file__), "files/fraszka-do-anusie.xml")),
107             (None, join(dirname(__file__), "files/fraszki.xml")),
108             ]
109
110         url = utils.create_zip(paths, 'test-zip-slug')
111         self.assertEqual("zip/test-zip-slug.zip", url)
112         self.assertTrue(exists(join(settings.MEDIA_ROOT, url)))
113
114         utils.remove_zip('test-zip-slug')
115         self.assertFalse(exists(join(settings.MEDIA_ROOT, url)))
116
117     def test_remove_zip_on_media_change(self):
118         bm = models.BookMedia(book=self.book, type='ogg', name="Title")
119         self.set_title(bm.name)
120         bm.file.save(None, self.file)
121         bm.save()
122
123         zip_url = self.book.zip_audiobooks('ogg')
124         self.assertEqual('zip/'+self.book.slug+'_ogg.zip', zip_url)
125         self.assertTrue(exists(join(settings.MEDIA_ROOT, zip_url)))
126
127         bm2 = models.BookMedia(book=self.book, type='ogg', name="Other title")
128         self.set_title(bm2.name)
129         bm2.file.save(None, self.file2)
130         self.set_title("Title")
131         bm2.save()
132         # was the audiobook zip deleted?
133         self.assertFalse(exists(join(settings.MEDIA_ROOT, zip_url)))