-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"go.buildTags": "-tags integration", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |