-
Notifications
You must be signed in to change notification settings - Fork 1
/
runtests.py
101 lines (80 loc) · 2.93 KB
/
runtests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
import unittest
import logging
import re
import django
from django.conf import settings
try:
from django.utils.functional import empty
except ImportError:
empty = None
logger = logging.getLogger(__name__)
def setup_test_environment():
# reset settings
settings._wrapped = empty
apps = [
'django_pg_current_timestamp',
'django_pg_current_timestamp.django_pg_current_timestamp_test_app',
]
settings_dict = {
'DATABASES': {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_pg_current_timestamp',
},
},
'MIDDLEWARE_CLASSES': [],
'INSTALLED_APPS': apps,
'SITE_ID': 1,
'DEFAULT_INDEX_TABLESPACE': '',
}
# set up settings for running tests for all apps
settings.configure(**settings_dict)
try:
from django import setup
setup() # Needed for Django>=1.7
except ImportError:
pass
class TestCase(unittest.TestCase):
"""Initializes the db with models from `test_app`, see http://stackoverflow.com/a/10657706/293064."""
initiated = False
@classmethod
def setUpClass(cls, *args, **kwargs):
if not TestCase.initiated:
setup_test_environment()
TestCase.create_models_from_app('django_pg_current_timestamp.django_pg_current_timestamp_test_app')
TestCase.initiated = True
super(TestCase, cls).setUpClass(*args, **kwargs)
@classmethod
def create_models_from_app(cls, app_name):
"""
Manually create Models (used only for testing) from the specified string app name.
Models are loaded from the module '<app_name>.models'
"""
from django.db import connection, DatabaseError
from django.test.simple import DjangoTestSuiteRunner
# NB: `load_app` is deprecated as of Django 1.7.
if re.match(r'''^1\.7.*$''', django.get_version()) is not None:
from django.apps import apps
# NB: Django 1.7 doesn't work with the fully-qualified app name.
app = apps.get_app_config(app_name.split('.')[1])
else:
from django.db.models.loading import load_app
app = load_app(app_name)
from django.core.management import sql
from django.core.management.color import no_style
sql = sql.sql_create(app, no_style(), connection)
cursor = connection.cursor()
for statement in sql:
try:
cursor.execute(statement)
except DatabaseError, excn:
logger.debug(excn.message)
def testIt(self):
from django.test.simple import DjangoTestSuiteRunner
runner = DjangoTestSuiteRunner(verbosity=1, interactive=False, failfast=False)
return runner.run_tests(['django_pg_current_timestamp'])
def runtests():
unittest.main()
if __name__ == '__main__':
runtests()