title: Mocking Postgresql now() function for testing
body: <p>I have the following stack</p>
  
  <ul>
  <li>Node/Express backend</li>
  <li>Postgresql 10 database</li>
  <li>Mocha for testing</li>
  <li>Sinon for mocking</li>
  </ul>
  
  <p>I have written a bunch of end-to-end tests to test all my webservices. The problem is that some of them are time dependent (as in "give me the modified records of the last X seconds"). </p>
  
  <p><code>sinon</code> is pretty good at mocking all the time/dated related stuff in Node, however I have a <code>modified</code> field in my Postgresql tables that is populated with a trigger:</p>
  
  <pre><code>CREATE FUNCTION update_modified_column()
    RETURNS TRIGGER AS $$
  BEGIN
    NEW.modified = now();
    RETURN NEW;
  END;
  $$ LANGUAGE 'plpgsql';
  </code></pre>
  
  <p>The problem of course is that <code>sinon</code> can't override that <code>now()</code> function.</p>
  
  <p>Any idea on how I could solve this? The problem is not setting a specific date at the start of the test, but advancing time faster than real-time (in one of my tests I want to change some stuff in the database, advance the 'current time' with one day, change some more stuff in the database and do webservice calls to see the result).</p>
  
  <p>I can figure out a few solutions myself, but they all involve changing the application code and making it less elegant. I don't think your application code should be impacted by the fact that you want to test it.</p>