Remove ssify.
[wolnelektury.git] / src / catalogue / test_utils.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 abspath, dirname, join
6 import tempfile
7 from traceback import extract_stack
8 from django.test import TestCase
9 from django.test.utils import override_settings
10 from slugify import slugify
11 from librarian import WLURI
12 from django.conf import settings
13
14
15 @override_settings(
16     MEDIA_ROOT=tempfile.mkdtemp(prefix='djangotest_'),
17     CATALOGUE_DONT_BUILD={'pdf', 'mobi', 'epub', 'txt', 'fb2', 'cover'},
18     NO_SEARCH_INDEX=True,
19     CELERY_ALWAYS_EAGER=True,
20     CACHES={
21             'default': {'BACKEND': 'django.core.cache.backends.dummy.DummyCache'},
22         },
23     SOLR=settings.SOLR_TEST,
24 )
25 class WLTestCase(TestCase):
26     """
27         Generic base class for tests. Adds settings freeze and clears MEDIA_ROOT.
28     """
29     longMessage = True
30
31
32 class PersonStub(object):
33
34     def __init__(self, first_names, last_name):
35         self.first_names = first_names
36         self.last_name = last_name
37
38     def readable(self):
39         return " ".join(self.first_names + (self.last_name,))
40
41
42 class BookInfoStub(object):
43     _empty_fields = ['cover_url', 'variant_of']
44     # allow single definition for multiple-value fields
45     _salias = {
46         'authors': 'author',
47     }
48
49     def __init__(self, **kwargs):
50         self.__dict = kwargs
51
52     def __setattr__(self, key, value):
53         if not key.startswith('_'):
54             self.__dict[key] = value
55         return object.__setattr__(self, key, value)
56
57     def __getattr__(self, key):
58         try:
59             return self.__dict[key]
60         except KeyError as e:
61             if key in self._empty_fields:
62                 return None
63             elif key in self._salias:
64                 return [getattr(self, self._salias[key])]
65             else:
66                 raise AttributeError(e)
67
68     def to_dict(self):
69         return dict((key, str(value)) for key, value in self.__dict.items())
70
71
72 def info_args(title, language=None):
73     """ generate some keywords for comfortable BookInfoCreation  """
74     slug = str(slugify(title))
75     if language is None:
76         language = u'pol'
77     return {
78         'title': str(title),
79         'url': WLURI.from_slug(slug),
80         'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
81         'language': language,
82     }
83
84
85 def get_fixture(path, app=None):
86     if app is not None:
87         mod_path = app.__file__
88         f_path = join(dirname(abspath(mod_path)), 'tests/files', path)
89     else:
90         mod_path = extract_stack(limit=2)[0][0]
91         f_path = join(dirname(abspath(mod_path)), 'files', path)
92     return f_path
93
94
95 def get_mp3_length(path):
96     return 60