Skip to content

Commit

Permalink
Do not rely on the column type when quoting infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrif committed Jul 3, 2014
1 parent 03c9c0e commit 81b7187
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Float < Type::Float # :nodoc:

def cast_value(value)
case value
when ::Float then value
when 'Infinity' then ::Float::INFINITY
when '-Infinity' then -::Float::INFINITY
when 'NaN' then ::Float::NAN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def quote(value, column = nil) #:nodoc:
else super
end
when Float
if value.infinite? && column.type == :datetime
"'#{value.to_s.downcase}'"
elsif value.infinite? || value.nan?
if value.infinite? || value.nan?
"'#{value.to_s}'"
else
super
Expand Down
44 changes: 44 additions & 0 deletions activerecord/test/cases/adapters/postgresql/infinity_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "cases/helper"

class PostgresqlInfinityTest < ActiveRecord::TestCase
class PostgresqlInfinity < ActiveRecord::Base
end

setup do
@connection = ActiveRecord::Base.connection
@connection.create_table(:postgresql_infinities) do |t|
t.float :float
t.datetime :datetime
end
end

teardown do
@connection.execute("DROP TABLE IF EXISTS postgresql_infinities")
end

test "type casting infinity on a float column" do
record = PostgresqlInfinity.create!(float: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.float
end

test "update_all with infinity on a float column" do
record = PostgresqlInfinity.create!
PostgresqlInfinity.update_all(float: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.float
end

test "type casting infinity on a datetime column" do
record = PostgresqlInfinity.create!(datetime: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.datetime
end

test "update_all with infinity on a datetime column" do
record = PostgresqlInfinity.create!
PostgresqlInfinity.update_all(datetime: Float::INFINITY)
record.reload
assert_equal Float::INFINITY, record.datetime
end
end

0 comments on commit 81b7187

Please sign in to comment.