Code layout change.
[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 fnpdjango.utils.text.slughifi import slughifi
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=set(['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     SSIFY_RENDER=False,
25 )
26 class WLTestCase(TestCase):
27     """
28         Generic base class for tests. Adds settings freeze and clears MEDIA_ROOT.
29     """
30     longMessage = True
31
32
33 class PersonStub(object):
34
35     def __init__(self, first_names, last_name):
36         self.first_names = first_names
37         self.last_name = last_name
38
39     def readable(self):
40         return " ".join(self.first_names + (self.last_name,))
41
42
43 class BookInfoStub(object):
44     _empty_fields = ['cover_url', 'variant_of']
45     # allow single definition for multiple-value fields
46     _salias = {
47         'authors': 'author',
48     }
49
50     def __init__(self, **kwargs):
51         self.__dict = kwargs
52
53     def __setattr__(self, key, value):
54         if not key.startswith('_'):
55             self.__dict[key] = value
56         return object.__setattr__(self, key, value)
57
58     def __getattr__(self, key):
59         try:
60             return self.__dict[key]
61         except KeyError:
62             if key in self._empty_fields:
63                 return None
64             elif key in self._salias:
65                 return [getattr(self, self._salias[key])]
66             else:
67                 raise
68
69     def to_dict(self):
70         return dict((key, unicode(value)) for key, value in self.__dict.items())
71
72
73 def info_args(title, language=None):
74     """ generate some keywords for comfortable BookInfoCreation  """
75     slug = unicode(slughifi(title))
76     if language is None:
77         language = u'pol'
78     return {
79         'title': unicode(title),
80         'url': WLURI.from_slug(slug),
81         'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
82         'language': language,
83     }
84
85
86 def get_fixture(path, app=None):
87     if app is not None:
88         mod_path = app.__file__
89         f_path = join(dirname(abspath(mod_path)), 'tests/files', path)
90     else:
91         mod_path = extract_stack(limit=2)[0][0]
92         f_path = join(dirname(abspath(mod_path)), 'files', path)
93     return f_path