-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathruntests.jl
397 lines (344 loc) · 16.7 KB
/
runtests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using Test, MySQL, DBInterface, Tables, Dates, DecFP
conn = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
DBInterface.close!(conn)
# https://github.com/JuliaDatabases/MySQL.jl/issues/170
conn = DBInterface.connect(MySQL.Connection, "mysql://127.0.0.1", "root"; port=3306)
DBInterface.close!(conn)
# AbstractString as a connection parameter or an option
conn = DBInterface.connect(MySQL.Connection, SubString("127.0.0.1"), SubString("root"), SubString(""); port=3306, charset_name=SubString("utf8mb4"))
DBInterface.close!(conn)
# load host/user + options from file
conn = DBInterface.connect(MySQL.Connection, "", ""; option_file=joinpath(dirname(pathof(MySQL)), "../test/", "my.ini"))
@test isopen(conn)
DBInterface.execute(conn, "DROP DATABASE if exists mysqltest")
DBInterface.execute(conn, "CREATE DATABASE mysqltest")
DBInterface.execute(conn, "use mysqltest")
DBInterface.execute(conn, """CREATE TABLE Employee
(
ID INT NOT NULL AUTO_INCREMENT,
OfficeNo TINYINT,
DeptNo SMALLINT,
EmpNo BIGINT UNSIGNED,
Wage FLOAT(7,2),
Salary DOUBLE,
Rate DECIMAL(5, 3),
LunchTime TIME,
JoinDate DATE,
LastLogin DATETIME,
LastLogin2 TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
Initial CHAR(1),
Name VARCHAR(255),
Photo BLOB,
JobType ENUM('HR', 'Management', 'Accounts'),
Senior BIT(1),
PRIMARY KEY (ID)
);""")
DBInterface.execute(conn, """INSERT INTO Employee (OfficeNo, DeptNo, EmpNo, Wage, Salary, Rate, LunchTime, JoinDate, LastLogin, LastLogin2, Initial, Name, Photo, JobType, Senior)
VALUES
(1, 2, 1301, 3.14, 10000.50, 1.001, '12:00:00', '2015-8-3', '2015-9-5 12:31:30', '2015-9-5 12:31:30', 'A', 'John', 'abc', 'HR', b'1'),
(1, 2, 1422, 3.14, 20000.25, 2.002, '13:00:00', '2015-8-4', '2015-10-12 13:12:14', '2015-10-12 13:12:14', 'B', 'Tom', 'def', 'HR', b'1'),
(1, 2, 1567, 3.14, 30000.00, 3.003, '12:30:00', '2015-6-2', '2015-9-5 10:05:10', '2015-9-5 10:05:10', 'C', 'Jim', 'ghi', 'Management', b'0'),
(1, 2, 3200, 3.14, 15000.50, 2.5, '12:30:00', '2015-7-25', '2015-10-10 12:12:25', '2015-10-10 12:12:25', 'D', 'Tim', 'jkl', 'Accounts', b'1');
""")
expected = (
ID = Int32[1, 2, 3, 4],
OfficeNo = Union{Missing, Int8}[1, 1, 1, 1],
DeptNo = Union{Missing, Int16}[2, 2, 2, 2],
EmpNo = Union{Missing, UInt64}[1301, 1422, 1567, 3200],
Wage = Union{Missing, Float32}[3.14, 3.14, 3.14, 3.14],
Salary = Union{Missing, Float64}[10000.5, 20000.25, 30000.0, 15000.5],
Rate = Union{Missing, Dec64}[d64"1.001", d64"2.002", d64"3.003", d64"2.5"],
LunchTime = Union{Missing, Dates.Time}[Dates.Time(12,00,00), Dates.Time(13,00,00), Dates.Time(12,30,00), Dates.Time(12,30,00)],
JoinDate = Union{Missing, Dates.Date}[Date("2015-08-03"), Date("2015-08-04"), Date("2015-06-02"), Date("2015-07-25")],
LastLogin = Union{Missing, Dates.DateTime}[DateTime("2015-09-05T12:31:30"), DateTime("2015-10-12T13:12:14"), DateTime("2015-09-05T10:05:10"), DateTime("2015-10-10T12:12:25")],
LastLogin2 = Dates.DateTime[DateTime("2015-09-05T12:31:30"), DateTime("2015-10-12T13:12:14"), DateTime("2015-09-05T10:05:10"), DateTime("2015-10-10T12:12:25")],
Initial = Union{Missing, String}["A", "B", "C", "D"],
Name = Union{Missing, String}["John", "Tom", "Jim", "Tim"],
Photo = Union{Missing, Vector{UInt8}}[b"abc", b"def", b"ghi", b"jkl"],
JobType = Union{Missing, String}["HR", "HR", "Management", "Accounts"],
Senior = Union{Missing, MySQL.API.Bit}[MySQL.API.Bit(1), MySQL.API.Bit(1), MySQL.API.Bit(0), MySQL.API.Bit(1)],
)
cursor = DBInterface.execute(conn, "select * from Employee")
@test DBInterface.lastrowid(cursor) == 1
@test eltype(cursor) == MySQL.TextRow
@test Tables.istable(cursor)
@test Tables.rowaccess(cursor)
@test Tables.rows(cursor) === cursor
@test Tables.schema(cursor) == Tables.Schema(propertynames(expected), eltype.(collect(expected)))
@test Base.IteratorSize(typeof(cursor)) == Base.HasLength()
@test length(cursor) == 4
row = first(cursor)
@test Base.IndexStyle(typeof(row)) == Base.IndexLinear()
@test length(row) == length(expected)
@test propertynames(row) == collect(propertynames(expected))
for (i, prop) in enumerate(propertynames(row))
@test getproperty(row, prop) == row[prop] == row[i] == expected[prop][1]
end
res = DBInterface.execute(conn, "select * from Employee") |> columntable
@test length(res) == 16
@test length(res[1]) == 4
@test res == expected
# as a prepared statement
stmt = DBInterface.prepare(conn, "select * from Employee")
cursor = DBInterface.execute(stmt)
@test DBInterface.lastrowid(cursor) == 1
@test eltype(cursor) == MySQL.Row
@test Tables.istable(cursor)
@test Tables.rowaccess(cursor)
@test Tables.rows(cursor) === cursor
@test Tables.schema(cursor) == Tables.Schema(propertynames(expected), eltype.(collect(expected)))
@test Base.IteratorSize(typeof(cursor)) == Base.HasLength()
@test length(cursor) == 4
row = first(cursor)
@test Base.IndexStyle(typeof(row)) == Base.IndexLinear()
@test length(row) == length(expected)
@test propertynames(row) == collect(propertynames(expected))
for (i, prop) in enumerate(propertynames(row))
@test getproperty(row, prop) == row[prop] == row[i] == expected[prop][1]
end
res = DBInterface.execute(stmt) |> columntable
@test length(res) == 16
@test length(res[1]) == 4
@test res == expected
@test DBInterface.close!(stmt) === nothing
# insert null row
DBInterface.execute(conn, "INSERT INTO Employee () VALUES ();")
for i = 1:length(expected)
if i == 1
push!(expected[i], 5)
elseif i == 11
else
push!(expected[i], missing)
end
end
res = DBInterface.execute(conn, "select * from Employee") |> columntable
@test length(res) == 16
@test length(res[1]) == 5
for i = 1:length(expected)
if i != 11
@test isequal(res[i], expected[i])
end
end
stmt = DBInterface.prepare(conn, "select * from Employee")
res = DBInterface.execute(stmt) |> columntable
DBInterface.close!(stmt)
@test length(res) == 16
@test length(res[1]) == 5
for i = 1:length(expected)
if i != 11
@test isequal(res[i], expected[i])
end
end
# MySQL.load
MySQL.load(Base.structdiff(expected, NamedTuple{(:LastLogin2, :Senior,)}), conn, "Employee_copy"; limit=4, columnsuffix=Dict(:Name=>"CHARACTER SET utf8mb4"), debug=true)
res = DBInterface.execute(conn, "select * from Employee_copy") |> columntable
@test length(res) == 14
@test length(res[1]) == 4
for nm in keys(res)
@test isequal(res[nm], expected[nm][1:4])
end
# now test insert/parameter binding
DBInterface.execute(conn, "DELETE FROM Employee")
for i = 1:length(expected)
if i != 11
pop!(expected[i])
end
end
stmt = DBInterface.prepare(conn,
"INSERT INTO Employee (OfficeNo, DeptNo, EmpNo, Wage, Salary, Rate, LunchTime, JoinDate, LastLogin, LastLogin2, Initial, Name, Photo, JobType, Senior)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
DBInterface.executemany(stmt, Base.structdiff(expected, NamedTuple{(:ID,)}))
stmt2 = DBInterface.prepare(conn, "select * from Employee")
res = DBInterface.execute(stmt2) |> columntable
DBInterface.close!(stmt2)
@test length(res) == 16
@test length(res[1]) == 4
for i = 1:length(expected)
if i != 11 && i != 1
@test isequal(res[i], expected[i])
end
end
DBInterface.execute(stmt, [missing, missing, missing, missing, missing, missing, missing, missing, missing, DateTime("2015-09-05T12:31:30"), missing, missing, missing, missing, missing])
DBInterface.close!(stmt)
stmt = DBInterface.prepare(conn, "select * from Employee")
res = DBInterface.execute(stmt) |> columntable
DBInterface.close!(stmt)
for i = 1:length(expected)
if i != 11 && i != 1
@test res[i][end] === missing
end
end
# mysql_use_result
res = DBInterface.execute(conn, "select DeptNo, OfficeNo from Employee"; mysql_store_result=false) |> columntable
@test length(res) == 2
@test length(res[1]) == 5
@test isequal(res.OfficeNo, [1, 1, 1, 1, missing])
stmt = DBInterface.prepare(conn, "select DeptNo, OfficeNo from Employee")
res = DBInterface.execute(stmt; mysql_store_result=false) |> columntable
DBInterface.close!(stmt)
@test length(res) == 2
@test length(res[1]) == 5
@test isequal(res.OfficeNo, [1, 1, 1, 1, missing])
stmt = DBInterface.prepare(conn, "select DeptNo, OfficeNo from Employee where OfficeNo = ?")
res = DBInterface.execute(stmt, 1; mysql_store_result=false) |> columntable
DBInterface.close!(stmt)
@test length(res) == 2
@test length(res[1]) == 4
@test isequal(res.OfficeNo, [1, 1, 1, 1])
DBInterface.execute(conn, "DROP TABLE if exists negative_int")
DBInterface.execute(conn, "CREATE TABLE negative_int (id int(11) default null)")
stmt = DBInterface.prepare(conn, "INSERT INTO negative_int (id) VALUES (?);")
DBInterface.execute(stmt, -1)
res = DBInterface.execute(conn, "select id from negative_int") |> columntable
@test length(res) == 1
@test res[1][1] === Int32(-1)
DBInterface.execute(conn, "DROP TABLE if exists text_field")
DBInterface.execute(conn, "CREATE TABLE text_field (id int(11), t text)")
stmt = DBInterface.prepare(conn, "INSERT INTO text_field (id, t) VALUES (?, ?);")
DBInterface.execute(stmt, [-1, "hey there sailor"])
res = DBInterface.execute(conn, "select id, t from text_field") |> columntable
@test length(res) == 2
@test res[2][1] === "hey there sailor"
DBInterface.execute(conn, "DROP TABLE if exists blob_field")
DBInterface.execute(conn, "CREATE TABLE blob_field (id int(11), t blob)")
stmt = DBInterface.prepare(conn, "INSERT INTO blob_field (id, t) VALUES (?, ?);")
DBInterface.execute(stmt, [-1, "hey there sailor"])
res = DBInterface.execute(conn, "select id, t from blob_field") |> columntable
@test length(res) == 2
@test res[2][1] == [0x68, 0x65, 0x79, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65, 0x20, 0x73, 0x61, 0x69, 0x6c, 0x6f, 0x72]
# https://github.com/JuliaDatabases/MySQL.jl/issues/175
DBInterface.execute(conn, "DROP TABLE if exists datetime_field")
DBInterface.execute(conn, "CREATE TABLE datetime_field (id int(11), t DATETIME)")
stmt = DBInterface.prepare(conn, "INSERT INTO datetime_field (id, t) VALUES (?, ?);")
DBInterface.execute(stmt, [1, DateTime(1970, 1, 1, 3)])
resstmt = DBInterface.prepare(conn, "select id, t from datetime_field")
res = DBInterface.execute(resstmt) |> columntable
@test length(res) == 2
@test res[2][1] == DateTime(1970, 1, 1, 3)
# https://github.com/JuliaDatabases/MySQL.jl/issues/165
DBInterface.execute(conn, "DROP TABLE if exists datetime6_field")
DBInterface.execute(conn, "CREATE TABLE datetime6_field (id int(11), t DATETIME(6))")
stmt = DBInterface.prepare(conn, "INSERT INTO datetime6_field (id, t) VALUES (?, ?);")
DBInterface.execute(stmt, [1, DateAndTime(Date(2021, 1, 2), Time(1, 2, 3, 456, 789))])
resstmt = DBInterface.prepare(conn, "select id, t from datetime6_field"; mysql_date_and_time=true)
res = DBInterface.execute(resstmt) |> columntable
@test length(res) == 2
@test res[2][1] == DateAndTime(Date(2021, 1, 2), Time(1, 2, 3, 456, 789))
res = DBInterface.execute(conn, "select id, t from datetime6_field"; mysql_date_and_time=true) |> columntable
@test length(res) == 2
@test res[2][1] == DateAndTime(Date(2021, 1, 2), Time(1, 2, 3, 456, 789))
DBInterface.execute(conn, """
CREATE PROCEDURE get_employee()
BEGIN
select * from Employee;
END
""")
res = DBInterface.execute(conn, "call get_employee()") |> columntable
@test length(res) > 0
@test length(res[1]) == 5
res = DBInterface.execute(conn, "call get_employee()") |> columntable
@test length(res) > 0
@test length(res[1]) == 5
# test that we can call multiple stored procedures in a row w/o collecting results (they get cleaned up properly internally)
res = DBInterface.execute(conn, "call get_employee()")
res = DBInterface.execute(conn, "call get_employee()")
# and for prepared statements
stmt = DBInterface.prepare(conn, "call get_employee()")
res = DBInterface.execute(stmt) |> columntable
@test length(res) > 0
@test length(res[1]) == 5
res = DBInterface.execute(stmt) |> columntable
@test length(res) > 0
@test length(res[1]) == 5
res = DBInterface.execute(stmt)
res = DBInterface.execute(stmt)
results = DBInterface.executemultiple(conn, "select * from Employee; select DeptNo, OfficeNo from Employee where OfficeNo IS NOT NULL")
state = iterate(results)
@test state !== nothing
res, st = state
@test !st
@test length(res) == 5
ret = columntable(res)
@test length(ret[1]) == 5
state = iterate(results, st)
@test state !== nothing
res, st = state
@test !st
@test length(res) == 4
ret = columntable(res)
@test length(ret[1]) == 4
# multiple-queries not supported by mysql w/ prepared statements
@test_throws MySQL.API.StmtError DBInterface.prepare(conn, "select * from Employee; select DeptNo, OfficeNo from Employee where OfficeNo IS NOT NULL")
# GitHub issue [#173](https://github.com/JuliaDatabases/MySQL.jl/issues/173)
DBInterface.execute(conn, "DROP TABLE if exists unsigned_float")
DBInterface.execute(conn, "CREATE TABLE unsigned_float(x FLOAT unsigned)")
DBInterface.execute(conn, "INSERT INTO unsigned_float VALUES (1.1), (1.2)")
res = DBInterface.execute(conn, "select x from unsigned_float") |> columntable
@test res.x == Vector{Float32}([1.1, 1.2])
# end issue #173
# execute fails when the sql is an AbstractString, but not a String (#189)
DBInterface.execute(conn, SubString("select * from Employee"))
stmt = DBInterface.prepare(conn, SubString("select * from Employee"))
DBInterface.execute(stmt)
# AbstractString as an input parameter
DBInterface.execute(conn, "DROP TABLE if exists abstract_string")
DBInterface.execute(conn, "CREATE TABLE abstract_string(str VARCHAR(255))")
stmt = DBInterface.prepare(conn, "INSERT INTO abstract_string (str) VALUES (?)")
DBInterface.execute(stmt, [SubString("foo")])
# escaping AbstractString
@test MySQL.escape(conn, SubString("'); DROP TABLE Employee; --")) == "\\'); DROP TABLE Employee; --"
# https://github.com/JuliaDatabases/MySQL.jl/issues/194
ct = (x = UInt8[1, 2, 3],)
MySQL.load(ct, conn, "test194")
ct2 = columntable(DBInterface.execute(conn, "select * from test194"))
# https://github.com/JuliaDatabases/MySQL.jl/issues/186
DBInterface.execute(conn, "SET SESSION SQL_MODE=''")
dt = DBInterface.execute(conn, "SELECT CAST('0000-00-00' as DATETIME) as dt ") |> Tables.columntable
@test dt.dt[1] == DateTime(0)
dt = DBInterface.execute(conn, "SELECT CAST('0000-00-00' as DATETIME) as dt "; mysql_date_and_time=true) |> Tables.columntable
@test dt.dt[1].date == DateTime(0)
# 156
res = DBInterface.execute(conn, "select * from Employee")
DBInterface.close!(conn)
ret = columntable(res)
# load on closed connection should throw
@test_throws ArgumentError MySQL.load(ct, conn, "test194")
@testset "transactions" begin
conn = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
try
DBInterface.execute(conn, "DROP DATABASE if exists mysqltest")
DBInterface.execute(conn, "CREATE DATABASE mysqltest")
DBInterface.execute(conn, "use mysqltest")
DBInterface.execute(conn, "DROP TABLE IF EXISTS TransactionTest")
DBInterface.execute(conn, "CREATE TABLE TransactionTest (a int)")
conn2 = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
DBInterface.execute(conn2, "use mysqltest")
try
# happy path
DBInterface.transaction(conn) do
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)")
# we can see the result inside our transaction
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
@test result.a == [1]
# and can't see it outside our transaction
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable
@test isempty(result.a)
end
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
@test result.a == [1]
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable
@test result.a == [1]
# roll back due to exception
@test_throws ErrorException DBInterface.transaction(conn) do
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (2)")
error("force rollback")
end
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
@test result.a == [1] # the table did not change
finally
DBInterface.close!(conn2)
end
finally
DBInterface.close!(conn)
end
end