-
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Use codeinclude in module examples (#1245)
- Loading branch information
1 parent
b2699cc
commit 2da2080
Showing
25 changed files
with
177 additions
and
331 deletions.
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
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 @@ | ||
!!!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). |
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
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 |
---|---|---|
@@ -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. |
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 |
---|---|---|
@@ -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" |
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 |
---|---|---|
@@ -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" |
Oops, something went wrong.