Fixture + SQLAlchemy

Using LoadableFixtureを読む

A DataSet class is loaded via some storage medium, say, an object that implements a Data Mapper or Active Record pattern. A Fixture is an environment that knows how to load data using the right objects. Behind the scenes the rows and columns of the DataSet are simply passed to the storage medium so that it can save the data.


The Fixture class is designed to support many different types of databases and other storage media by hooking into 3rd party libraries that know how to work with that media. There is also a section later about creating your own Fixture.


Fixture is designed for applications that already have a way to store data; the LoadableFixture just hooks in to that interface.

Fixture はデータの格納方法を既にそなえたアプリケーションのためにデザインされている。LoadableFixture はただそのインターフェースにフックしているに過ぎない。
以下An Example of Loading Data Using SQLAlchemy を参考にデータセットとテストコードを書いた。

import sqlalchemy as sa
from sqlalchemy.orm import scoped_session, sessionmaker, relation
from sqlalchemy.ext.declarative import declarative_base

engine = sa.create_engine('sqlite:////tmp/fixture_example.db', echo=True)

#'transactional' flag on sessionmaker() and others is removed.
# Use 'autocommit=True' to indicate 'transactional=False'.
Session = scoped_session(sessionmaker(bind=engine,

Base = declarative_base()
metadata = Base.metadata
metadata.bind = engine

class Usr(Base):
    __tablename__ = 'usr'
    __table_args__ = {'useexisting': True}
    id = sa.Column('id', sa.types.Integer, primary_key=True)
    addresses = relation('Address', backref='usr',
                      cascade='all, delete, delete-orphan')
    name = sa.Column(sa.types.String, unique=True)

class Address(Base):
    __tablename__ = 'address'
    __table_args__ = {'useexisting': True}
    id = sa.Column('id', sa.types.Integer, primary_key=True)
    usr_id = sa.Column(sa.types.Integer, sa.ForeignKey(''),
    name = sa.Column(sa.types.String)

from fixture import DataSet

class AddressData(DataSet):

    class tokyo:
        name = 'tokyo Japan'

    class tronto:
        name = 'toronto Canada'

    class newyork:
        name = 'new york US'

class UsrData(DataSet):

    class bob_foo:
        name = 'bob foo'
        addresses = []

    class tom_bar:
        name = 'tom bar'
        addresses = [AddressData.tronto, AddressData.newyork]

import unittest
from fixture import SQLAlchemyFixture, DataTestCase
from mytable import Usr, Address, engine, Session, metadata
from mydataset import UsrData, AddressData

dbfixture = SQLAlchemyFixture(
     env={'UsrData': Usr, 'AddressData': Address},

class TestUsr(unittest.TestCase, DataTestCase):
    fixture = dbfixture
    datasets = [UsrData, AddressData]

    def setUp(self):
        # AddressData loads implicitly. But keep explicitrly. =, AddressData)

    def test_usr_in(self):
        bob = Session.query(Usr).\
            filter( ==
        tom = Session.query(Usr).\
            filter( ==
        assert bob
        assert tom
        assert in \
            [ for adr in bob.addresses]
        assert in \
            [ for adr in tom.addresses]
        assert in \
            [ for adr in tom.addresses]

    def tearDown(self):

run the test.

% nosetests -v 
test_usr_in (mytest.TestUsr) ... ok

Ran 1 test in 0.026s



Posted: May 13th, 2010 | Author: | Filed under: 技術 | Tags: , , , , | No Comments »

Leave a Reply