testRunner.py 11.3 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/python
"""
Runs all tests that exist in the tests directories.

The class should have a name based on the file's path, like this:
FilenameDirectory -> DefaultTasksModel

for example:
applications/app/tests/controllers/default.py
is
class DefaultController(unittest.TestCase)

BEWARE that the name is NOT in plural (controllers->Controller)

15 16 17 18
require: 
pip install unittest2
pip install unittest-xml-reporting

Marc Duez's avatar
Marc Duez committed
19
Execute with:
20
>   python web2py.py -S vidjil -M -R testRunner.py
Marc Duez's avatar
Marc Duez committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35



made using web2py slices (http://www.web2pyslices.com) 1392 and 1465 from 

Jon Vlachoyiannis
jon@emotionull.com

Nico de Groot
ndegroot0@gmail.com

"""



36

37 38
from __future__ import print_function

Marc Duez's avatar
Marc Duez committed
39
from gluon import current
Marc Duez's avatar
Marc Duez committed
40
import gluon
Marc Duez's avatar
Marc Duez committed
41 42
from gluon.tools import Auth
import unittest
43
import xmlrunner
Marc Duez's avatar
Marc Duez committed
44 45 46 47 48 49 50
import glob
import sys
import doctest
import os
import traceback
import shutil
from copy import copy
51
import vidjil_utils
Marc Duez's avatar
Marc Duez committed
52

53
execfile("applications/vidjil/controllers/default.py", globals())
Marc Duez's avatar
Marc Duez committed
54 55 56


# create a test database by copying the original db
57 58
test_db = DAL('sqlite://testing1234.sqlite')

Marc Duez's avatar
Marc Duez committed
59
for tablename in db.tables:  # Copy tables!
60
    table_copy = [f.clone() for f in db[tablename]]
Marc Duez's avatar
Marc Duez committed
61 62 63
    test_db.define_table(tablename, *table_copy)

db = test_db
64
auth.db = test_db
Marc Duez's avatar
Marc Duez committed
65

66 67
# build default database if doesn't exist
vidjil_utils.init_db_helper(db, auth, True)
Marc Duez's avatar
Marc Duez committed
68

69 70
fake_sample_set_id = db.sample_set.insert(sample_type = 'patient')

71 72 73 74
# add a fake group
fake_group_id = db.auth_group.insert(role="test_group_0", description="test group")

db.auth_permission.insert(group_id=fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
75
                            name=PermissionEnum.access.value,
76 77 78
                            table_name="sample_set",
                            record_id=fake_sample_set_id)
db.auth_permission.insert(group_id=fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
79
                            name=PermissionEnum.access.value,
80 81 82
                            table_name="auth_group",
                            record_id=fake_group_id)

83
# use a fake user
Marc Duez's avatar
Marc Duez committed
84 85 86 87
user_id = db.auth_user.insert(
    first_name='Testers',
    last_name='Inc',
    email='test@vidjil.org',
88
    password= db.auth_user.password.validate('1234')[0],
Marc Duez's avatar
Marc Duez committed
89 90 91 92 93 94 95 96
)
unique_group = db.auth_group.insert(role="user_"+str(user_id), description=" ")
db.auth_membership.insert(user_id=user_id, group_id=unique_group)

# with admin privilege
group_id = 1 #admin group
db.auth_membership.insert(user_id=user_id, group_id=group_id)

Marc Duez's avatar
Marc Duez committed
97

98 99 100 101 102 103 104
# add fake config
fake_config_id = db.config.insert(name="config_test_popipo",
                                    info="popapipapo",
                                    command="-plop",
                                    fuse_command="-plop",
                                    program="plop.cpp"
                                    )
105 106 107 108 109

fake_pre_process_id = db.pre_process.insert(name="foobar",
					    command="cat &file1& &file2& > &result&",
					    info="barfoo"
				            )
Marc Duez's avatar
Marc Duez committed
110
                                    
111
db.auth_permission.insert(group_id = fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
112
                        name = PermissionEnum.admin.value,
113 114
                        table_name = "sample_set",
                        record_id = 0
Marc Duez's avatar
Marc Duez committed
115
                        )
