v2 / vlib / db / mysql / mysql_test.v
184 lines · 165 sloc · 4.57 KB · 298a573a3e49302a8d24faec28e187d24acc85df
Raw
1// vtest build: started_mysqld?
2import db.mysql
3import orm
4
5fn test_mysql() {
6 $if !network ? {
7 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
8 eprintln('> This test requires a working mysql server running on localhost.')
9 return
10 }
11 config := mysql.Config{
12 host: '127.0.0.1'
13 port: 3306
14 username: 'root'
15 password: '12345678'
16 dbname: 'mysql'
17 }
18
19 mut db := mysql.connect(config)!
20 defer {
21 db.close() or {}
22 }
23
24 assert db.validate()!
25
26 mut conn := orm.TransactionalConnection(db)
27 mut tx := orm.begin(mut conn)!
28 tx.transaction[int](fn (mut tx orm.Tx) !int {
29 return 1
30 })!
31 tx.commit()!
32
33 mut response := db.exec('drop table if exists users')!
34 assert response == []mysql.Row{}
35
36 response = db.exec('create table if not exists users (
37 id INT PRIMARY KEY AUTO_INCREMENT,
38 username TEXT,
39 last_name TEXT NULL DEFAULT NULL
40 )')!
41 assert response == []mysql.Row{}
42
43 mut result_code := db.exec_none('insert into users (username) values ("jackson")')
44 assert result_code == 0
45 result_code = db.exec_none('insert into users (username) values ("shannon")')
46 assert result_code == 0
47 result_code = db.exec_none('insert into users (username) values ("bailey")')
48 assert result_code == 0
49 result_code = db.exec_none('insert into users (username) values ("blaze")')
50 assert result_code == 0
51 rows := db.exec_param('insert into users (username) values (?)', 'Hi')!
52 assert rows == []mysql.Row{}
53
54 // Regression testing to ensure the query and exec return the same values
55 res := db.query('select * from users')!
56 response = res.rows()
57 assert response[0].vals[1] == 'jackson'
58 response = db.exec('select * from users')!
59 assert response[0].vals[1] == 'jackson'
60
61 response = db.exec('select * from users where id = 400')!
62 assert response.len == 0
63
64 single_row := db.exec_one('select * from users')!
65 assert single_row.vals[1] == 'jackson'
66
67 response = db.exec_param_many('select * from users where username = ?', [
68 'jackson',
69 ])!
70 assert response[0] == mysql.Row{
71 vals: ['1', 'jackson', '']
72 }
73
74 response = db.exec_param_many('select * from users where username = ? and id = ?', [
75 'bailey',
76 '3',
77 ])!
78 assert response[0] == mysql.Row{
79 vals: ['3', 'bailey', '']
80 }
81
82 response = db.exec_param_many('select * from users', [''])!
83 assert response == [
84 mysql.Row{
85 vals: ['1', 'jackson', '']
86 },
87 mysql.Row{
88 vals: ['2', 'shannon', '']
89 },
90 mysql.Row{
91 vals: ['3', 'bailey', '']
92 },
93 mysql.Row{
94 vals: ['4', 'blaze', '']
95 },
96 mysql.Row{
97 vals: ['5', 'Hi', '']
98 },
99 ]
100
101 response = db.exec_param('select * from users where username = ?', 'blaze')!
102 assert response[0] == mysql.Row{
103 vals: ['4', 'blaze', '']
104 }
105
106 // transaction test
107 // turn off `autocommit` first
108 db.autocommit(false)!
109 // begin a new transaction
110 db.begin()!
111 result_code = db.exec_none('insert into users (username) values ("tom")')
112 assert result_code == 0
113 // make a savepoint
114 db.savepoint('savepoint1')!
115 result_code = db.exec_none('insert into users (username) values ("kitty")')
116 assert result_code == 0
117 // rollback to `savepoint1`
118 db.rollback_to('savepoint1')!
119 result_code = db.exec_none('insert into users (username) values ("mars")')
120 assert result_code == 0
121 db.commit()!
122 response = db.exec_param_many('select * from users', [''])!
123 assert response == [
124 mysql.Row{
125 vals: ['1', 'jackson', '']
126 },
127 mysql.Row{
128 vals: ['2', 'shannon', '']
129 },
130 mysql.Row{
131 vals: ['3', 'bailey', '']
132 },
133 mysql.Row{
134 vals: ['4', 'blaze', '']
135 },
136 mysql.Row{
137 vals: ['5', 'Hi', '']
138 },
139 mysql.Row{
140 vals: ['6', 'tom', '']
141 },
142 mysql.Row{
143 vals: ['8', 'mars', '']
144 },
145 ]
146}
147
148fn mysql_query_count_from_shared_connection(db mysql.DB) !int {
149 result := db.query('SELECT COUNT(*) as table_count FROM information_schema.tables')!
150 rows := result.maps()
151 return rows[0]['table_count'].int()
152}
153
154fn test_query_is_serialized_for_shared_connections() {
155 $if !network ? {
156 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
157 eprintln('> This test requires a working mysql server running on localhost.')
158 return
159 }
160 config := mysql.Config{
161 host: '127.0.0.1'
162 port: 3306
163 username: 'root'
164 password: '12345678'
165 dbname: 'mysql'
166 }
167
168 mut db := mysql.connect(config)!
169 defer {
170 db.close() or {}
171 }
172
173 threads := [
174 spawn mysql_query_count_from_shared_connection(db),
175 spawn mysql_query_count_from_shared_connection(db),
176 spawn mysql_query_count_from_shared_connection(db),
177 spawn mysql_query_count_from_shared_connection(db),
178 ]
179 results := threads.wait()!
180 assert results.len == 4
181 for count in results {
182 assert count > 0
183 }
184}
185