хранилище. go
Const UPDATE_REQUEST_QUERY = "UPDATE requests SET status = ‘completed' , request_end_time = ? WHERE id = ? and type = ‘mobile'"
func (repo *Repository) Update(info *model.Request) error {
if info == nil {
return nil
}
query := repo.db.Exec(UPDATE_REQUEST_QUERY, info.RequestEndTime, info.ID )
if rowsAffected, err := query.RowsAffected, query.Error; rowsAffected != 1 || err != nil {
return errors.Wrap(err, "Some Error message.")
}
return nil
}
Макет
repository_test. go
func TestRepository_Update(t *testing.T) {
type testData struct {
input *model.Requests
queryString string
queryArgs []driver.Value
updateErr error
hasErr bool
}
db, mock, _ := sqlmock.New()
defer db.Close()
dbInstance, _ := gorm.Open("postgres", db)
testDataList := []testData{
{
input: &model.Requests{
RequestEndTime: time.Time,
Id: 100,
},
queryString: `UPDATE "requests" SET "status" = ‘completed’, request_end_time = $1 WHERE id = $2 and type = ‘mobile'`,
queryArgs: []driver.Value{Anytime{}, 100},
updateErr: nil,
hasErr: false,
},
}
for _, data := range testDataList {
repo := Repository(zerolog.Nop(), dbInstance)
if data.queryString != "" {
mock.ExpectBegin()
m := mock.ExpectExec(regexp.QuoteMeta(data.queryString)).WithArgs(data.queryArgs...)
if data.hasErr {
m.WillReturnError(data.updateErr)
} else {
m.WillReturnResult(sqlmock.NewResult(1, 1))
}
mock.ExpectCommit()
}
resultErr := repo.Update(data.input)
if data.hasErr {
assert.NotNil(t, resultErr)
} else {
assert.Nil(t, resultErr). //Error thrown in this line
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
}
Ошибка, которая выдается
Error: Expected nil, but got: Update failed.: call to ExecQuery 'UPDATE requests SET status = 'completed' , request_end_time = $1 WHERE id = $2 and type = 'mobile'' with args [{Name: Ordinal:1 Value:0001-01-01 00:00:00 +0000 UTC} {Name: Ordinal:2 Value:0}] was not expected