On Mar 11, 2011, at 2:25 PM, Todd Rowell wrote:

> Hi All-
> 
> I'm a long-time Django user who has become accustomed to Django's easy
> generation and loading of test fixtures (i.e., a known database state
> for testing) and I'm looking for something similar for SQLAlchemy.
> 
> I've seen and been working with Kumar's fixture project (http://
> pypi.python.org/pypi/fixture) but it seems largely intended to allow
> loading of simpler, hand-created fixtures; support for creating
> fixtures from an existing database is pretty limited and seems to
> require additional coding instead of just using your existing ORM
> models. It also looks like the export functionality runs a single
> query at a time; not that big a limitation but not as nice as the
> Django fixture generator.
> 
> I've also seen BootAlchemy, but it doesn't seem to provide for
> generating fixtures.


not knowing what a "fixture generator" means, I googled and found this:

https://github.com/alex/django-fixture-generator

Looking at that I don't see *too* much automation - at the very least I see 
explicit usage of models and explicit fixture data on the part of the 
developer.  I'm not really sure what we get when we run "manage.py" though, 
"test_users()" and "test_groups()" already seem to be doing the work that is 
specific to these fixtures.

Perhaps this is one of those 20 foot cultural divide kind of things, but the 
problem of "consistent fixtures" is something I usually address with plain 
coding conventions, though the decorator trickery I see there, which appears to 
mark various fixture-generation methods into some kind of registry, is probably 
not hard to roll either, and would make a nice external project for SQLAlchemy 
if you were inclined.

As to how I approach that kind of thing, building off the transactional example 
at 
http://www.sqlalchemy.org/docs/orm/session.html#joining-a-session-into-an-external-transaction
 , these days I tend to organize fixtures, common assertion methods, into a 
structure like that below.   Mixins define things like transactional behavior 
and common fixture/assertion methods:

class TransactionalTest(object):
    """Mixin which sets up/tears down a transaction"""

    def setUp(self):
        # connect to the database
        self.connection = engine.connect()

        # begin a non-ORM transaction
        self.trans = connection.begin()

        # bind an individual Session to the connection
        self.session = Session(bind=self.connection)

    def tearDown(self):
        # rolls everything back.
        self.session.close()
        self.trans.rollback()
        self.connection.close()

class UserTest(object):
    """A test that deals with User objects."""

    def _user_fixture(self):
        """Example fixture method."""
        self.session.add_all([User(name='ed'),User(name='wendy')])

    def _assert_ed(self, user):
        """Example assertion method."""
        assert user.name == 'ed', "Username is not 'ed'"

class MyTest(UserTest, TransactionalTest, TestCase):

    def test_ed(self):
        self._user_fixture()

        ed = self.session.query(User).filter_by(name='ed').one()
        self._assert_ed(ed)




> 
> Any ideas?
> 
> Thanks,
> Todd
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to