/
ckan_nose_plugin.py
133 lines (118 loc) · 4.4 KB
/
ckan_nose_plugin.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# encoding: utf-8
from nose.plugins import Plugin
from inspect import isclass
import hashlib
import os
import sys
import re
import pkg_resources
from paste.deploy import loadapp
from ckan.common import config
import unittest
import time
class CkanNose(Plugin):
settings = None
def startContext(self, ctx):
# import needs to be here or setup happens too early
import ckan.model as model
if 'legacy' not in repr(ctx):
# We don't want to do the stuff below for new-style tests.
if not CkanNose.settings.reset_database:
model.repo.tables_created_and_initialised = True
return
if isclass(ctx):
if hasattr(ctx, "no_db") and ctx.no_db:
return
if (not CkanNose.settings.reset_database
and not CkanNose.settings.ckan_migration):
model.Session.close_all()
model.repo.tables_created_and_initialised = True
model.repo.rebuild_db()
self.is_first_test = False
elif self.is_first_test or CkanNose.settings.ckan_migration:
model.Session.close_all()
model.repo.clean_db()
self.is_first_test = False
if CkanNose.settings.ckan_migration:
model.Session.close_all()
model.repo.upgrade_db()
## This is to make sure the configuration is run again.
## Plugins use configure to make their own tables and they
## may need to be recreated to make tests work.
from ckan.plugins import PluginImplementations
from ckan.plugins.interfaces import IConfigurable
for plugin in PluginImplementations(IConfigurable):
plugin.configure(config)
# init_db is run at the start of every class because
# when you use an in-memory sqlite db, it appears that
# the db is destroyed after every test when you Session.Remove().
model.repo.init_db()
def options(self, parser, env):
parser.add_option(
'--ckan',
action='store_true',
dest='is_ckan',
help='Always set this when testing CKAN.')
parser.add_option(
'--ckan-migration',
action='store_true',
dest='ckan_migration',
help='set this when wanting to test migrations')
parser.add_option(
'--docstrings',
action='store_true',
dest='docstrings',
help='set this to display test docstrings instead of module names')
parser.add_option(
'--segments',
dest='segments',
help='A string containing a hex digits that represent which of'
'the 16 test segments to run. i.e 15af will run segments 1,5,a,f')
parser.add_option(
'--reset-db',
action='store_true',
dest='reset_database',
help='drop database and reinitialize before tests are run')
def wantClass(self, cls):
if self.segments and str(hashlib.md5(
cls.__name__).hexdigest())[0] not in self.segments:
return False
def wantFunction(self, fn):
if self.segments and hashlib.md5(
fn.__name__).hexdigest()[0] not in self.segments:
return False
def finalize(self, report):
if self.segments:
print('Segments: %s' % self.segments)
def configure(self, settings, config):
CkanNose.settings = settings
if settings.is_ckan:
self.enabled = True
self.is_first_test = True
self.segments = settings.segments
def describeTest(self, test):
if not CkanNose.settings.docstrings:
# display module name instead of docstring
return False
def startTest(self, test):
"""
startTest: start timing.
"""
## self._started = time.time()
def stopTest(self, test):
"""
stopTest: stop timing, canonicalize the test name, and save
the run time.
"""
## runtime = time.time() - self._started
##
## # CTB: HACK!
## f = open('times.txt', 'a')
##
## testname = str(test)
## #if ' ' in testname:
## # testname = testname.split()[1]
##
## f.write('%s,%s\n' % (testname, str(runtime)))
##
## f.close()