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