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.
2
4
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)
9
6
response = MySQL. mysql_query (con. ptr, command)
10
7
11
8
if (response == 0 )
12
- println (successmsg)
9
+ println (" SUCCESS: " * msg)
10
+ return true
13
11
else
14
- println (failmsg)
12
+ println (" FAILED: " * msg)
13
+ return false
15
14
end
16
15
end
17
16
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
+
18
40
function create_table ()
19
41
command = """ CREATE TABLE Employee
20
42
(
@@ -26,12 +48,7 @@ function create_table()
26
48
LunchTime TIME,
27
49
PRIMARY KEY (ID)
28
50
);"""
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" )
35
52
end
36
53
37
54
function insert_values ()
@@ -42,20 +59,25 @@ function insert_values()
42
59
('Jim', 30000.00, '2015-6-2', '2015-9-5 10:05:10', '12:30:00'),
43
60
('Tim', 15000.50, '2015-7-25', '2015-10-10 12:12:25', '12:30:00');
44
61
"""
45
- run_query_helper (command, " Insert succeeded " , " Insert failed " )
62
+ @test run_query_helper (command, " Insert" )
46
63
end
47
64
48
65
function update_values ()
49
66
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" )
51
73
end
52
74
53
75
function do_multi_statement ()
54
76
command = """ INSERT INTO Employee (Name, Salary, JoinDate, LastLogin, LunchTime)
55
77
VALUES
56
78
('Donald', 30000.00, '2014-2-2', '2015-8-8 13:14:15', '14:01:02');
57
79
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" )
59
81
end
60
82
61
83
function show_as_dataframe ()
@@ -80,22 +102,43 @@ function show_as_dataframe()
80
102
println (dframe)
81
103
end
82
104
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
+
83
115
function run_test ()
84
- global con = MySQL. mysql_init_and_connect (HOST, USER, PASSWD, DBNAME)
85
- create_table ()
86
116
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)
89
124
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 ()
90
130
update_values ()
91
- show_as_dataframe ()
92
-
93
131
# Subsequent queries fail after multi statement, need to debug.
94
132
# do_multi_statement()
95
- # show_as_dataframe()
96
-
133
+ show_as_dataframe ()
97
134
drop_table ()
98
135
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)
99
142
end
100
143
101
144
run_test ()
0 commit comments