Skip to content

Commit

Permalink
✨ integration-test
Browse files Browse the repository at this point in the history
  • Loading branch information
perebaj committed Jan 4, 2024
1 parent 6dee279 commit 28f01ff
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.buildTags": "-tags integration",
}
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ test:
go test ./... -timeout 10s -race; \
fi

.PHONY: integration-test
integration-test:
go test -timeout 5s -tags=integration ./...

## builds the service
.PHONY: service
service:
Expand Down Expand Up @@ -84,6 +88,10 @@ dev/logs:
dev/stop:
docker-compose stop

## Access the container
dev:
@$(devrun) bash

## Display help for all targets
.PHONY: help
help:
Expand Down
11 changes: 10 additions & 1 deletion cmd/newsletter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ func main() {
if err != nil {
panic(err)
}
fmt.Println("PING")

fmt.Println("Connected to MongoDB!")

collection := client.Database("newsletter").Collection("newsletter")
res, err := collection.InsertOne(ctx, map[string]string{"name": "pi", "value": "3.14159"})
if err != nil {
panic(err)
}
id := res.InsertedID
fmt.Println(id)
}
28 changes: 28 additions & 0 deletions mongodb/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Package mongodb provides a MongoDB connection.
package mongodb

import (
"context"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Config is the configuration for the MongoDB connection.
type Config struct {
URI string
}

// Connect connects to the MongoDB instance.
func Connect(ctx context.Context, cfg Config) (*mongo.Client, error) {
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.URI))
if err != nil {
return nil, err
}
err = client.Ping(ctx, nil)
if err != nil {
return nil, err
}

return client, nil
}
51 changes: 51 additions & 0 deletions mongodb/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build integration
// +build integration

package mongodb

import (
"context"
"testing"

"go.mongodb.org/mongo-driver/mongo"
)

func setup(ctx context.Context, t testing.TB) (*mongo.Collection, func()) {
client, err := Connect(ctx, Config{
URI: "mongodb://root:root@mongodb:27017",
})

if err != nil {
panic(err)
}

randCollection := t.Name()

collection := client.Database("test").Collection(randCollection)

teardown := func() {
err = collection.Drop(ctx)
if err != nil {
panic(err)
}
}

return collection, teardown
}

func TestConnect(t *testing.T) {
collection, teardown := setup(context.Background(), t)

defer teardown()

resp, err := collection.InsertOne(context.Background(), map[string]string{"name": "pi", "value": "3.14159"})

if err != nil {
t.Fatal(err)
}
t.Log(resp.InsertedID)

result := collection.FindOne(context.Background(), map[string]string{"name": "pi", "value": "3.14159"})
t.Log(result.Raw())

}
11 changes: 11 additions & 0 deletions scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package newsletter

import (
"fmt"
"testing"
)

func TestFake(t *testing.T) {
fmt.Println("This test shouldn't fail")
t.Log("This test shouldn't fail")
}

0 comments on commit 28f01ff

Please sign in to comment.