1 from django.conf import settings
2 from django.test import TestCase
5 from slughifi import slughifi
6 from librarian import WLURI
8 class WLTestCase(TestCase):
10 Generic base class for tests. Adds settings freeze and clears MEDIA_ROOT.
15 self._MEDIA_ROOT, settings.MEDIA_ROOT = settings.MEDIA_ROOT, tempfile.mkdtemp(prefix='djangotest_')
16 settings.NO_SEARCH_INDEX = settings.NO_BUILD_PDF = settings.NO_BUILD_MOBI = settings.NO_BUILD_EPUB = settings.NO_BUILD_TXT = settings.NO_BUILD_FB2 = True
17 settings.CELERY_ALWAYS_EAGER = True
18 self._CACHES, settings.CACHES = settings.CACHES, {
20 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
25 shutil.rmtree(settings.MEDIA_ROOT, True)
26 settings.MEDIA_ROOT = self._MEDIA_ROOT
27 settings.CACHES = self._CACHES
30 class PersonStub(object):
32 def __init__(self, first_names, last_name):
33 self.first_names = first_names
34 self.last_name = last_name
37 return " ".join(self.first_names + (self.last_name,))
40 class BookInfoStub(object):
41 _empty_fields = ['cover_url', 'variant_of']
42 # allow single definition for multiple-value fields
47 def __init__(self, **kwargs):
50 def __setattr__(self, key, value):
51 if not key.startswith('_'):
52 self.__dict[key] = value
53 return object.__setattr__(self, key, value)
55 def __getattr__(self, key):
57 return self.__dict[key]
59 if key in self._empty_fields:
61 elif key in self._salias:
62 return [getattr(self, self._salias[key])]
67 return dict((key, unicode(value)) for key, value in self.__dict.items())
70 def info_args(title, language=None):
71 """ generate some keywords for comfortable BookInfoCreation """
72 slug = unicode(slughifi(title))
76 'title': unicode(title),
77 'url': WLURI.from_slug(slug),
78 'about': u"http://wolnelektury.pl/example/URI/%s" % slug,