ing-bank / gormtestutil

Utilities for writing unit-tests with Gorm

Home Page:https://pkg.go.dev/github.com/ing-bank/gormtestutil

Repository from Github https://github.coming-bank/gormtestutilRepository from Github https://github.coming-bank/gormtestutil

🦁 Gorm Test Utils

Go package GitHub GitHub go.mod Go version

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

  1. Clone the repository
  2. Run make tools to install necessary tools
  3. Run make t to run unit tests
  4. Run make fmt to format code
  5. Run make lint to lint your code

You can run make to see a list of useful commands.

πŸ”­ Future Plans

Nothing here yet!

About

Utilities for writing unit-tests with Gorm

https://pkg.go.dev/github.com/ing-bank/gormtestutil

License:MIT License


Languages

Language:Go 96.7%Language:Makefile 3.3%