mysql2 0.3.18 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -0
  3. data/LICENSE +21 -0
  4. data/README.md +69 -17
  5. data/examples/eventmachine.rb +1 -1
  6. data/examples/threaded.rb +4 -6
  7. data/ext/mysql2/client.c +230 -179
  8. data/ext/mysql2/client.h +18 -1
  9. data/ext/mysql2/extconf.rb +95 -35
  10. data/ext/mysql2/infile.c +2 -2
  11. data/ext/mysql2/mysql2_ext.c +1 -0
  12. data/ext/mysql2/mysql2_ext.h +5 -6
  13. data/ext/mysql2/mysql_enc_name_to_ruby.h +2 -2
  14. data/ext/mysql2/mysql_enc_to_ruby.h +25 -22
  15. data/ext/mysql2/result.c +509 -138
  16. data/ext/mysql2/result.h +12 -6
  17. data/ext/mysql2/statement.c +504 -0
  18. data/ext/mysql2/statement.h +19 -0
  19. data/lib/mysql2/client.rb +71 -25
  20. data/lib/mysql2/console.rb +1 -1
  21. data/lib/mysql2/em.rb +5 -6
  22. data/lib/mysql2/error.rb +18 -27
  23. data/lib/mysql2/field.rb +3 -0
  24. data/lib/mysql2/statement.rb +17 -0
  25. data/lib/mysql2/version.rb +1 -1
  26. data/lib/mysql2.rb +38 -18
  27. data/spec/em/em_spec.rb +21 -21
  28. data/spec/mysql2/client_spec.rb +456 -362
  29. data/spec/mysql2/error_spec.rb +37 -36
  30. data/spec/mysql2/result_spec.rb +222 -208
  31. data/spec/mysql2/statement_spec.rb +703 -0
  32. data/spec/spec_helper.rb +7 -0
  33. data/spec/ssl/ca-cert.pem +17 -0
  34. data/spec/ssl/ca-key.pem +27 -0
  35. data/spec/ssl/ca.cnf +22 -0
  36. data/spec/ssl/cert.cnf +22 -0
  37. data/spec/ssl/client-cert.pem +17 -0
  38. data/spec/ssl/client-key.pem +27 -0
  39. data/spec/ssl/client-req.pem +15 -0
  40. data/spec/ssl/gen_certs.sh +48 -0
  41. data/spec/ssl/pkcs8-client-key.pem +28 -0
  42. data/spec/ssl/pkcs8-server-key.pem +28 -0
  43. data/spec/ssl/server-cert.pem +17 -0
  44. data/spec/ssl/server-key.pem +27 -0
  45. data/spec/ssl/server-req.pem +15 -0
  46. data/support/mysql_enc_to_ruby.rb +7 -8
  47. data/support/ruby_enc_to_mysql.rb +1 -1
  48. metadata +42 -47
@@ -1,39 +1,51 @@
1
1
  # encoding: UTF-8
2
2
  require 'spec_helper'
3
3
 
4
- describe Mysql2::Result do
4
+ RSpec.describe Mysql2::Result do
5
5
  before(:each) do
6
6
  @result = @client.query "SELECT 1"
7
7
  end
8
8
 
9
+ it "should raise a TypeError exception when it doesn't wrap a result set" do
10
+ r = Mysql2::Result.new
11
+ expect { r.count }.to raise_error(TypeError)
12
+ expect { r.fields }.to raise_error(TypeError)
13
+ expect { r.size }.to raise_error(TypeError)
14
+ expect { r.each }.to raise_error(TypeError)
15
+ end
16
+
9
17
  it "should have included Enumerable" do
10
- Mysql2::Result.ancestors.include?(Enumerable).should be_true
18
+ expect(Mysql2::Result.ancestors.include?(Enumerable)).to be true
11
19
  end
12
20
 
13
21
  it "should respond to #each" do
14
- @result.should respond_to(:each)
22
+ expect(@result).to respond_to(:each)
23
+ end
24
+
25
+ it "should respond to #free" do
26
+ expect(@result).to respond_to(:free)
15
27
  end
16
28
 
17
29
  it "should raise a Mysql2::Error exception upon a bad query" do
