Original post

Hi,

I am currently using the following couchbase package in project.
“gopkg.in/couchbase/gocb.v1”

cbConnStr = config.Viper.GetString(
“database.couchbase.connectionstring”)
cbBucket = config.Viper.GetString(
“database.couchbase.bucket”)
)

type CbRepository struct {
Cluster *gocb.Cluster
Bucket *gocb.Bucket
}

func NewCbRepository() *CbRepository {
logger.BootstrapLogger.Debug(“Entering Repository.NewCbRepository() …”)

cluster, err := gocb.Connect(cbConnStr)
if err != nil {
	logger.BootstrapLogger.Error(err)
	panic(err)
}
cluster.Authenticate(gocb.PasswordAuthenticator{
	Username: config.Viper.GetString("database.couchbase.username"),
	Password: config.Viper.GetString("database.couchbase.password"),
})

bucket, err := cluster.OpenBucket(cbBucket, "")
if err != nil {
	logger.BootstrapLogger.Error(err)
	panic(err)
}
return &CbRepository{
	Cluster: cluster,
	Bucket:  bucket,
}

}

func (r CbRepository) Insert(item entity.Favorite) error {
logger.Logger.Debug(“Entering CbRepository.Insert() …”)
_, err := r.Bucket.Insert(“DocumentName”, item, 0)
if err != nil {
logger.Logger.Error(err)
switch err {
/
* Include -case gocb.Err**- to handle Couchbase error types here as required.
* Fallback to default if none match
*/
case gocb.ErrTimeout:
return entity.ErrDatabaseFailure
case gocb.ErrKeyExists:
return entity.ErrItemExists
default:
return entity.ErrDefault
}
}
return nil
}

This couchbase package is not interface driven, so how do I mock the database request?
r.Bucket.Insert() —
r.Bucket.Upsert() etc

My program structure looks like this:

  1. Main > Handler > Service layer > repositoryCb>repository

Note: Repository can be more than one, say couchbase or AWS Documentdb. That why I have separate Implementation for repositoryCb

Using mockGen,
I have mocked repository interface in service layer.
Mocked service layer in handler

My problem is how to mock the core CRUD operations like bucket.Insert(), bucket.Upsert(), bucket.Get() etc

Kindly share your thoughts.