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
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
-
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_pathin a fixtureSET search_pathinto test. One line less convenience, but still acceptable.pool.options().get_max_connections()says 5), soSETonly affects one of thesesearch_pathfor the database, but database name is volatilealter tablequery and execute itNotes:
search_pathfor 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_cataloginsearch_pathis 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