tests: simplify the sqlalchemy tests
Move the imports and class definitions to the top-level so that
we don't need to use "global".
@@ -6,24 +6,15 @@ import jsonpickle
from helper import SkippableTest
-
-Table = None
-
-
-class SQLAlchemyTestCase(SkippableTest):
-
- def setUp(self):
- global Table
-
try:
import sqlalchemy as sqa
- self.should_skip = False
- except ImportError:
- self.should_skip = True
- return
-
from sqlalchemy.ext import declarative
from sqlalchemy.orm import Session
+ HAS_SQA = True
+except ImportError:
+ HAS_SQA = False
+
+if HAS_SQA:
Base = declarative.declarative_base()
@@ -33,15 +24,20 @@ class SQLAlchemyTestCase(SkippableTest):
name = sqa.Column(sqa.Text)
value = sqa.Column(sqa.Float)
- url = 'sqlite:///:memory:'
- self.engine = engine = sqa.create_engine(url)
- Base.metadata.drop_all(engine)
- Base.metadata.create_all(engine)
+class SQLAlchemyTestCase(SkippableTest):
+
+ def setUp(self):
+ """Create a new sqlalchemy engine for the test"""
- self.Base = Base
- self.Session = Session
- self.sqa = sqa
+ if HAS_SQA:
+ url = 'sqlite:///:memory:'
+ self.engine = sqa.create_engine(url)
+ Base.metadata.drop_all(self.engine)
+ Base.metadata.create_all(self.engine)
+ self.should_skip = False
+ else:
+ self.should_skip = True
def test_sqlalchemy_roundtrip_with_detached_session(self):
"""Test cloned SQLAlchemy objects detached from any session"""
@@ -51,7 +47,7 @@ class SQLAlchemyTestCase(SkippableTest):
expect = Table(name='coolness', value=11.0)
- session = self.Session(bind=self.engine, expire_on_commit=False)
+ session = Session(bind=self.engine, expire_on_commit=False)
session.add(expect)
session.commit()
@@ -75,7 +71,7 @@ class SQLAlchemyTestCase(SkippableTest):
expect = Table(name='coolness', value=11.0)
- session = self.Session(bind=self.engine, expire_on_commit=False)
+ session = Session(bind=self.engine, expire_on_commit=False)
session.add(expect)
session.commit()
