# README
🦁 Gorm Test Utils
Small utility functions for testing Gorm-related code. Such as sqlite database instantiation and wait groups with callbacks.
⬇️ Installation
go get github.com/ing-bank/gormtestutil
📋 Usage
Database Instantiation
package main
import (
"testing"
"github.com/ing-bank/gormtestutil"
)
func TestProductService_FetchAll_ReturnsAllProducts(t *testing.T) {
// Arrange
db := gormtestutil.NewMemoryDatabase(t,
gormtestutil.WithName(t.Name()),
gormtestutil.WithoutForeignKeys(),
gormtestutil.WithSingularConnection())
// [...]
}
Hooks
package main
import (
"github.com/ing-bank/gormtestutil"
"time"
"testing"
)
func TestProductService_Create_CreatesProduct(t *testing.T) {
// Arrange
db := gormtestutil.NewMemoryDatabase(t)
expectation := gormtestutil.ExpectCreated(t, db, Product{}, gormtestutil.WithCalls(1))
// Act
go Create(db, Product{Name: "Test"})
// Assert
gormtestutil.EnsureCompletion(t, expectation, gormtestutil.WithTimeout(30*time.Second))
}
🚀 Development
- Clone the repository
- Run
make tools
to install necessary tools - Run
make t
to run unit tests - Run
make fmt
to format code - Run
make lint
to lint your code
You can run make
to see a list of useful commands.
🔭 Future Plans
Nothing here yet!
# Functions
EnsureCompletion ensures that the waitgroup completes within a specified duration or else fails.
ExpectCreated asserts that an insert statement has at least been executed on the model.
ExpectDeleted asserts that a delete statement has at least been executed on the model.
ExpectUpdated asserts that an update statement has at least been executed on the model.
NewMemoryDatabase returns a sqlite database that runs in-memory, allowing you to use a database without a running instance.
WithCalls is used to expect an invocation an X amount of times.
WithExpectation allows you to chain wait groups and expectations together.
WithName gives the database a name, allowing you to use NewMemoryDatabase multiple times to connect to the same database instance.
WithoutForeignKeys disabled foreign keys for testing purposes.
WithoutMaximum instructs the expectation to ignore an excess amount of calls.
WithSingularConnection is useful when you're getting 'database table is locked' errors while working with goroutines.
WithTimeout is used to set a timeout for EnsureCompletion.
# Type aliases
EnsureOption allows various options to be supplied to EnsureCompletion.
ExpectOption allows various options to be supplied to Expect* functions.
MemoryDatabaseOption allows various options to be supplied to NewMemoryDatabase.