Created
April 24, 2012 07:23
-
-
Save ento/2477429 to your computer and use it in GitHub Desktop.
Getting the model instance from Python's fixture dataset row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| import sqlalchemy as sa | |
| from sqlalchemy import MetaData | |
| from sqlalchemy import orm, create_engine | |
| from sqlalchemy.orm import scoped_session, sessionmaker | |
| from fixture import DataSet | |
| from fixture import SQLAlchemyFixture | |
| from fixture.style import NamedDataStyle | |
| log = logging.getLogger(__name__) | |
| ##### SQLAlchemy stuff | |
| # SQLAlchemy database engine. | |
| engine = create_engine('sqlite://') | |
| # SQLAlchemy session manager. | |
| Session = scoped_session(sessionmaker()) | |
| # Global metadata. If you have multiple databases with overlapping table | |
| # names, you'll need a metadata for each database | |
| metadata = MetaData() | |
| t_people = sa.Table('people', metadata, | |
| sa.Column('id', sa.types.Integer, primary_key=True), | |
| sa.Column('name', sa.types.String(100)), | |
| sa.Column('email', sa.types.String(100)) | |
| ) | |
| t_addresses_people = sa.Table('addresses_people', metadata, | |
| sa.Column('id', sa.types.Integer, primary_key=True), | |
| sa.Column('person_id', sa.types.Integer, sa.ForeignKey('people.id')), | |
| sa.Column('address_id', sa.types.Integer, sa.ForeignKey('addresses.id')) | |
| ) | |
| t_addresses = sa.Table('addresses', metadata, | |
| sa.Column('id', sa.types.Integer, primary_key=True), | |
| sa.Column('address', sa.types.String(100)) | |
| ) | |
| class Person(object): | |
| @property | |
| def namecard(self): | |
| return '%s <%s>' % (self.name, self.email) | |
| class Address(object): | |
| pass | |
| orm.mapper(Address, t_addresses) | |
| orm.mapper(Person, t_people, properties = { | |
| 'my_addresses' : orm.relation(Address, secondary = t_addresses_people), | |
| }) | |
| ##### fixture data | |
| class AddressData(DataSet): | |
| class joe_in_montego: | |
| address = "111 St. James St, Montego Bay, Jamaica" | |
| class joe_in_ny: | |
| address = "111 S. 2nd Ave, New York, NY" | |
| class PersonData(DataSet): | |
| class joe_gibbs: | |
| name = "Joe Gibbs" | |
| email = "joe@joegibbs.com" | |
| my_addresses = [ | |
| AddressData.joe_in_montego, | |
| AddressData.joe_in_ny] | |
| db = SQLAlchemyFixture( | |
| env=globals(), style=NamedDataStyle(), | |
| engine=engine) | |
| ##### 'The' function to get the model instance | |
| def Model(dataset_class): | |
| loaded = db.loaded[dataset_class._dataset] | |
| datarow = getattr(loaded, dataset_class.__name__) | |
| return datarow._dataset.meta._stored_objects.get_object(datarow._key) | |
| def test(): | |
| log.info("Creating tables") | |
| # Create the tables if they don't already exist | |
| metadata.create_all(bind=engine) | |
| log.info("Successfully setup") | |
| # load some initial data during setup-app : | |
| data = db.data(PersonData) | |
| log.info("Inserting initial data") | |
| data.setup() | |
| log.info("Done") | |
| print Model(PersonData.joe_gibbs) | |
| print Model(PersonData.joe_gibbs).namecard | |
| if __name__ == '__main__': | |
| test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment