Fixture + SQLAlchemy
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.
DataSetクラスはなんらかの格納媒体(以下ORMapper)例えばDataMapperやActiveRecordパターンで実装されたオブジェクトによってロードされる。Fixtureは正しいオブジェクトを使って、どのようにデータをロードするかを知っている環境だ。内部的にはDataSetの行と列は単純にORMapperに渡されて、ORMapperがデータを保存する。
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クラスは多くの異なるタイプのデータベースやストレージメディアに対し、どのように接続するかを知っているサードパーティーのライブラリにフックすることで、それらをサポートするようデザインされている。後のほうに独自の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 を参考にデータセットとテストコードを書いた。
mytable.py
#coding:utf-8 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, autoflush=True, autocommit=False)) 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('usr.id'), nullable=False) name = sa.Column(sa.types.String)
mydataset.py
#coding:utf-8 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 = [AddressData.tokyo] class tom_bar: name = 'tom bar' addresses = [AddressData.tronto, AddressData.newyork]
mytest.py
#coding:utf-8 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}, engine=engine, ) class TestUsr(unittest.TestCase, DataTestCase): fixture = dbfixture datasets = [UsrData, AddressData] def setUp(self): #Session.remove() metadata.create_all() # AddressData loads implicitly. But keep explicitrly. self.data = self.fixture.data(UsrData, AddressData) self.data.setup() def test_usr_in(self): bob = Session.query(Usr).\ filter(Usr.name == UsrData.bob_foo.name).one() tom = Session.query(Usr).\ filter(Usr.name == UsrData.tom_bar.name).one() assert bob assert tom assert AddressData.tokyo.name in \ [adr.name for adr in bob.addresses] assert AddressData.newyork.name in \ [adr.name for adr in tom.addresses] assert AddressData.tronto.name in \ [adr.name for adr in tom.addresses] def tearDown(self): metadata.drop_all()
run the test.
% nosetests -v mytest.py test_usr_in (mytest.TestUsr) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.026s OK
予めテストデータをDataSetとして定義しておくと、assersionでのハードコーディングが減るのでとてもテストが書きやすい。
Posted: May 13th, 2010 | Author: yamakk | Filed under: 技術 | Tags: database, fixture, python, sqlalchemy, test | No Comments »
Leave a Reply