Skip to content

Commit 07790d5

Browse files
committed
test cases to describe the MySQL boolean behavior.
1 parent 331c59f commit 07790d5

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
require "cases/helper"
2+
3+
class Mysql2BooleanTest < ActiveRecord::TestCase
4+
self.use_transactional_fixtures = false
5+
6+
class BooleanType < ActiveRecord::Base
7+
self.table_name = "mysql_booleans"
8+
end
9+
10+
setup do
11+
@connection = ActiveRecord::Base.connection
12+
@connection.create_table("mysql_booleans") do |t|
13+
t.boolean "archived"
14+
t.string "published", limit: 1
15+
end
16+
BooleanType.reset_column_information
17+
18+
@emulate_booleans = ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans
19+
end
20+
21+
teardown do
22+
emulate_booleans @emulate_booleans
23+
@connection.drop_table "mysql_booleans"
24+
end
25+
26+
test "column type with emulated booleans" do
27+
emulate_booleans true
28+
29+
assert_equal :boolean, boolean_column.type
30+
assert_equal :string, string_column.type
31+
end
32+
33+
test "column type without emulated booleans" do
34+
emulate_booleans false
35+
36+
assert_equal :integer, boolean_column.type
37+
assert_equal :string, string_column.type
38+
end
39+
40+
test "test type casting with emulated booleans" do
41+
emulate_booleans true
42+
43+
boolean = BooleanType.create!(archived: true, published: true)
44+
attributes = boolean.reload.attributes_before_type_cast
45+
46+
assert_equal 1, attributes["archived"]
47+
assert_equal "1", attributes["published"]
48+
49+
assert_equal "t", @connection.type_cast(true, boolean_column)
50+
assert_equal "t", @connection.type_cast(true, string_column)
51+
end
52+
53+
test "test type casting without emulated booleans" do
54+
emulate_booleans false
55+
56+
boolean = BooleanType.create!(archived: true, published: true)
57+
attributes = boolean.reload.attributes_before_type_cast
58+
59+
assert_equal 1, attributes["archived"]
60+
assert_equal "1", attributes["published"]
61+
62+
assert_equal 1, @connection.type_cast(true, boolean_column)
63+
assert_equal "t", @connection.type_cast(true, string_column)
64+
end
65+
66+
test "with booleans stored as 1 and 0" do
67+
@connection.execute "INSERT INTO mysql_booleans(archived, published) VALUES(1, '1')"
68+
boolean = BooleanType.first
69+
assert_equal true, boolean.archived
70+
assert_equal "1", boolean.published
71+
end
72+
73+
test "with booleans stored as t" do
74+
@connection.execute "INSERT INTO mysql_booleans(published) VALUES('t')"
75+
boolean = BooleanType.first
76+
assert_equal "t", boolean.published
77+
end
78+
79+
def boolean_column
80+
BooleanType.columns.find { |c| c.name == 'archived' }
81+
end
82+
83+
def string_column
84+
BooleanType.columns.find { |c| c.name == 'published' }
85+
end
86+
87+
def emulate_booleans(value)
88+
ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans = value
89+
BooleanType.reset_column_information
90+
end
91+
end

0 commit comments

Comments
 (0)