Skip to content

Commit 5cd0a6f

Browse files
docs(ClickHouse): Add example (#1421)
Co-authored-by: Andre Hofmeister <[email protected]>
1 parent 6306178 commit 5cd0a6f

File tree

8 files changed

+108
-5
lines changed

8 files changed

+108
-5
lines changed

docs/modules/activemq.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ You can start an Apache ActiveMQ Artemis container instance from any .NET applic
3030

3131
Connect to the container and produce a message:
3232

33-
=== "EstablishesConnection"
33+
=== "Establish connection"
3434
```csharp
35-
--8<-- "tests/Testcontainers.ActiveMq.Tests/ArtemisContainerTest.cs:ArtemisContainerEstablishesConnection"
35+
--8<-- "tests/Testcontainers.ActiveMq.Tests/ArtemisContainerTest.cs:EstablishConnection"
3636
```
3737

3838
The test example uses the following NuGet dependencies:

docs/modules/clickhouse.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ClickHouse
2+
3+
[ClickHouse](https://clickhouse.com/) is a high-performance, column-oriented SQL database management system (DBMS) for online analytical processing (OLAP).
4+
5+
Add the following dependency to your project file:
6+
7+
```shell title="NuGet"
8+
dotnet add package Testcontainers.ClickHouse
9+
```
10+
11+
You can start a ClickHouse 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.
12+
13+
=== "Test class"
14+
```csharp
15+
--8<-- "tests/Testcontainers.ClickHouse.Tests/ClickHouseContainerTest.docs.cs:UseClickHouseContainer"
16+
}
17+
```
18+
19+
Connect to the container:
20+
21+
=== "Establish connection"
22+
```csharp
23+
--8<-- "tests/Testcontainers.ClickHouse.Tests/ClickHouseContainerTest.docs.cs:EstablishConnection"
24+
```
25+
26+
Execute a SQL script:
27+
28+
=== "Run SQL script"
29+
```csharp
30+
--8<-- "tests/Testcontainers.ClickHouse.Tests/ClickHouseContainerTest.docs.cs:RunSQLScript"
31+
```
32+
33+
The test example uses the following NuGet dependencies:
34+
35+
=== "Package References"
36+
```xml
37+
--8<-- "tests/Testcontainers.ClickHouse.Tests/Testcontainers.ClickHouse.Tests.csproj:PackageReferences"
38+
```
39+
40+
To execute the tests, use the command `dotnet test` from a terminal.
41+
42+
--8<-- "docs/modules/_call_out_test_projects.txt"

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ nav:
5353
- modules/pulsar.md # Apache
5454
- modules/eventhubs.md # Azure
5555
- modules/servicebus.md # Azure
56+
- modules/clickhouse.md
5657
- modules/db2.md
5758
- modules/elasticsearch.md
5859
- modules/mongodb.md

tests/Testcontainers.ActiveMq.Tests/ArtemisContainerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ await DisposeAsyncCore()
3131
}
3232
// # --8<-- [end:UseArtemisContainer]
3333

34-
// # --8<-- [start:ArtemisContainerEstablishesConnection]
34+
// # --8<-- [start:EstablishConnection]
3535
[Fact]
3636
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
3737
public async Task EstablishesConnection()
@@ -74,7 +74,7 @@ await producer.SendAsync(producedMessage)
7474

7575
Assert.Equal(producedMessage.Text, receivedMessage.Body<string>());
7676
}
77-
// # --8<-- [end:ArtemisContainerEstablishesConnection]
77+
// # --8<-- [end:EstablishConnection]
7878

7979
protected virtual ValueTask DisposeAsyncCore()
8080
{

tests/Testcontainers.ClickHouse.Tests/ClickHouseContainerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Testcontainers.ClickHouse;
22

3-
public abstract class ClickHouseContainerTest(ClickHouseContainerTest.ClickHouseDefaultFixture fixture)
3+
public abstract partial class ClickHouseContainerTest(ClickHouseContainerTest.ClickHouseDefaultFixture fixture)
44
{
55
[Fact]
66
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace Testcontainers.ClickHouse;
2+
3+
public abstract partial class ClickHouseContainerTest
4+
{
5+
// <!-- -8<- [start:UseClickHouseContainer] -->
6+
public sealed class ClickHouseContainerExample : IAsyncLifetime
7+
{
8+
private readonly ClickHouseContainer _clickHouseContainer = new ClickHouseBuilder().Build();
9+
10+
public async ValueTask InitializeAsync()
11+
{
12+
await _clickHouseContainer.StartAsync(TestContext.Current.CancellationToken)
13+
.ConfigureAwait(false);
14+
}
15+
16+
public async ValueTask DisposeAsync()
17+
{
18+
await _clickHouseContainer.DisposeAsync()
19+
.ConfigureAwait(false);
20+
}
21+
// <!-- -8<- [end:UseClickHouseContainer] -->
22+
23+
// <!-- -8<- [start:EstablishConnection] -->
24+
[Fact]
25+
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
26+
public void ConnectionStateReturnsOpen()
27+
{
28+
// Given
29+
using DbConnection connection = new ClickHouseConnection(_clickHouseContainer.GetConnectionString());
30+
31+
// When
32+
connection.Open();
33+
34+
// Then
35+
Assert.Equal(ConnectionState.Open, connection.State);
36+
}
37+
// <!-- -8<- [end:EstablishConnection] -->
38+
39+
// <!-- -8<- [start:RunSQLScript] -->
40+
[Fact]
41+
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
42+
public async Task ExecScriptReturnsSuccessful()
43+
{
44+
// Given
45+
const string scriptContent = "SELECT 1;";
46+
47+
// When
48+
var execResult = await _clickHouseContainer.ExecScriptAsync(scriptContent, TestContext.Current.CancellationToken)
49+
.ConfigureAwait(true);
50+
51+
// Then
52+
Assert.True(0L.Equals(execResult.ExitCode), execResult.Stderr);
53+
Assert.Empty(execResult.Stderr);
54+
}
55+
// <!-- -8<- [end:RunSQLScript] -->
56+
}
57+
}

tests/Testcontainers.ClickHouse.Tests/Testcontainers.ClickHouse.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
<OutputType>Exe</OutputType>
77
</PropertyGroup>
88
<ItemGroup>
9+
<!-- -8<- [start:PackageReferences] -->
910
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
1011
<PackageReference Include="coverlet.collector"/>
1112
<PackageReference Include="xunit.runner.visualstudio"/>
1213
<PackageReference Include="xunit.v3"/>
1314
<PackageReference Include="ClickHouse.Client"/>
15+
<!-- -8<- [end:PackageReferences] -->
1416
</ItemGroup>
1517
<ItemGroup>
1618
<ProjectReference Include="../../src/Testcontainers.ClickHouse/Testcontainers.ClickHouse.csproj"/>

tests/Testcontainers.ClickHouse.Tests/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
global using System.Data;
22
global using System.Data.Common;
3+
global using System.Threading;
34
global using System.Threading.Tasks;
45
global using ClickHouse.Client.ADO;
56
global using DotNet.Testcontainers.Builders;

0 commit comments

Comments
 (0)