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