Skip to content

Testing

Tom Laird-McConnell edited this page May 24, 2026 · 1 revision

Testing

LottaDB is designed to run against real Azure Table Storage in production but swap in lightweight providers for tests. The Use*() extension methods make it easy to switch providers with a single line.

Storage providers

Provider Method NuGet Package
Memory UseMemory() LottaDB.Memory
FileSystem UseFileSystem(rootPath) LottaDB.FileSystem
SQLite UseSQLite(rootPath) LottaDB.SQLite
Azure UseAzure(connectionString) LottaDB (core)

Each provider is a drop-in replacement. Pick one based on your needs:

  • Memory -- fastest, no disk I/O, data lost on process exit. Good for unit tests.
  • FileSystem -- data persisted as files on disk. Good for debugging test failures.
  • SQLite -- everything in a SQLite database file. Good balance of speed and persistence.
  • Azurite -- real Azure Table Storage emulator. Good for integration tests.

Configuring test providers

Use the Use*() extension methods to wire up a provider in one call:

// Memory provider -- fastest for unit tests
var catalog = new LottaCatalog("tests", catalog => catalog.UseMemory());

// SQLite provider -- persistent, good for debugging
var catalog = new LottaCatalog("tests", catalog => catalog.UseSQLite(Path.GetTempPath()));

// FileSystem provider -- inspect data as files on disk
var catalog = new LottaCatalog("tests", catalog => catalog.UseFileSystem(Path.GetTempPath()));

Test base class pattern

Create a base class that sets up a catalog per test class and a database per test method:

public abstract class LottaTestBase : IDisposable
{
    private readonly LottaCatalog _catalog;

    protected LottaTestBase()
    {
        var name = string.Join("", GetType().Name.Where(char.IsLetterOrDigit).Take(60));
        _catalog = new LottaCatalog(name, catalog => catalog.UseMemory());
    }

    protected LottaCatalog Catalog => _catalog;

    protected async Task<LottaDB> CreateDbAsync(
        Action<ILottaConfiguration>? configure = null,
        [CallerMemberName] string? testName = null)
    {
        var databaseId = string.Join("", testName!.Where(char.IsLetterOrDigit).Take(60));
        var db = await _catalog.GetDatabaseAsync(databaseId, config =>
        {
            config.Store<MyType>();
            configure?.Invoke(config);
        });
        await db.ResetDatabaseAsync();
        return db;
    }

    public void Dispose() => _catalog.Dispose();
}

Each test method gets an isolated database within the shared catalog:

public class ActorTests : LottaTestBase
{
    [Fact]
    public async Task SaveAndSearch()
    {
        var db = await CreateDbAsync();
        await db.SaveAsync(new Actor { Username = "alice", DisplayName = "Alice" });

        var results = db.Search<Actor>().Where(a => a.Username == "alice").ToList();
        Assert.Single(results);
    }

    [Fact]
    public async Task ChangeAsync_Increments()
    {
        var db = await CreateDbAsync();
        await db.SaveAsync(new Actor { Username = "bob", FollowerCount = 0 });

        await db.ChangeAsync<Actor>("bob", a => a.FollowerCount++);

        var actor = await db.GetAsync<Actor>("bob");
        Assert.Equal(1, actor!.FollowerCount);
    }
}

Switching providers

To swap providers, just change the Use*() call. For example, to switch from in-memory to SQLite for debugging a test failure:

// Change this one line to switch storage:
_catalog = new LottaCatalog(name, catalog => catalog.UseMemory());
// _catalog = new LottaCatalog(name, catalog => catalog.UseSQLite(Path.GetTempPath()));
// _catalog = new LottaCatalog(name, catalog => catalog.UseFileSystem(Path.GetTempPath()));

See Configuration for full details on each provider.

Clone this wiki locally