|
|
|
|
|
by joevandyk
4428 days ago
|
|
A problem with running all your tests in a single transaction is that that's not actually what happens when your code is ran. You will have multiple transactions (unless for some reason you wrap ever single web request inside a transaction, which I think is a terrible idea). There's slightly different things that happen: now() will always return the same time, deferrable constraints/triggers are useless, you can't have another database connection looking at the test results or modifying the database (say you are testing deadlocks or concurrent updates, or you have code that opens a new database connection to write data to the database outside the current transaction), etc. It's fine for simple, vanilla ActiveRecord use where you aren't using lots of database features, I suppose. |
|
* at the start of a test run, create a new database, load the schema into it, load all the 'global' data that all the tests need into the database.
* write that data to a sql file using pg_dump --inserts -a
* before each test runs, I disable triggers (with set session_replication_role=replica), then delete all the data from each table, then load the data from the sql file back into the database.
This allows me to have data quickly cleaned out and restored on every test run and gives me real transactions during tests.