mysql2 0.4.4 → 0.4.10

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.
@@ -1,6 +1,5 @@
1
1
  # encoding: UTF-8
2
2
  require 'spec_helper'
3
- require 'stringio'
4
3
 
5
4
  RSpec.describe Mysql2::Client do
6
5
  context "using defaults file" do
@@ -8,14 +7,13 @@ RSpec.describe Mysql2::Client do
8
7
 
9
8
  it "should not raise an exception for valid defaults group" do
10
9
  expect {
11
- opts = DatabaseCredentials['root'].merge(:default_file => cnf_file, :default_group => "test")
12
- @client = Mysql2::Client.new(opts)
10
+ new_client(:default_file => cnf_file, :default_group => "test")
13
11
  }.not_to raise_error
14
12
  end
15
13
 
16
14
  it "should not raise an exception without default group" do
17
15
  expect {
18
- @client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:default_file => cnf_file))
16
+ new_client(:default_file => cnf_file)
19
17
  }.not_to raise_error
20
18
  end
21
19
  end
@@ -24,23 +22,29 @@ RSpec.describe Mysql2::Client do
24
22
  expect {
25
23
  # The odd local host IP address forces the mysql client library to
26
24
  # use a TCP socket rather than a domain socket.
27
- Mysql2::Client.new DatabaseCredentials['root'].merge('host' => '127.0.0.2', 'port' => 999999)
25
+ new_client('host' => '127.0.0.2', 'port' => 999999)
28
26
  }.to raise_error(Mysql2::Error)
29
27
  end
30
28
 
31
29
  it "should raise an exception on create for invalid encodings" do
32
30
  expect {
33
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "fake"))
31
+ new_client(:encoding => "fake")
34
32
  }.to raise_error(Mysql2::Error)
35
33
  end
36
34
 
35
+ it "should raise an exception on non-string encodings" do
36
+ expect {
37
+ new_client(:encoding => :fake)
38
+ }.to raise_error(TypeError)
39
+ end
40
+
37
41
  it "should not raise an exception on create for a valid encoding" do
38
42
  expect {
39
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
43
+ new_client(:encoding => "utf8")
40
44
  }.not_to raise_error
41
45
 
42
46
  expect {
43
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
47
+ new_client(DatabaseCredentials['root'].merge(:encoding => "big5"))
44
48
  }.not_to raise_error
45
49
  end
46
50
 
@@ -83,7 +87,7 @@ RSpec.describe Mysql2::Client do
83
87
  it "should execute init command" do
84
88
  options = DatabaseCredentials['root'].dup
85
89
  options[:init_command] = "SET @something = 'setting_value';"
86
- client = Mysql2::Client.new(options)
90
+ client = new_client(options)
87
91
  result = client.query("SELECT @something;")
88
92
  expect(result.first['@something']).to eq('setting_value')
89
93
  end
@@ -92,7 +96,7 @@ RSpec.describe Mysql2::Client do
92
96
  options = DatabaseCredentials['root'].dup
93
97
  options[:init_command] = "SET @something = 'setting_value';"
94
98
  options[:reconnect] = true
95
- client = Mysql2::Client.new(options)
99
+ client = new_client(options)
96
100
 
97
101
  result = client.query("SELECT @something;")
98
102
  expect(result.first['@something']).to eq('setting_value')
@@ -133,15 +137,13 @@ RSpec.describe Mysql2::Client do
133
137
  ssl_client = nil
