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