views:

132

answers:

1

How do you unit test your python DAL that is using postgresql.

In sqlite you could create in-memory database for every test but this cannot be done for postgresql.

I want a library that could be used to setup a database and clean it once the test is done.

I am using Sqlalchemy as my ORM.

+1  A: 

You can use nose to write your tests, then just use SQLAlchemy to create and clean the test database in your setup/teardown methods.

Luper Rouch
@Luper Rouch: How should I clean up so that database is in the same state once the test is finished. I don't think starting/rollback a transaction is the solution.
StackUnderflow
You can start by dropping the entire database in the teardown.
Luper Rouch
@Luper Rouch: So I would have to create a new database instance for every test.. wont that make tests very slow?.. I would just want to clean all the data in the database... I guess I can write SQL to clean all the table data in teardown.. Though I was looking for something like SQLUnit
StackUnderflow
Yes that will certainly be slower than doing the same in memory with sqlite, but I'm not sure doing the cleanup by hand (truncating tables, resetting sequences, etc...) will be any faster than dropping the entire database... not counting the fact that you add potential bugs to your tests. Regarding SQLUnit, why would you want to write your tests in XML or other inappropriate language when you can write them way shorter in Python ?
Luper Rouch
@Luper Rouch: You r right... SQLUnit is kind of ugly... k.. I will go about creating and dropping dbs.. and if it is slow will thing about other routes.. thanks
StackUnderflow