@SevenOfNein I have in the past. But not in the last 2 years. And I used Django, and everything “just worked”.
1. Create the test_db database, create the schema, fill in any data you need for every test
2. Create a new database using this database as template: CREATE DATABASE test_db_clean with template test_db;
3. Before each test run drop database test_db; CREATE DATABASE test_db with template test_db_clean;
@SevenOfNein Depending on your test setup, you might need to close all other connections to your test db, other wise dropping and recreating will not work. You can use something like that: SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'test_db' pid <> pg_backend_pid();
@SevenOfNein For actually starting and stopping the database, we use testcontainers-java, there is also a python port: https://github.com/testcontainers/testcontainers-python
@Atoro This sounds very interesting. Thank you! I will give this a try.
@Atoro I just implemented this and it works as intended. However it is very slow (~ 0.25 seconds on my machine per test compared to 0,0001 seconds per test with sqlite)
Do you know of any ways to speed this up?
@SevenOfNein hmm not really. We are using this in our integration tests which have a long runtime anyway.
@Atoro Currently looking at transactionalizing test. Where the test is embedded in a transaction that is rolled back after.
Apparently this is the fastes way. I just need to figure out how this works if the code being tested uses transactions too.
@SevenOfNein Using transaction works very good, if the tested code doesn't use transactions. Couldn't get it to work for code that is using transactions itself.
Maybe truncating all tables before a test would be an idea: https://stackoverflow.com/a/2829485/755330
That should give you an empty database. I can't use that, since I actually need a prefilled database for our integration tests.
@Atoro This was the first thing I tried, but truncating all tables is even slower than copying the database :D
chaos.social – a Fediverse instance for & by the Chaos community