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.
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
from sqlalchemy.orm import scoped_session, sessionmaker, relation |
from sqlalchemy.ext.declarative import declarative_base |
Session = scoped_session(sessionmaker(bind = engine, |
Base = declarative_base() |
__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 ) |
__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' ), |
name = sa.Column(sa.types.String) |
mydataset.py
from fixture import DataSet |
class AddressData(DataSet): |
addresses = [AddressData.tokyo] |
addresses = [AddressData.tronto, AddressData.newyork] |
mytest.py
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): |
datasets = [UsrData, AddressData] |
self .data = self .fixture.data(UsrData, AddressData) |
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 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] |
run the test.
test_usr_in (mytest.TestUsr) ... ok |
---------------------------------------------------------------------- |
予めテストデータをDataSetとして定義しておくと、assersionでのハードコーディングが減るのでとてもテストが書きやすい。
Posted: May 13th, 2010 | Author: yamakk | Filed under: 技術 | Tags: database, fixture, python, sqlalchemy, test | No Comments »
Leave a Reply