mobi icon
[wolnelektury.git] / apps / catalogue / test_utils.py
1 from django.conf import settings
2 from django.test import TestCase
3 import shutil
4 import tempfile
5 from slughifi import slughifi
6
7 class WLTestCase(TestCase):
8     """
9         Generic base class for tests. Adds settings freeze and clears MEDIA_ROOT.
10     """
11     def setUp(self):
12         self._MEDIA_ROOT, settings.MEDIA_ROOT = settings.MEDIA_ROOT, tempfile.mkdtemp(prefix='djangotest_')
13         settings.NO_BUILD_PDF = settings.NO_BUILD_MOBI = settings.NO_BUILD_EPUB = settings.NO_BUILD_TXT = True
14         settings.CELERY_ALWAYS_EAGER = True
15
16     def tearDown(self):
17         shutil.rmtree(settings.MEDIA_ROOT, True)
18         settings.MEDIA_ROOT = self._MEDIA_ROOT
19
20 class PersonStub(object):
21
22     def __init__(self, first_names, last_name):
23         self.first_names = first_names
24         self.last_name = last_name
25
26
27 class BookInfoStub(object):
28
29     def __init__(self, **kwargs):
30         self.__dict = kwargs
31
32     def __setattr__(self, key, value):
33         if not key.startswith('_'):
34             self.__dict[key] = value
35         return object.__setattr__(self, key, value)
36
37     def __getattr__(self, key):
38         return self.__dict[key]
39
40     def to_dict(self):
41         return dict((key, unicode(value)) for key, value in self.__dict.items())
42
43
44 def info_args(title):
45     """ generate some keywords for comfortable BookInfoCreation  """
46     slug = unicode(slughifi(title))
47     return {
48         'title': unicode(title),
49         'slug': slug,
50         'url': u"http://wolnelektury.pl/example/%s" % slug,
51         'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
52     }