Skip to content
Dead simple integration tests with SQL Server or Postgres throwaway databases that are created on the fly, used briefly then disposed of automagically.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
build
src
tests
.gitattributes
.gitignore
.nuke
LICENSE
README.md
build.ps1
build.sh

README.md

ThrowawayDb

Easily create a disposable database that integration tests dead simple for Sql server using throwaway databases.

Available Packages

Package Supports Version
ThrowawayDb SQL Server Nuget
ThrowawayDb.Postgres SQL Server Nuget

Using SQL Server

Install ThrowawayDb from Nuget

dotent add package ThrowawayDb

Use from your code

public static void Main(string[] args)
{
    using (var database = ThrowawayDatabase.FromLocalInstance("localhost\\SQLEXPRESS"))
    {
        Console.WriteLine($"Created database {database.Name}");

        // - Apply database migrations here if necessary
        // - Seed the database with data
        // - Execute your code against this database

        using (var connection = new SqlConnection(database.ConnectionString))
        {
            connection.Open();
            using (var cmd = new SqlCommand("SELECT 1", connection))
            {
                var result = Convert.ToInt32(cmd.ExecuteScalar());
                Console.WriteLine(result);
            }
        }
    }
}

In the snippet above, a dummy database with a random name is created using the current local instance of SQL Express on my machine. Moreover, the object database is an IDisposable and will drop the database at the end of the using clause when it's Dispose() is executed. It is that simple!

You can create the throwaway database in multiple ways:

// from Sql Express server locally with Integrated security
ThrowawayDatabase.FromLocalInstance("localhost\\SQLEXPRESS")

// Uses the default instance locally where Data Source = .
ThrowawayDatabase.FromDefaultLocalInstance()

// Using SQL Authentication with user credentials and an arbibrary host
ThrowawayDatabase.Create(username: "Zaid", password: "strongPassword", host: "192.168.1.100")

// Using a connection string where Initial Catalog is master
ThrowawayDatabase.Create(connectionString: "Data Source=localhost\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;")

Using PostgreSQL server

Install ThrowawayDb.Postgres from Nuget

dotnet add package ThrowawayDb.Postgres

use from your code:

static void Main(string[] args)
{
    using (var database = ThrowawayDatabase.Create(username: "postgres", password: "postgres", host: "localhost"))
    {
        using (var connection = new NpgsqlConnection(database.ConnectionString))
        {
            connection.Open();
            using (var cmd = new NpgsqlCommand("SELECT 1", connection))
            {
                var result = Convert.ToInt32(cmd.ExecuteScalar());
                Console.WriteLine(result);
            }
        }
    }
}
You can’t perform that action at this time.