https://github.com/akhettar/docker-db
Run DB as docker container for integration tests
https://github.com/akhettar/docker-db
database docker-databases integration-tests
Last synced: 6 months ago
JSON representation
Run DB as docker container for integration tests
- Host: GitHub
- URL: https://github.com/akhettar/docker-db
- Owner: akhettar
- License: mit
- Created: 2020-04-07T15:02:50.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-27T08:32:36.000Z (about 6 years ago)
- Last Synced: 2024-06-20T03:32:21.169Z (about 2 years ago)
- Topics: database, docker-databases, integration-tests
- Language: Go
- Size: 102 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Docker Test

[](https://godoc.org/github.com/akhettar/docker-db)

This is a Go library to run database containers as part of running the integration tests. The following databases are supported:
* Postgres
* MongoDB - DocumentDB
# How to use
## Running Postgres container
An example of running `postgres` container is present in this project. See the following files:
* [Integratino test](docker_test.go)
* [Test fixture](init_test.go)
## Running MongoDB container
`Integration test snipppet`
```go
func TestPublishAppStatus_WithInvalidAppPlatformReturnBadRequestResponse(t *testing.T) {
t.Logf("Given the app status api is up and running")
{
platform := "dummy"
t.Logf("\tWhen Sending Publish App status request to endpoint with unsupported platform value: \"%s\"", platform)
{
mockUnleash := test.GetMockUnleashClient(t)
handler := NewAppStatusHandler(Repository, mockUnleash)
router := handler.CreateRouter()
version := "1.0"
body := model.ReleaseRequest{Version: version, Platform: platform}
req, err := test.HttpRequest(body, "/status", http.MethodPost, test.ValidToken)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// check call success
test.Ok(err, t)
if w.Code == http.StatusBadRequest {
t.Logf("\t\tShould receive a \"%d\" status. %v", http.StatusBadRequest, test.CheckMark)
} else {
t.Errorf("\t\tShould receive a \"%d\" status. %v %v", http.StatusBadRequest, test.BallotX, w.Code)
}
}
}
}
```
In the same package include a test file with the name: `init_test.go` and include the following
```go
import (
"github.com/akhettar/app-features-manager/repository"
"context"
"flag"
"github.com/akhettar/docker-db"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"net/http/httptest"
"os"
"testing"
)
const ProfileEnvVar = "PROFILE"
var (
// The repository instance configured to run against the Docker DB Test container
Repository *repository.MongoRepository
)
// TestFixture wraps all tests with the needed initialized mock DB and fixtures
// This test runs before other integration test. It starts an instance of mongo db in the background (provided you have mongo
// installed on the server on which this test will be running) and shuts it down.
func TestMain(m *testing.M) {
container := dbtest.StartMongoContainer()
log.Printf("running mongo with Ip %s", container.Host())
uri := fmt.Sprintf("mongodb://%s:%d", container.Host(), container.Port())
clientOptions := options.Client().ApplyURI(uri)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
panic(err)
}
// Check the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
Repository = &repository.MongoRepository{client, repository.DBInfo{uri, repository.DefaultDBName, repository.DefaultCollection}}
// Run the test suite
retCode := m.Run()
c.Destroy()
// call with result of m.Run()
os.Exit(retCode)
}
```
# License
[MIT](LICENSE)