Skip to content

Assert output equals expected result array/dataframe in test files #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ function Base.convert(::Type{MYSQL_TIME}, dtime::MySQLDateTime)
MYSQL_TIME(dtime.date.year, dtime.date.month, dtime.date.day,
dtime.time.hour, dtime.time.minute, dtime.time.second, 0, 0, 0)
end

function ==(a::MySQLDate, b::MySQLDate)
a.year == b.year && a.month == b.month && a.day == b.day
end

function ==(a::MySQLTime, b::MySQLTime)
a.hour == b.hour && a.minute == b.minute && a.second == b.second
end

function ==(a::MySQLDateTime, b::MySQLDateTime)
a.date == b.date && a.time == b.time
end
45 changes: 45 additions & 0 deletions test/test_basic.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
# A basic test that uses `MySQL.mysql_query` to execute the queries in
# test_common.jl

using DataFrames

include("test_common.jl")

const ArrayResults = Array{Any}[
[1, "John", 10000.5f0, MySQLDate("2015-8-3"),
MySQLDateTime("2015-9-5 12:31:30"), MySQLTime("12:0:0"),
1, "HR", 0x01, 1301],

[2, "Tom", 20000.25f0, MySQLDate("2015-8-4"),
MySQLDateTime("2015-10-12 13:12:14"), MySQLTime("13:0:0"),
12, "HR", 0x01, 1422],

[3, "Jim", 25000.0f0, MySQLDate("2015-6-2"),
MySQLDateTime("2015-9-5 10:5:10"), MySQLTime("12:30:0"),
45, "Management", Void, 1567],

[4, "Tim", 25000.0f0, MySQLDate("2015-7-25"),
MySQLDateTime("2015-10-10 12:12:25"), MySQLTime("12:30:0"),
56, "Accounts", 0x01, 3200],

[5, Void, Void, Void, Void, Void, Void, Void, Void, Void]]

const DataFrameResults = DataFrame(
ID=[1, 2, 3, 4, 5],
Name=@data(["John", "Tom", "Jim", "Tim", NA]),
Salary=@data([10000.5, 20000.3, 25000.0, 25000.0, NA]),
JoinDate=@data([MySQLDate("2015-8-3"), MySQLDate("2015-8-4"),
MySQLDate("2015-6-2"), MySQLDate("2015-7-25"), NA]),
LastLogin=@data([MySQLDateTime("2015-9-5 12:31:30"),
MySQLDateTime("2015-10-12 13:12:14"),
MySQLDateTime("2015-9-5 10:5:10"),
MySQLDateTime("2015-10-10 12:12:25"), NA]),
LunchTime=@data([MySQLTime("12:0:0"), MySQLTime("13:0:0"),
MySQLTime("12:30:0"), MySQLTime("12:30:0"), NA]),
OfficeNo=@data([1, 12, 45, 56, NA]),
JobType=@data(["HR", "HR", "Management", "Accounts", NA]),
Senior=@data([0x01, 0x01, NA, 0x01, NA]),
empno=@data([1301, 1422, 1567, 3200, NA]))

function run_query_helper(command, msg)
response = mysql_query(hndl, command)

Expand All @@ -19,6 +57,8 @@ function show_results()
command = """SELECT * FROM Employee;"""
dframe = mysql_execute_query(hndl, command)
println("\n *** Results as Dataframe: \n", dframe)
println(DataFrameResults)
@test dfisequal(dframe, DataFrameResults)

retarr = mysql_execute_query(hndl, command, MYSQL_ARRAY)
println("\n *** Results as Array: \n", retarr)
Expand All @@ -30,8 +70,13 @@ function show_results()

result = mysql_store_result(hndl)

i = 1
for row in MySQLRowIterator(result)
println(row)
for j = 1:length(row)
@test row[j] == ArrayResults[i][j]
end
i += 1
end

mysql_free_result(result)
Expand Down
27 changes: 27 additions & 0 deletions test/test_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# then retrieves the data using a select query and converts the results
# to a julia datastructure.

using DataFrames

function run_query_helper(command, msg)
error("API not implemented: `run_query_helper`")
end
Expand Down Expand Up @@ -126,3 +128,28 @@ function run_test()
@test drop_test_database()
mysql_disconnect(hndl)
end

"""
A function to check if two dataframes are equal
"""
function dfisequal(dfa, dfb)
if size(dfa) != size(dfb)
return false
end

row, col = size(dfa)

for i = 1:col
for j = 1:row
if isna(dfa[col][row]) && isna(dfb[col][row])
continue
elseif isna(dfa[col][row]) || isna(dfb[col][row])
return false
elseif dfa[col][row] != dfb[col][row]
return false
end
end
end

return true
end
19 changes: 19 additions & 0 deletions test/test_prep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

include("test_common.jl")

const DataFrameResultsPrep = DataFrame(
ID=[1, 2, 3, 4, 5],
Name=@data(["John", "Tom", "Jim", "Tim", NA]),
Salary=@data([10000.5, 20000.5, 25000.0, 25000.0, NA]),
JoinDate=@data([MySQLDate("2015-8-3"), MySQLDate("2015-8-4"),
MySQLDate("2015-6-2"), MySQLDate("2015-7-25"), NA]),
LastLogin=@data([MySQLDateTime("2015-9-5 12:31:30"),
MySQLDateTime("2015-10-12 13:12:14"),
MySQLDateTime("2015-9-5 10:5:10"),
MySQLDateTime("2015-10-10 12:12:25"), NA]),
LunchTime=@data([MySQLTime("12:0:0"), MySQLTime("13:0:0"),
MySQLTime("12:30:0"), MySQLTime("12:30:0"), NA]),
OfficeNo=@data([1, 12, 45, 56, NA]),
JobType=@data([NA, NA, NA, NA, NA]),
Senior=@data([NA, NA, NA, NA, NA]),
empno=@data([1301, 1422, 1567, 3200, NA]))


function run_query_helper(command, msg)
stmt = mysql_stmt_init(hndl)

Expand Down Expand Up @@ -132,6 +150,7 @@ function show_results()
dframe = mysql_stmt_result_to_dataframe(stmt)
mysql_stmt_close(stmt)
println(dframe)
@test dfisequal(dframe, DataFrameResultsPrep)
end

println("\n*** Running Prepared Statement Test ***\n")
Expand Down