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.
5 from os.path import abspath, dirname, join
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
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,
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'},
25 SOLR = settings.SOLR_TEST,
27 class WLTestCase(TestCase):
29 Generic base class for tests. Adds settings freeze and clears MEDIA_ROOT.
34 class PersonStub(object):
36 def __init__(self, first_names, last_name):
37 self.first_names = first_names
38 self.last_name = last_name
41 return " ".join(self.first_names + (self.last_name,))
44 class BookInfoStub(object):
45 _empty_fields = ['cover_url', 'variant_of']
46 # allow single definition for multiple-value fields
51 def __init__(self, **kwargs):
54 def __setattr__(self, key, value):
55 if not key.startswith('_'):
56 self.__dict[key] = value
57 return object.__setattr__(self, key, value)
59 def __getattr__(self, key):
61 return self.__dict[key]
63 if key in self._empty_fields:
65 elif key in self._salias:
66 return [getattr(self, self._salias[key])]
71 return dict((key, unicode(value)) for key, value in self.__dict.items())
74 def info_args(title, language=None):
75 """ generate some keywords for comfortable BookInfoCreation """
76 slug = unicode(slughifi(title))
80 'title': unicode(title),
81 'url': WLURI.from_slug(slug),
82 'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
87 def get_fixture(path, app=None):
89 mod_path = app.__file__
90 f_path = join(dirname(abspath(mod_path)), 'tests/files', path)
92 mod_path = extract_stack(limit=2)[0][0]
93 f_path = join(dirname(abspath(mod_path)), 'files', path)