https://github.com/pedrohenriques/go-dbfixtures-mongodb-driver
A MongoDB driver for the Golang go-dbfixtures package.
https://github.com/pedrohenriques/go-dbfixtures-mongodb-driver
fixtures golang mongodb testing testing-tools
Last synced: 3 months ago
JSON representation
A MongoDB driver for the Golang go-dbfixtures package.
- Host: GitHub
- URL: https://github.com/pedrohenriques/go-dbfixtures-mongodb-driver
- Owner: PedroHenriques
- License: mit
- Created: 2023-05-01T23:27:34.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-11T09:44:22.000Z (almost 3 years ago)
- Last Synced: 2025-04-17T12:31:40.129Z (about 1 year ago)
- Topics: fixtures, golang, mongodb, testing, testing-tools
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://coveralls.io/github/PedroHenriques/go-dbfixtures-mongodb-driver?branch=main)


# Fixtures Manager MongoDB Driver
An abstraction layer for the [mongodb package](https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo) to facilitate handling database fixtures for testing purposes, in a MongoDB database.
This package is ment to be used in conjunction with the [dbfixtures package](https://pkg.go.dev/github.com/PedroHenriques/go-dbfixtures/dbfixtures), but can also be used by itself.
## Installation
```sh
go get github.com/PedroHenriques/go-dbfixtures-mongodb-driver
```
## Usage
This package exposes the `func New(mongoClient *mongo.Client, dbName string, dbOpts *options.DatabaseOptions) dbfixtures.IDriver` function that returns an instance of the driver.
**Note:** For detailed information about the `options.DatabaseOptions` argument, please consult the [MongoDB Golang driver's documentation]https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.12.1/mongo/options#DatabaseOptions).
An instance of the driver exposes the following interface
```go
type IDriver interface {
// clears the specified "tables" of any content
Truncate(tableNames []string) error
// inserts the supplied "rows" into the specified "table"
InsertFixtures(tableName string, fixtures []interface{}) error
// cleanup and terminate the connection to the database
Close() error
}
```
### Example
```go
package driver_test
import (
"context"
"github.com/PedroHenriques/go-dbfixtures-mongodb-driver/driver"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type driverE2eTestSuite struct {
suite.Suite
mongoCliente *mongo.Client
mongoDbName string
}
func (suite *driverE2eTestSuite) SetupSuite() {
ConnUrl := "mongodb://testmongo:27017"
opts := options.Client().ApplyURI(ConnUrl)
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
panic(err)
}
suite.mongoCliente = client
suite.mongoDbName = "testCol"
}
func (suite *driverE2eTestSuite) TearDownSuite() {
suite.mongoCliente.Disconnect(context.TODO())
}
func (suite *driverE2eTestSuite) TestItShouldWork() {
database := suite.mongoCliente.Database(suite.mongoDbName)
col1 := database.Collection("col1")
insRes1, _ := col1.InsertMany(
context.TODO(),
[]interface{}{
testDocument{Name: "doc 11", Age: 3},
testDocument{Name: "doc 12", Age: 33},
},
)
require.Equal(suite.T(), 2, len(insRes1.InsertedIDs))
expectedDocuments := &[]interface{}{
testDocument{Name: "doc 26", Age: 86},
}
driver := driver.New(
suite.mongoCliente, suite.mongoDbName, &options.DatabaseOptions{},
)
err := driver.InsertFixtures("col1", *expectedDocuments)
require.Nil(suite.T(), err)
colDocs, _ := col1.Find(context.TODO(), bson.D{})
actualDocuments := &[]testDocument{}
colDocs.All(context.TODO(), actualDocuments)
require.EqualValues(suite.T(), len(*expectedDocuments), len(*actualDocuments))
for i, actualDocument := range *actualDocuments {
require.EqualExportedValues(suite.T(), (*expectedDocuments)[i], actualDocument)
}
}
```
## Testing This Package
* `cd` into the package's root directory
* run `sh cli/test.sh -b -gv 1.20`