timecop icon indicating copy to clipboard operation
timecop copied to clipboard

SQL 'NOW()' is not impacted by timecop

Open cyrilchampier opened this issue 6 years ago • 5 comments

I am not sure how to handle this case, or even if it should be handled by Timecop, but I cannot find any documentation about it.

This test fails:

  it 'test sql now' do
    Timecop.freeze(1.year.ago)
    sql_now = ActiveRecord::Base.connection.execute('SELECT now()').first['now']
    assert_equal sql_now.to_date.year, Time.current.year
  end

Because SQL time is not impacted by Timecop. There is the same problem with javascript, but we succeeded to synchronize it with rails date with sinon.useFakeTimers.

Do you have any idea on how to propagate the date to the sql server ?

cyrilchampier avatar Aug 22 '18 12:08 cyrilchampier

You cannot fake any functions in MySQL except by using the procedures. timecop is just for the faking the date and times on ruby level, not anywhere else.

dmitry avatar Jun 14 '19 12:06 dmitry

I understand that, unfortunately :( Do you think a README warning notice could be helpful?

cyrilchampier avatar Jun 14 '19 15:06 cyrilchampier

Probably would be good to add some notice in the readme, that it doesn't mock anything outside ruby runtime.

dmitry avatar Jun 15 '19 08:06 dmitry

what about adding this in README?

cyrilchampier avatar Jun 17 '19 08:06 cyrilchampier

The PR is closed on this. If @cyrilchampier still wants to contribute to the docs on this please let me know. We'd be happy to include your idea with a few updates to the language.

joshuacronemeyer avatar Oct 15 '20 20:10 joshuacronemeyer