Browse files

add failing test exposing mysql adapter tinyint bug

in myself, a column with type TINYINT(N) where N > 1 can be used to
represent an integer, but the rails mysql adapter refuses to interpret
as anything but a boolean.
  • Loading branch information...
1 parent 40d02eb commit d2d7492ebaccaaab3fdd069ef03672259e3a4d3d @phinze phinze committed with rafaelfranca May 14, 2013
Showing with 16 additions and 3 deletions.
  1. +16 −3 activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
View
19 activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
@@ -95,14 +95,27 @@ def test_pk_and_sequence_for_with_custom_index_type_pk
assert_equal @conn.default_sequence_name('ex_with_custom_index_type_pk', 'id'), seq
end
+ def test_tinyint_integer_typecasting
+ @conn.exec_query('drop table if exists ex_with_non_boolean_tinyint_column')
+ @conn.exec_query(<<-eosql)
+ CREATE TABLE `ex_with_non_boolean_tinyint_column` (
+ `status` TINYINT(4))
+ eosql
+ insert(@conn, { 'status' => 2 }, 'ex_with_non_boolean_tinyint_column')
+
+ result = @conn.exec_query('SELECT status FROM ex_with_non_boolean_tinyint_column')
+
+ assert_equal 2, result.column_types['status'].type_cast(result.last['status'])
+ end
+
private
- def insert(ctx, data)
+ def insert(ctx, data, table='ex')
binds = data.map { |name, value|
- [ctx.columns('ex').find { |x| x.name == name }, value]
+ [ctx.columns(table).find { |x| x.name == name }, value]
}
columns = binds.map(&:first).map(&:name)
- sql = "INSERT INTO ex (#{columns.join(", ")})
+ sql = "INSERT INTO #{table} (#{columns.join(", ")})
VALUES (#{(['?'] * columns.length).join(', ')})"
ctx.exec_insert(sql, 'SQL', binds)

0 comments on commit d2d7492

Please sign in to comment.