Leave a Comment
Mock sql (sqlx) db on golang
I am using this library — https://pkg.go.dev/github.com/data-dog/go-sqlmock.
That’s how you can use it for mocking db querying:
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 |
import ( "database/sql/driver" "testing" "github.com/go-playground/assert/v2" "github.com/jmoiron/sqlx" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "gopkg.in/DATA-DOG/go-sqlmock.v1" ) type behaviourDB struct { orderIDs []driver.Value data *sqlmock.Rows error error } type DBTestSuite struct { suite.Suite } func Test_DBTestSuite(t *testing.T) { suite.Run(t, new(DBTestSuite)) } func (s DBTestSuite) Test_GetResults() { tt := []struct { name string orderIDs []int64 behaviourDB expectedResults []models.Result expectedError error }{ { name: "happy path", orderIDs: []int64{1, 2}, behaviourDB: behaviourDB{ orderIDs: []driver.Value{1, 2}, data: sqlmock. NewRows([]string{"quantity", "order_id"}). AddRow(10, 1). AddRow(11, 2), error: nil, }, expectedResults: nil, expectedError: nil, }, } for _, tc := range tt { s.T().Run(tc.name, func(t *testing.T) { db, mock, err := sqlmock.New() require.NoError(t, err, "creating a db mock") defer db.Close() dbx := sqlx.NewDb(db, "sqlmock") expectedQuery := mock.ExpectQuery(`SELECT t.quantity, t.order_id FROM table AS t WHERE t.order_id IN \(\?,\?\) ORDER BY t.order_id`). WithArgs(tc.orderIDs) if tc.behaviourDB.error != nil { expectedQuery.WillReturnError(tc.behaviourDB.error) } else { expectedQuery.WillReturnRows(tc.behaviourDB.data) } r, err := NewRepository(dbx) require.NoError(t, err, "creating the shared repo") results, err := r.GetResults(context.Background(), tc.orderIDs) require.NoError(t, helpers.CompareErrors(tc.expectedError, err)) assert.Equal(t, tc.expectedResults, results) }) } } |
That’s how you can test a function that only does the rows.StructScan
, for instance:
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 |
// ... the same as above func (s DBTestSuite) Test_doScan() { tt := []struct { name string rows *sqlmock.Rows triggerFakeScanError bool expectedResult models.Result expectedError error }{ { name: "happy path", rows: sqlmock. NewRows([]string{"quantity", "order_id"}). AddRow(10, 1), triggerFakeScanError: false, expectedResult: models.Result{ // ... }, expectedError: nil, }, } for _, tc := range tt { s.T().Run(tc.name, func(t *testing.T) { db, mock, err := sqlmock.New() require.NoError(t, err, "creating a db mock") defer db.Close() dbx := sqlx.NewDb(db, "sqlmock") mock.ExpectQuery("SELECT 1"). WillReturnRows(tc.rows) rows, err := dbx.Queryx("SELECT 1") require.NoError(t, err, "running fake select") if !tc.triggerFakeScanError { rows.Next() } result, err := doScan(rows) require.NoError(t, helpers.CompareErrors(tc.expectedError, err)) assert.Equal(t, tc.expectedResult, result) }) } } |
Similar Posts
LEAVE A COMMENT
Для отправки комментария вам необходимо авторизоваться.