Skip to content

Commit 080ea37

Browse files
authored
Merge pull request #904 from sodabrew/better_specs
2 parents b6cbf17 + f8ee23a commit 080ea37

File tree

5 files changed

+495
-30
lines changed

5 files changed

+495
-30
lines changed

.travis_mysql57.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -eux
55
apt-get purge -qq '^mysql*' '^libmysql*'
66
rm -fr /etc/mysql
77
rm -fr /var/lib/mysql
8-
apt-key adv --keyserver pgp.mit.edu --recv-keys 5072E1F5
8+
apt-key add - < support/5072E1F5.asc
99
add-apt-repository 'deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.7'
1010
apt-get update -qq
1111
apt-get install -qq mysql-server libmysqlclient-dev

.travis_mysql80.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -eux
55
apt-get purge -qq '^mysql*' '^libmysql*'
66
rm -fr /etc/mysql
77
rm -fr /var/lib/mysql
8-
apt-key adv --keyserver pgp.mit.edu --recv-keys 5072E1F5
8+
apt-key add - < support/5072E1F5.asc
99
add-apt-repository 'deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-8.0'
1010
apt-get update -qq
1111
apt-get install -qq mysql-server libmysqlclient-dev

spec/mysql2/result_spec.rb

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,36 +208,43 @@
208208
expect(@test_result['tiny_int_test']).to eql(1)
209209
end
210210

211-
it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
212-
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
213-
id1 = @client.last_id
214-
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
215-
id2 = @client.last_id
216-
@client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
217-
id3 = @client.last_id
218-
219-
result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
220-
result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
221-
result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
222-
expect(result1.first['bool_cast_test']).to be true
223-
expect(result2.first['bool_cast_test']).to be false
224-
expect(result3.first['bool_cast_test']).to be true
211+
context "cast booleans for TINYINT if :cast_booleans is enabled" do
212+
# rubocop:disable Style/Semicolon
213+
let(:id1) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)'; @client.last_id }
214+
let(:id2) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)'; @client.last_id }
215+
let(:id3) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'; @client.last_id }
216+
# rubocop:enable Style/Semicolon
217+
218+
after do
219+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
220+
end
225221

226-
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
222+
it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
223+
result1 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id1} LIMIT 1", :cast_booleans => true
224+
result2 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id2} LIMIT 1", :cast_booleans => true
225+
result3 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id3} LIMIT 1", :cast_booleans => true
226+
expect(result1.first['bool_cast_test']).to be true
227+
expect(result2.first['bool_cast_test']).to be false
228+
expect(result3.first['bool_cast_test']).to be true
229+
end
227230
end
228231

229-
it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
230-
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
231-
id1 = @client.last_id
232-
@client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
233-
id2 = @client.last_id
232+
context "cast booleans for BIT(1) if :cast_booleans is enabled" do
233+
# rubocop:disable Style/Semicolon
234+
let(:id1) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'; @client.last_id }
235+
let(:id2) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'; @client.last_id }
236+
# rubocop:enable Style/Semicolon
234237

235-
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
236-
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
237-
expect(result1.first['single_bit_test']).to be true
238-
expect(result2.first['single_bit_test']).to be false
238+
after do
239+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
240+
end
239241

240-
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
242+
it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
243+
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
244+
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
245+
expect(result1.first['single_bit_test']).to be true
246+
expect(result2.first['single_bit_test']).to be false
247+
end
241248
end
242249

243250
it "should return Fixnum for a SMALLINT value" do
@@ -285,6 +292,30 @@
285292
expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
286293
end
287294

295+
it "should return Time values with microseconds" do
296+
now = Time.now
297+
if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506
298+
result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
299+
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
300+
else
301+
result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
302+
# microseconds is 6 digits after the decimal, but only test on 5 significant figures
303+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
304+
end
305+
end
306+
307+
it "should return DateTime values with microseconds" do
308+
now = DateTime.now
309+
if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506
310+
result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
311+
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
312+
else
313+
result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
314+
# microseconds is 6 digits after the decimal, but only test on 5 significant figures
315+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
316+
end
317+
end
318+
288319
if 1.size == 4 # 32bit
289320
klass = if RUBY_VERSION =~ /1.8/
290321
DateTime

spec/mysql2/statement_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def stmt_count
150150
if RUBY_VERSION =~ /1.8/
151151
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
152152
else
153-
expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
153+
# microseconds is six digits after the decimal, but only test on 5 significant figures
154+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
154155
end
155156
end
156157

@@ -161,7 +162,8 @@ def stmt_count
161162
if RUBY_VERSION =~ /1.8/
162163
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
163164
else
164-
expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
165+
# microseconds is six digits after the decimal, but only test on 5 significant figures
166+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
165167
end
166168
end
167169

@@ -374,7 +376,7 @@ def stmt_count
374376
expect(@test_result['tiny_int_test']).to eql(1)
375377
end
376378

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

0 commit comments

Comments
 (0)