|
1 | 1 | # encoding: UTF-8
|
2 | 2 | require 'spec_helper'
|
3 | 3 |
|
| 4 | +# The matrix of error encoding tests: |
| 5 | +# ('Enc = X' means 'Encoding.default_internal = X') |
| 6 | +# MySQL < 5.5 MySQL >= 5.5 |
| 7 | +# Ruby 1.8 N/A N/A |
| 8 | +# Ruby 1.9+ |
| 9 | +# Enc = nil |
| 10 | +# :enc = nil BINARY UTF-8 |
| 11 | +# |
| 12 | +# Enc = XYZ |
| 13 | +# :enc = XYZ BINARY XYZ |
| 14 | +# |
| 15 | +# Enc = FOO |
| 16 | +# :enc = BAR BINARY FOO |
| 17 | +# |
| 18 | + |
| 19 | + |
4 | 20 | describe Mysql2::Error do
|
5 |
| - before(:each) do |
| 21 | + shared_examples "mysql2 error" do |
6 | 22 | begin
|
7 |
| - @err_client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8")) |
8 |
| - @err_client.query("HAHAHA") |
| 23 | + err_client = Mysql2::Client.new(DatabaseCredentials['root']) |
| 24 | + err_client.query("HAHAHA") |
9 | 25 | rescue Mysql2::Error => e
|
10 |
| - @error = e |
| 26 | + error = e |
11 | 27 | ensure
|
12 |
| - @err_client.close |
| 28 | + err_client.close |
13 | 29 | end
|
14 | 30 |
|
| 31 | + subject { error } |
| 32 | + it { should respond_to(:error_number) } |
| 33 | + it { should respond_to(:sql_state) } |
| 34 | + |
| 35 | + # Mysql gem compatibility |
| 36 | + it { should respond_to(:errno) } |
| 37 | + it { should respond_to(:error) } |
| 38 | + end |
| 39 | + |
| 40 | + shared_examples "mysql2 error encoding" do |db_enc, def_enc, err_enc| |
| 41 | + Encoding.default_internal = def_enc |
| 42 | + |
15 | 43 | begin
|
16 |
| - @err_client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5")) |
17 |
| - @err_client2.query("HAHAHA") |
| 44 | + err_client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => db_enc)) |
| 45 | + err_client.query("造字") |
18 | 46 | rescue Mysql2::Error => e
|
19 |
| - @error2 = e |
| 47 | + error = e |
20 | 48 | ensure
|
21 |
| - @err_client2.close |
| 49 | + err_client.close |
22 | 50 | end
|
23 |
| - end |
24 | 51 |
|
25 |
| - it "should respond to #error_number" do |
26 |
| - @error.should respond_to(:error_number) |
27 |
| - end |
| 52 | + subject { error.message.encoding } |
| 53 | + it "#message should transcode from #{db_enc.inspect} to #{err_enc}" do should eql(err_enc) end |
28 | 54 |
|
29 |
| - it "should respond to #sql_state" do |
30 |
| - @error.should respond_to(:sql_state) |
31 |
| - end |
| 55 | + subject { error.error.encoding } |
| 56 | + it "#error should transcode from #{db_enc.inspect} to #{err_enc}" do should eql(err_enc) end |
32 | 57 |
|
33 |
| - # Mysql gem compatibility |
34 |
| - it "should alias #error_number to #errno" do |
35 |
| - @error.should respond_to(:errno) |
| 58 | + subject { error.sql_state.encoding } |
| 59 | + it "#sql_state should transcode from #{db_enc.inspect} to #{err_enc}" do should eql(err_enc) end |
36 | 60 | end
|
37 | 61 |
|
38 |
| - it "should alias #message to #error" do |
39 |
| - @error.should respond_to(:error) |
| 62 | + shared_examples "mysql2 error encoding (MySQL < 5.5)" do |db_enc, def_enc, err_enc| |
| 63 | + include_examples "mysql2 error encoding", db_enc, def_enc, err_enc |
40 | 64 | end
|
41 | 65 |
|
42 |
| - unless RUBY_VERSION =~ /1.8/ |
43 |
| - it "#message encoding should match the connection's encoding, or Encoding.default_internal if set" do |
44 |
| - if Encoding.default_internal.nil? |
45 |
| - @error.message.encoding.should eql(@err_client.encoding) |
46 |
| - @error2.message.encoding.should eql(@err_client2.encoding) |
47 |
| - else |
48 |
| - @error.message.encoding.should eql(Encoding.default_internal) |
49 |
| - @error2.message.encoding.should eql(Encoding.default_internal) |
50 |
| - end |
51 |
| - end |
| 66 | + shared_examples "mysql2 error encoding (MySQL >= 5.5)" do |db_enc, def_enc, err_enc| |
| 67 | + include_examples "mysql2 error encoding", db_enc, def_enc, err_enc |
| 68 | + end |
52 | 69 |
|
53 |
| - it "#error encoding should match the connection's encoding, or Encoding.default_internal if set" do |
54 |
| - if Encoding.default_internal.nil? |
55 |
| - @error.error.encoding.should eql(@err_client.encoding) |
56 |
| - @error2.error.encoding.should eql(@err_client2.encoding) |
57 |
| - else |
58 |
| - @error.error.encoding.should eql(Encoding.default_internal) |
59 |
| - @error2.error.encoding.should eql(Encoding.default_internal) |
60 |
| - end |
61 |
| - end |
| 70 | + it_behaves_like "mysql2 error" |
62 | 71 |
|
63 |
| - it "#sql_state encoding should match the connection's encoding, or Encoding.default_internal if set" do |
64 |
| - if Encoding.default_internal.nil? |
65 |
| - @error.sql_state.encoding.should eql(@err_client.encoding) |
66 |
| - @error2.sql_state.encoding.should eql(@err_client2.encoding) |
67 |
| - else |
68 |
| - @error.sql_state.encoding.should eql(Encoding.default_internal) |
69 |
| - @error2.sql_state.encoding.should eql(Encoding.default_internal) |
70 |
| - end |
| 72 | + unless RUBY_VERSION =~ /1.8/ |
| 73 | + mysql_ver = Mysql2::Client.new(DatabaseCredentials['root']).server_info[:id] |
| 74 | + if mysql_ver < 50505 |
| 75 | + it_behaves_like "mysql2 error encoding (MySQL < 5.5)", nil, nil, Encoding::ASCII_8BIT |
| 76 | + it_behaves_like "mysql2 error encoding (MySQL < 5.5)", 'utf8', Encoding::UTF_8, Encoding::ASCII_8BIT |
| 77 | + it_behaves_like "mysql2 error encoding (MySQL < 5.5)", 'big5', Encoding::Big5, Encoding::ASCII_8BIT |
| 78 | + it_behaves_like "mysql2 error encoding (MySQL < 5.5)", 'big5', Encoding::US_ASCII, Encoding::ASCII_8BIT |
| 79 | + else |
| 80 | + it_behaves_like "mysql2 error encoding (MySQL >= 5.5)", nil, nil, Encoding::UTF_8 |
| 81 | + it_behaves_like "mysql2 error encoding (MySQL >= 5.5)", 'utf8', Encoding::UTF_8, Encoding::UTF_8 |
| 82 | + it_behaves_like "mysql2 error encoding (MySQL >= 5.5)", 'big5', Encoding::Big5, Encoding::Big5 |
| 83 | + it_behaves_like "mysql2 error encoding (MySQL >= 5.5)", 'big5', Encoding::US_ASCII, Encoding::US_ASCII |
71 | 84 | end
|
72 | 85 | end
|
73 | 86 | end
|
0 commit comments