Unit testing Golang

I was wondering how to properly unit test Couchbase data driven code. I can’t seem to get the interface setup working.

I want to do something along the lines of:

type Bucket struct {
*gocb.Bucket
}

type CouchbaseQuerier interface {
ExecuteN1qlQuery(*gocb.N1qlQuery, interface{}) (gocb.QueryResults, error)
}

func (bucket *Bucket) ExecuteN1qlQuery(query *gocb.N1qlQuery, params interface{}) (rows gocb.QueryResults, err error) {
rows, err = bucket.ExecuteN1qlQuery(query, params)
if err != nil {
return
}
return
}

func QueryCB(bucket CouchbaseQuerier, queryString string, params interface{}) (rows gocb.QueryResults, err error) {
query := gocb.NewN1qlQuery(queryString)
rows, err = bucket.ExecuteN1qlQuery(query, params)
if err != nil {
return
}

return

}

func main() {
cb := CouchbaseDB{
ConnSpecStr: “couchbase://127.0.0.1”,
Bucket: “projects”,
Password: “”,
}

Bucket := connectDB(&cb)

const query = "SELECT * FROM `projects`"
rows, err := QueryCB(Bucket, query, nil)
if err != nil {
	panic(err)
}
var row interface{}
for rows.Next(&row) {
	fmt.Printf("Row: %v", row)
}

}

This all works until I try to unit test as I’m not able to implement query results.

Hopes this makes sense.

Hey @nokkie,

QueryResults is an interface type, you should be able to build your own struct which mocks it. You can find its definition here: https://godoc.org/github.com/couchbase/gocb#QueryResults

Cheers, Brett