# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------
"""
Helper functions for the test suite.
(c) Sebastien Bigaret 2002
$Id: utils.py 932 2004-07-20 06:21:57Z sbigaret $
"""
# inspired by TAL tests.utils
__version__='$Revision: 932 $'[11:-2]
import os, sys, unittest
#
def fixpath(include_testPackages=1):
"""
Prepends directory ../.. to sys.path, so that any import from Modeling
imports the modules in this directory, rather then the possibly installed
one: we want to test the framework in this directory.
If include_testPackages is true, we also prepends the testPackages/
directory to sys.path
"""
# mydir is where this file is, codedir is 2 level up
mydir = os.path.abspath(os.path.dirname(__file__))
codedir = os.path.dirname(os.path.dirname(mydir))
if codedir not in sys.path:
sys.path=[codedir]+sys.path
# add the testPackages directory to the python path
if include_testPackages:
testPackageDir=os.path.join(mydir, 'testPackages')
if testPackageDir not in sys.path:
sys.path=[testPackageDir]+sys.path
def dynamically_build_test_packages(argv):
"""
Uses Modeling.dynamic to dynamically create the test packages, depending on
the values stored in the list 'argv':
- if argv contains neither '-c', '-m' nor '-M', simply return
- otherwise, load the models AuthorBooks and StoreEmployees, then:
- if '-c' is in argv, dynamic.build(define_properties=0) the package
- if '-C' is in argv, dynamic.build(define_properties=1) the package
- if '-m' is in argv, dynamic.build_with_metaclass(define_properties=0)
the package
- if '-M' is in argv, dynamic.build_with_metaclass(define_properties=1)
the package
when this method returns, either '-c', '-C', '-m' or '-M' is removed from
the list 'argv'.
See the tests usage(), dynamic.build(), dynamic.build_with_metaclass()
"""
if '-c' not in argv and '-C' not in argv \
and '-m' not in argv and '-M' not in argv:
return
from Modeling import ModelSet,Model
from Modeling.dynamic import build,build_with_metaclass
verbose='-v' in argv or '-V' in argv
model1=Model.searchModel('AuthorBooks', 'xmlmodels', verbose=verbose)
model2=Model.searchModel('StoreEmployees', 'xmlmodels', verbose=verbose)
ModelSet.defaultModelSet().addModel(model1)
ModelSet.defaultModelSet().addModel(model2)
if '-c' in argv:
build(model1, define_properties=0)
build(model2, define_properties=0)
argv.remove('-c')
if '-C' in argv:
build(model1, define_properties=1)
build(model2, define_properties=1)
argv.remove('-C')
elif '-m' in argv:
build_with_metaclass(model1, define_properties=0, verbose=verbose)
build_with_metaclass(model2, define_properties=0, verbose=verbose)
argv.remove('-m')
elif '-M' in argv:
build_with_metaclass(model1, define_properties=1, verbose=verbose)
build_with_metaclass(model2, define_properties=1, verbose=verbose)
argv.remove('-M')
def run_suite(suite, outf=sys.stdout, errf=None, verbosity=0):
"""
Runs the suite.
Verbosity: 0: not verbose, 1: just print points, >1: verbose
"""
runner = unittest.TextTestRunner(outf, verbosity=verbosity)
result = runner.run(suite)
newerrs = len(result.errors) + len(result.failures)
if newerrs:
print "'Errors' indicate exceptions other than AssertionError."
print "'Failures' indicate AssertionError"
if errf is None:
errf = sys.stderr
errf.write("%d errors, %d failures\n"
% (len(result.errors), len(result.failures)))
return newerrs
def disable_model_cache():
import os
try:
del os.environ['MDL_ENABLE_SIMPLE_METHOD_CACHE']
except KeyError:
pass
def enable_model_cache_and_compute():
os.environ['MDL_ENABLE_SIMPLE_METHOD_CACHE']='yes'
from Modeling.ModelSet import defaultModelSet
for m in defaultModelSet().models(): # re-enable caching
m.cacheSimpleMethods()
|