116
db.auth_permission.insert(group_id = fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
117
                        name = PermissionEnum.read.value,
118 119 120
                        table_name = "sample_set",
                        record_id = 0
                        )
121 122 123 124
db.auth_permission.insert(group_id=fake_group_id,
                        name=PermissionEnum.access.value,
                        table_name="sample_set",
                        record_id=0)
125
db.auth_permission.insert(group_id = fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
126
                        name = PermissionEnum.access.value,
Marc Duez's avatar
Marc Duez committed
127 128 129
                        table_name = "config",
                        record_id = fake_config_id
                        )
130

131 132 133 134 135 136
# add fake patient
fake_patient_id = db.patient.insert(first_name="plop",
                                   last_name="plop",
                                   birth="1902-02-02",
                                   info="plop",
                                   id_label="plop",
137 138
                                   creator=user_id,
				   sample_set_id=fake_sample_set_id)
139

140
db.auth_permission.insert(group_id = fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
141
                        name = PermissionEnum.access.value,
Marc Duez's avatar
Marc Duez committed
142 143 144
                        table_name = "patient",
                        record_id = fake_patient_id
                        )
145

146 147 148 149 150 151 152 153 154 155 156 157 158
# for permission testing
permission_sample_set = db.sample_set.insert(sample_type="patient")
permission_patient = db.patient.insert(first_name="foo",
                                      last_name="bar",
                                      info="permission",
                                      sample_set_id=permission_sample_set)

db.auth_permission.insert(group_id = fake_group_id,
                          name = PermissionEnum.access.value,
                          table_name = "patient",
                          record_id = permission_patient
                          )

159 160 161 162 163 164 165
fake_run_id = db.run.insert(name="foobar",
                            run_date="2010-10-25",
                            info="foobar",
                            id_label="foobar",
                            creator=user_id,
                            sample_set_id=fake_sample_set_id)

166
db.auth_permission.insert(group_id = fake_group_id,
HERBERT Ryan's avatar
HERBERT Ryan committed
167
                        name = PermissionEnum.access.value,
168
                        table_name = 'run',
169 170
                        record_id = fake_run_id)

171 172 173 174 175 176 177 178 179
# and a fake file for this patient
fake_file_id = db.sequence_file.insert(sampling_date="1903-02-02",
                                    info="plop",
                                    pcr="plop",
                                    sequencer="plop",
                                    producer="plop",
                                    filename="plop",
                                    provider=user_id)

180 181 182 183
fake_sample_set_membership = db.sample_set_membership.insert(sample_set_id = fake_sample_set_id,
				    sequence_file_id = fake_file_id
)

184 185 186 187 188 189 190 191 192 193 194 195
fake_file_id2 = db.sequence_file.insert(sampling_date="1903-02-02",
                                    info="plop2",
                                    pcr="plop2",
                                    sequencer="plop2",
                                    producer="plop2",
                                    filename="plop2",
                                    provider=user_id)

fake_sample_set_membership2 = db.sample_set_membership.insert(sample_set_id = fake_sample_set_id,
				    sequence_file_id = fake_file_id2
)

196 197 198 199 200 201 202
# and a fake result for this file
stream = open("../../doc/analysis-example.vidjil", 'rb')
fake_result_id = db.results_file.insert(sequence_file_id = fake_file_id,
                                    config_id = fake_config_id,
                                    run_date = "2014-09-19 00:00:00",
                                    data_file = db.results_file.data_file.store(stream, "plop.data")
                                    )
203

204
stream.seek(0)
205 206 207 208 209 210
fake_result_id2 = db.results_file.insert(sequence_file_id = fake_file_id2,
                                    config_id = fake_config_id,
                                    run_date = "2014-09-19 00:00:00",
                                    data_file = db.results_file.data_file.store(stream, "plop.data")
                                    )

211
stream.seek(0)
212
fake_fused_id = db.fused_file.insert(sample_set_id = fake_sample_set_id,
213 214 215 216
                                    config_id = fake_config_id,
                                    fuse_date = "2014-09-19 00:00:00",
                                    fused_file = db.fused_file.fused_file.store(stream, "plop.data")
                                    )
