@@ -40,7 +40,7 @@ class SqlExecutionTest extends Specification {
40
40
Global . session = null
41
41
}
42
42
43
- def ' should execute DDL statements successfully and return null ' () {
43
+ def ' should execute DDL statements successfully and return success map ' () {
44
44
given :
45
45
def JDBC_URL = ' jdbc:h2:mem:test_ddl_' + Random . newInstance(). nextInt(1_000_000)
46
46
def sql = Sql . newInstance(JDBC_URL , ' sa' , null )
@@ -58,29 +58,32 @@ class SqlExecutionTest extends Specification {
58
58
statement : ' CREATE TABLE test_table(id INT PRIMARY KEY, name VARCHAR(255))'
59
59
])
60
60
61
- then : ' Table should be created and result should be null '
61
+ then : ' Table should be created and result should indicate success '
62
62
sql. rows(' SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \' TEST_TABLE\' ' ). size() > 0
63
- createResult == null
63
+ createResult. success == true
64
+ createResult. result == null
64
65
65
66
when : ' Altering the table'
66
67
def alterResult = sqlExtension. sqlExecute([
67
68
db : ' test' ,
68
69
statement : ' ALTER TABLE test_table ADD COLUMN description VARCHAR(255)'
69
70
])
70
71
71
- then : ' Column should be added and result should be null '
72
+ then : ' Column should be added and result should indicate success '
72
73
sql. rows(' SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \' TEST_TABLE\' AND COLUMN_NAME = \' DESCRIPTION\' ' ). size() > 0
73
- alterResult == null
74
+ alterResult. success == true
75
+ alterResult. result == null
74
76
75
77
when : ' Dropping the table'
76
78
def dropResult = sqlExtension. sqlExecute([
77
79
db : ' test' ,
78
80
statement : ' DROP TABLE test_table'
79
81
])
80
82
81
- then : ' Table should be dropped and result should be null '
83
+ then : ' Table should be dropped and result should indicate success '
82
84
sql. rows(' SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \' TEST_TABLE\' ' ). size() == 0
83
- dropResult == null
85
+ dropResult. success == true
86
+ dropResult. result == null
84
87
}
85
88
86
89
def ' should execute DML statements successfully and return affected row count' () {
@@ -104,30 +107,33 @@ class SqlExecutionTest extends Specification {
104
107
statement : ' INSERT INTO test_dml (id, name, value) VALUES (1, \' item1\' , 100)'
105
108
])
106
109
107
- then : ' Row should be inserted and result should be 1 '
110
+ then : ' Row should be inserted and result should indicate success with 1 row affected '
108
111
sql. rows(' SELECT * FROM test_dml' ). size() == 1
109
112
sql. firstRow(' SELECT * FROM test_dml WHERE id = 1' ). name == ' item1'
110
- insertResult == 1
113
+ insertResult. success == true
114
+ insertResult. result == 1
111
115
112
116
when : ' Updating data'
113
117
def updateResult = sqlExtension. sqlExecute([
114
118
db : ' test' ,
115
119
statement : ' UPDATE test_dml SET value = 200 WHERE id = 1'
116
120
])
117
121
118
- then : ' Row should be updated and result should be 1 '
122
+ then : ' Row should be updated and result should indicate success with 1 row affected '
119
123
sql. firstRow(' SELECT value FROM test_dml WHERE id = 1' ). value == 200
120
- updateResult == 1
124
+ updateResult. success == true
125
+ updateResult. result == 1
121
126
122
127
when : ' Deleting data'
123
128
def deleteResult = sqlExtension. sqlExecute([
124
129
db : ' test' ,
125
130
statement : ' DELETE FROM test_dml WHERE id = 1'
126
131
])
127
132
128
- then : ' Row should be deleted and result should be 1 '
133
+ then : ' Row should be deleted and result should indicate success with 1 row affected '
129
134
sql. rows(' SELECT * FROM test_dml' ). size() == 0
130
- deleteResult == 1
135
+ deleteResult. success == true
136
+ deleteResult. result == 1
131
137
}
132
138
133
139
def ' should return correct affected row count for multiple row operations' () {
@@ -149,40 +155,42 @@ class SqlExecutionTest extends Specification {
149
155
sqlExtension. init(session)
150
156
151
157
when : ' Inserting data'
152
- def insertCount = sqlExtension. sqlExecute([
158
+ def insertResult = sqlExtension. sqlExecute([
153
159
db : ' test' ,
154
160
statement : ' INSERT INTO test_update (id, name, value) VALUES (4, \' item4\' , 100)'
155
161
])
156
162
157
- then : ' Should return 1 affected row'
158
- insertCount == 1
163
+ then : ' Should return success with 1 affected row'
164
+ insertResult. success == true
165
+ insertResult. result == 1
159
166
sql. rows(' SELECT * FROM test_update' ). size() == 4
160
167
161
168
when : ' Updating multiple rows'
162
- def updateCount = sqlExtension. sqlExecute([
169
+ def updateResult = sqlExtension. sqlExecute([
163
170
db : ' test' ,
164
171
statement : ' UPDATE test_update SET value = 200 WHERE value = 100'
165
172
])
166
173
167
- then : ' Should return 4 affected rows'
168
- updateCount == 4
174
+ then : ' Should return success with 4 affected rows'
175
+ updateResult. success == true
176
+ updateResult. result == 4
169
177
sql. rows(' SELECT * FROM test_update WHERE value = 200' ). size() == 4
170
178
171
179
when : ' Deleting multiple rows'
172
- def deleteCount = sqlExtension. sqlExecute([
180
+ def deleteResult = sqlExtension. sqlExecute([
173
181
db : ' test' ,
174
182
statement : ' DELETE FROM test_update WHERE value = 200'
175
183
])
176
184
177
- then : ' Should return 4 affected rows'
178
- deleteCount == 4
185
+ then : ' Should return success with 4 affected rows'
186
+ deleteResult. success == true
187
+ deleteResult. result == 4
179
188
sql. rows(' SELECT * FROM test_update' ). size() == 0
180
189
}
181
190
182
191
def ' should handle invalid SQL correctly' () {
183
192
given :
184
193
def JDBC_URL = ' jdbc:h2:mem:test_error_' + Random . newInstance(). nextInt(1_000_000)
185
- def sql = Sql . newInstance(JDBC_URL , ' sa' , null )
186
194
187
195
and :
188
196
def session = Mock (Session ) {
@@ -192,22 +200,24 @@ class SqlExecutionTest extends Specification {
192
200
sqlExtension. init(session)
193
201
194
202
when : ' Executing invalid SQL'
195
- sqlExtension. sqlExecute([
203
+ def invalidResult = sqlExtension. sqlExecute([
196
204
db : ' test' ,
197
205
statement : ' INVALID SQL STATEMENT'
198
206
])
199
207
200
- then : ' Should throw an exception'
201
- thrown(Exception )
208
+ then : ' Should return failure with error message'
209
+ invalidResult. success == false
210
+ invalidResult. error != null
202
211
203
212
when : ' Executing query with invalid table name'
204
- sqlExtension. sqlExecute([
213
+ def noTableResult = sqlExtension. sqlExecute([
205
214
db : ' test' ,
206
215
statement : ' SELECT * FROM non_existent_table'
207
216
])
208
217
209
- then : ' Should throw an exception'
210
- thrown(Exception )
218
+ then : ' Should return failure with error message'
219
+ noTableResult. success == false
220
+ noTableResult. error != null
211
221
}
212
222
213
223
def ' should handle invalid database configuration correctly' () {
@@ -219,30 +229,36 @@ class SqlExecutionTest extends Specification {
219
229
sqlExtension. init(session)
220
230
221
231
when : ' Using non-existent database alias'
222
- sqlExtension. sqlExecute([
232
+ def nonExistentDbResult = sqlExtension. sqlExecute([
223
233
db : ' non_existent_db' ,
224
234
statement : ' SELECT 1'
225
235
])
226
236
227
- then : ' Should throw an IllegalArgumentException'
228
- thrown(IllegalArgumentException )
237
+ then : ' Should return failure with error message'
238
+ nonExistentDbResult. success == false
239
+ nonExistentDbResult. error != null
240
+ nonExistentDbResult. error. contains(' Unknown db name' )
229
241
230
242
when : ' Missing statement parameter'
231
- sqlExtension. sqlExecute([
243
+ def missingStatementResult = sqlExtension. sqlExecute([
232
244
db : ' test'
233
245
])
234
246
235
- then : ' Should throw an IllegalArgumentException'
236
- thrown(IllegalArgumentException )
247
+ then : ' Should return failure with error message'
248
+ missingStatementResult. success == false
249
+ missingStatementResult. error != null
250
+ missingStatementResult. error. contains(' Missing required parameter' )
237
251
238
252
when : ' Empty statement parameter'
239
- sqlExtension. sqlExecute([
253
+ def emptyStatementResult = sqlExtension. sqlExecute([
240
254
db : ' test' ,
241
255
statement : ' '
242
256
])
243
257
244
- then : ' Should throw an IllegalArgumentException'
245
- thrown(IllegalArgumentException )
258
+ then : ' Should return failure with error message'
259
+ emptyStatementResult. success == false
260
+ emptyStatementResult. error != null
261
+ emptyStatementResult. error. contains(' Missing required parameter' )
246
262
}
247
263
248
264
def ' should handle statement normalization correctly' () {
@@ -258,23 +274,25 @@ class SqlExecutionTest extends Specification {
258
274
sqlExtension. init(session)
259
275
260
276
when : ' Executing statement without semicolon'
261
- def result = sqlExtension. sqlExecute([
277
+ def createResult = sqlExtension. sqlExecute([
262
278
db : ' test' ,
263
279
statement : ' CREATE TABLE test_norm(id INT PRIMARY KEY)'
264
280
])
265
281
266
- then : ' Statement should be executed successfully and result should be null '
282
+ then : ' Statement should be executed successfully'
267
283
sql. rows(' SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \' TEST_NORM\' ' ). size() > 0
268
- result == null
284
+ createResult. success == true
285
+ createResult. result == null
269
286
270
287
when : ' Executing statement with trailing whitespace'
271
288
def dropResult = sqlExtension. sqlExecute([
272
289
db : ' test' ,
273
290
statement : ' DROP TABLE test_norm '
274
291
])
275
292
276
- then : ' Statement should be executed successfully and result should be null '
293
+ then : ' Statement should be executed successfully'
277
294
sql. rows(' SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \' TEST_NORM\' ' ). size() == 0
278
- dropResult == null
295
+ dropResult. success == true
296
+ dropResult. result == null
279
297
}
280
298
}
0 commit comments