v / vlib / db / mysql / prepared_stmt_test.v
169 lines · 151 sloc · 3.85 KB · 8b1b2501d3cdf111cdd8ccd24a6a11573dd4b4cf
Raw
1// vtest build: started_mysqld?
2import db.mysql
3
4fn test_prep() {
5 $if !network ? {
6 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
7 eprintln('> This test requires a working mysql server running on localhost.')
8 return
9 }
10 config := mysql.Config{
11 host: '127.0.0.1'
12 port: 3306
13 username: 'root'
14 password: '12345678'
15 dbname: 'mysql'
16 }
17
18 db := mysql.connect(config)!
19
20 mut response := db.exec('drop table if exists test')!
21 assert response == []mysql.Row{}
22
23 response = db.exec('create table if not exists test (
24 id INT PRIMARY KEY AUTO_INCREMENT,
25 value TEXT)')!
26 assert response == []mysql.Row{}
27
28 stmt := db.prepare('insert into test (value) values (?)')!
29 defer {
30 stmt.close()
31 }
32
33 names := ['jackson', 'hello', 'Disney', 'Marz', 'Bailey', 'Claxton']
34 for name in names {
35 response = stmt.execute([name])!
36 assert response == []mysql.Row{}
37 }
38
39 response = db.exec_param_many('select * from test', [''])!
40 assert response == [
41 mysql.Row{
42 vals: ['1', 'jackson']
43 },
44 mysql.Row{
45 vals: ['2', 'hello']
46 },
47 mysql.Row{
48 vals: ['3', 'Disney']
49 },
50 mysql.Row{
51 vals: ['4', 'Marz']
52 },
53 mysql.Row{
54 vals: ['5', 'Bailey']
55 },
56 mysql.Row{
57 vals: ['6', 'Claxton']
58 },
59 ]
60}
61
62fn test_stmt_bind_result_buffer_without_bind_res() {
63 $if !network ? {
64 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
65 eprintln('> This test requires a working mysql server running on localhost.')
66 return
67 }
68 config := mysql.Config{
69 host: '127.0.0.1'
70 port: 3306
71 username: 'root'
72 password: '12345678'
73 dbname: 'mysql'
74 }
75
76 mut db := mysql.connect(config)!
77 defer {
78 db.close() or {}
79 }
80
81 db.exec('drop table if exists stmt_result_buffer_test')!
82 db.exec('create table if not exists stmt_result_buffer_test (
83 id INT PRIMARY KEY AUTO_INCREMENT,
84 value TEXT)')!
85 db.exec_param('insert into stmt_result_buffer_test (value) values (?)', 'hello')!
86
87 mut stmt := db.init_stmt('select value from stmt_result_buffer_test where id = ?')
88 defer {
89 stmt.close() or {}
90 }
91 stmt.prepare()!
92 id := 1
93 stmt.bind_int(&id)
94 stmt.bind_params()!
95 stmt.execute()!
96 stmt.bind_result_buffer()!
97 stmt.store_result()!
98 first_fetch := stmt.fetch_stmt()!
99 assert first_fetch != 100
100 second_fetch := stmt.fetch_stmt()!
101 assert second_fetch == 100
102}
103
104fn test_stmt_prepare_returns_mysql_error_code() {
105 $if !network ? {
106 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
107 eprintln('> This test requires a working mysql server running on localhost.')
108 return
109 }
110 config := mysql.Config{
111 host: '127.0.0.1'
112 port: 3306
113 username: 'root'
114 password: '12345678'
115 dbname: 'mysql'
116 }
117
118 mut db := mysql.connect(config)!
119 defer {
120 db.close() or {}
121 }
122
123 mut stmt := db.init_stmt('insert into missing_stmt_errno_test (value) values (?)')
124 defer {
125 stmt.close() or {}
126 }
127 stmt.prepare() or {
128 assert err.code() == 1146
129 return
130 }
131 assert false
132}
133
134fn test_prepare_execute_returns_mysql_error_code() {
135 $if !network ? {
136 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
137 eprintln('> This test requires a working mysql server running on localhost.')
138 return
139 }
140 config := mysql.Config{
141 host: '127.0.0.1'
142 port: 3306
143 username: 'root'
144 password: '12345678'
145 dbname: 'mysql'
146 }
147
148 mut db := mysql.connect(config)!
149 defer {
150 db.close() or {}
151 }
152
153 db.exec('drop table if exists stmt_handle_errno_test')!
154 db.exec('create table if not exists stmt_handle_errno_test (
155 id INT PRIMARY KEY AUTO_INCREMENT,
156 value VARCHAR(255) NOT NULL UNIQUE
157 )')!
158 db.exec_param('insert into stmt_handle_errno_test (value) values (?)', 'duplicate')!
159
160 stmt := db.prepare('insert into stmt_handle_errno_test (value) values (?)')!
161 defer {
162 stmt.close()
163 }
164 stmt.execute(['duplicate']) or {
165 assert err.code() == 1062
166 return
167 }
168 assert false
169}
170