217 218 219 220 221 222 223 224

fake_notification_id = db.notification.insert(title='test',
                                            message_content='test',
                                            message_type='login',
                                            priority='header',
                                            expiration='1990-01-01',
                                            creator=user_id
                                            )
225 226 227 228 229 230

fake_mail_preference_id = db.user_preference.insert(user_id =user_id,
                                                    preference='mail',
                                                    val=fake_notification_id
                                                    )

231 232
fake_task_id = db.scheduler_task.insert(application_name='vidjil_test',
                                        task_name='test_task')
233 234 235 236 237 238 239

first_fake_tag_id = db.tag.insert(name="first_fake_tag")
sec_fake_tag_id = db.tag.insert(name="sec_fake_tag")

db.group_tag.insert(group_id=unique_group, tag_id=first_fake_tag_id)
db.group_tag.insert(group_id=fake_group_id, tag_id=sec_fake_tag_id)

240
db.commit()
Marc Duez's avatar
Marc Duez committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276


#fake log to avoid polluting log
#
'''
def _init_log2():
    import logging

    logger = logging.getLogger('vidjil') # (request.application)
    if not logger.handlers:
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('[%(process)d] %(asctime)s %(levelname)8s - %(filename)s:%(lineno)d\t%(message)s')

        handler = logging.FileHandler('applications/vidjil/tests/vidjil-debug.log')
        handler.setLevel(logging.DEBUG)
        handler.setFormatter(formatter)
        logger.addHandler(handler) 

        handler = logging.FileHandler('applications/vidjil/tests/vidjil.log')
        handler.setLevel(logging.INFO)
        handler.setFormatter(formatter)
        logger.addHandler(handler) 

        logger.info("Creating logger")
    return MsgUserAdapter(logger, {})

log = _init_log2()
'''




suite = unittest.TestSuite()

def showfeedback():
    exc_type, exc_value, exc_traceback = sys.exc_info()
277
    print('-'*60)
Marc Duez's avatar
Marc Duez committed
278
    for line in traceback.format_exception(exc_type, exc_value,exc_traceback):
279 280
        print(line[:-1])
    print('-'*60)
Marc Duez's avatar
Marc Duez committed
281 282 283 284 285 286 287


def custom_execfile(test_file):
    try:
        sys.path.append(os.path.split(test_file)[0]) # to support imports form current folder in the testfiles
        g=copy(globals())
        execfile(test_file, g) 
288
    except (OSError,ValueError,SystemExit):
Marc Duez's avatar
Marc Duez committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        pass # we know about the rotating logger error...
             # and SystemExit is not useful to detect
    except:
        showfeedback()
    return g

appname = 'vidjil'






# find doctests in controller
path=os.path.join('applications',appname,'controllers','*.py')
doc_test_files = glob.glob(path)
305
print(len(doc_test_files)," controller files with possible doctests found.")
Marc Duez's avatar
Marc Duez committed
306 307

# get all files with tests
308
test_files = glob.glob('applications/'+appname+'/tests/unit/*/*.py')
Marc Duez's avatar
Marc Duez committed
309 310 311 312 313

if not len(test_files):
    raise Exception("No files found for app: " + sys.argv[2])

# Bring in all doc tests and submit them
314
print("Run doctests" if doc_test_files else "No doctests")
Marc Duez's avatar
Marc Duez committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
for f in  doc_test_files:
    g=custom_execfile(f)
    suite.addTest(doctest.DocFileSuite(f, globs=g,
            module_relative=False))
    
# Bring all unit tests in and their controllers/models/whatever
for test_file in test_files:
    execfile(test_file, globals())

    # Create the appropriate class name based on filename and path
    filename =  str.capitalize(test_file.split("/")[-1][:-3])
    directory =  str.capitalize(test_file.split("/")[-2][:-1])

    suite.addTest(unittest.makeSuite(globals()[filename+directory]))

330
#unittest.TextTestRunner(verbosity=2).run(suite)
331 332 333
result = xmlrunner.XMLTestRunner(output='test-reports', verbosity=1).run(suite)

sys.exit(not result.wasSuccessful())