-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMySQLTest.java
More file actions
259 lines (199 loc) · 6.92 KB
/
Copy pathMySQLTest.java
File metadata and controls
259 lines (199 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package advancedsql;
import advancedsql.query.*;
import advancedsql.query.action.Add;
import advancedsql.query.action.Modify;
import advancedsql.table.ITable;
import org.junit.Test;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
public class MySQLTest {
public MySQL connect() throws SQLException {
return new MySQL("127.0.0.1", 3306, "root", "", "unittesting");
}
@Test public void testConnection() {
try {
MySQL mySQL = connect();
assertTrue(mySQL.isConnected());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testCreateTable() {
try {
MySQL mySQL = connect();
// Table
ITable table = mySQL.table("users");
if (mySQL.table("test").exists()) {
System.out.println("Table test already exists, deleting.");
table.drop().execute();
}
// Create table
Create create = table.create();
// Table columns
create.id();
create.string("first_name");
create.string("last_name");
create.string("test");
Boolean result = create.execute();
// Print query string and result.
System.out.println(create);
System.out.println(result);
assertFalse(result);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testAfterTable() {
try {
MySQL mySQL = connect();
// Alter columns
Alter alter = mySQL.table("users").alter();
// Add columns
Add add = alter.add();
add.string("token").nullable();
add.string("connection_id").nullable();
// Drop columns
advancedsql.query.action.Drop drop = alter.drop();
drop.column("test");
// Modify columns
Modify modify = alter.modify();
modify.integer("connection_id").nullable();
// Execute query
Boolean result = alter.execute();
// Print query string and result.
System.out.println(alter);
System.out.println(result);
assertFalse(result);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testInsert() {
try {
MySQL mySQL = connect();
// Insert
Insert query = mySQL.table("users").insert(new HashMap<>(){{
put("first_name", "Denzel");
put("last_name", "Code");
}});
int execute = query.execute();
// Print query string and result.
System.out.println(query);
System.out.println(execute);
assertEquals(1, execute);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testUpdate() {
try {
MySQL mySQL = connect();
// Update
Update query = mySQL.table("users").update(new HashMap<>(){{
put("token", "Advanced");
}}).where("first_name = ?", "Denzel");
int execute = query.execute();
// Print query string and result.
System.out.println(query);
System.out.println(execute);
assertEquals(1, execute);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testSelect() {
try {
MySQL mySQL = connect();
// Select
Select query = mySQL.table("users").select();
Map<String, Object> fetch = query.fetch();
// Print query string and result.
System.out.println(query);
System.out.println(fetch);
assertEquals(fetch.get("first_name"), "Denzel");
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testCreateSelectJoin() {
try {
MySQL mySQL = connect();
// Create information table
ITable table = mySQL.table("information");
// Delete table if exists.
if (mySQL.table("information").exists()) table.drop().execute();
// Create table
Create create = table.create();
// Table columns
create.id();
create.integer("user_id").nullable();
create.string("address").nullable();
Boolean execute = create.execute();
// Print query and result.
System.out.println(create);
System.out.println(execute);
String address = "20 Cooper Square";
// Insert value
table.insert(new HashMap<>(){{
put("user_id", 1);
put("address", address);
}}).execute();
table = mySQL.table("users");
Select query = table.select(new String[]{"information.address", "users.first_name"})
.innerJoin("information")
.on("users.id = information.user_id")
.where("first_name = ?", "Denzel");
Map<String, Object> user = query.fetch();
// Print query and result.
System.out.println(query);
System.out.println(user);
assertEquals(address, user.get("address"));
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testDelete() {
try {
MySQL mySQL = connect();
// Delete
Delete query = mySQL.table("users").delete().where("first_name = ?", "Denzel");
int execute = query.execute();
// Print query and result.
System.out.println(query);
System.out.println(execute);
assertEquals(1, execute);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testTruncate() {
try {
MySQL mySQL = connect();
// Truncate table
Truncate query = mySQL.table("users").truncate();
Boolean execute = query.execute();
// Print query and result.
System.out.println(query);
System.out.println(execute);
assertFalse(execute);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test public void testDrop() {
try {
MySQL mySQL = connect();
// Drop table
Drop query = mySQL.table("users").drop();
Boolean execute = query.execute();
// Print query and result.
System.out.println(query);
System.out.println(execute);
assertFalse(execute);
} catch (SQLException e) {
e.printStackTrace();
}
}
}