Skip to content

Commit aa8234d

Browse files
authored
feat: Add Respawn example (#1307)
1 parent 6051cdb commit aa8234d

15 files changed

+208
-5
lines changed

examples/Flyway/Directory.Packages.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<!-- Unit and integration test dependencies: -->
8-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
9-
<PackageVersion Include="Testcontainers.PostgreSql" Version="3.7.0"/>
10-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7"/>
11-
<PackageVersion Include="xunit" Version="2.7.0"/>
8+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
9+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0"/>
10+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
11+
<PackageVersion Include="xunit" Version="2.9.2"/>
1212
<!-- Third-party client dependencies to connect and interact with the containers: -->
13-
<PackageVersion Include="Npgsql" Version="6.0.10"/>
13+
<PackageVersion Include="Npgsql" Version="6.0.11"/>
1414
</ItemGroup>
1515
</Project>

examples/Respawn/.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
root = true

examples/Respawn/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<PropertyGroup>
4+
<Version>0.1.0</Version>
5+
<PackageId>$(AssemblyName)</PackageId>
6+
<AssemblyVersion>$(Version)</AssemblyVersion>
7+
<FileVersion>$(Version)</FileVersion>
8+
<InformationalVersion>$(Version)</InformationalVersion>
9+
<NeutralLanguage>en-US</NeutralLanguage>
10+
<Authors>Andre Hofmeister</Authors>
11+
<Company></Company>
12+
<Description></Description>
13+
<Summary></Summary>
14+
<PackageIconUrl></PackageIconUrl>
15+
<PackageLicenseExpression></PackageLicenseExpression>
16+
<PackageProjectUrl></PackageProjectUrl>
17+
<PackageTags></PackageTags>
18+
<RepositoryType>git</RepositoryType>
19+
<RepositoryUrl>https://github.com/testcontainers/testcontainers-dotnet-sample</RepositoryUrl>
20+
</PropertyGroup>
21+
<PropertyGroup>
22+
<LangVersion>10.0</LangVersion>
23+
<Nullable>enable</Nullable>
24+
</PropertyGroup>
25+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<PropertyGroup>
4+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<!-- Unit and integration test dependencies: -->
8+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
9+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0"/>
10+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
11+
<PackageVersion Include="xunit" Version="2.9.2"/>
12+
<!-- Third-party client dependencies to connect and interact with the containers: -->
13+
<PackageVersion Include="Npgsql" Version="6.0.11"/>
14+
<PackageVersion Include="Respawn" Version="6.2.1"/>
15+
</ItemGroup>
16+
</Project>

examples/Respawn/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Testcontainers for .NET Respawn example
2+
3+
This example demonstrates how to use Respawn to reset the state of a PostgreSQL database between tests, ensuring isolation and consistency. The `RespawnTest` test class interacts with a pre-configured database, using Respawn to clear the data before each test run. This allows the tests to execute in a clean environment without interfering with each other.
4+
5+
```console
6+
git clone --branch develop [email protected]:testcontainers/testcontainers-dotnet.git
7+
cd ./testcontainers-dotnet/examples/Respawn/
8+
dotnet test Respawn.sln --configuration=Release
9+
```

examples/Respawn/Respawn.sln

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.31903.59
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{424CFC36-D6F7-4DBB-BD1C-0C84FE30E665}"
6+
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Respawn.Tests", "tests\Respawn.Tests\Respawn.Tests.csproj", "{75E66E21-E169-4504-89FA-4784CB66DEB5}"
8+
EndProject
9+
Global
10+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
11+
Debug|Any CPU = Debug|Any CPU
12+
Release|Any CPU = Release|Any CPU
13+
EndGlobalSection
14+
GlobalSection(SolutionProperties) = preSolution
15+
HideSolutionNode = FALSE
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{75E66E21-E169-4504-89FA-4784CB66DEB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{75E66E21-E169-4504-89FA-4784CB66DEB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{75E66E21-E169-4504-89FA-4784CB66DEB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{75E66E21-E169-4504-89FA-4784CB66DEB5}.Release|Any CPU.Build.0 = Release|Any CPU
22+
EndGlobalSection
23+
GlobalSection(NestedProjects) = preSolution
24+
{75E66E21-E169-4504-89FA-4784CB66DEB5} = {424CFC36-D6F7-4DBB-BD1C-0C84FE30E665}
25+
EndGlobalSection
26+
EndGlobal

examples/Respawn/src/.gitkeep

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Respawn.Tests;
2+
3+
[UsedImplicitly]
4+
public sealed class DbFixture : IAsyncLifetime
5+
{
6+
private readonly IContainer _postgreSqlContainer;
7+
8+
public DbFixture()
9+
{
10+
// Testcontainers starts the dependent database (PostgreSQL) and copies the SQL scripts
11+
// to the container before it starts. The PostgreSQL container runs the scripts
12+
// automatically during startup, creating the database schema.
13+
_postgreSqlContainer = new PostgreSqlBuilder()
14+
.WithImage("postgres:15-alpine")
15+
.WithResourceMapping("migrate/", "/docker-entrypoint-initdb.d/")
16+
.Build();
17+
}
18+
19+
public DbConnection DbConnection => new NpgsqlConnection(((PostgreSqlContainer)_postgreSqlContainer).GetConnectionString());
20+
21+
public Task InitializeAsync()
22+
{
23+
return _postgreSqlContainer.StartAsync();
24+
}
25+
26+
public Task DisposeAsync()
27+
{
28+
return _postgreSqlContainer.DisposeAsync().AsTask();
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net8.0</TargetFrameworks>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
8+
<PackageReference Include="Testcontainers.PostgreSql"/>
9+
<PackageReference Include="xunit.runner.visualstudio"/>
10+
<PackageReference Include="xunit"/>
11+
<PackageReference Include="Npgsql"/>
12+
<PackageReference Include="Respawn"/>
13+
</ItemGroup>
14+
<ItemGroup>
15+
<None Include="migrate/*.sql" CopyToOutputDirectory="PreserveNewest"/>
16+
</ItemGroup>
17+
</Project>

0 commit comments

Comments
 (0)