134
138
  expect {
135
139
  # rubocop:disable Style/TrailingComma
136
- ssl_client = Mysql2::Client.new(
137
- DatabaseCredentials['root'].merge(
138
- 'host' => 'mysql2gem.example.com', # must match the certificates
139
- :sslkey => '/etc/mysql/client-key.pem',
140
- :sslcert => '/etc/mysql/client-cert.pem',
141
- :sslca => '/etc/mysql/ca-cert.pem',
142
- :sslcipher => 'DHE-RSA-AES256-SHA',
143
- :sslverify => true
144
- )
140
+ ssl_client = new_client(
141
+ 'host' => 'mysql2gem.example.com', # must match the certificates
142
+ :sslkey => '/etc/mysql/client-key.pem',
143
+ :sslcert => '/etc/mysql/client-cert.pem',
144
+ :sslca => '/etc/mysql/ca-cert.pem',
145
+ :sslcipher => 'DHE-RSA-AES256-SHA',
146
+ :sslverify => true
145
147
  )
146
148
  # rubocop:enable Style/TrailingComma
147
149
  }.not_to raise_error
@@ -152,8 +154,6 @@ RSpec.describe Mysql2::Client do
152
154
 
153
155
  expect(ssl_client.ssl_cipher).not_to be_empty
154
156
  expect(results['Ssl_cipher']).to eql(ssl_client.ssl_cipher)
155
-
156
- ssl_client.close
157
157
  end
158
158
 
159
159
  def run_gc
@@ -167,10 +167,21 @@ RSpec.describe Mysql2::Client do
167
167
 
168
168
  it "should terminate connections when calling close" do
169
169
  expect {
170
- Mysql2::Client.new(DatabaseCredentials['root']).close
170
+ client = Mysql2::Client.new(DatabaseCredentials['root'])
171
+ connection_id = client.thread_id
172
+ client.close
173
+
174
+ # mysql_close sends a quit command without waiting for a response
175
+ # so give the server some time to handle the detect the closed connection
176
+ closed = false
177
+ 10.times do
178
+ closed = @client.query("SHOW PROCESSLIST").none? { |row| row['Id'] == connection_id }
179
+ break if closed
180
+ sleep(0.1)
181
+ end
182
+ expect(closed).to eq(true)
171
183
  }.to_not change {
172
- @client.query("SHOW STATUS LIKE 'Aborted_%'").to_a +
173
- @client.query("SHOW STATUS LIKE 'Threads_connected'").to_a
184
+ @client.query("SHOW STATUS LIKE 'Aborted_%'").to_a
174
185
  }
175
186
  end
176
187
 
@@ -194,37 +205,35 @@ RSpec.describe Mysql2::Client do
194
205
 
195
206
  context "#automatic_close" do
196
207
  it "is enabled by default" do
197
- client = Mysql2::Client.new(DatabaseCredentials['root'])
198
- expect(client.automatic_close?).to be(true)
208
+ expect(new_client.automatic_close?).to be(true)
199
209
  end
200
210
 
201
211
  if RUBY_PLATFORM =~ /mingw|mswin/
202
212
  it "cannot be disabled" do
203
- stderr, $stderr = $stderr, StringIO.new
204
-
205
- begin
206
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:automatic_close => false))
207
- expect($stderr.string).to include('always closed by garbage collector')
208
- $stderr.reopen
209
-
210
- client = Mysql2::Client.new(DatabaseCredentials['root'])
213
+ expect do
214
+ client = new_client(:automatic_close => false)
215
+ expect(client.automatic_close?).to be(true)
216
+ end.to output(/always closed by garbage collector/).to_stderr
217
+
218
+ expect do
219
+ client = new_client(:automatic_close => true)
220
+ expect(client.automatic_close?).to be(true)
221
+ end.to_not output(/always closed by garbage collector/).to_stderr
222
+
223
+ expect do
224
+ client = new_client(:automatic_close => true)
211
225
  client.automatic_close = false
212
- expect($stderr.string).to include('always closed by garbage collector')
213
- $stderr.reopen
214
-
215
- expect { client.automatic_close = true }.to_not change { $stderr.string }
216
- ensure
217
- $stderr = stderr
218
- end
226
+ expect(client.automatic_close?).to be(true)
227
+ end.to output(/always closed by garbage collector/).to_stderr
219
228
  end
220
229
  else
221
230
  it "can be configured" do
222
- client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:automatic_close => false))
231
+ client = new_client(:automatic_close => false)
223
232
  expect(client.automatic_close?).to be(false)
224
233
  end
225
234
 
226
235
  it "can be assigned" do
227
- client = Mysql2::Client.new(DatabaseCredentials['root'])
236
+ client = new_client
228
237
  client.automatic_close = false
229
238
  expect(client.automatic_close?).to be(false)
230
239
 
@@ -243,33 +252,31 @@ RSpec.describe Mysql2::Client do
243
252
  client = Mysql2::Client.new(DatabaseCredentials['root'])
244
253
  client.automatic_close = false
245
254
 
