@@ -23,8 +23,8 @@ func TestInsertReindexRequestSuccess(t *testing.T) {
23
23
24
24
createdAt := time .Now ()
25
25
26
- query := `INSERT INTO reindex_requests(request_id, status, created_at, last_updated) VALUES ($1, $2, $3, $4) ;`
27
- mock .ExpectExec (query ).WithArgs (sqlmock . AnyArg (), "running" , createdAt , createdAt ).WillReturnResult (sqlmock .NewResult ( 1 , 1 ))
26
+ query := `INSERT INTO reindex_requests(status, created_at, last_updated) VALUES ($1, $2, $3) RETURNING id ;`
27
+ mock .ExpectQuery (query ).WithArgs ("running" , createdAt , createdAt ).WillReturnRows (sqlmock .NewRows ([] string { "id" }). AddRow ( 1 ))
28
28
29
29
requestID , err := db .InsertReindexRequest ("running" , createdAt )
30
30
assert .NoError (t , err )
@@ -42,11 +42,11 @@ func TestInsertReindexRequestFailure(t *testing.T) {
42
42
43
43
createdAt := time .Now ()
44
44
45
- query := `INSERT INTO reindex_requests(request_id, status, created_at, last_updated) VALUES ($1, $2, $3, $4) ;`
46
- mock .ExpectExec (query ).WithArgs (sqlmock . AnyArg (), "running" , createdAt , createdAt ).WillReturnError (fmt .Errorf ("insert error" ))
45
+ query := `INSERT INTO reindex_requests(status, created_at, last_updated) VALUES ($1, $2, $3) RETURNING id ;`
46
+ mock .ExpectQuery (query ).WithArgs ("running" , createdAt , createdAt ).WillReturnError (fmt .Errorf ("insert error" ))
47
47
48
48
_ , err = db .InsertReindexRequest ("running" , createdAt )
49
- assert .Equal (t , "insert error" , err .Error ())
49
+ assert .Equal (t , "failed to insert reindex request: insert error" , err .Error ())
50
50
}
51
51
52
52
func TestUpdateReindexRequestSuccess (t * testing.T ) {
@@ -60,7 +60,7 @@ func TestUpdateReindexRequestSuccess(t *testing.T) {
60
60
61
61
updatedAt := time .Now ()
62
62
63
- query := `UPDATE reindex_requests SET status = $1, last_updated = $2 WHERE request_id = $3;`
63
+ query := `UPDATE reindex_requests SET status = $1, last_updated = $2 WHERE id = $3;`
64
64
mock .ExpectExec (query ).WithArgs ("completed" , updatedAt , 1 ).WillReturnResult (sqlmock .NewResult (1 , 1 ))
65
65
66
66
err = db .UpdateReindexRequest (1 , "completed" , updatedAt )
@@ -78,91 +78,13 @@ func TestUpdateReindexRequestFailure(t *testing.T) {
78
78
79
79
updatedAt := time .Now ()
80
80
81
- query := `UPDATE reindex_requests SET status = $1, last_updated = $2 WHERE request_id = $3;`
81
+ query := `UPDATE reindex_requests SET status = $1, last_updated = $2 WHERE id = $3;`
82
82
mock .ExpectExec (query ).WithArgs ("completed" , updatedAt , 1 ).WillReturnError (fmt .Errorf ("update error" ))
83
83
84
84
err = db .UpdateReindexRequest (1 , "completed" , updatedAt )
85
85
assert .Equal (t , "update error" , err .Error ())
86
86
}
87
87
88
- func TestInsertReindexRequestDetailedSuccess (t * testing.T ) {
89
- dbConn , mock , err := sqlmock .New (sqlmock .QueryMatcherOption (sqlmock .QueryMatcherEqual ))
90
- assert .NoError (t , err )
91
- defer dbConn .Close ()
92
-
93
- db := & storage.DB {
94
- DbMap : & gorp.DbMap {Db : dbConn , Dialect : gorp.PostgresDialect {}},
95
- }
96
-
97
- detail := storage.ReindexRequestDetailed {
98
- RequestID : 1 ,
99
- Index : "index1" ,
100
- FromVersion : "1.0" ,
101
- ToVersion : "2.0" ,
102
- Stage : []storage.StageDetail {{Stage : "stage1" , Status : "running" , UpdatedAt : time .Now ()}},
103
- OsTaskID : "task1" ,
104
- Heartbeat : time .Now (),
105
- HavingAlias : true ,
106
- AliasList : "alias1,alias2" ,
107
- CreatedAt : time .Now (),
108
- UpdatedAt : time .Now (),
109
- }
110
-
111
- stageJSON , err := json .Marshal (detail .Stage )
112
- assert .NoError (t , err )
113
-
114
- // Mock the query to fetch existing stages
115
- selectQuery := `SELECT stage FROM reindex_request_detailed WHERE request_id = $1 AND index = $2 ORDER BY updated_at DESC LIMIT 1`
116
- mock .ExpectQuery (selectQuery ).WithArgs (detail .RequestID , detail .Index ).
117
- WillReturnRows (sqlmock .NewRows ([]string {"stage" }).AddRow (`[]` ))
118
-
119
- // Mock the insert query
120
- query := `INSERT INTO reindex_request_detailed(request_id, index, from_version, to_version, stage, os_task_id, heartbeat, having_alias, alias_list, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);`
121
- mock .ExpectExec (query ).WithArgs (detail .RequestID , detail .Index , detail .FromVersion , detail .ToVersion , stageJSON , detail .OsTaskID , sqlmock .AnyArg (), detail .HavingAlias , detail .AliasList , sqlmock .AnyArg (), sqlmock .AnyArg ()).WillReturnResult (sqlmock .NewResult (1 , 1 ))
122
-
123
- err = db .InsertReindexRequestDetailed (detail , detail .CreatedAt )
124
- assert .NoError (t , err )
125
- }
126
-
127
- func TestInsertReindexRequestDetailedFailure (t * testing.T ) {
128
- dbConn , mock , err := sqlmock .New (sqlmock .QueryMatcherOption (sqlmock .QueryMatcherEqual ))
129
- assert .NoError (t , err )
130
- defer dbConn .Close ()
131
-
132
- db := & storage.DB {
133
- DbMap : & gorp.DbMap {Db : dbConn , Dialect : gorp.PostgresDialect {}},
134
- }
135
-
136
- detail := storage.ReindexRequestDetailed {
137
- RequestID : 1 ,
138
- Index : "index1" ,
139
- FromVersion : "1.0" ,
140
- ToVersion : "2.0" ,
141
- Stage : []storage.StageDetail {{Stage : "stage1" , Status : "running" , UpdatedAt : time .Now ()}},
142
- OsTaskID : "task1" ,
143
- Heartbeat : time .Now (),
144
- HavingAlias : true ,
145
- AliasList : "alias1,alias2" ,
146
- CreatedAt : time .Now (),
147
- UpdatedAt : time .Now (),
148
- }
149
-
150
- stageJSON , err := json .Marshal (detail .Stage )
151
- assert .NoError (t , err )
152
-
153
- // Mock the query to fetch existing stages
154
- selectQuery := `SELECT stage FROM reindex_request_detailed WHERE request_id = $1 AND index = $2 ORDER BY updated_at DESC LIMIT 1`
155
- mock .ExpectQuery (selectQuery ).WithArgs (detail .RequestID , detail .Index ).
156
- WillReturnRows (sqlmock .NewRows ([]string {"stage" }).AddRow (`[]` ))
157
-
158
- // Mock the insert query to return an error
159
- query := `INSERT INTO reindex_request_detailed(request_id, index, from_version, to_version, stage, os_task_id, heartbeat, having_alias, alias_list, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);`
160
- mock .ExpectExec (query ).WithArgs (detail .RequestID , detail .Index , detail .FromVersion , detail .ToVersion , stageJSON , detail .OsTaskID , sqlmock .AnyArg (), detail .HavingAlias , detail .AliasList , sqlmock .AnyArg (), sqlmock .AnyArg ()).WillReturnError (fmt .Errorf ("insert error" ))
161
-
162
- err = db .InsertReindexRequestDetailed (detail , detail .CreatedAt )
163
- assert .Equal (t , "insert error" , err .Error ())
164
- }
165
-
166
88
func TestDeleteReindexRequestSuccess (t * testing.T ) {
167
89
dbConn , mock , err := sqlmock .New (sqlmock .QueryMatcherOption (sqlmock .QueryMatcherEqual ))
168
90
assert .NoError (t , err )
@@ -172,7 +94,7 @@ func TestDeleteReindexRequestSuccess(t *testing.T) {
172
94
DbMap : & gorp.DbMap {Db : dbConn , Dialect : gorp.PostgresDialect {}},
173
95
}
174
96
175
- query := `DELETE FROM reindex_requests WHERE request_id = $1;`
97
+ query := `DELETE FROM reindex_requests WHERE id = $1;`
176
98
mock .ExpectExec (query ).WithArgs (1 ).WillReturnResult (sqlmock .NewResult (1 , 1 ))
177
99
178
100
err = db .DeleteReindexRequest (1 )
@@ -188,45 +110,13 @@ func TestDeleteReindexRequestFailure(t *testing.T) {
188
110
DbMap : & gorp.DbMap {Db : dbConn , Dialect : gorp.PostgresDialect {}},
189
111
}
190
112
191
- query := `DELETE FROM reindex_requests WHERE request_id = $1;`
113
+ query := `DELETE FROM reindex_requests WHERE id = $1;`
192
114
mock .ExpectExec (query ).WithArgs (1 ).WillReturnError (fmt .Errorf ("delete error" ))
193
115
194
116
err = db .DeleteReindexRequest (1 )
195
117
assert .Equal (t , "delete error" , err .Error ())
196
118
}
197
119
198
- func TestDeleteReindexRequestDetailSuccess (t * testing.T ) {
199
- dbConn , mock , err := sqlmock .New (sqlmock .QueryMatcherOption (sqlmock .QueryMatcherEqual ))
200
- assert .NoError (t , err )
201
- defer dbConn .Close ()
202
-
203
- db := & storage.DB {
204
- DbMap : & gorp.DbMap {Db : dbConn , Dialect : gorp.PostgresDialect {}},
205
- }
206
-
207
- query := `DELETE FROM reindex_request_detailed WHERE id = $1;`
208
- mock .ExpectExec (query ).WithArgs (1 ).WillReturnResult (sqlmock .NewResult (1 , 1 ))
209
-
210
- err = db .DeleteReindexRequestDetail (1 )
211
- assert .NoError (t , err )
212
- }
213
-
214
- func TestDeleteReindexRequestDetailFailure (t * testing.T ) {
215
- dbConn , mock , err := sqlmock .New (sqlmock .QueryMatcherOption (sqlmock .QueryMatcherEqual ))
216
- assert .NoError (t , err )
217
- defer dbConn .Close ()
218
-
219
- db := & storage.DB {
220
- DbMap : & gorp.DbMap {Db : dbConn , Dialect : gorp.PostgresDialect {}},
221
- }
222
-
223
- query := `DELETE FROM reindex_request_detailed WHERE id = $1;`
224
- mock .ExpectExec (query ).WithArgs (1 ).WillReturnError (fmt .Errorf ("delete error" ))
225
-
226
- err = db .DeleteReindexRequestDetail (1 )
227
- assert .Equal (t , "delete error" , err .Error ())
228
- }
229
-
230
120
func TestGetReindexStatusSuccess (t * testing.T ) {
231
121
dbConn , mock , err := sqlmock .New (sqlmock .QueryMatcherOption (sqlmock .QueryMatcherEqual ))
232
122
assert .NoError (t , err )
@@ -242,8 +132,8 @@ func TestGetReindexStatusSuccess(t *testing.T) {
242
132
heartbeat := time .Now ()
243
133
244
134
// Mock the reindex_requests table query
245
- requestQuery := `SELECT request_id , status, created_at, last_updated FROM reindex_requests WHERE request_id = $1 ORDER BY last_updated DESC LIMIT 1;`
246
- requestColumns := []string {"request_id " , "status" , "created_at" , "last_updated" }
135
+ requestQuery := `SELECT id , status, created_at, last_updated FROM reindex_requests WHERE id = $1 ORDER BY last_updated DESC LIMIT 1;`
136
+ requestColumns := []string {"id " , "status" , "created_at" , "last_updated" }
247
137
mock .ExpectQuery (requestQuery ).WithArgs (requestID ).
248
138
WillReturnRows (sqlmock .NewRows (requestColumns ).AddRow (requestID , "completed" , createdAt , updatedAt ))
249
139
@@ -281,7 +171,7 @@ func TestGetReindexStatusFailure(t *testing.T) {
281
171
requestID := 1
282
172
283
173
// Mock the reindex_requests table query to return an error
284
- requestQuery := `SELECT request_id , status, created_at, last_updated FROM reindex_requests WHERE request_id = $1 ORDER BY last_updated DESC LIMIT 1;`
174
+ requestQuery := `SELECT id , status, created_at, last_updated FROM reindex_requests WHERE id = $1 ORDER BY last_updated DESC LIMIT 1;`
285
175
mock .ExpectQuery (requestQuery ).WithArgs (requestID ).WillReturnError (fmt .Errorf ("query error" ))
286
176
287
177
// Call the function
@@ -306,8 +196,8 @@ func TestGetReindexStatusNoDetails(t *testing.T) {
306
196
updatedAt := time .Now ()
307
197
308
198
// Mock the reindex_requests table query
309
- requestQuery := `SELECT request_id , status, created_at, last_updated FROM reindex_requests WHERE request_id = $1 ORDER BY last_updated DESC LIMIT 1;`
310
- requestColumns := []string {"request_id " , "status" , "created_at" , "last_updated" }
199
+ requestQuery := `SELECT id , status, created_at, last_updated FROM reindex_requests WHERE id = $1 ORDER BY last_updated DESC LIMIT 1;`
200
+ requestColumns := []string {"id " , "status" , "created_at" , "last_updated" }
311
201
mock .ExpectQuery (requestQuery ).WithArgs (requestID ).
312
202
WillReturnRows (sqlmock .NewRows (requestColumns ).AddRow (requestID , "completed" , createdAt , updatedAt ))
313
203
0 commit comments