Testing Stores
Testing Philosophy
Store tests use real in-memory SQLite databases, not mocks. This ensures your code works in real scenarios, catching issues that mocks might miss. The test environment is bootstrapped via internal/testutils.Setup() which calls migrations.MigrateAll(app) to apply all store migrations automatically.
How Interfaces Help Testing
Because stores and entities are accessed via interfaces, you can easily swap implementations or inject test doubles if needed.
Test Setup
Tests use testutils.Setup() with store-specific options to enable only the stores needed for each test. This keeps tests fast and isolated:
func TestExampleStore_CreateAndRetrieve(t *testing.T) {
app := testutils.Setup(
testutils.WithExampleStore(true),
)
store := app.GetExampleStore()
// ... test logic
}
Example Test Code
func TestExampleStore_CreateAndRetrieve(t *testing.T) {
app := testutils.Setup(
testutils.WithExampleStore(true),
)
store := app.GetExampleStore()
example := store.NewExample()
example.SetID("1234")
// ... set fields
err := store.ExampleCreate(t.Context(), example)
require.NoError(t, err)
retrieved, err := store.ExampleGet(t.Context(), "1234")
require.NoError(t, err)
require.Equal(t, "1234", retrieved.GetID())
}