246
- # this empty `fork` call fixes this tests on RBX; without it, the next
247
- # `fork` call hangs forever. WTF?
248
- fork {}
249
-
250
- fork do
255
+ child = fork do
251
256
  client.query('SELECT 1')
252
257
  client = nil
253
258
  run_gc
254
259
  end
255
260
 
256
- Process.wait
261
+ Process.wait(child)
257
262
 
258
263
  # this will throw an error if the underlying socket was shutdown by the
259
264
  # child's GC
260
265
  expect { client.query('SELECT 1') }.to_not raise_exception
266
+ client.close
261
267
  end
262
268
  end
263
269
  end
264
270
 
265
271
  it "should be able to connect to database with numeric-only name" do
266
- creds = DatabaseCredentials['numericuser']
267
- @client.query "CREATE DATABASE IF NOT EXISTS `#{creds['database']}`"
268
- @client.query "GRANT ALL ON `#{creds['database']}`.* TO #{creds['username']}@`#{creds['host']}`"
272
+ database = 1235
273
+ @client.query "CREATE DATABASE IF NOT EXISTS `#{database}`"
269
274
 
270
- expect { Mysql2::Client.new(creds) }.not_to raise_error
275
+ expect {
276
+ new_client('database' => database)
277
+ }.not_to raise_error
271
278
 
272
- @client.query "DROP DATABASE IF EXISTS `#{creds['database']}`"
279
+ @client.query "DROP DATABASE IF EXISTS `#{database}`"
273
280
  end
274
281
 
275
282
  it "should respond to #close" do
@@ -283,6 +290,25 @@ RSpec.describe Mysql2::Client do
283
290
  }.to raise_error(Mysql2::Error)
284
291
  end
285
292
 
293
+ context "#closed?" do
294
+ it "should return false when connected" do
295
+ expect(@client.closed?).to eql(false)
296
+ end
297
+
298
+ it "should return true after close" do
299
+ @client.close
300
+ expect(@client.closed?).to eql(true)
301
+ end
302
+ end
303
+
304
+ it "should not try to query closed mysql connection" do
305
+ client = new_client(:reconnect => true)
306
+ expect(client.close).to be_nil
307
+ expect {
308
+ client.query "SELECT 1"
309
+ }.to raise_error(Mysql2::Error)
310
+ end
311
+
286
312
  it "should respond to #query" do
287
313
  expect(@client).to respond_to(:query)
288
314
  end
@@ -301,8 +327,8 @@ RSpec.describe Mysql2::Client do
301
327
  context "when has a warnings" do
302
328
  it "should > 0" do
303
329
  # "the statement produces extra information that can be viewed by issuing a SHOW WARNINGS"
304
- # http://dev.mysql.com/doc/refman/5.0/en/explain-extended.html
305
- @client.query("explain extended select 1")
330
+ # https://dev.mysql.com/doc/refman/5.7/en/show-warnings.html
331
+ @client.query('DROP TABLE IF EXISTS test.no_such_table')
306
332
  expect(@client.warning_count).to be > 0
307
333
  end
308
334
  end
@@ -339,67 +365,72 @@ RSpec.describe Mysql2::Client do
339
365
 
340
366
  context ":local_infile" do
341
367
  before(:all) do
342
- @client_i = Mysql2::Client.new DatabaseCredentials['root'].merge(:local_infile => true)
343
- local = @client_i.query "SHOW VARIABLES LIKE 'local_infile'"
344
- local_enabled = local.any? { |x| x['Value'] == 'ON' }
345
- pending("DON'T WORRY, THIS TEST PASSES - but LOCAL INFILE is not enabled in your MySQL daemon.") unless local_enabled
346
-
347
- @client_i.query %[
348
- CREATE TABLE IF NOT EXISTS infileTest (
349
- id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
350
- foo VARCHAR(10),
351
- bar MEDIUMTEXT
352
- )
353
- ]
368
+ new_client(:local_infile => true) do |client|
369
+ local = client.query "SHOW VARIABLES LIKE 'local_infile'"
370
+ local_enabled = local.any? { |x| x['Value'] == 'ON' }
371
+ skip("DON'T WORRY, THIS TEST PASSES - but LOCAL INFILE is not enabled in your MySQL daemon.") unless local_enabled
372
+
373
+ client.query %[
374
+ CREATE TABLE IF NOT EXISTS infileTest (
375
+ id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
376
+ foo VARCHAR(10),
377
+ bar MEDIUMTEXT
378
+ )
379
+ ]
380
+ end
354
381
  end
