On Jan 23, 2008 4:36 PM, Kumar McMillan <[EMAIL PROTECTED]> wrote:
> ...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.

ah, I just had to put the setup/teardown routines in their own
respective transactions too.  Now it passes.  Thanks!  Next... to see
if I can clean it up a bit and fit it into my app.

Passing test:


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)
    mapper(SomeObject, sometable)

    conn = engine.connect()
    PrivateSession.configure(bind=conn)
    trans = conn.begin()
    fixture_session = PrivateSession()
    # create some data to test with :
    so = SomeObject()
    so.keyname = "some unique key name"
    fixture_session.save(so)
    fixture_session.flush()
    trans.commit()

    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
    trans = conn.begin()
    fixture_session.delete(so)
    fixture_session.flush()
    trans.commit()
    rs = fixture_session.query(SomeObject).all()
    assert rs == [], "unexpected: %s" % rs

if __name__ == '__main__':
    test_sa_scoping()

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to