Fix DirectMessage.today on different time zones

The dates are saved on UTC times on the database. So, for example,
if living in West Australia, `Date.current.beginning_of_day` will be
stored as UTC's yesterday at 15:15:00, while `Date.current.end_of_day`
will be stored as UTC's today at 15:14:59.

When we use the `DATE` database function, PostgreSQL will select the
records with the same UTC date as the current UTC date. However, we need
the records with the same application date (as defined in
`config.time_zone`) as the current application date. The test passed
(for us) because we were using `beginning_of_day + 3.hours` to make sure
we were creating records when the date in Madrid was the same as the UTC
date.

Using a ruby interval for the time condition solves the problem.
This commit is contained in:
Javi Martín
2019-06-18 21:44:12 +02:00
parent f031d1289f
commit c574a4d93a
3 changed files with 11 additions and 5 deletions

View File

@@ -75,11 +75,11 @@ describe DirectMessage do
describe "scopes" do
describe "today" do
describe "today", :with_non_utc_time_zone do
it "returns direct messages created today" do
direct_message1 = create(:direct_message, created_at: Time.now.utc.beginning_of_day + 3.hours)
direct_message2 = create(:direct_message, created_at: Time.now.utc)
direct_message3 = create(:direct_message, created_at: Time.now.utc.end_of_day)
create(:direct_message, created_at: Date.current.beginning_of_day)
create(:direct_message, created_at: Time.current)
create(:direct_message, created_at: Date.current.end_of_day)
expect(described_class.today.count).to eq 3
end