Skip to content

Commit 6b9c23d

Browse files
committed
Tests for local infile
1 parent a9571b6 commit 6b9c23d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

spec/mysql2/client_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,49 @@ def connect *args
197197
end
198198
end
199199

200+
context ":local_infile" do
201+
before(:all) do
202+
@client_i = Mysql2::Client.new DatabaseCredentials['root'].merge(:local_infile => true)
203+
local = @client_i.query "SHOW VARIABLES LIKE 'local_infile'"
204+
local_enabled = local.any? {|x| x['Value'] == 'ON'}
205+
pending("DON'T WORRY, THIS TEST PASSES - but LOCAL INFILE is not enabled in your MySQL daemon.") unless local_enabled
206+
207+
@client_i.query %[
208+
CREATE TABLE IF NOT EXISTS infileTest (
209+
id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
210+
foo VARCHAR(10),
211+
bar MEDIUMTEXT
212+
)
213+
]
214+
end
215+
216+
after(:all) do
217+
@client_i.query "DROP TABLE infileTest"
218+
end
219+
220+
it "should raise an error when local_infile is disabled" do
221+
client = Mysql2::Client.new DatabaseCredentials['root'].merge(:local_infile => false)
222+
lambda {
223+
client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
224+
}.should raise_error(Mysql2::Error, %r{command is not allowed})
225+
end
226+
227+
it "should raise an error when a non-existent file is loaded" do
228+
lambda {
229+
@client_i.query "LOAD DATA LOCAL INFILE 'this/file/is/not/here' INTO TABLE infileTest"
230+
}.should_not raise_error(Mysql2::Error, %r{file not found: this/file/is/not/here})
231+
end
232+
233+
it "should LOAD DATA LOCAL INFILE" do
234+
@client_i.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
235+
info = @client_i.query_info
236+
info.should eql({:records => 1, :deleted => 0, :skipped => 0, :warnings => 0})
237+
238+
result = @client_i.query "SELECT * FROM infileTest"
239+
result.first.should eql({'id' => 1, 'foo' => 'Hello', 'bar' => 'World'})
240+
end
241+
end
242+
200243
it "should expect connect_timeout to be a positive integer" do
201244
lambda {
202245
Mysql2::Client.new(:connect_timeout => -1)

spec/test_data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
\N Hello World

0 commit comments

Comments
 (0)