|
1 | 1 | # encoding: UTF-8
|
2 |
| -require 'spec_helper' |
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 | 2 |
|
| 3 | +require 'spec_helper' |
19 | 4 |
|
20 | 5 | describe Mysql2::Error do
|
21 |
| - shared_examples "mysql2 error" do |
| 6 | + let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) } |
| 7 | + |
| 8 | + let :error do |
22 | 9 | begin
|
23 |
| - err_client = Mysql2::Client.new(DatabaseCredentials['root']) |
24 |
| - err_client.query("HAHAHA") |
| 10 | + client.query("HAHAHA") |
25 | 11 | rescue Mysql2::Error => e
|
26 | 12 | error = e
|
27 | 13 | ensure
|
28 |
| - err_client.close |
| 14 | + client.close |
29 | 15 | end
|
30 | 16 |
|
31 |
| - subject { error } |
32 |
| - it { should respond_to(:error_number) } |
33 |
| - it { should respond_to(:sql_state) } |
| 17 | + error |
| 18 | + end |
| 19 | + |
| 20 | + it "responds to error_number and sql_state, with aliases" do |
| 21 | + error.should respond_to(:error_number) |
| 22 | + error.should respond_to(:sql_state) |
34 | 23 |
|
35 | 24 | # Mysql gem compatibility
|
36 |
| - it { should respond_to(:errno) } |
37 |
| - it { should respond_to(:error) } |
| 25 | + error.should respond_to(:errno) |
| 26 | + error.should respond_to(:error) |
38 | 27 | end
|
39 | 28 |
|
40 |
| - shared_examples "mysql2 error encoding" do |db_enc, def_enc, err_enc| |
41 |
| - Encoding.default_internal = def_enc |
| 29 | + if "".respond_to? :encoding |
| 30 | + let :error do |
| 31 | + client = Mysql2::Client.new(DatabaseCredentials['root']) |
| 32 | + begin |
| 33 | + client.query("造字") |
| 34 | + rescue Mysql2::Error => e |
| 35 | + error = e |
| 36 | + ensure |
| 37 | + client.close |
| 38 | + end |
42 | 39 |
|
43 |
| - begin |
44 |
| - err_client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => db_enc)) |
45 |
| - err_client.query("造字") |
46 |
| - rescue Mysql2::Error => e |
47 |
| - error = e |
48 |
| - ensure |
49 |
| - err_client.close |
| 40 | + error |
50 | 41 | end
|
51 | 42 |
|
52 |
| - subject { error.message.encoding } |
53 |
| - it "#message should transcode from #{db_enc.inspect} to #{err_enc}" do should eql(err_enc) end |
| 43 | + let :bad_err do |
| 44 | + client = Mysql2::Client.new(DatabaseCredentials['root']) |
| 45 | + begin |
| 46 | + client.query("\xE5\xC6\x7D\x1F") |
| 47 | + rescue Mysql2::Error => e |
| 48 | + error = e |
| 49 | + ensure |
| 50 | + client.close |
| 51 | + end |
54 | 52 |
|
55 |
| - subject { error.error.encoding } |
56 |
| - it "#error should transcode from #{db_enc.inspect} to #{err_enc}" do should eql(err_enc) end |
57 |
| - |
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 |
60 |
| - end |
| 53 | + error |
| 54 | + end |
61 | 55 |
|
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 |
64 |
| - end |
| 56 | + it "returns error messages as UTF-8" do |
| 57 | + error.message.encoding.should eql(Encoding::UTF_8) |
| 58 | + error.message.valid_encoding? |
65 | 59 |
|
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 |
| 60 | + bad_err.message.encoding.should eql(Encoding::UTF_8) |
| 61 | + bad_err.message.valid_encoding? |
69 | 62 |
|
70 |
| - it_behaves_like "mysql2 error" |
| 63 | + bad_err.message.should include("??}\u001F") |
| 64 | + end |
71 | 65 |
|
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 |
| 66 | + it "returns sql state as ASCII" do |
| 67 | + error.sql_state.encoding.should eql(Encoding::US_ASCII) |
| 68 | + error.sql_state.valid_encoding? |
84 | 69 | end
|
85 | 70 | end
|
86 | 71 | end
|
0 commit comments