Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: use Go testable examples in modules #1603

Merged
merged 39 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ba14798
fix: rename vault container in tests
mdelapenya Sep 6, 2023
b192a81
chore: adjust comment in template
mdelapenya Sep 6, 2023
64bd746
docs(vault): use RunContainer example in usage section
mdelapenya Sep 6, 2023
14a6022
docs: update vault docs
mdelapenya Sep 6, 2023
d5b68da
docs: add RunContainer example for redpanda
mdelapenya Sep 7, 2023
4cb9f60
docs: add RunContainer example for redis
mdelapenya Sep 7, 2023
93f2a5c
docs: document creating networks
mdelapenya Sep 7, 2023
61b37ec
docs: add RunContainer example for pulsar
mdelapenya Sep 7, 2023
e547200
docs: add RunContainer example for postgres
mdelapenya Sep 7, 2023
2273da3
docs: add RunContainer example for neo4j
mdelapenya Sep 7, 2023
a2b03fd
docs: add RunContainer example for nats
mdelapenya Sep 7, 2023
8325e51
docs: update quickstart to not use testing libraries
mdelapenya Sep 7, 2023
2886ebd
docs: add RunContainer example for mysql
mdelapenya Sep 7, 2023
72cabe1
docs: add RunContainer example for mongo
mdelapenya Sep 7, 2023
86015c5
docs: add RunContainer example for mariadb
mdelapenya Sep 7, 2023
fc9b9ce
chore: use logger to print out hostname external reason
mdelapenya Sep 7, 2023
7ad9043
docs: document new Go examples file
mdelapenya Sep 7, 2023
fc9bea3
docs: add RunContainer example for localstack
mdelapenya Sep 7, 2023
9bd0377
chore: convert k3s test into a testable example
mdelapenya Sep 7, 2023
9911613
chore: convert mongodb test into testable example
mdelapenya Sep 7, 2023
8f90e0a
fix: handle errors in example tests for elasticsearch
mdelapenya Sep 7, 2023
af18064
fix: do not deprecate used fields
mdelapenya Sep 8, 2023
286c86f
chore: convert couchbase test into testable example
mdelapenya Sep 8, 2023
7a48310
chore: convert clickhouse test into testable example
mdelapenya Sep 8, 2023
ac4c5f0
docs: adjust artemis docs
mdelapenya Sep 8, 2023
a6c4780
chore: simplify postgres example
mdelapenya Sep 8, 2023
b01f6af
fix: lint
mdelapenya Sep 8, 2023
94386b9
Merge branch 'main' into migrate-examples
mdelapenya Sep 10, 2023
d411e3a
chore(deps): bump github.com/hashicorp/vault-client-go in /modules/va…
mmorel-35 Sep 11, 2023
7c5f468
ci(lint): enable gocritic linter (#1605)
mmorel-35 Sep 11, 2023
a85ac93
ci(lint): enable errorlint linter (#1604)
mmorel-35 Sep 11, 2023
d6c4221
fix: handle errors
mdelapenya Sep 11, 2023
9235361
chore(pulsar): create/remove the network properly
mdelapenya Sep 11, 2023
b5d3749
Merge branch 'main' into migrate-examples
mdelapenya Sep 11, 2023
0e28881
chore: better container logs on errors during startup
mdelapenya Sep 11, 2023
25adc7a
Revert "chore: better container logs on errors during startup"
mdelapenya Sep 11, 2023
2d91912
chore: better container logs on errors during startup
mdelapenya Sep 11, 2023
c8a8a7b
fix: do not wrap error but print it
mdelapenya Sep 11, 2023
1f64536
fix: avoid polluting default wait strategies in pulsar
mdelapenya Sep 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: add RunContainer example for mongo
  • Loading branch information
mdelapenya committed Sep 8, 2023
commit 72cabe19c1e4e3dc2dd6807776ebd9e9a2d21213
10 changes: 6 additions & 4 deletions docs/modules/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/modules/mongodb
## Usage example

<!--codeinclude-->
[Creating a MongoDB container](../../modules/mongodb/mongodb_test.go) inside_block:createMongoDBContainer
[Creating a MongoDB container](../../modules/mongodb/examples_test.go) inside_block:runMongoDBContainer
<!--/codeinclude-->

## Module reference
Expand Down Expand Up @@ -66,9 +66,11 @@ The MongoDB container exposes the following methods:

#### ConnectionString

This method returns the connection string to connect to the MongoDB container.
The `ConnectionString` method returns the connection string to connect to the MongoDB container.
It returns a string with the format `mongodb://<host>:<port>`.

It can be use to configure a MongoDB client (`go.mongodb.org/mongo-driver/mongo`), e.g.:

<!--codeinclude-->
[Get connection string](../../modules/mongodb/mongodb_test.go) inside_block:connectionString
<!--/codeinclude-->
[Using ConnectionString with the MongoDB client](../../modules/mongodb/examples_test.go) inside_block:connectToMongo
<!--/codeinclude-->
78 changes: 78 additions & 0 deletions modules/mongodb/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package mongodb_test

import (
"context"
"fmt"

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

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mongodb"
)

func ExampleRunContainer() {
// runMongoDBContainer {
ctx := context.Background()

mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6"))
if err != nil {
panic(err)
}

// Clean up the container
defer func() {
if err := mongodbContainer.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

state, err := mongodbContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}

func ExampleRunContainer_connect() {
// connectToMongo {
ctx := context.Background()

mongodbContainer, err := mongodb.RunContainer(ctx, testcontainers.WithImage("mongo:6"))
if err != nil {
panic(err)
}

// Clean up the container
defer func() {
if err := mongodbContainer.Terminate(ctx); err != nil {
panic(err)
}
}()

endpoint, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
panic(err)
}

mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(endpoint))
if err != nil {
panic(err)
}
// }

err = mongoClient.Ping(ctx, nil)
if err != nil {
panic(err)
}

fmt.Println(mongoClient.Database("test").Name())

// Output:
// test
}
4 changes: 1 addition & 3 deletions modules/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import (
func TestMongoDB(t *testing.T) {
ctx := context.Background()

// createMongoDBContainer {
container, err := RunContainer(ctx)
if err != nil {
t.Fatal(err)
}
// }

// Clean up the container after the test is complete
t.Cleanup(func() {
Expand All @@ -30,10 +28,10 @@ func TestMongoDB(t *testing.T) {

// connectionString {
endpoint, err := container.ConnectionString(ctx)
// }
if err != nil {
t.Error(fmt.Errorf("failed to get connection string: %w", err))
}
// }

mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(endpoint))
if err != nil {
Expand Down