355
382
 
356
383
  after(:all) do
357
- @client_i.query "DROP TABLE infileTest"
384
+ new_client do |client|
385
+ client.query "DROP TABLE IF EXISTS infileTest"
386
+ end
358
387
  end
359
388
 
360
389
  it "should raise an error when local_infile is disabled" do
361
- client = Mysql2::Client.new DatabaseCredentials['root'].merge(:local_infile => false)
390
+ client = new_client(:local_infile => false)
362
391
  expect {
363
392
  client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
364
393
  }.to raise_error(Mysql2::Error, /command is not allowed/)
365
394
  end
366
395
 
367
396
  it "should raise an error when a non-existent file is loaded" do
397
+ client = new_client(:local_infile => true)
368
398
  expect {
369
- @client_i.query "LOAD DATA LOCAL INFILE 'this/file/is/not/here' INTO TABLE infileTest"
399
+ client.query "LOAD DATA LOCAL INFILE 'this/file/is/not/here' INTO TABLE infileTest"
370
400
  }.to raise_error(Mysql2::Error, 'No such file or directory: this/file/is/not/here')
371
401
  end
372
402
 
373
403
  it "should LOAD DATA LOCAL INFILE" do
374
- @client_i.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
375
- info = @client_i.query_info
404
+ client = new_client(:local_infile => true)
405
+ client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
406
+ info = client.query_info
376
407
  expect(info).to eql(:records => 1, :deleted => 0, :skipped => 0, :warnings => 0)
377
408
 
378
- result = @client_i.query "SELECT * FROM infileTest"
409
+ result = client.query "SELECT * FROM infileTest"
379
410
  expect(result.first).to eql('id' => 1, 'foo' => 'Hello', 'bar' => 'World')
380
411
  end
381
412
  end
382
413
 
383
414
  it "should expect connect_timeout to be a positive integer" do
384
415
  expect {
385
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:connect_timeout => -1))
416
+ new_client(:connect_timeout => -1)
386
417
  }.to raise_error(Mysql2::Error)
387
418
  end
388
419
 
389
420
  it "should expect read_timeout to be a positive integer" do
390
421
  expect {
391
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:read_timeout => -1))
422
+ new_client(:read_timeout => -1)
392
423
  }.to raise_error(Mysql2::Error)
393
424
  end
394
425
 
395
426
  it "should expect write_timeout to be a positive integer" do
396
427
  expect {
397
- Mysql2::Client.new(DatabaseCredentials['root'].merge(:write_timeout => -1))
428
+ new_client(:write_timeout => -1)
398
429
  }.to raise_error(Mysql2::Error)
399
430
  end
400
431
 
401
432
  it "should allow nil read_timeout" do
402
- client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:read_timeout => nil))
433
+ client = new_client(:read_timeout => nil)
403
434
 
404
435
  expect(client.read_timeout).to be_nil
405
436
  end
@@ -469,6 +500,25 @@ RSpec.describe Mysql2::Client do
469
500
  }.to raise_error(Mysql2::Error)
470
501
  end
471
502
 
503
+ it "should detect closed connection on query read error" do
504
+ connection_id = @client.thread_id
505
+ Thread.new do
506
+ sleep(0.1)
507
+ Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
508
+ supervisor.query("KILL #{connection_id}")
509
+ end.close
510
+ end
511
+ expect {
512
+ @client.query("SELECT SLEEP(1)")
513
+ }.to raise_error(Mysql2::Error, /Lost connection to MySQL server/)
514
+
515
+ if RUBY_PLATFORM !~ /mingw|mswin/
516
+ expect {
517
+ @client.socket
518
+ }.to raise_error(Mysql2::Error, 'MySQL client is not connected')
519
+ end
520
+ end
521
+
472
522
  if RUBY_PLATFORM !~ /mingw|mswin/
473
523
  it "should not allow another query to be sent without fetching a result first" do
474
524
  @client.query("SELECT 1", :async => true)
@@ -485,7 +535,7 @@ RSpec.describe Mysql2::Client do
485
535
  end
486
536
 
487
537
  it "should timeout if we wait longer than :read_timeout" do
