-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use UTC when encoding datetime values into cursor. #92
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,45 @@ | |
expect(decoded.order_field_value).to eq record.author | ||
end | ||
end | ||
|
||
context 'when ordering by created_at' do | ||
before { record.update!(created_at: '2022-10-04 16:58:43 +0200') } | ||
|
||
after do | ||
ActiveRecord::Base.time_zone_aware_attributes = true | ||
ActiveRecord::Base.default_timezone == :utc | ||
end | ||
|
||
subject(:encoded) do | ||
described_class.from_record(record: record, | ||
order_field: :created_at).encode | ||
end | ||
|
||
let(:decoded_time) do | ||
decoded = described_class.decode(encoded_string: encoded, | ||
order_field: :created_at) | ||
Time.parse(decoded.order_field_value) | ||
end | ||
|
||
it 'can be decoded back to the originally encoded value' do | ||
expect(decoded_time).to eq record.created_at | ||
expect(decoded_time).to be_utc | ||
end | ||
|
||
it 'respects the time_zone_aware_attributes setting' do | ||
ActiveRecord::Base.time_zone_aware_attributes = false | ||
|
||
expect(decoded_time).to eq record.created_at | ||
expect(decoded_time).not_to be_utc | ||
end | ||
|
||
it 'respects the default_timezone setting' do | ||
ActiveRecord::Base.default_timezone = :local | ||
|
||
expect(decoded_time).to eq record.created_at | ||
expect(decoded_time).not_to be_utc | ||
end | ||
Comment on lines
+79
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are altering the context in which these examples are run I propose moving them to their own context. See the comment below. Something like context 'when timezone aware attributes are disabled' do
before do
ActiveRecord::Base.time_zone_aware_attributes = false
end
# your examples here
end
context 'when using :local as default timezone' do
before do
ActiveRecord::Base.default_timezone = :local
end
# your examples here
end |
||
end | ||
end | ||
|
||
describe '.from_record' do | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,10 @@ class Post < ActiveRecord::Base; end | |||||||
ActiveRecord::Base.logger = Logger.new(ENV['VERBOSE'] ? $stdout : nil) | ||||||||
ActiveRecord::Migration.verbose = ENV.fetch('VERBOSE', nil) | ||||||||
|
||||||||
# Configure time_zone like it would be in rails applications | ||||||||
ActiveRecord::Base.time_zone_aware_attributes = true | ||||||||
Time.zone = 'Berlin' | ||||||||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather mock the response values or change the setup only in the tests where we need it rather than changing the configuration for the whole test suite. Something like the code below would be enough probably. # This can be different for different contexts
before do
ActiveRecord::Base.time_zone_aware_attributes = true
Time.zone = 'Berlin'
end
after do
# what you did in the file above
end It looks like stubbing |
||||||||
|
||||||||
ActiveRecord::Base.establish_connection( | ||||||||
adapter: ENV.fetch('DB_ADAPTER', 'mysql2'), | ||||||||
database: 'rails_cursor_pagination_testing', | ||||||||
|
@@ -35,6 +39,7 @@ class Post < ActiveRecord::Base; end | |||||||
ActiveRecord::Migration.create_table :posts do |t| | ||||||||
t.string :author | ||||||||
t.string :content | ||||||||
t.datetime :created_at | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about adding two test cases, one for timestamps and one for a datetime attribute? They are slightly different and would better reflect a real world scenario
Suggested change
|
||||||||
end | ||||||||
|
||||||||
config.before(:each) { Post.delete_all } | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would try to make this more generig