SoftThorn.Respawn
6.2.2-alpha.0.3
dotnet add package SoftThorn.Respawn --version 6.2.2-alpha.0.3
NuGet\Install-Package SoftThorn.Respawn -Version 6.2.2-alpha.0.3
<PackageReference Include="SoftThorn.Respawn" Version="6.2.2-alpha.0.3" />
paket add SoftThorn.Respawn --version 6.2.2-alpha.0.3
#r "nuget: SoftThorn.Respawn, 6.2.2-alpha.0.3"
// Install SoftThorn.Respawn as a Cake Addin #addin nuget:?package=SoftThorn.Respawn&version=6.2.2-alpha.0.3&prerelease // Install SoftThorn.Respawn as a Cake Tool #tool nuget:?package=SoftThorn.Respawn&version=6.2.2-alpha.0.3&prerelease
SoftThorn.Respawn
NOTE: This is a fork from the official Respawn, that only includes this PR
Respawn is a small utility to help in resetting test databases to a clean state. Instead of deleting data at the end of a test or rolling back a transaction, Respawn resets the database back to a clean, empty state by intelligently deleting data from tables.
To use, create a Respawner
and initialize with tables you want to skip, or schemas you want to keep/ignore:
var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
{
TablesToIgnore = new Table[]
{
"sysdiagrams",
"tblUser",
"tblObjectType",
new Table("MyOtherSchema", "MyOtherTable")
},
SchemasToExclude = new []
{
"RoundhousE"
}
});
Or if you want to use a different database:
var respawner = await Respawner.CreateAsync(connection, new RespawnerOptions
{
SchemasToInclude = new []
{
"public"
},
DbAdapter = DbAdapter.Postgres
});
In your tests, in the fixture setup, reset to a clean state:
await respawner.ResetAsync("MyConnectionStringName");
or if you're using a database besides SQL Server, pass an open DbConnection
:
using (var conn = new NpgsqlConnection("ConnectionString"))
{
await conn.OpenAsync();
await respawner.ResetAsync(conn);
}
How does it work?
Respawn examines the SQL metadata intelligently to build a deterministic order of tables to delete based on foreign key relationships between tables. It navigates these relationships to build a DELETE script starting with the tables with no relationships and moving inwards until all tables are accounted for.
Once this in-order list of tables is created in the CreateAsync
factory, the Respawner object keeps this list of tables privately so that the list of tables and the order is only calculated once.
In your tests, you Reset your database before each test run. If there are any tables/schemas that you don't want to be cleared out, include these in the configuration of your RespawnerOptions.
In benchmarks, a deterministic deletion of tables is faster than truncation, since truncation requires disabling or deleting foreign key constraints. Deletion results in easier test debugging/maintenance, as transaction rollbacks/post-test deletion still rely on that mechanism at the beginning of each test. If data comes in from another source, your test might fail. Respawning to a clean state assures you have a known starting point before each test.
Installing Respawn
You should install Respawn with NuGet:
Install-Package Respawn
Or via the .NET Core CLI:
dotnet add package Respawn
This command from Package Manager Console will download and install Respawn.
Local development
To install and run local dependencies needed for tests, (PostgreSQL and MySQL) install Docker for Windows and from the command line at the solution root run:
docker-compose up -d
This will pull down the latest container images and run them. You can then run the local build/tests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Data.SqlClient (>= 4.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.2.2-alpha.0.3 | 92 | 4/28/2024 |