Skip to content
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

Don't overwrite flags of timestamp coders #525

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/pg/binary_decoder/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ module BinaryDecoder
class TimestampUtc < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC)
end
end
class TimestampUtcToLocal < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
end
end
class TimestampLocal < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pg/binary_encoder/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module BinaryEncoder
class TimestampUtc < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_UTC, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC)
end
end
class TimestampLocal < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_LOCAL, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_LOCAL)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/pg/text_decoder/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ module TextDecoder
class TimestampUtc < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC)
end
end
class TimestampUtcToLocal < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
end
end
class TimestampLocal < Timestamp
def initialize(hash={}, **kwargs)
warn "PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}" unless hash.empty?
super(flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL, **hash, **kwargs)
super(**hash, **kwargs, flags: PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL)
end
end

Expand Down
26 changes: 25 additions & 1 deletion spec/pg/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,15 @@ def expect_deprecated_coder_init
end
end

it "should overwrite default values as hash" do
it "should overwrite default format" do
t = nil
expect_deprecated_coder_init do
t = PG::BinaryEncoder::Int4.new({format: 0})
end
expect( t.format ).to eq( 0 )

t = PG::BinaryEncoder::Int4.new(format: 0)
expect( t.format ).to eq( 0 )
end

it "should take hash argument" do
Expand Down Expand Up @@ -559,6 +562,27 @@ def expect_deprecated_coder_init
expect( t.name ).to eq( "abcä" )
end

it "shouldn't overwrite timestamp flags" do
t = PG::TextDecoder::TimestampUtc.new({flags: PG::Coder::TIMESTAMP_DB_LOCAL})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC )
t = PG::TextDecoder::TimestampUtcToLocal.new({flags: PG::Coder::TIMESTAMP_APP_UTC})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL )
t = PG::TextDecoder::TimestampLocal.new({flags: PG::Coder::TIMESTAMP_DB_UTC})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL )

t = PG::BinaryDecoder::TimestampUtc.new({flags: PG::Coder::TIMESTAMP_DB_LOCAL})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_UTC )
t = PG::BinaryDecoder::TimestampUtcToLocal.new({flags: PG::Coder::TIMESTAMP_APP_UTC})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL )
t = PG::BinaryDecoder::TimestampLocal.new({flags: PG::Coder::TIMESTAMP_DB_UTC})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_LOCAL | PG::Coder::TIMESTAMP_APP_LOCAL )

t = PG::BinaryEncoder::TimestampUtc.new({flags: PG::Coder::TIMESTAMP_DB_LOCAL})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_UTC )
t = PG::BinaryEncoder::TimestampLocal.new({flags: PG::Coder::TIMESTAMP_APP_LOCAL})
expect( t.flags ).to eq( PG::Coder::TIMESTAMP_DB_LOCAL )
end

it "should deny changes when frozen" do
t = PG::TextEncoder::String.new.freeze
expect{ t.format = 1 }.to raise_error(FrozenError)
Expand Down