Mocking now()
in PostgreSQL
#3657
AMDmi3
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
TL;DR: Use this as a fixture to make PostgreSQL
now()
return fixed timestamp for your tests:Sharing because for me coming to this was not trivial:
CREATE SCHEMA
+CREATE FUNCTION mock_now.now()
+SET search_path
in a fixtureSET search_path
into test. One line less convenience, but still acceptable.pool.options().get_max_connections()
says 5), soSET
only affects one of thesesearch_path
for the database, but database name is volatilealter table
query and execute itNotes:
search_path
for both the database and session, there may be connections opened even before the fixture is applied, and these may be reused by the test. But it seems to work now, even if applied after migrations and other fixtures.pg_catalog
insearch_path
is important, as otherwise it's implicitly prepended to the path, and builtinnow()
still has higher precedence. Had to learn this the hard way too.Beta Was this translation helpful? Give feedback.
All reactions