Commits

Michael Trier committed d413cee

Refactored the mock_engine in the tests so it's not duplicated in several places. Closes #1098

  • Participants
  • Parent commits 6ee8fb4

Comments (0)

Files changed (3)

File test/engine/ddlevents.py

 from sqlalchemy import create_engine
 from testlib.sa import MetaData, Table, Column, Integer, String
 import testlib.sa as tsa
-from testlib import TestBase, testing
+from testlib import TestBase, testing, engines
 
 
 class DDLEventTest(TestBase):
             assert bind is self.bind
             self.state = action
 
-    def mock_engine(self):
-        buffer = []
-        def executor(sql, *a, **kw):
-            buffer.append(sql)
-        engine = create_engine(testing.db.name + '://',
-                               strategy='mock', executor=executor)
-        assert not hasattr(engine, 'mock')
-        engine.mock = buffer
-        return engine
-
     def setUp(self):
-        self.bind = self.mock_engine()
+        self.bind = engines.mock_engine()
         self.metadata = MetaData()
         self.table = Table('t', self.metadata, Column('id', Integer))
 
 
 
 class DDLExecutionTest(TestBase):
-    def mock_engine(self):
-        buffer = []
-        def executor(sql, *a, **kw):
-            buffer.append(sql)
-        engine = create_engine(testing.db.name + '://',
-                               strategy='mock', executor=executor)
-        assert not hasattr(engine, 'mock')
-        engine.mock = buffer
-        return engine
-
     def setUp(self):
-        self.engine = self.mock_engine()
+        self.engine = engines.mock_engine()
         self.metadata = MetaData(self.engine)
         self.users = Table('users', self.metadata,
                            Column('user_id', Integer, primary_key=True),

File test/engine/metadata.py

 import testenv; testenv.configure_for_tests()
 import pickle
-from sqlalchemy import MetaData, create_engine
+from sqlalchemy import MetaData
 from testlib.sa import Table, Column, Integer, String, UniqueConstraint, \
      CheckConstraint, ForeignKey
 import testlib.sa as tsa
-from testlib import TestBase, ComparesTables, testing
+from testlib import TestBase, ComparesTables, testing, engines
 
 
 class MetaDataTest(TestBase, ComparesTables):
 
 
 class TableOptionsTest(TestBase):
-    def mock_engine(self):
-        buffer = []
-        def executor(sql, *a, **kw):
-            buffer.append(sql)
-        engine = create_engine(testing.db.name + '://',
-                               strategy='mock', executor=executor)
-        assert not hasattr(engine, 'mock')
-        engine.mock = buffer
-        return engine
-
     def setUp(self):
-        self.engine = self.mock_engine()
+        self.engine = engines.mock_engine()
         self.metadata = MetaData(self.engine)
 
     def test_prefixes(self):

File test/testlib/engines.py

 import sys, types, weakref
-from testlib import config
+from sqlalchemy import create_engine
+from testlib import config, testing
 from testlib.compat import set, _function_named, deque
 
-
 class ConnectionKiller(object):
     def __init__(self):
         self.proxy_refs = weakref.WeakKeyDictionary()
 
     return testing_engine(url, options)
 
+def mock_engine(db=None):
+    """Provides a mocking engine based on the current testing.db."""
+    
+    dbi = db or testing.db
+    buffer = []
+    def executor(sql, *a, **kw):
+        buffer.append(sql)
+    engine = create_engine(dbi.name + '://',
+                           strategy='mock', executor=executor)
+    assert not hasattr(engine, 'mock')
+    engine.mock = buffer
+    return engine
 
 class ReplayableSession(object):
     """A simple record/playback tool.