@@ -21,6 +21,7 @@ import (
21
21
"database/sql"
22
22
"testing"
23
23
24
+ "github.com/DATA-DOG/go-sqlmock"
24
25
"github.com/crossplane-contrib/provider-sql/apis/postgresql/v1alpha1"
25
26
"github.com/google/go-cmp/cmp"
26
27
"github.com/google/go-cmp/cmp/cmpopts"
@@ -221,6 +222,9 @@ func TestObserve(t *testing.T) {
221
222
reason : "We should return ResourceExists: false when no default grant is found" ,
222
223
fields : fields {
223
224
db : mockDB {
225
+ MockQuery : func (ctx context.Context , q xsql.Query ) (* sql.Rows , error ) {
226
+ return mockRowsToSQLRows (sqlmock .NewRows ([]string {})), nil
227
+ },
224
228
MockScan : func (ctx context.Context , q xsql.Query , dest ... interface {}) error {
225
229
// Default value is empty, so we don't need to do anything here
226
230
return nil
@@ -248,6 +252,12 @@ func TestObserve(t *testing.T) {
248
252
reason : "We should return any errors encountered while trying to show the default grant" ,
249
253
fields : fields {
250
254
db : mockDB {
255
+ MockQuery : func (ctx context.Context , q xsql.Query ) (* sql.Rows , error ) {
256
+ r := sqlmock .NewRows ([]string {"PRIVILEGE" }).
257
+ AddRow ("UPDATE" ).
258
+ AddRow ("SELECT" )
259
+ return mockRowsToSQLRows (r ), errBoom
260
+ },
251
261
MockScan : func (ctx context.Context , q xsql.Query , dest ... interface {}) error {
252
262
return errBoom
253
263
},
@@ -275,11 +285,22 @@ func TestObserve(t *testing.T) {
275
285
reason : "We should return no error if we can find the right permissions in the default grant" ,
276
286
fields : fields {
277
287
db : mockDB {
278
- MockScan : func (ctx context.Context , q xsql.Query , dest ... interface {}) error {
279
- bv := dest [0 ].(* []string )
280
- * bv = []string {"SELECT" , "UPDATE" }
281
- return nil
288
+ MockQuery : func (ctx context.Context , q xsql.Query ) (* sql.Rows , error ) {
289
+ r := sqlmock .NewRows ([]string {"PRIVILEGE" }).
290
+ AddRow ("UPDATE" ).
291
+ AddRow ("SELECT" )
292
+ return mockRowsToSQLRows (r ), nil
282
293
},
294
+ // MockScan: func(ctx context.Context, q xsql.Query, dest ...interface{}) error {
295
+ // if len(dest) == 0 {
296
+ // runtime.Breakpoint()
297
+ // return nil
298
+ // }
299
+ // // populate the dest slice with the expected values
300
+ // // so we can compare them in the test
301
+ // *dest[0].(*string) = "SELECT"
302
+ // return nil
303
+ // },
283
304
},
284
305
},
285
306
args : args {
@@ -320,6 +341,17 @@ func TestObserve(t *testing.T) {
320
341
}
321
342
}
322
343
344
+ func mockRowsToSQLRows (mockRows * sqlmock.Rows ) * sql.Rows {
345
+ db , mock , _ := sqlmock .New ()
346
+ mock .ExpectQuery ("select" ).WillReturnRows (mockRows )
347
+ rows , err := db .Query ("select" )
348
+ if err != nil {
349
+ println ("%v" , err )
350
+ return nil
351
+ }
352
+ return rows
353
+ }
354
+
323
355
func TestCreate (t * testing.T ) {
324
356
errBoom := errors .New ("boom" )
325
357
0 commit comments