I ended up finding a solution to this: setting web.ctx.orm manually in
setUp, and removing it in tearDown. I ended up with a Mixin class for
my tests that looks like this:
class
TestDBMixin(object):
    def
setUp(self):
 
db.Base.metadata.create_all(engine)
        web.ctx.orm =
scoped_session(sessionmaker(bind=engine))

    def
tearDown(self):
 
db.Base.metadata.drop_all(engine)
        web.ctx.orm =
None

And test classes that look like this:
class TestUser(unittest.TestCase, TestDBMixin):
    def setUp(self):
        TestDBMixin.setUp(self)

    def tearDown(self):
        TestDBMixin.tearDown(self)

    def
test_add(self):
        u = User('name')
        u.add('pass')

On 2 Hun, 00:04, Ben  Hearsum <[email protected]> wrote:
> Hi,
>
> I'm writing a web application with web.py and sqlalchemy. I've 
> usedhttp://webpy.org/cookbook/sqlalchemyas a guide to get me up and
> running. As well as code like the tutorial, I've got Models that have
> various methods that query the database, and Controllers that consume
> them. The methods in my models like something like this:
> def add(self):
>   web.ctx.orm.add(User('blah'))
>
> They work perfectly fine when called by a Controller, presumably
> because the context is flushed out at that time. However, tests always
> fail with:
> AttributeError: 'ThreadedDict' object has no attribute 'orm'
>
> I've tried all sorts of various hacks and tricks to try and make this
> work, to no avail. I _could_ drop all of failing tests, and rely on
> the associated Controller tests to catch any issues, but this seems
> suboptimal.
>
> Is there a known solution/pattern web.py + sqlalchemy + testing?

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to