fixture.loadable.sqlalchemy_loadable
Components for loading and unloading data using SQLAlchemy.
See Using LoadableFixture for examples.
-
class fixture.loadable.sqlalchemy_loadable.SQLAlchemyFixture(engine=None, connection=None, session=None, scoped_session=None, **kw)
Bases: fixture.loadable.loadable.DBLoadableFixture
A fixture that knows how to load DataSet objects into SQLAlchemy objects.
>>> from fixture import SQLAlchemyFixture
The recommended way to deal with connections is to either pass in your own engine object
or let implicit binding govern how connections are made. This is because
SQLAlchemyFixture will use an internally scoped session to avoid conflicts
with that of the Application Under Test. If you need to bypass this behavior then
pass in your own session or scoped_session.
For examples of usage see Using LoadableFixture
Keyword Arguments:
- style
- A Style object to translate names with
- env
- A dict or module that contains either mapped classes or Table objects,
or both. This will be searched when Style
translates DataSet names into
storage media. See EnvLoadableFixture.attach_storage_medium for details on
how env works.
- engine
- A specific connectable/engine object to use when one is not bound.
engine.connect() will be called.
- session
- A session from sqlalchemy.create_session(). See Contextual/Thread-local Sessions
for more info. This will override the
ScopedSession and SessionContext approaches. Only declare a session if you have to.
The preferred way is to let fixture use its own session in a private scope.
- scoped_session
- A class-like Session object created by scoped_session(sessionmaker()).
Only declare a custom Session if you have to. The preferred way
is to let fixture use its own Session which defines a private scope to
avoid conflicts with that of the Application Under Test.
- connection
- A specific connection / engine to use when one is not bound.
- dataclass
- SuperSet class to represent loaded data with
- medium
- A custom StorageMediumAdapter
to instantiate when storing a DataSet.
By default, a medium adapter will be negotiated based on the type of
SQLAlchemy object so you should only set this if you know what you
doing.
-
begin(unloading=False)
Begin loading data
- creates and stores a connection with engine.connect() if an engine was passed
- binds the connection or engine to fixture’s internal session
- uses an unbound internal session if no engine or connection was passed in
-
commit()
- Commit the load transaction and flush the session
-
create_transaction()
Create a session transaction or a connection transaction
- if a custom connection was used, calls connection.begin
- otherwise calls session.begin()
-
dispose()
Dispose of this fixture instance entirely
Closes all connection, session, and transaction objects and calls engine.dispose()
After calling fixture.dispose() you cannot use the fixture instance.
Instead you have to create a new instance like:
fixture = SQLAlchemyFixture(...)
-
rollback()
- Rollback load transaction
-
class fixture.loadable.sqlalchemy_loadable.MappedClassMedium(*a, **kw)
Bases: fixture.loadable.loadable.StorageMediumAdapter
Adapter for SQLAlchemy mapped classes.
For example, in mapper(TheClass, the_table) TheClass is a mapped class.
If using Elixir then any class descending from elixir.Entity is treated like a mapped class.
-
clear(obj)
- Delete this object from the session
-
save(row, column_vals)
- Save a new object to the session if it doesn’t already exist in the session.
-
visit_loader(loader)
- Visits the SQLAlchemyFixture loader and stores a reference to its session
-
class fixture.loadable.sqlalchemy_loadable.TableMedium(*a, **kw)
Bases: fixture.loadable.loadable.StorageMediumAdapter
Adapter for SQLAlchemy Table objects
If no connection or engine is configured in the SQLAlchemyFixture
then statements will be executed directly on the Table object itself which adheres
to implicit connection rules. Otherwise,
the respective connection or engine will be used to execute statements.
-
clear(obj)
- Constructs a delete statement per each primary key and
executes it either explicitly or implicitly
-
save(row, column_vals)
- Constructs an insert statement with the given values and
executes it either explicitly or implicitly
-
visit_loader(loader)
- Visits the SQLAlchemyFixture loader and stores a reference
to its connection if there is one.