genericrepotests

package module
v0.0.0-...-c12b53d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 5 Imported by: 0

README

This is WORK IN PROGRESS repository. Be careful to use it in production code. I think better way is to copy/paste this code to your project.

Generic tests

Test your storage/repository layer with generic tests similar to quick check with "one line".

How it works

This tests use (Property testing)[https://en.wikipedia.org/wiki/Property_testing] approach similar to quick check tests.

Usually repository have several generic properties likes this:

  • After create record you could Load record with exact fields of reate
  • After update you could Load record with next fields

So this kind of tests is here.

How to add

  • We need adapter of your repository interface to generic repository of this package.
  • Also your Model/Aggregate should have property ID, because Load method needs it.

So in your test file:

// ...
import genericrepotests "github.com/adzeitor/golang_generic_repo_tests"

type RepoAdapter struct {
	Repo MyRepo
}

func (r *RepoAdapter) Create(ctx context.Context, t *Model) error {
	return r.Repo.Save(ctx, t)
}

func (r *RepoAdapter) Update(ctx context.Context, t *Model) error {
	return r.Repo.Save(ctx, t)
}

func (r *RepoAdapter) Load(ctx context.Context, id uuid.UUID) (Model, error){
	return r.Repo.GetByID(ctx, id)
}


func TestGeneric(t *testing.T) {
	myRepo := MyRepo.New()
	genericrepotests.GenericTest(RepoAdapter{Repo: myRepo}, t)
}

In case of matched interface (rare case)

// ...
import "github.com/adzeitor/golang_generic_repo_tests"

/...

func TestGeneric(t *testing.T) {
        // this repo should have Create/Update/Load methods
	myRepo := MyRepo.New()
	GenericTest(myRepo, t)
}

Copy/Paste

Another way just copy/paste this code to your repo and change method names to your convention.

How to change testdata config?

import "github.com/kyuff/testdata"

// ...

cfg := testdata.NewConfig(
	testdata.WithGenerator(func(rand *rand.Rand) []HistoryItem {
		// for example history should have
		// more than one element
		historySize := rand.IntN(10) + 1
		history := []HistoryItem{}
		for i := 0; i < historySize; i++ {
			history = append(history, testdata.Make[HistoryItem](t))
		}
		return history
	}),
)
GenericTestWithConfig(repo, cfg, t)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenericTest

func GenericTest[T any, ID any](repo Repo[T, ID], t *testing.T)

func GenericTestWithConfig

func GenericTestWithConfig[T any, ID any](
	repo Repo[T, ID],
	newCfg Config[T],
	t *testing.T,
)

Types

type Config

type Config[T any] struct {
	Compare  func(t *testing.T, expected T, got T)
	MakeData func(t *testing.T) T
}

func DefaultConfig

func DefaultConfig[T any]() Config[T]

type Repo

type Repo[T any, ID any] interface {
	Create(context.Context, *T) error
	Update(context.Context, *T) error
	Load(context.Context, ID) (*T, error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL