Hi
On Jan 23, 2008 4:07 PM, Michael Bayer <[EMAIL PROTECTED]> wrote:
> your teardown code can't have any dependencies on the test code
> itself. So at the very least start the teardown phase with
> "PrivateSession.close()" so that you start fresh.
I tried adding that to the teardown code but then this assertion fails:
assert so in fixture_session
and if I comment out the assertion, I get the same ConcurrentModificationError
without a stored reference to the object that was saved, I'm not sure
how to delete it [without monkeying with last inserted id].
>
> the two ways to create tests that use isolated data are to either
> create and drop tables local to the unit tests themselves, or to run
> the unit tests within an enclosing Transaction (as in, conn =
> engine.connect(); trans = conn.begin(); session.bind=conn) which is
> rolled back at the end of the unit tests. The SQLA unit tests
> themselves use the former method but I have applied the latter method
> to Pylons tests (and is also what you usually do with Java/Hibernate
> unit tests).
ok, I think I see what you're saying. Removing the
PrivateSession.close(), I tried implementing begin/rollback by
changing the app segment to:
conn = engine.connect()
AppSession.configure(bind=conn)
app_session = AppSession()
trans = conn.begin()
so2 = SomeObject()
so2.keyname = "some unique key name"
app_session.save(so2)
try:
app_session.flush()
except IntegrityError:
# violated unique key
trans.rollback()
else:
trans.commit()
app_session.close()
...but it still fails with the same error, Deleted rowcount 0 does not
match number of objects deleted 1. What am I missing? I don't
understand how the teardown code is dependent on the app code if it is
using a different session and a different connection (now) to save the
same mapped class instances.
Here is the altered test case:
from sqlalchemy import *
from sqlalchemy.orm import scoped_session, sessionmaker, mapper
from sqlalchemy.exceptions import IntegrityError
PrivateSession = scoped_session(
sessionmaker(autoflush=False, transactional=True),
scopefunc=lambda:__name__) # a private scope
AppSession = scoped_session(
sessionmaker(autoflush=False, transactional=True))
dsn = 'sqlite:///:memory:'
def test_sa_scoping():
engine = create_engine(dsn)
metadata = MetaData()
sometable = Table('sometable', metadata,
Column('id', Integer, primary_key=True),
Column('keyname', String(30), unique=True))
class SomeObject(object):
pass
metadata.create_all(bind=engine)
PrivateSession.configure(bind=engine)
mapper(SomeObject, sometable)
fixture_session = PrivateSession()
# create some data to test with :
so = SomeObject()
so.keyname = "some unique key name"
fixture_session.save(so)
fixture_session.flush()
conn = engine.connect()
AppSession.configure(bind=conn)
app_session = AppSession()
trans = conn.begin()
so2 = SomeObject()
so2.keyname = "some unique key name"
app_session.save(so2)
try:
app_session.flush()
except IntegrityError:
# violated unique key
trans.rollback()
else:
trans.commit()
app_session.close()
# after testing application code, I want to tear down
# test even if the app had an error :
assert so in fixture_session
fixture_session.delete(so)
fixture_session.flush()
rs = fixture_session.query(SomeObject).all()
assert rs == [], "unexpected: %s" % rs
if __name__ == '__main__':
test_sa_scoping()
same exception...
sqlalchemy.exceptions.ConcurrentModificationError: Deleted rowcount 0
does not match number of objects deleted 1
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---