18
- lambda {
30
+ expect {
19
31
  @client.query "bad sql"
20
- }.should raise_error(Mysql2::Error)
32
+ }.to raise_error(Mysql2::Error)
21
33
 
22
- lambda {
34
+ expect {
23
35
  @client.query "SELECT 1"
24
- }.should_not raise_error(Mysql2::Error)
36
+ }.not_to raise_error
25
37
  end
26
38
 
27
39
  it "should respond to #count, which is aliased as #size" do
28
40
  r = @client.query "SELECT 1"
29
- r.should respond_to :count
30
- r.should respond_to :size
41
+ expect(r).to respond_to :count
42
+ expect(r).to respond_to :size
31
43
  end
32
44
 
33
45
  it "should be able to return the number of rows in the result set" do
34
46
  r = @client.query "SELECT 1"
35
- r.count.should eql(1)
36
- r.size.should eql(1)
47
+ expect(r.count).to eql(1)
48
+ expect(r.size).to eql(1)
37
49
  end
38
50
 
39
51
  context "metadata queries" do
@@ -45,39 +57,44 @@ describe Mysql2::Result do
45
57
  context "#each" do
46
58
  it "should yield rows as hash's" do
47
59
  @result.each do |row|
48
- row.class.should eql(Hash)
60
+ expect(row).to be_an_instance_of(Hash)
49
61
  end
50
62
  end
51
63
 
52
64
  it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
53
65
  @result.each(:symbolize_keys => true) do |row|
54
- row.keys.first.class.should eql(Symbol)
66
+ expect(row.keys.first).to be_an_instance_of(Symbol)
55
67
  end
56
68
  end
57
69
 
58
70
  it "should be able to return results as an array" do
59
71
  @result.each(:as => :array) do |row|
60
- row.class.should eql(Array)
72
+ expect(row).to be_an_instance_of(Array)
61
73
  end
62
74
  end
63
75
 
64
76
  it "should cache previously yielded results by default" do
65
- @result.first.object_id.should eql(@result.first.object_id)
77
+ expect(@result.first.object_id).to eql(@result.first.object_id)
66
78
  end
67
79
 
68
80
  it "should not cache previously yielded results if cache_rows is disabled" do
69
81
  result = @client.query "SELECT 1", :cache_rows => false
70
- result.first.object_id.should_not eql(result.first.object_id)
82
+ expect(result.first.object_id).not_to eql(result.first.object_id)
83
+ end
84
+
85
+ it "should be able to iterate a second time even if cache_rows is disabled" do
86
+ result = @client.query "SELECT 1 UNION SELECT 2", :cache_rows => false
87
+ expect(result.to_a).to eql(result.to_a)
71
88
  end
72
89
 
73
90
  it "should yield different value for #first if streaming" do
74
91
  result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
75
- result.first.should_not eql(result.first)
92
+ expect(result.first).not_to eql(result.first)
76
93
  end
77
94
 
78
95
  it "should yield the same value for #first if streaming is disabled" do
79
96
  result = @client.query "SELECT 1 UNION SELECT 2", :stream => false
80
- result.first.should eql(result.first)
97
+ expect(result.first).to eql(result.first)
81
98
  end
82
99
 
83
100
  it "should throw an exception if we try to iterate twice when streaming is enabled" do
@@ -96,41 +113,43 @@ describe Mysql2::Result do
96
113
  end
97
114
 
98
115
  it "method should exist" do
99
- @test_result.should respond_to(:fields)
116
+ expect(@test_result).to respond_to(:fields)
100
117
  end
101
118
 
102
119
  it "should return an array of field names in proper order" do
103
120
  result = @client.query "SELECT 'a', 'b', 'c'"
104
- result.fields.should eql(['a', 'b', 'c'])
121
+ expect(result.fields).to eql(%w(a b c))
105
122
  end
106
123
  end
107
124
 
108
125
  context "streaming" do
109
126
  it "should maintain a count while streaming" do
110
- result = @client.query('SELECT 1')
111
-
112
- result.count.should eql(1)
127
+ result = @client.query('SELECT 1', :stream => true, :cache_rows => false)
128
+ expect(result.count).to eql(0)
113
129
  result.each.to_a
