Actions
Defect #33941
openDo not use timezone unaware methods
Status:
New
Priority:
Normal
Assignee:
-
Category:
Rails support
Target version:
-
Start date:
Due date:
% Done:
0%
Estimated time:
Resolution:
Affected version:
Description
If you set the timezone in config/additional_environment.rb
and test it, any tests may fail.
config.time_zone = 'International Date Line West' # UTC-12:00
The test will always fail if the date of today on UTC
and the date of today on config.time_zone
are different.
$ RAILS_ENV=test bundle exec rake db:migrate:reset $ RAILS_ENV=test bundle exec rake test Run options: --seed 21968 # Running: ....................F Failure: MailerTest#test_reminders [./test/unit/mailer_test.rb:694]: "Bug #3: Error 281 when updating a recipe (5 days late)" not found in "1 issue(s) that are assigned to you are due in the next 42 days:: * eCookbook - Bug #3: Error 281 when updating a recipe (6 days late) View all issues (2 open) http://localhost:3000/issues?assigned_to_id=me&set_filter=1&sort=due_date%3Aasc -- You have received this notification because you have either subscribed to it, or are involved in it. To change your notification preferences, please click here: http://hostname/my/account " bin/rails test test/unit/mailer_test.rb:687 ................F Failure: MailerTest#test_reminders_should_sort_issues_by_due_date [./test/unit/mailer_test.rb:823]: <(?-mix:foo \(1 day late\))> expected but was <eCookbook - Bug #18: foo (2 days late)>.. Expected 0 to be >= 1. bin/rails test test/unit/mailer_test.rb:811 ........F Failure: MailerTest#test_reminders_language_auto [./test/unit/mailer_test.rb:717]: "Bug #3: Error 281 when updating a recipe (En retard de 5 jours)" not found in "1 demande(s) qui vous sont assignées arrivent à échéance dans les 42 prochains jours :: * eCookbook - Bug #3: Error 281 when updating a recipe (En retard de 6 jours) Voir toutes les demandes (2 ouvertes) http://localhost:3000/issues?assigned_to_id=me&set_filter=1&sort=due_date%3Aasc -- You have received this notification because you have either subscribed to it, or are involved in it. To change your notification preferences, please click here: http://hostname/my/account " bin/rails test test/unit/mailer_test.rb:708 ....F Failure: MailerTest#test_reminder_should_include_issues_assigned_to_groups [./test/unit/mailer_test.rb:773]: "Assigned to group (Due in 5 days)" not found in "1 issue(s) that are assigned to you are due in the next 7 days:: * eCookbook - Bug #15: Assigned to group (Due in 4 days) View all issues (2 open) http://localhost:3000/issues?assigned_to_id=me&set_filter=1&sort=due_date%3Aasc -- You have received this notification because you have either subscribed to it, or are involved in it. To change your notification preferences, please click here: http://hostname/my/account " bin/rails test test/unit/mailer_test.rb:749 .......F Failure: MailerTest#test_reminders_for_users [./test/unit/mailer_test.rb:746]: "Bug #3: Error 281 when updating a recipe (5 days late)" not found in "1 issue(s) that are assigned to you are due in the next 42 days:: * eCookbook - Bug #3: Error 281 when updating a recipe (6 days late) View all issues (2 open) http://localhost:3000/issues?assigned_to_id=me&set_filter=1&sort=due_date%3Aasc -- You have received this notification because you have either subscribed to it, or are involved in it. To change your notification preferences, please click here: http://hostname/my/account " bin/rails test test/unit/mailer_test.rb:738 ........... Finished in 411.745744s, 12.2090 runs/s, 56.3649 assertions/s. 5027 runs, 23208 assertions, 5 failures, 0 errors, 7 skips You have skipped tests. Run with --verbose for details. $
Files
Related issues
Updated by Yuichi HARADA about 4 years ago
There are two possible causes for the test to fail.
- When using
Date.today, Time.now, DateTime.now
, the timezone in config.time_zone is ignored. You should useDate.current, Time.current, DateTime.current
. - Times of the day are not stored in table columns of type of date. However, in
Query#date_clause
(source:/trunk/app/models/query.rb#L1465), because the where condition considering the time difference is created for this Date type table column, the SQL execution result is strange.
Updated by Yuichi HARADA about 4 years ago
- File fixed-33941.patch fixed-33941.patch added
Yuichi HARADA wrote:
There are two possible causes for the test to fail.
- When using
Date.today, Time.now, DateTime.now
, the timezone in config.time_zone is ignored. You should useDate.current, Time.current, DateTime.current
.- Times of the day are not stored in table columns of type of date. However, in
Query#date_clause
(source:/trunk/app/models/query.rb#L1465), because the where condition considering the time difference is created for this Date type table column, the SQL execution result is strange.
I will attach a patch to resolve these.
I've tested in the following time zones to make sure the tests don't fail.
config.time_zone = 'UTC' # UTC+-00:00
config.time_zone = 'Tokyo' # UTC+09:00
config.time_zone = 'Samoa' # UTC+13:00
config.time_zone = 'Alaska' # UTC-09:00
config.time_zone = 'International Date Line West' # UTC-12:00
Updated by Mizuki ISHIKAWA over 3 years ago
+1
After applying this patch, you should be able to enable Rails/Date for Rubocop, which is currently disabled.
https://www.rubydoc.info/gems/rubocop/0.41.2/RuboCop/Cop/Rails/Date
Updated by Go MAEDA over 3 years ago
- Subject changed from Test fails depending on time zone setting to Do not use timezone unaware methods
- Category set to Rails support
Updated by Go MAEDA over 3 years ago
- File RuboCop-RailsTimezone-RailsDate-offenses.txt RuboCop-RailsTimezone-RailsDate-offenses.txt added
Here is the result of the following command:
bundle exec rubocop --ignore-disable-comments --only Rails/TimeZone,Rails/Date
Updated by Go MAEDA almost 3 years ago
- Related to Defect #23189: Spent time by filter, offset by one day added
Actions