From 28f01ff4eeb2a00ea4221ebc16ab1648ee245687 Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 4 Jan 2024 12:52:11 -0300 Subject: [PATCH] :sparkles: integration-test --- .vscode/settings.json | 3 +++ Makefile | 8 +++++++ cmd/newsletter/main.go | 11 ++++++++- mongodb/db.go | 28 +++++++++++++++++++++++ mongodb/db_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++ scrape_test.go | 11 +++++++++ 6 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 mongodb/db.go create mode 100644 mongodb/db_test.go create mode 100644 scrape_test.go diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..fdae17a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "go.buildTags": "-tags integration", +} diff --git a/Makefile b/Makefile index cf35dc1..0164eab 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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: diff --git a/cmd/newsletter/main.go b/cmd/newsletter/main.go index 6ec3943..86d3ded 100644 --- a/cmd/newsletter/main.go +++ b/cmd/newsletter/main.go @@ -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) } diff --git a/mongodb/db.go b/mongodb/db.go new file mode 100644 index 0000000..b4583e9 --- /dev/null +++ b/mongodb/db.go @@ -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 +} diff --git a/mongodb/db_test.go b/mongodb/db_test.go new file mode 100644 index 0000000..3aa30e6 --- /dev/null +++ b/mongodb/db_test.go @@ -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()) + +} diff --git a/scrape_test.go b/scrape_test.go new file mode 100644 index 0000000..0c862c2 --- /dev/null +++ b/scrape_test.go @@ -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") +}