[Models] Use fast (but approximate) counting
[mygpo.git] / mygpo / test.py
blob23b563f43bd3c6b852ab320bf22b842ebeb9e702
1 import os.path
3 from django.conf import settings
4 from django.core.urlresolvers import resolve
5 from django.contrib.auth.models import AnonymousUser
6 from django.test.client import RequestFactory
8 from couchdbkit import Database
9 from couchdbkit.loaders import FileSystemDocsLoader
10 from couchdbkit.ext.django.testrunner import CouchDbKitTestSuiteRunner
12 from mygpo.db.couchdb.utils import sync_design_docs
15 # inspired by
16 # http://djangosnippets.org/snippets/2211/
18 class MygpoTestSuiteRunner(CouchDbKitTestSuiteRunner):
19 """
20 Test runner that is able to skip some tests according to settings.py
21 """
23 def __init__(self, *args, **kwargs):
24 self.EXCLUDED_APPS = getattr(settings, 'TEST_EXCLUDE', [])
25 settings.TESTING = True
26 super(MygpoTestSuiteRunner, self).__init__(*args, **kwargs)
29 def setup_databases(self, **kwargs):
30 ret = super(MygpoTestSuiteRunner, self).setup_databases(**kwargs)
31 sync_design_docs()
32 return ret
35 def build_suite(self, *args, **kwargs):
36 suite = super(MygpoTestSuiteRunner, self).build_suite(*args, **kwargs)
37 if not args[0] and not getattr(settings, 'RUN_ALL_TESTS', False):
38 tests = []
39 for case in suite:
40 pkg = case.__class__.__module__.split('.')[0]
41 if pkg not in self.EXCLUDED_APPS:
42 tests.append(case)
43 suite._tests = tests
44 return suite
47 def create_auth_string(username, password):
48 import base64
49 credentials = base64.encodestring("%s:%s" % (username, password)).rstrip()
50 auth_string = 'Basic %s' % credentials
51 return auth_string
54 def anon_request(url):
55 """ Emulates an anonymous request, returns the response
57 """
58 factory = RequestFactory()
59 request = factory.get(url)
60 request.user = AnonymousUser()
61 view = resolve(url)
63 response = view.func(request, *view.args, **view.kwargs)
64 return response