From e9f51b2456d7ddfaee6acb6efe8abdfeded6d72f Mon Sep 17 00:00:00 2001 From: Jason Kirtland Date: Fri, 27 Jul 2007 20:21:36 +0000 Subject: [PATCH] Fix coverage again- try really hard not to load anything from lib/ until after the coverage hook runs. --- test/testlib/config.py | 1 + test/testlib/profiling.py | 1 + test/testlib/schema.py | 10 +++++++++- test/testlib/testing.py | 41 +++++++++++++++------------------------ 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/test/testlib/config.py b/test/testlib/config.py index f05cda46d3..987832b11c 100644 --- a/test/testlib/config.py +++ b/test/testlib/config.py @@ -1,3 +1,4 @@ +import testbase import optparse, os, sys, ConfigParser, StringIO logging, require = None, None diff --git a/test/testlib/profiling.py b/test/testlib/profiling.py index 697df4ea2e..efcc1340fb 100644 --- a/test/testlib/profiling.py +++ b/test/testlib/profiling.py @@ -1,5 +1,6 @@ """Profiling support for unit and performance tests.""" +import testbase from testlib.config import parser, post_configure import testlib.config diff --git a/test/testlib/schema.py b/test/testlib/schema.py index a2fc912650..6ca15bb6c4 100644 --- a/test/testlib/schema.py +++ b/test/testlib/schema.py @@ -1,5 +1,5 @@ import testbase -from sqlalchemy import schema +schema = None __all__ = 'Table', 'Column', @@ -8,6 +8,10 @@ table_options = {} def Table(*args, **kw): """A schema.Table wrapper/hook for dialect-specific tweaks.""" + global schema + if schema is None: + from sqlalchemy import schema + test_opts = dict([(k,kw.pop(k)) for k in kw.keys() if k.startswith('test_')]) @@ -23,6 +27,10 @@ def Table(*args, **kw): def Column(*args, **kw): """A schema.Column wrapper/hook for dialect-specific tweaks.""" + global schema + if schema is None: + from sqlalchemy import schema + # TODO: a Column that creates a Sequence automatically for PK columns, # which would help Oracle tests return schema.Column(*args, **kw) diff --git a/test/testlib/testing.py b/test/testlib/testing.py index 213772e9e1..9b92f46d5c 100644 --- a/test/testlib/testing.py +++ b/test/testlib/testing.py @@ -2,11 +2,12 @@ # monkeypatches unittest.TestLoader.suiteClass at import time +import testbase import unittest, re, sys, os from cStringIO import StringIO -from sqlalchemy import MetaData, sql -from sqlalchemy.orm import clear_mappers import testlib.config as config +sql, Metadata, clear_mappers = None, None, None + __all__ = 'PersistTest', 'AssertMixin', 'ORMTest' @@ -70,6 +71,10 @@ class ExecutionContextWrapper(object): can be tracked.""" def __init__(self, ctx): + global sql + if sql is None: + from sqlalchemy import sql + self.__dict__['ctx'] = ctx def __getattr__(self, key): return getattr(self.ctx, key) @@ -228,7 +233,11 @@ class ORMTest(AssertMixin): keep_data = False def setUpAll(self): - global _otest_metadata + global MetaData, _otest_metadata + + if MetaData is None: + from sqlalchemy import MetaData + _otest_metadata = MetaData(config.db) self.define_tables(_otest_metadata) _otest_metadata.create_all() @@ -248,6 +257,9 @@ class ORMTest(AssertMixin): _otest_metadata.drop_all() def tearDown(self): + if clear_mappers is None: + from sqlalchemy.orm import clear_mappers + if not self.keep_mappers: clear_mappers() if not self.keep_data: @@ -305,30 +317,9 @@ class TTestSuite(unittest.TestSuite): return (exctype, excvalue, tb) return (exctype, excvalue, tb) +# monkeypatch unittest.TestLoader.suiteClass = TTestSuite -def _iter_covered_files(): - import sqlalchemy - for rec in os.walk(os.path.dirname(sqlalchemy.__file__)): - for x in rec[2]: - if x.endswith('.py'): - yield os.path.join(rec[0], x) - -def cover(callable_, file_=None): - from testlib import coverage - coverage_client = coverage.the_coverage - coverage_client.get_ready() - coverage_client.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') - coverage_client.erase() - coverage_client.start() - try: - return callable_() - finally: - coverage_client.stop() - coverage_client.save() - coverage_client.report(list(_iter_covered_files()), - show_missing=False, ignore_errors=False, - file=file_) class DevNullWriter(object): def write(self, msg): -- 2.47.3