Skip to content

Commit

Permalink
Make MySQL driver serialize decimals properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jefmathiot committed Mar 7, 2015
1 parent 033df3a commit 866adfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/gros_calin/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ class Mysql
register 'mysql'

def query(id, sql)
Mysql2::Client.new(@options).query(sql).each
Mysql2::Client.new(@options).query(sql).map do |row|
{}.tap do |result|
row.each do|key, value|
result[key] = value.is_a?(BigDecimal) ? value.to_f : value
end
end
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions spec/gros_calin/mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
it 'queries the database' do
Mysql2::Client.expects(:new).with(options={an_option: 1}).
returns(client=mock)
client.expects(:query).with('select').returns(result=mock)
result.expects(:each).returns(data=[])
subject.new(options).query('id', 'select').must_equal data
client.expects(:query).with('select').
returns(result=[{ 'id'=>1, 'value'=>BigDecimal.new(0.0075, 2) }])
subject.new(options).query('id', 'select').
must_equal [{'id'=>1, 'value'=> 0.0075}]
end

end

0 comments on commit 866adfa

Please sign in to comment.