So far development and tests have been done on SQLite, while production was on Postgres. Now everything needs to run on Postgres, and a large number of tests broke. The reason is, IDs for each test don't start with 1, but continue between tests.
One way how to fix this is to use TransactionTestCase, so changing
class FooCase(APITestCase):
with
class FooCase(APITransactionTestCase):
reset_sequences = True
This works, but it becomes slow
While I can try and fix tests, it is quite difficult because many tests have mocked methods. Is there another way, where I can reset sequences and keep it all fast and tidy?
You can reset sequences before tests using sql:
SELECT setval(sequence_name, 1), sequence_name FROM information_schema.sequences;
But remember the following:
SELECT currval('my_sequence'); --Returns 1
SELECT nextval('my_sequence'); --Returns 2
So, if you call nextval directly on your code, the sequence will return 2, instead of 1.
Also, sequences in postgres are like tables.
SELECT last_value FROM my_sequence; --Returns 1
But this approach bypass currval ACID protections, meaning that isn't concurrency safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With