import sys, os sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) import pkg_resources pkg_resources.require('SQLObject2') import py import sqlobject connection_shortcuts = { 'mysql': 'mysql://test@localhost/test', 'postgres': 'postgres:///test', 'postgresql': 'postgres:///test', 'pygresql': 'pygresql://localhost/test', 'sqlite': 'sqlite:/:memory:', 'sybase': 'sybase://test:test123@sybase/test?autoCommit=0', 'firebird': 'firebird://sysdba:masterkey@localhost/var/lib/firebird/data/test.gdb', 'mssql': 'mssql://sa:@127.0.0.1/test' } Option = py.test.Config.Option option = py.test.Config.addoptions( "SQLObject options", # @@: Why is this long option capitalized? Option('-D', '--database', action="store", dest="database", default='sqlite', help="The database to run the tests under (default sqlite). " "Can also use an alias from: %s" % (', '.join(connection_shortcuts.keys()))), Option('-S', '--sql', action="store_true", dest="show_sql", default=False, help="Show SQL queries"), Option('-O', '--output', action='store_true', dest='show_output', default=False, help="Show SQL output"), ) ## This disables the "testing" of Test* SQLObject subclasses class SQLObjectClass(py.test.collect.Class): """ This surpresses trying to run any subclass of SQLObject as a test (even if the name starts with Test). """ def run(self): if (isinstance(self.obj, type) and issubclass(self.obj, sqlobject.SQLObject)): return [] return super(SQLObjectClass, self).run() Class = SQLObjectClass class Directory(py.test.collect.Directory): def __init__(self, *args, **kw): py.test.collect.Directory.__init__(self, *args, **kw) setup_tests() is_setup = False def setup_tests(): global is_setup if is_setup: return is_setup = True conn_uri = option.database conn_uri = connection_shortcuts.get(conn_uri, conn_uri) sqlobject.sqlhub.push_process_conn(conn_uri) conn = sqlobject.sqlhub.get_connection() if option.show_sql or option.show_output: class Logger: def write(self, op, args=None): if args is not None: op = op % args print op def dummy(self, op, args=None): pass info = warn = error = write if option.show_output: debug = write else: debug = dummy conn.logger = Logger()