lundi 22 avril 2019

How to check how many rows have been updated

Firs time using Go, so kind of stuck. I am using sqlmock to mock my queries. Here is how i have set it up

ctx := context.Background()
db, mock, _ := sqlmock.New()
rows := sqlmock.NewRows([]string{"account_id", "home_id", "zip_code"}).
    AddRow("account-uuid-1", "home-uuid-1", "xxxx").
    AddRow("account-uuid-2", "home-uuid-2", "xxxx").
    AddRow("account-uuid-3", "home-uuid-3", "xxxx")
mock.ExpectExec(
    regexp.QuoteMeta(MyQuery)).
    WithArgs("DDDD", "account-uuid-2", "home-uuid-2").
    WillReturnResult(sqlmock.NewResult(0, 1))

account := Account {
    AccountID: "account-uuid-2",
    HomeID: "home-uuid-2",
    Zipcode: models.JsonNullString{Value: sql.NullString{String: "DDDD", Valid: true}},
}

err := account.UpdateAccountZipCode(ctx, db)
assert.Nil(t, err)
print (rows)
assert.Nil(t, mock.ExpectationsWereMet())

The test case by itself is working fine. But it's because i am only testing if there was an error while executing the query. What i actually want to check is to make sure

  1. Only one row i.e., account-uuid-2 & home-uuid-2 has been updated
  2. The row has been updated with correct values

I thought that the rows object might give me correct information. But it didn't so what should be the correct way to test this?

Aucun commentaire:

Enregistrer un commentaire