Skip to content

Commit d06efcc

Browse files
committed
Added support for Pkg.test()
1 parent 6cbccf0 commit d06efcc

File tree

3 files changed

+87
-26
lines changed

3 files changed

+87
-26
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ make sure LD_LIBRARY_PATH contains the MariaDB/MySQL .so file directory path. Us
6464
* OSX: Same as above. In this case the file will be something like libmysqlclient.dylib.
6565
* Windows: There is no `@windows_only lib_choices` currently. Please add one and send a pull request.
6666

67+
# Tests
68+
69+
To run the tests you must have MySQL server running on the host. Set the constants HOST and ROOTPASS
70+
in test/runtests.jl to the host and root password on your test setup. Run the tests using:
71+
```
72+
Pkg.test("MySQL")
73+
```
74+
6775
# Performance
6876

6977
A total of 67,000 insert queries were executed batch wise in batch sizes of 50, 100, 150 ... so on.

test/runtests.jl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using MySQL
2+
using Base.Test
3+
4+
# Change the below values to whatever it is on your test setup.
5+
const HOST = "127.0.0.1"
6+
const ROOTPASS = "root"
7+
8+
for file in ["test_basic.jl", ]
9+
include(file)
10+
end

test/test.jl test/test_basic.jl

+69-26
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1-
using MySQL
1+
# A basic test that includes creating a database, a user, a table and
2+
# then inserting, updating, selecting , etc. And finally cleaning up by
3+
# dropping table, user and database.
24

3-
const HOST = "127.0.0.1"
4-
const USER = "root"
5-
const PASSWD = "root"
6-
const DBNAME = "mysqltest"
7-
8-
function run_query_helper(command, successmsg, failmsg)
5+
function run_query_helper(command, msg)
96
response = MySQL.mysql_query(con.ptr, command)
107

118
if (response == 0)
12-
println(successmsg)
9+
println("SUCCESS: " * msg)
10+
return true
1311
else
14-
println(failmsg)
12+
println("FAILED: " * msg)
13+
return false
1514
end
1615
end
1716

17+
function connect_as_root()
18+
global con = MySQL.mysql_init_and_connect(HOST, "root", ROOTPASS, "")
19+
end
20+
21+
function create_test_database()
22+
command = """CREATE DATABASE mysqltest;"""
23+
@test run_query_helper(command, "Create database")
24+
end
25+
26+
function create_test_user()
27+
command = "CREATE USER test@$HOST IDENTIFIED BY 'test';"
28+
@test run_query_helper(command, "Create user")
29+
end
30+
31+
function grant_test_user_privilege()
32+
command = "GRANT ALL ON mysqltest.* TO test@$HOST;"
33+
@test run_query_helper(command, "Grant privilege")
34+
end
35+
36+
function connect_as_test_user()
37+
global con = MySQL.mysql_init_and_connect(HOST, "test", "test", "mysqltest")
38+
end
39+
1840
function create_table()
1941
command = """CREATE TABLE Employee
2042
(
@@ -26,12 +48,7 @@ function create_table()
2648
LunchTime TIME,
2749
PRIMARY KEY (ID)
2850
);"""
29-
run_query_helper(command, "Create table succeeded", "Create table failed")
30-
end
31-
32-
function drop_table()
33-
command = """DROP TABLE Employee;"""
34-
run_query_helper(command, "Drop table succeeded", "Drop table failed")
51+
@test run_query_helper(command, "Create table")
3552
end
3653

3754
function insert_values()
@@ -42,20 +59,25 @@ function insert_values()
4259
('Jim', 30000.00, '2015-6-2', '2015-9-5 10:05:10', '12:30:00'),
4360
('Tim', 15000.50, '2015-7-25', '2015-10-10 12:12:25', '12:30:00');
4461
"""
45-
run_query_helper(command, "Insert succeeded", "Insert failed")
62+
@test run_query_helper(command, "Insert")
4663
end
4764

4865
function update_values()
4966
command = """UPDATE Employee SET Salary = 25000.00 WHERE ID > 2;"""
50-
run_query_helper(command, "Update success", "Update failed")
67+
@test run_query_helper(command, "Update")
68+
end
69+
70+
function drop_table()
71+
command = """DROP TABLE Employee;"""
72+
@test run_query_helper(command, "Drop table")
5173
end
5274

5375
function do_multi_statement()
5476
command = """INSERT INTO Employee (Name, Salary, JoinDate, LastLogin, LunchTime)
5577
VALUES
5678
('Donald', 30000.00, '2014-2-2', '2015-8-8 13:14:15', '14:01:02');
5779
UPDATE Employee SET LunchTime = '15:00:00' WHERE LENGTH(Name) > 5;"""
58-
run_query_helper(command, "Multi statement success", "Multi statement failed")
80+
@test run_query_helper(command, "Multi statement")
5981
end
6082

6183
function show_as_dataframe()
@@ -80,22 +102,43 @@ function show_as_dataframe()
80102
println(dframe)
81103
end
82104

105+
function drop_test_user()
106+
command = """DROP USER test@$HOST;"""
107+
@test run_query_helper(command, "Drop user")
108+
end
109+
110+
function drop_test_database()
111+
command = """DROP DATABASE mysqltest;"""
112+
@test run_query_helper(command, "Drop database")
113+
end
114+
83115
function run_test()
84-
global con = MySQL.mysql_init_and_connect(HOST, USER, PASSWD, DBNAME)
85-
create_table()
86116

87-
insert_values()
88-
show_as_dataframe()
117+
# Connect as root and setup database, user and privilege
118+
# for the user.
119+
connect_as_root()
120+
create_test_database()
121+
create_test_user()
122+
grant_test_user_privilege()
123+
MySQL.mysql_disconnect(con)
89124

125+
# Connect as test user and do insert, update etc.
126+
# and finally drop the table.
127+
connect_as_test_user()
128+
create_table()
129+
insert_values()
90130
update_values()
91-
show_as_dataframe()
92-
93131
# Subsequent queries fail after multi statement, need to debug.
94132
# do_multi_statement()
95-
# show_as_dataframe()
96-
133+
show_as_dataframe()
97134
drop_table()
98135
MySQL.mysql_disconnect(con)
136+
137+
# Drop the test user and database.
138+
connect_as_root()
139+
drop_test_user()
140+
drop_test_database()
141+
MySQL.mysql_disconnect(con)
99142
end
100143

101144
run_test()

0 commit comments

Comments
 (0)