storage

package
v0.0.0-...-2fda283 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateOrUpdate

func CreateOrUpdate[T any](ctx context.Context, storage Storage, id string, value T) error

func Get

func Get[T any](ctx context.Context, storage Storage, id string) (T, error)

func GetOrDefault

func GetOrDefault[T any](ctx context.Context, storage Storage, id string, defaultValue T) (T, error)

func GetTypeName

func GetTypeName[T any]() string

Types

type Storage

type Storage interface {
	Create(ctx context.Context, id, typeName string, value any) error
	Read(ctx context.Context, id, typeName string) (any, error)
	Update(ctx context.Context, id, typeName string, value any) error
	Exists(ctx context.Context, id, typeName string) bool
}

type StorageMock

type StorageMock struct {
	// CreateFunc mocks the Create method.
	CreateFunc func(ctx context.Context, id string, typeName string, value any) error

	// ExistsFunc mocks the Exists method.
	ExistsFunc func(ctx context.Context, id string, typeName string) bool

	// ReadFunc mocks the Read method.
	ReadFunc func(ctx context.Context, id string, typeName string) (any, error)

	// UpdateFunc mocks the Update method.
	UpdateFunc func(ctx context.Context, id string, typeName string, value any) error
	// contains filtered or unexported fields
}

StorageMock is a mock implementation of Storage.

func TestSomethingThatUsesStorage(t *testing.T) {

	// make and configure a mocked Storage
	mockedStorage := &StorageMock{
		CreateFunc: func(ctx context.Context, id string, typeName string, value any) error {
			panic("mock out the Create method")
		},
		ExistsFunc: func(ctx context.Context, id string, typeName string) bool {
			panic("mock out the Exists method")
		},
		ReadFunc: func(ctx context.Context, id string, typeName string) (any, error) {
			panic("mock out the Read method")
		},
		UpdateFunc: func(ctx context.Context, id string, typeName string, value any) error {
			panic("mock out the Update method")
		},
	}

	// use mockedStorage in code that requires Storage
	// and then make assertions.

}

func (*StorageMock) Create

func (mock *StorageMock) Create(ctx context.Context, id string, typeName string, value any) error

Create calls CreateFunc.

func (*StorageMock) CreateCalls

func (mock *StorageMock) CreateCalls() []struct {
	Ctx      context.Context
	ID       string
	TypeName string
	Value    any
}

CreateCalls gets all the calls that were made to Create. Check the length with:

len(mockedStorage.CreateCalls())

func (*StorageMock) Exists

func (mock *StorageMock) Exists(ctx context.Context, id string, typeName string) bool

Exists calls ExistsFunc.

func (*StorageMock) ExistsCalls

func (mock *StorageMock) ExistsCalls() []struct {
	Ctx      context.Context
	ID       string
	TypeName string
}

ExistsCalls gets all the calls that were made to Exists. Check the length with:

len(mockedStorage.ExistsCalls())

func (*StorageMock) Read

func (mock *StorageMock) Read(ctx context.Context, id string, typeName string) (any, error)

Read calls ReadFunc.

func (*StorageMock) ReadCalls

func (mock *StorageMock) ReadCalls() []struct {
	Ctx      context.Context
	ID       string
	TypeName string
}

ReadCalls gets all the calls that were made to Read. Check the length with:

len(mockedStorage.ReadCalls())

func (*StorageMock) Update

func (mock *StorageMock) Update(ctx context.Context, id string, typeName string, value any) error

Update calls UpdateFunc.

func (*StorageMock) UpdateCalls

func (mock *StorageMock) UpdateCalls() []struct {
	Ctx      context.Context
	ID       string
	TypeName string
	Value    any
}

UpdateCalls gets all the calls that were made to Update. Check the length with:

len(mockedStorage.UpdateCalls())

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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