114
- result.count.should eql(1)
130
+ expect(result.count).to eql(1)
115
131
  end
116
132
 
117
- it "should set the actual count of rows after streaming" do
118
- result = @client.query("SELECT * FROM mysql2_test", :stream => true, :cache_rows => false)
119
- result.count.should eql(0)
120
- result.each {|r| }
121
- result.count.should eql(1)
133
+ it "should retain the count when mixing first and each" do
134
+ result = @client.query("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false)
135
+ expect(result.count).to eql(0)
136
+ result.first
137
+ expect(result.count).to eql(1)
138
+ result.each.to_a
139
+ expect(result.count).to eql(2)
122
140
  end
123
141
 
124
142
  it "should not yield nil at the end of streaming" do
125
143
  result = @client.query('SELECT * FROM mysql2_test', :stream => true, :cache_rows => false)
126
- result.each { |r| r.should_not be_nil}
144
+ result.each { |r| expect(r).not_to be_nil }
127
145
  end
128
146
 
129
147
  it "#count should be zero for rows after streaming when there were no results" do
148
+ @client.query "USE test"
130
149
  result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
131
- result.count.should eql(0)
150
+ expect(result.count).to eql(0)
132
151
  result.each.to_a
133
- result.count.should eql(0)
152
+ expect(result.count).to eql(0)
134
153
  end
135
154
 
136
155
  it "should raise an exception if streaming ended due to a timeout" do
@@ -147,12 +166,12 @@ describe Mysql2::Result do
147
166
  client.query "SET net_write_timeout = 1"
148
167
  res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
149
168
 
150
- lambda {
151
- res.each_with_index do |row, i|
169
+ expect {
170
+ res.each_with_index do |_, i|
152
171
  # Exhaust the first result packet then trigger a timeout
153
172
  sleep 2 if i > 0 && i % 1000 == 0
154
173
  end
155
- }.should raise_error(Mysql2::Error, /Lost connection/)
174
+ }.to raise_error(Mysql2::Error, /Lost connection/)
156
175
  end
157
176
  end
158
177
 
@@ -163,32 +182,32 @@ describe Mysql2::Result do
163
182
 
164
183
  it "should return nil values for NULL and strings for everything else when :cast is false" do
165
184
  result = @client.query('SELECT null_test, tiny_int_test, bool_cast_test, int_test, date_test, enum_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast => false).first
166
- result["null_test"].should be_nil
167
- result["tiny_int_test"].should eql("1")
168
- result["bool_cast_test"].should eql("1")
169
- result["int_test"].should eql("10")
170
- result["date_test"].should eql("2010-04-04")
171
- result["enum_test"].should eql("val1")
185
+ expect(result["null_test"]).to be_nil
186
+ expect(result["tiny_int_test"]).to eql("1")
187
+ expect(result["bool_cast_test"]).to eql("1")
188
+ expect(result["int_test"]).to eql("10")
189
+ expect(result["date_test"]).to eql("2010-04-04")
190
+ expect(result["enum_test"]).to eql("val1")
172
191
  end
173
192
 
174
193
  it "should return nil for a NULL value" do
175
- @test_result['null_test'].class.should eql(NilClass)
176
- @test_result['null_test'].should eql(nil)
194
+ expect(@test_result['null_test']).to be_an_instance_of(NilClass)
195
+ expect(@test_result['null_test']).to eql(nil)
177
196
  end
178
197
 
179
198
  it "should return String for a BIT(64) value" do
180
- @test_result['bit_test'].class.should eql(String)
181
- @test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005")
199
+ expect(@test_result['bit_test']).to be_an_instance_of(String)
200
+ expect(@test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
182
201
  end
183
202
 
184
203
  it "should return String for a BIT(1) value" do
185
- @test_result['single_bit_test'].class.should eql(String)
186
- @test_result['single_bit_test'].should eql("\001")
204
+ expect(@test_result['single_bit_test']).to be_an_instance_of(String)
205
+ expect(@test_result['single_bit_test']).to eql("\001")
187
206
  end
188
207
 
189
208
  it "should return Fixnum for a TINYINT value" do
190
- [Fixnum, Bignum].should include(@test_result['tiny_int_test'].class)
191
- @test_result['tiny_int_test'].should eql(1)
209
+ expect([Fixnum, Bignum]).to include(@test_result['tiny_int_test'].class)
210
+ expect(@test_result['tiny_int_test']).to eql(1)
192
211
  end
193
212
 
194
213
  it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
@@ -202,9 +221,9 @@ describe Mysql2::Result do
202
221
  result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
203
222
  result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
204
223
  result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
205
- result1.first['bool_cast_test'].should be_true
206
- result2.first['bool_cast_test'].should be_false
207
- result3.first['bool_cast_test'].should be_true
224
+ expect(result1.first['bool_cast_test']).to be true
225
+ expect(result2.first['bool_cast_test']).to be false
226
+ expect(result3.first['bool_cast_test']).to be true
208
227
 
209
228
  @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
210
229
  end
@@ -217,219 +236,214 @@ describe Mysql2::Result do
217
236
 
218
237
  result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
219
238
  result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
220
- result1.first['single_bit_test'].should be_true
221
- result2.first['single_bit_test'].should be_false
239
+ expect(result1.first['single_bit_test']).to be true
240
+ expect(result2.first['single_bit_test']).to be false
222
241
 
223
242
  @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
224
243
  end
225
244
 
226
245
  it "should return Fixnum for a SMALLINT value" do
227
- [Fixnum, Bignum].should include(@test_result['small_int_test'].class)
228
- @test_result['small_int_test'].should eql(10)
246
+ expect([Fixnum, Bignum]).to include(@test_result['small_int_test'].class)
247
+ expect(@test_result['small_int_test']).to eql(10)
229
248
  end
230
249
 
231
250
  it "should return Fixnum for a MEDIUMINT value" do
232
- [Fixnum, Bignum].should include(@test_result['medium_int_test'].class)
233
- @test_result['medium_int_test'].should eql(10)
251
+ expect([Fixnum, Bignum]).to include(@test_result['medium_int_test'].class)
252
+ expect(@test_result['medium_int_test']).to eql(10)
234
253
  end
235
254
 
236
255
  it "should return Fixnum for an INT value" do
237
- [Fixnum, Bignum].should include(@test_result['int_test'].class)
238
- @test_result['int_test'].should eql(10)
256
+ expect([Fixnum, Bignum]).to include(@test_result['int_test'].class)
257
+ expect(@test_result['int_test']).to eql(10)
239
258
  end
240
259
 
241
260
  it "should return Fixnum for a BIGINT value" do
242
- [Fixnum, Bignum].should include(@test_result['big_int_test'].class)
243
- @test_result['big_int_test'].should eql(10)
261
+ expect([Fixnum, Bignum]).to include(@test_result['big_int_test'].class)
262
+ expect(@test_result['big_int_test']).to eql(10)
244
263
  end
245
264
 
246
265
  it "should return Fixnum for a YEAR value" do
247
- [Fixnum, Bignum].should include(@test_result['year_test'].class)
248
- @test_result['year_test'].should eql(2009)
266
+ expect([Fixnum, Bignum]).to include(@test_result['year_test'].class)
267
+ expect(@test_result['year_test']).to eql(2009)
249
268
  end
250
269
 
251
270
  it "should return BigDecimal for a DECIMAL value" do
252
- @test_result['decimal_test'].class.should eql(BigDecimal)
253
- @test_result['decimal_test'].should eql(10.3)
271
+ expect(@test_result['decimal_test']).to be_an_instance_of(BigDecimal)
272
+ expect(@test_result['decimal_test']).to eql(10.3)
254
273
  end
255
274
 
256
275
  it "should return Float for a FLOAT value" do
257
- @test_result['float_test'].class.should eql(Float)
258
- @test_result['float_test'].should eql(10.3)
276
+ expect(@test_result['float_test']).to be_an_instance_of(Float)
277
+ expect(@test_result['float_test']).to eql(10.3)
259
278
  end
260
279
 
261
280
  it "should return Float for a DOUBLE value" do
262
- @test_result['double_test'].class.should eql(Float)
263
- @test_result['double_test'].should eql(10.3)
281
+ expect(@test_result['double_test']).to be_an_instance_of(Float)
282
+ expect(@test_result['double_test']).to eql(10.3)
264
283
  end
265
284
 
266
285
  it "should return Time for a DATETIME value when within the supported range" do
267
- @test_result['date_time_test'].class.should eql(Time)
268
- @test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2010-04-04 11:44:00')
286
+ expect(@test_result['date_time_test']).to be_an_instance_of(Time)
287
+ expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
269
288
  end
270
289
 
271
290
  if 1.size == 4 # 32bit
272
- unless RUBY_VERSION =~ /1.8/
273
- klass = Time
291
+ klass = if RUBY_VERSION =~ /1.8/
292
+ DateTime
274
293
  else
275
- klass = DateTime
294
+ Time
276
295
  end
277
296
 
278
297
  it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
279
- # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
298
+ # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
280
299
  r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
281
- r.first['test'].class.should eql(klass)
300
+ expect(r.first['test']).to be_an_instance_of(klass)
282
301
  end
283
302
 
284
303
  it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
285
- # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
304
+ # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
286
305
  r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
287
- r.first['test'].class.should eql(klass)
306
+ expect(r.first['test']).to be_an_instance_of(klass)
288
307
  end
289
308
  elsif 1.size == 8 # 64bit
290
- unless RUBY_VERSION =~ /1.8/
291
- it "should return Time when timestamp is < 1901-12-13 20:45:52" do
292
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
293
- r.first['test'].class.should eql(Time)
294
- end
295
-
296
- it "should return Time when timestamp is > 2038-01-19T03:14:07" do
297
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
298
- r.first['test'].class.should eql(Time)
299
- end
300
- else
309
+ if RUBY_VERSION =~ /1.8/
301
310
  it "should return Time when timestamp is > 0138-12-31 11:59:59" do
302
311
  r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
303
- r.first['test'].class.should eql(Time)
312
+ expect(r.first['test']).to be_an_instance_of(Time)
304
313
  end
305
314
 
306
315
  it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
307
316
  r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
308
- r.first['test'].class.should eql(DateTime)
317
+ expect(r.first['test']).to be_an_instance_of(DateTime)
309
318
  end
310
319
 
311
320
  it "should return Time when timestamp is > 2038-01-19T03:14:07" do
312
321
  r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
313
- r.first['test'].class.should eql(Time)
322
+ expect(r.first['test']).to be_an_instance_of(Time)
323
+ end
324
+ else
325
+ it "should return Time when timestamp is < 1901-12-13 20:45:52" do
326
+ r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
327
+ expect(r.first['test']).to be_an_instance_of(Time)
328
+ end
329
+
330
+ it "should return Time when timestamp is > 2038-01-19T03:14:07" do
331
+ r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
332
+ expect(r.first['test']).to be_an_instance_of(Time)
314
333
  end
315
334
  end
316
335
  end
317
336
 
318
337
  it "should return Time for a TIMESTAMP value when within the supported range" do
319
- @test_result['timestamp_test'].class.should eql(Time)
320
- @test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2010-04-04 11:44:00')
338
+ expect(@test_result['timestamp_test']).to be_an_instance_of(Time)
339
+ expect(@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
321
340
  end
322
341
 
323
342
  it "should return Time for a TIME value" do
324
- @test_result['time_test'].class.should eql(Time)
325
- @test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2000-01-01 11:44:00')
343
+ expect(@test_result['time_test']).to be_an_instance_of(Time)
344
+ expect(@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2000-01-01 11:44:00')
326
345
  end
327
346
 
328
347
  it "should return Date for a DATE value" do
329
- @test_result['date_test'].class.should eql(Date)
330
- @test_result['date_test'].strftime("%Y-%m-%d").should eql('2010-04-04')
348
+ expect(@test_result['date_test']).to be_an_instance_of(Date)
349
+ expect(@test_result['date_test'].strftime("%Y-%m-%d")).to eql('2010-04-04')
331
350
  end
332
351
 
333
352
  it "should return String for an ENUM value" do
334
- @test_result['enum_test'].class.should eql(String)
335
- @test_result['enum_test'].should eql('val1')
353
+ expect(@test_result['enum_test']).to be_an_instance_of(String)
354
+ expect(@test_result['enum_test']).to eql('val1')
336
355
  end
337
356
 
338
357
  it "should raise an error given an invalid DATETIME" do
339
- begin
340
- @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each
341
- rescue Mysql2::Error => e
342
- error = e
343
- end
344
-
345
- error.message.should eql("Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
358
+ expect { @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each }.to \
359
+ raise_error(Mysql2::Error, "Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
346
360
  end
347
361
 
348
- if defined? Encoding
349
- context "string encoding for ENUM values" do
350
- it "should default to the connection's encoding if Encoding.default_internal is nil" do
351
- with_internal_encoding nil do
352
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
353
- result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
362
+ context "string encoding for ENUM values" do
363
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
354
364
 
355
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
356
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
357
- result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
358
- client2.close
359
- end
365
+ it "should default to the connection's encoding if Encoding.default_internal is nil" do
366
+ with_internal_encoding nil do
367
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
368
+ expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
369
+
370
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
371
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
372
+ expect(result['enum_test'].encoding).to eql(Encoding::ASCII)
373
+ client2.close
360
374
  end
375
+ end
361
376
 
362
- it "should use Encoding.default_internal" do
363
- with_internal_encoding 'utf-8' do
364
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
365
- result['enum_test'].encoding.should eql(Encoding.default_internal)
366
- end
377
+ it "should use Encoding.default_internal" do
378
+ with_internal_encoding Encoding::UTF_8 do
379
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
380
+ expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
381
+ end
367
382
 
368
- with_internal_encoding 'us-ascii' do
369
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
370
- result['enum_test'].encoding.should eql(Encoding.default_internal)
371
- end
383
+ with_internal_encoding Encoding::ASCII do
384
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
385
+ expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
372
386
  end
373
387
  end
374
388
  end
375
389
 
376
390
  it "should return String for a SET value" do
377
- @test_result['set_test'].class.should eql(String)
378
- @test_result['set_test'].should eql('val1,val2')
391
+ expect(@test_result['set_test']).to be_an_instance_of(String)
392
+ expect(@test_result['set_test']).to eql('val1,val2')
379
393
  end
380
394
 
381
- if defined? Encoding
382
- context "string encoding for SET values" do
383
- it "should default to the connection's encoding if Encoding.default_internal is nil" do
384
- with_internal_encoding nil do
385
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
386
- result['set_test'].encoding.should eql(Encoding.find('utf-8'))
395
+ context "string encoding for SET values" do
396
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
387
397
 
388
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
389
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
390
- result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
391
- client2.close
392
- end
398
+ it "should default to the connection's encoding if Encoding.default_internal is nil" do
399
+ with_internal_encoding nil do
400
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
401
+ expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
402
+
403
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
404
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
405
+ expect(result['set_test'].encoding).to eql(Encoding::ASCII)
406
+ client2.close
393
407
  end
408
+ end
394
409
 
395
- it "should use Encoding.default_internal" do
396
- with_internal_encoding 'utf-8' do
397
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
398
- result['set_test'].encoding.should eql(Encoding.default_internal)
399
- end
410
+ it "should use Encoding.default_internal" do
411
+ with_internal_encoding Encoding::UTF_8 do
412
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
413
+ expect(result['set_test'].encoding).to eql(Encoding.default_internal)
414
+ end
400
415
 
401
- with_internal_encoding 'us-ascii' do
402
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
403
- result['set_test'].encoding.should eql(Encoding.default_internal)
404
- end
416
+ with_internal_encoding Encoding::ASCII do
417
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
418
+ expect(result['set_test'].encoding).to eql(Encoding.default_internal)
405
419
  end
406
420
  end
407
421
  end
408
422
 
409
423
  it "should return String for a BINARY value" do
410
- @test_result['binary_test'].class.should eql(String)
411
- @test_result['binary_test'].should eql("test#{"\000"*6}")
424
+ expect(@test_result['binary_test']).to be_an_instance_of(String)
425
+ expect(@test_result['binary_test']).to eql("test#{"\000" * 6}")
412
426
  end
413
427
 
414
- if defined? Encoding
415
- context "string encoding for BINARY values" do
416
- it "should default to binary if Encoding.default_internal is nil" do
417
- with_internal_encoding nil do
418
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
419
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
420
- end
428
+ context "string encoding for BINARY values" do
429
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
430
+
431
+ it "should default to binary if Encoding.default_internal is nil" do
432
+ with_internal_encoding nil do
433
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
434
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
421
435
  end
436
+ end
422
437
 
423
- it "should not use Encoding.default_internal" do
424
- with_internal_encoding 'utf-8' do
425
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
426
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
427
- end
438
+ it "should not use Encoding.default_internal" do
439
+ with_internal_encoding Encoding::UTF_8 do
440
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
441
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
442
+ end
428
443
 
429
- with_internal_encoding 'us-ascii' do
430
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
431
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
432
- end
444
+ with_internal_encoding Encoding::ASCII do
445
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
446
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
433
447
  end
434
448
  end
435
449
  end
@@ -445,57 +459,57 @@ describe Mysql2::Result do
445
459
  'medium_blob_test' => 'MEDIUMBLOB',
446
460
  'medium_text_test' => 'MEDIUMTEXT',
447
461
  'long_blob_test' => 'LONGBLOB',
448
- 'long_text_test' => 'LONGTEXT'
462
+ 'long_text_test' => 'LONGTEXT',
449
463
  }.each do |field, type|
450
464
  it "should return a String for #{type}" do
451
- @test_result[field].class.should eql(String)
452
- @test_result[field].should eql("test")
465
+ expect(@test_result[field]).to be_an_instance_of(String)
466
+ expect(@test_result[field]).to eql("test")
453
467
  end
454
468
 
455
- if defined? Encoding
456
- context "string encoding for #{type} values" do
457
- if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
458
- it "should default to binary if Encoding.default_internal is nil" do
459
- with_internal_encoding nil do
460
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
461
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
462
- end
469
+ context "string encoding for #{type} values" do
470
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
471
+
472
+ if %w(VARBINARY TINYBLOB BLOB MEDIUMBLOB LONGBLOB).include?(type)
473
+ it "should default to binary if Encoding.default_internal is nil" do
474
+ with_internal_encoding nil do
475
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
476
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
463
477
  end
478
+ end
464
479
 
465
- it "should not use Encoding.default_internal" do
466
- with_internal_encoding 'utf-8' do
467
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
468
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
469
- end
480
+ it "should not use Encoding.default_internal" do
481
+ with_internal_encoding Encoding::UTF_8 do
482
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
483
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
484
+ end
470
485
 
471
- with_internal_encoding 'us-ascii' do
472
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
473
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
474
- end
486
+ with_internal_encoding Encoding::ASCII do
487
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
488
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
475
489
  end
476
- else
477
- it "should default to utf-8 if Encoding.default_internal is nil" do
478
- with_internal_encoding nil do
479
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
480
- result[field].encoding.should eql(Encoding.find('utf-8'))
481
-
482
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
483
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
484
- result[field].encoding.should eql(Encoding.find('us-ascii'))
485
- client2.close
486
- end
490
+ end
491
+ else
492
+ it "should default to utf-8 if Encoding.default_internal is nil" do
493
+ with_internal_encoding nil do
494
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
495
+ expect(result[field].encoding).to eql(Encoding::UTF_8)
496
+
497
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
498
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
499
+ expect(result[field].encoding).to eql(Encoding::ASCII)
500
+ client2.close
487
501
  end
502
+ end
488
503
 
489
- it "should use Encoding.default_internal" do
490
- with_internal_encoding 'utf-8' do
491
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
492
- result[field].encoding.should eql(Encoding.default_internal)
493
- end
504
+ it "should use Encoding.default_internal" do
505
+ with_internal_encoding Encoding::UTF_8 do
506
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
507
+ expect(result[field].encoding).to eql(Encoding.default_internal)
508
+ end
494
509
 
495
- with_internal_encoding 'us-ascii' do
496
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
497
- result[field].encoding.should eql(Encoding.default_internal)
498
- end
510
+ with_internal_encoding Encoding::ASCII do
511
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
512
+ expect(result[field].encoding).to eql(Encoding.default_internal)
499
513
  end
500
514
  end
501
515
  end