Skip to content

Commit

Permalink
docs: Use codeinclude in module examples (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Aug 30, 2024
1 parent b2699cc commit 2da2080
Show file tree
Hide file tree
Showing 25 changed files with 177 additions and 331 deletions.
2 changes: 1 addition & 1 deletion docs/examples/aspnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dotnet add package Testcontainers.MsSql --version 3.0.0

!!! note

The Microsoft SQL Server Docker image is not compatible with ARM devices, such as Macs with Apple Silicon. Instead, you can use the [SqlEdge](https://www.nuget.org/packages/Testcontainers.SqlEdge) module or [Testcontainers Cloud](https://www.testcontainers.cloud/).
When running on Macs with Apple Silicon, make sure that you are using Docker Desktop for macOS version 4.16 or newer, and that the Virtualization Framework is enabled, with Rosetta 2 installed to run [x86-64](https://techcommunity.microsoft.com/t5/sql-server-blog/azure-sql-edge-update-september-2023/ba-p/3930827) based containers.

The `WeatherForecastContainer` class configures in the default constructor all dependencies to start the container that hosts our application.

Expand Down
3 changes: 3 additions & 0 deletions docs/modules/_call_out_test_projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!!!tip

For the complete source code of this example and additional information, please refer to our [test projects](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/tests).
50 changes: 13 additions & 37 deletions docs/modules/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,28 @@

[Elasticsearch](https://www.elastic.co/elasticsearch/) is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease.

The following example uses the following NuGet packages:
Add the following dependency to your project file:

```console title="Install the NuGet dependencies"
```console title="NuGet"
dotnet add package Testcontainers.Elasticsearch
dotnet add package Elastic.Clients.Elasticsearch
dotnet add package xunit
```

IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`.
You can start an Elasticsearch container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

Copy and paste the following code into a new `.cs` test file within an existing test project.
<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.Elasticsearch.Tests/ElasticsearchContainerTest.cs) inside_block:UseElasticsearchContainer
<!--/codeinclude-->

```csharp
using Elastic.Clients.Elasticsearch;
using Elastic.Transport;
using Testcontainers.Elasticsearch;
using Xunit;
The test example uses the following NuGet dependencies:

public sealed class ElasticsearchContainerTest : IAsyncLifetime
{
private readonly ElasticsearchContainer _elasticsearch
= new ElasticsearchBuilder().Build();

[Fact]
public async Task ReadFromElasticsearch()
{
var settings = new ElasticsearchClientSettings(new Uri(_elasticsearch.GetConnectionString()));
settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);

var client = new ElasticsearchClient(settings);

var stats = await client.PingAsync();

Assert.True(stats.IsValidResponse);
}

public Task InitializeAsync()
=> _elasticsearch.StartAsync();

public Task DisposeAsync()
=> _elasticsearch.DisposeAsync().AsTask();
}
```
<!--codeinclude-->
[Package References](../../tests/Testcontainers.Elasticsearch.Tests/Testcontainers.Elasticsearch.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"

## A Note To Developers

The Testcontainers module creates a container that listens to requests over **HTTPS**. To communicate with the Elasticsearch instance, developers must create a `ElasticsearchClientSettings` instance and set the `ServerCertificateValidationCallback` delegate to `CertificateValidations.AllowAll`. Failing to do so will result in a communication failure as the .NET will reject the certificate coming from the container.
The Testcontainers module creates a container that listens to requests over **HTTPS**. To communicate with the Elasticsearch instance, developers must create a `ElasticsearchClientSettings` instance and set the `ServerCertificateValidationCallback` delegate to `CertificateValidations.AllowAll`. Failing to do so will result in a communication failure as the .NET will reject the certificate coming from the container.
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ await moduleNameContainer.StartAsync();
| Kusto emulator | `mcr.microsoft.com/azuredataexplorer/kustainer-linux:latest` | [NuGet](https://www.nuget.org/packages/Testcontainers.Kusto) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Kusto) |
| LocalStack | `localstack/localstack:2.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.LocalStack) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.LocalStack) |
| MariaDB | `mariadb:10.10` | [NuGet](https://www.nuget.org/packages/Testcontainers.MariaDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MariaDb) |
| Milvus | `milvusdb/milvus:v2.3.10` | [NuGet](https://www.nuget.org/packages/Testcontainers.Milvus) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Milvus) |
| MinIO | `minio/minio:RELEASE.2023-01-31T02-24-19Z` | [NuGet](https://www.nuget.org/packages/Testcontainers.Minio) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Minio) |
| MongoDB | `mongo:6.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.MongoDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MongoDb) |
| MySQL | `mysql:8.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.MySql) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MySql) |
Expand Down
56 changes: 20 additions & 36 deletions docs/modules/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,38 @@

[MongoDB](https://www.mongodb.com/what-is-mongodb) is a cross-platform document-oriented database. MongoDB's document model is simple for developers to use within their applications, while still providing all the complex capabilities of traditional relational databases.

The following example uses the following NuGet packages:
Add the following dependency to your project file:

```console title="Install the NuGet dependencies"
```console title="NuGet"
dotnet add package Testcontainers.MongoDb
dotnet add package MongoDB.Driver
dotnet add package xunit
```

IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`.
You can start a MongoDB container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.

Copy and paste the following code into a new `.cs` test file within an existing test project.
<!--codeinclude-->
[Create Container Instance](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:CreateMongoDbContainer
<!--/codeinclude-->

```csharp
using MongoDB.Driver;
using Testcontainers.MongoDb;
using Xunit;
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

namespace TestcontainersModules;
<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:UseMongoDbContainer
<!--/codeinclude-->

public sealed class MongoDbContainerTest : IAsyncLifetime
{
private readonly MongoDbContainer _mongoDbContainer =
new MongoDbBuilder().Build();
The test example uses the following NuGet dependencies:

[Fact]
public async Task ReadFromMongoDbDatabase()
{
var client = new MongoClient(_mongoDbContainer.GetConnectionString());

using var databases = await client.ListDatabasesAsync();

Assert.True(await databases.AnyAsync());
}

public Task InitializeAsync()
=> _mongoDbContainer.StartAsync();

public Task DisposeAsync()
=> _mongoDbContainer.DisposeAsync().AsTask();
}
```
<!--codeinclude-->
[Package References](../../tests/Testcontainers.MongoDb.Tests/Testcontainers.MongoDb.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"

## MongoDb Replica Set

By default, MongoDB runs as a standalone instance. If your tests require a MongoDB replica set, use the code below which will initialize it as a single-node replica set:
By default, MongoDB runs as a standalone instance. If your tests require a MongoDB replica set, use the following configuration which will initialize a single-node replica set:

```csharp
MongoDbContainer _mongoDbContainer = new MongoDbBuilder().WithReplicaSet().Build();
```
<!--codeinclude-->
[Replica Set Configuration](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:ReplicaSetContainerConfiguration
<!--/codeinclude-->
57 changes: 18 additions & 39 deletions docs/modules/mssql.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,35 @@
# Microsoft SQL Server

[Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server), also known as MSSQL, is a relational database engine developed by Microsoft and is a popular choice in enterprise systems. The following example provides .NET developers with a starting point to use a Microsoft SQL Server instance in the [xUnit][xunit] tests.
[Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server), also known as MSSQL, is a relational database engine developed by Microsoft and is a popular choice in enterprise systems.

The following example uses the following NuGet packages:
Add the following dependency to your project file:

```console title="Install the NuGet dependencies"
```console title="NuGet"
dotnet add package Testcontainers.MsSql
dotnet add package Microsoft.Data.SqlClient
dotnet add package xunit
```

IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`.
You can start a MSSQL container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.

Copy and paste the following code into a new `.cs` test file within an existing test project.
<!--codeinclude-->
[Create Container Instance](../../tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs) inside_block:CreateMsSqlContainer
<!--/codeinclude-->

```csharp
using Microsoft.Data.SqlClient;
using Testcontainers.MsSql;
using Xunit;
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

namespace TestcontainersModules;
<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs) inside_block:UseMsSqlContainer
<!--/codeinclude-->

public sealed class MsSqlServerContainerTest : IAsyncLifetime
{
private readonly MsSqlContainer _msSqlContainer
= new MsSqlBuilder().Build();
The test example uses the following NuGet dependencies:

[Fact]
public async Task ReadFromMsSqlDatabase()
{
await using var connection = new SqlConnection(_msSqlContainer.GetConnectionString());
await connection.OpenAsync();

await using var command = connection.CreateCommand();
command.CommandText = "SELECT 1;";

var actual = await command.ExecuteScalarAsync() as int?;
Assert.Equal(1, actual.GetValueOrDefault());
}

public Task InitializeAsync()
=> _msSqlContainer.StartAsync();

public Task DisposeAsync()
=> _msSqlContainer.DisposeAsync().AsTask();
}
```
<!--codeinclude-->
[Package References](../../tests/Testcontainers.MsSql.Tests/Testcontainers.MsSql.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->

To execute the tests, use the command `dotnet test` from a terminal.

## A Note To Developers
--8<-- "docs/modules/_call_out_test_projects.md"

Once Testcontainers creates a server instance, developers may use the connection string with any of the popular data-access technologies found in the .NET Ecosystem. Some of these libraries include [Entity Framework Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore), [Dapper](https://www.nuget.org/packages/Dapper), and [NHibernate](https://www.nuget.org/packages/NHibernate). At which point, developers can execute database migrations and SQL scripts.
## A Note To Developers

[xunit]: https://xunit.net/
Once Testcontainers creates a server instance, developers may use the connection string with any of the popular data-access technologies found in the .NET ecosystem. Some of these libraries include [Entity Framework Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore), [Dapper](https://www.nuget.org/packages/Dapper), and [NHibernate](https://www.nuget.org/packages/NHibernate). At which point, developers can execute database migrations and SQL scripts.
50 changes: 13 additions & 37 deletions docs/modules/neo4j.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
# Neo4j

[Neo4j](https://neo4j.com/product/neo4j-graph-database/) is a graph database designed to work with nodes and edges. It is a ACID-compliant transactional graph database engine, and developers can communicate with it using the HTTP endpoint or by using the **Bolt** protocol.
[Neo4j](https://neo4j.com/product/neo4j-graph-database/) is a graph database designed to work with nodes and edges. It is an ACID-compliant transactional graph database engine, and developers can communicate with it using the HTTP endpoint or by using the **Bolt** protocol.

The following example uses the following NuGet packages:
Add the following dependency to your project file:

```console title="Install the NuGet dependencies"
```console title="NuGet"
dotnet add package Testcontainers.Neo4j
dotnet add package Neo4j.Driver
dotnet add package xunit
```

IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`.
You can start an Neo4j container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

Copy and paste the following code into a new `.cs` test file within an existing test project.
<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.Neo4j.Tests/Neo4jContainerTest.cs) inside_block:UseNeo4jContainer
<!--/codeinclude-->

```csharp
using Neo4j.Driver;
using Testcontainers.Neo4j;
using Xunit;
The test example uses the following NuGet dependencies:

namespace TestcontainersModules;

public sealed class Neo4jContainerTest : IAsyncLifetime
{
private readonly Neo4jContainer _neo4jContainer
= new Neo4jBuilder().Build();

[Fact]
public async Task CanReadNeo4jDatabase()
{
const string database = "neo4j";

await using var client = GraphDatabase.Driver(_neo4jContainer.GetConnectionString());

await using var session = client.AsyncSession(cfg => cfg.WithDatabase(database));

Assert.Equal(database, session.SessionConfig.Database);
}

public Task InitializeAsync()
=> _neo4jContainer.StartAsync();

public Task DisposeAsync()
=> _neo4jContainer.DisposeAsync().AsTask();
}
```
<!--codeinclude-->
[Package References](../../tests/Testcontainers.Neo4j.Tests/Testcontainers.Neo4j.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
53 changes: 21 additions & 32 deletions docs/modules/postgres.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
# PostgreSQL

Here is an example of a pre-configured PostgreSQL [module](https://www.nuget.org/packages/Testcontainers.PostgreSql). In the example, Testcontainers starts a PostgreSQL database in a [xUnit.net][xunit] test and executes a SQL query against it.

```csharp
public sealed class PostgreSqlContainerTest : IAsyncLifetime
{
private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder().Build();

public Task InitializeAsync()
{
return _postgreSqlContainer.StartAsync();
}

public Task DisposeAsync()
{
return _postgreSqlContainer.DisposeAsync().AsTask();
}

[Fact]
public void ExecuteCommand()
{
using (DbConnection connection = new NpgsqlConnection(_postgreSqlContainer.GetConnectionString()))
{
using (DbCommand command = new NpgsqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = "SELECT 1";
}
}
}
}
[PostgreSQL](https://www.postgresql.org/) is a powerful, open-source relational database management system (RDBMS) used to store, manage, and retrieve structured data.

Add the following dependency to your project file:

```console title="NuGet"
dotnet add package Testcontainers.PostgreSql
```

[xunit]: https://xunit.net/
You can start an PostgreSQL container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.

<!--codeinclude-->
[Usage Example](../../tests/Testcontainers.PostgreSql.Tests/PostgreSqlContainerTest.cs) inside_block:UsePostgreSqlContainer
<!--/codeinclude-->

The test example uses the following NuGet dependencies:

<!--codeinclude-->
[Package References](../../tests/Testcontainers.PostgreSql.Tests/Testcontainers.PostgreSql.Tests.csproj) inside_block:PackageReferences
<!--/codeinclude-->

To execute the tests, use the command `dotnet test` from a terminal.

--8<-- "docs/modules/_call_out_test_projects.md"
Loading

0 comments on commit 2da2080

Please sign in to comment.