Any access of session-bound objects after a commit() are going to access the database, which means the Session starts a new transaction.
Close out the transactional resources held by your Session after each test ends. Otherwise PG maintains locks on each table that has been accessed in the current transaction and the tables will not be able to be dropped. In a unit test scenario, the closing out of the Session is typically part of the tearDown(). http://www.sqlalchemy.org/docs/orm/session.html#closing On Jan 9, 2011, at 6:24 AM, Arturo Sevilla wrote: > Hello, > > I've been trying to do unit testing with SQLAlchemy and PostgreSQL. In > my model/tables I have two entities state and country, state has a > foreign key relating to country (no problems there). > > In my setUp() method I call create_all() for my metadata while in > tearDown() I call drop_all(). The problem that I was detecting is that > it nosetests frozes when is dropping the "state" table after running > the following test: > > def test_country_create_orm(self): > new_country = Country('Country') > id = new_country.id > session = Session() > session.add(new_country) > session.commit() > #session.flush() > test_country = session.query(Country).get(id) > assert test_country.id == id > > The Session class is generated with sessionmaker() in the following > way (where engine is the parameter that is passed on to init_model of > Pylons, however I tested the same conditions with create_engine): > > sm = orm.sessionmaker(bind=engine, autoflush=True, autocommit=False) > Session = orm.scoped_session(sm) > > I chased the problem through a debugger down onto the implementation > of do_execute for the cursor for the Connection object for PostgreSQL, > after which I strace'd it and discovered that it was being hold on the > poll(2) system call. > > I don't know if I'm doing something wrong but I discovered that my > unit tests work if I configure my Session class as: > > sm = orm.sessionmaker(bind=engine, autoflush=True, autocommit=True) > Session = orm.scoped_session(sm) > > And flush()ing instead of commit()ing. What also works is that if > instead of doing the query > > test_country = session.query(Country).get(id) > > I do this: > > count = session.query(Country).count() > > This doesn't appear to be restricted to the drop_all() method of > MetaData. If I import my Table object that represents my "State" table > and drop it directly it also freezes: > > from myapp.model.table import state > state.drop() # freezes > > I have tested this issue with Python 2.6.6, SQLAlchemy 0.6.5 and > 0.6.6, psycopg2 2.3.1 and 2.3.2 and PostgreSQL 8.4.5. > > Any insights? > > -- > 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. > -- 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.