488
- client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:read_timeout => 0))
538
+ client = new_client(:read_timeout => 0)
489
539
  expect {
490
540
  client.query('SELECT SLEEP(0.1)')
491
541
  }.to raise_error(Mysql2::Error)
@@ -533,15 +583,6 @@ RSpec.describe Mysql2::Client do
533
583
  }.to raise_error(Mysql2::Error)
534
584
  end
535
585
 
536
- it 'should be impervious to connection-corrupting timeouts in #query' do
537
- pending('`Thread.handle_interrupt` is not defined') unless Thread.respond_to?(:handle_interrupt)
538
- # attempt to break the connection
539
- expect { Timeout.timeout(0.1) { @client.query('SELECT SLEEP(0.2)') } }.to raise_error(Timeout::Error)
540
-
541
- # expect the connection to not be broken
542
- expect { @client.query('SELECT 1') }.to_not raise_error
543
- end
544
-
545
586
  it 'should be impervious to connection-corrupting timeouts in #execute' do
546
587
  # the statement handle gets corrupted and will segfault the tests if interrupted,
547
588
  # so we can't even use pending on this test, really have to skip it on older Rubies.
@@ -559,26 +600,26 @@ RSpec.describe Mysql2::Client do
559
600
  context 'when a non-standard exception class is raised' do
560
601
  it "should close the connection when an exception is raised" do
561
602
  expect { Timeout.timeout(0.1, ArgumentError) { @client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
562
- expect { @client.query('SELECT 1') }.to raise_error(Mysql2::Error, 'closed MySQL connection')
603
+ expect { @client.query('SELECT 1') }.to raise_error(Mysql2::Error, 'MySQL client is not connected')
563
604
  end
564
605
 
565
606
  it "should handle Timeouts without leaving the connection hanging if reconnect is true" do
566
- if RUBY_PLATFORM.include?('darwin') && Mysql2::Client.info.fetch(:version).start_with?('5.5')
567
- pending('libmysqlclient 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
607
+ if RUBY_PLATFORM.include?('darwin') && @client.server_info.fetch(:version).start_with?('5.5')
608
+ pending('MySQL 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
568
609
  end
569
610
 
570
- client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:reconnect => true))
611
+ client = new_client(:reconnect => true)
571
612
 
572
613
  expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
573
614
  expect { client.query('SELECT 1') }.to_not raise_error
574
615
  end
575
616
 
576
617
  it "should handle Timeouts without leaving the connection hanging if reconnect is set to true after construction" do
577
- if RUBY_PLATFORM.include?('darwin') && Mysql2::Client.info.fetch(:version).start_with?('5.5')
578
- pending('libmysqlclient 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
618
+ if RUBY_PLATFORM.include?('darwin') && @client.server_info.fetch(:version).start_with?('5.5')
619
+ pending('MySQL 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
579
620
  end
580
621
 
581
- client = Mysql2::Client.new(DatabaseCredentials['root'])
622
+ client = new_client
582
623
 
583
624
  expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
584
625
  expect { client.query('SELECT 1') }.to raise_error(Mysql2::Error)
@@ -596,8 +637,9 @@ RSpec.describe Mysql2::Client do
596
637
  # Note that each thread opens its own database connection
597
638
  threads = 5.times.map do
598
639
  Thread.new do
599
- client = Mysql2::Client.new(DatabaseCredentials.fetch('root'))
600
- client.query("SELECT SLEEP(#{sleep_time})")
640
+ new_client do |client|
641
+ client.query("SELECT SLEEP(#{sleep_time})")
642
+ end
601
643
  Thread.current.object_id
602
644
  end
603
645
  end
@@ -610,10 +652,11 @@ RSpec.describe Mysql2::Client do
610
652
  end
611
653
 
612
654
  it "evented async queries should be supported" do
655
+ skip("ruby 1.8 doesn't support IO.for_fd options") if RUBY_VERSION.start_with?("1.8.")
613
656
  # should immediately return nil
614
657
  expect(@client.query("SELECT sleep(0.1)", :async => true)).to eql(nil)
615
658
 
616
- io_wrapper = IO.for_fd(@client.socket)
659
+ io_wrapper = IO.for_fd(@client.socket, :autoclose => false)
617
660
  loops = 0
618
661
  loop do
619
662
  if IO.select([io_wrapper], nil, nil, 0.05)
@@ -633,7 +676,7 @@ RSpec.describe Mysql2::Client do
633
676
 
634
677
  context "Multiple results sets" do
635
678
  before(:each) do
636
- @multi_client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:flags => Mysql2::Client::MULTI_STATEMENTS))
679
+ @multi_client = new_client(:flags => Mysql2::Client::MULTI_STATEMENTS)
637
680
  end
638
681
 
639
682
  it "should raise an exception when one of multiple statements fails" do
@@ -711,7 +754,7 @@ RSpec.describe Mysql2::Client do
711
754
  it "#socket should raise as it's not supported" do
712
755
  expect {
713
756
  @client.socket
714
- }.to raise_error(Mysql2::Error)
757
+ }.to raise_error(Mysql2::Error, /Raw access to the mysql file descriptor isn't supported on Windows/)
715
758
  end
716
759
  end
717
760
 
@@ -790,7 +833,7 @@ RSpec.describe Mysql2::Client do
790
833
  context 'when mysql encoding is not utf8' do
791
834
  before { pending('Encoding is undefined') unless defined?(Encoding) }
792
835
 
793
- let(:client) { Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "ujis")) }
836
+ let(:client) { new_client(:encoding => "ujis") }
794
837
 
