From 79d735312a5c45815a22ec90425a9153a088ae7c Mon Sep 17 00:00:00 2001 From: nkottary Date: Fri, 23 Oct 2015 23:50:07 +0530 Subject: [PATCH] Assert output equals expected result array/dataframe in test files --- src/datetime.jl | 12 ++++++++++++ test/test_basic.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/test_common.jl | 27 +++++++++++++++++++++++++++ test/test_prep.jl | 19 +++++++++++++++++++ 4 files changed, 103 insertions(+) diff --git a/src/datetime.jl b/src/datetime.jl index 6e11b5d..6cc3316 100644 --- a/src/datetime.jl +++ b/src/datetime.jl @@ -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 diff --git a/test/test_basic.jl b/test/test_basic.jl index bc14b9c..e4efff2 100644 --- a/test/test_basic.jl +++ b/test/test_basic.jl @@ -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) @@ -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) @@ -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) diff --git a/test/test_common.jl b/test/test_common.jl index c305aa7..0ba8bef 100644 --- a/test/test_common.jl +++ b/test/test_common.jl @@ -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 @@ -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 diff --git a/test/test_prep.jl b/test/test_prep.jl index 2f35cef..4611661 100644 --- a/test/test_prep.jl +++ b/test/test_prep.jl @@ -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) @@ -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")