Go newbie here. I have the following structure in the main code:
func ExecuteQuery(db *sql.DB, sqlQuery string,
processSqlResults func(*sql.Rows) (*CustomStatus, error)) (*CustomStatus, error) {
retry, _ := RetryDeterminationFunctionThatTakesAnErrorFunctionAsArg(func() error {
rows, err = db.Query(sqlQuery)
// if mockDB == false { // does not look like a good idea (please read the explanation)
if rows == nil {
return err
}
// }
customStatus, handlerErr = processSqlResults(rows)
if customStatus.Status != "Success" {
return handlerErr
}
return err
}, secondArgument, thirdArgument)
if err != nil {
log.Printf("failed to run the query: %v, err: %v", sqlQuery, err)
return &CustomStatus{"Error"}, err
}
/*
do something here
*/
return customStatus, err
}
I have to use mocks to be able to test all the possible scenarios. The processSqlResults() function can be mocked as follows:
processSqlResults := func(rows *sql.Rows) (*CustomStatus, error) {
return &CustomStatus{Status: "Error"}, errors.New("Error in processSqlResults")
}
I also need to mock the db.Query() function so I can set rows as not nil, so that the execution can get to what's after that as well.
The problem is this function is not passed as an argument to ExecuteQuery() and as you can see is inside of a custom error function that is evaluated to determine whether or not a retry of the query execution is needed.
I thought of adding a flag to the main logic to specify whether we are testing versus whether it's the main execution, but that puts testing logic into the main code and does not look like a good way to me.
What is a way to achieve this? Again, Go newbie here so please explain as thoroughly as you can.
Aucun commentaire:
Enregistrer un commentaire