795
838
  it 'should return a internal encoding string if Encoding.default_internal is set' do
796
839
  with_internal_encoding Encoding::UTF_8 do
@@ -859,7 +902,7 @@ RSpec.describe Mysql2::Client do
859
902
  with_internal_encoding nil do
860
903
  expect(@client.server_info[:version].encoding).to eql(Encoding::UTF_8)
861
904
 
862
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
905
+ client2 = new_client(:encoding => 'ascii')
863
906
  expect(client2.server_info[:version].encoding).to eql(Encoding::ASCII)
864
907
  end
865
908
  end
@@ -877,11 +920,11 @@ RSpec.describe Mysql2::Client do
877
920
 
878
921
  it "should raise a Mysql2::Error exception upon connection failure" do
879
922
  expect {
880
- Mysql2::Client.new :host => "localhost", :username => 'asdfasdf8d2h', :password => 'asdfasdfw42'
923
+ new_client(:host => "localhost", :username => 'asdfasdf8d2h', :password => 'asdfasdfw42')
881
924
  }.to raise_error(Mysql2::Error)
882
925
 
883
926
  expect {
884
- Mysql2::Client.new DatabaseCredentials['root']
927
+ new_client(DatabaseCredentials['root'])
885
928
  }.not_to raise_error
886
929
  end
887
930
 
@@ -983,6 +1026,11 @@ RSpec.describe Mysql2::Client do
983
1026
  expect(@client.ping).to eql(false)
984
1027
  end
985
1028
 
1029
+ it "should be able to connect using plaintext password" do
1030
+ client = new_client(:enable_cleartext_plugin => true)
1031
+ client.query('SELECT 1')
1032
+ end
1033
+
986
1034
  unless RUBY_VERSION =~ /1.8/
987
1035
  it "should respond to #encoding" do
988
1036
  expect(@client).to respond_to(:encoding)
@@ -3,11 +3,9 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Mysql2::Error do
6
- let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) }
7
-
8
6
  let(:error) do
9
7
  begin
10
- client.query("HAHAHA")
8
+ @client.query("HAHAHA")
11
9
  rescue Mysql2::Error => e
12
10
  error = e
13
11
  end
@@ -28,16 +26,16 @@ RSpec.describe Mysql2::Error do
28
26
  let(:valid_utf8) { '造字' }
29
27
  let(:error) do
30
28
  begin
31
- client.query(valid_utf8)
29
+ @client.query(valid_utf8)
32
30
  rescue Mysql2::Error => e
33
31
  e
34
32
  end
35
33
  end
36
34
 
37
- let(:invalid_utf8) { "\xE5\xC6\x7D\x1F" }
35
+ let(:invalid_utf8) { ["e5c67d1f"].pack('H*').force_encoding(Encoding::UTF_8) }
38
36
  let(:bad_err) do
39
37
  begin
40
- client.query(invalid_utf8)
38
+ @client.query(invalid_utf8)
41
39
  rescue Mysql2::Error => e
42
40
  e
43
41
  end