Skip to content

Commit

Permalink
Backport the cast_booleans test structure to results spec
Browse files Browse the repository at this point in the history
  • Loading branch information
sodabrew committed Nov 14, 2017
1 parent b6cbf17 commit f971f36
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
67 changes: 37 additions & 30 deletions spec/mysql2/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,36 +208,43 @@
expect(@test_result['tiny_int_test']).to eql(1)
end

it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
id1 = @client.last_id
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
id2 = @client.last_id
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
id3 = @client.last_id

result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
expect(result1.first['bool_cast_test']).to be true
expect(result2.first['bool_cast_test']).to be false
expect(result3.first['bool_cast_test']).to be true

@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
end

it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
id1 = @client.last_id
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
id2 = @client.last_id

result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
expect(result1.first['single_bit_test']).to be true
expect(result2.first['single_bit_test']).to be false

@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
context "cast booleans for TINYINT if :cast_booleans is enabled" do
# rubocop:disable Style/Semicolon
let(:id1) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)'; @client.last_id }
let(:id2) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)'; @client.last_id }
let(:id3) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'; @client.last_id }
# rubocop:enable Style/Semicolon

after do
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
end

it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
result1 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id1} LIMIT 1", :cast_booleans => true
result2 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id2} LIMIT 1", :cast_booleans => true
result3 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id3} LIMIT 1", :cast_booleans => true
expect(result1.first['bool_cast_test']).to be true
expect(result2.first['bool_cast_test']).to be false
expect(result3.first['bool_cast_test']).to be true
end
end

context "cast booleans for BIT(1) if :cast_booleans is enabled" do
# rubocop:disable Style/Semicolon
let(:id1) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'; @client.last_id }
let(:id2) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'; @client.last_id }
# rubocop:enable Style/Semicolon

after do
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
end

it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
expect(result1.first['single_bit_test']).to be true
expect(result2.first['single_bit_test']).to be false
end
end

it "should return Fixnum for a SMALLINT value" do
Expand Down
2 changes: 1 addition & 1 deletion spec/mysql2/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def stmt_count
expect(@test_result['tiny_int_test']).to eql(1)
end

context "cast booleans for TINYINY if :cast_booleans is enabled" do
context "cast booleans for TINYINT if :cast_booleans is enabled" do
# rubocop:disable Style/Semicolon
let(:client) { new_client(:cast_booleans => true) }
let(:id1) { client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)'; client.last_id }
Expand Down

0 comments on commit f971f36

Please sign in to comment.