I'm trying to create a unit test for the following function that is making database calls internally using Postgres driver:
type DBer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (interface{}, error)
QueryRow(query string, args ...interface{}) *sql.Row
Prepare(query string) (*sql.Stmt, error)
}
type AppInfoCtrl struct {
DB DBer
}
type Rower interface {
Next() bool
Close() error
Scan(...interface{}) error
}
func parseRows(rows Rower, infos []AppInfo) ([]AppInfo, error) {
defer rows.Close()
for rows.Next() {
var i AppInfo
if err := rows.Scan(&i.Id, &i.Platform); err != nil {
return nil, err
}
infos = append(infos, i)
}
return infos, nil
}
func (a *AppInfoCtrl) List() ([]AppInfo, error) {
query := "select id, platform from appinfo where deleted = false"
rows, err := a.DB.Query(query)
if err != nil {
return nil, err
}
appInfos := []AppInfo{}
parseRows(rows, appInfos)
return appInfos, nil
}
And my test looks like this:
func (f *FakeDB) Query(query string, args ...interface{}) (*sql.Rows, error) {
fmt.Println(query)
var rows *sql.Rows
return rows, nil
}
However, after running this I get the following compilation error:
appinfo/controller.go:68:11: cannot use rows (type interface {}) as type Rower in argument to parseRows:
interface {} does not implement Rower (missing Close method)
When I look at the source code, sql.Rows does implement Close():
https://golang.org/pkg/database/sql/#Rows.Close
Any idea what I need to do here? Am I even taking the right approach to test List() here? I'm not particularly picky about testing parseRows() as it only contains calls to db.Rows, but I need to at least test List here.