testingtools

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package testingtools provides test helpers and mock infrastructure for requestCore.

Index

Constants

View Source
const Default = "default"

Default is the constant key for the default test configuration.

Variables

This section is empty.

Functions

func DefaultAPIList

func DefaultAPIList() map[string]libCallApi.RemoteAPI

DefaultAPIList returns a default map of remote API configurations for testing.

func DefaultAccessRoles

func DefaultAccessRoles() map[string]string

DefaultAccessRoles returns a default access-role map for testing.

func DefaultDB

func DefaultDB(t *testing.T) *sql.DB

DefaultDB returns a mock *sql.DB for testing.

func DefaultErrorDesc

func DefaultErrorDesc() map[string]string

DefaultErrorDesc returns a default error-description map for testing.

func GetAllSimulationHandler

func GetAllSimulationHandler[Req any](core requestCore.RequestCoreInterface) gin.HandlerFunc

GetAllSimulationHandler returns a gin handler that parses and responds with a list of simulation requests.

func GetDMLModel

func GetDMLModel(t *testing.T, m Model) libQuery.QueryRunnerModel

GetDMLModel handles INSERT, UPDATE, and DELETE in mock DB models.

func GetEnv

func GetEnv[Env any, PT interface {
	GetInterface() requestCore.RequestCoreInterface
	GetParams() libParams.ParamInterface
	SetInterface(requestCore.RequestCoreInterface)
	SetParams(libParams.ParamInterface)
	*Env
}](t *testing.T, defaultAPIList func() map[string]libCallApi.RemoteAPI) PT

GetEnv creates and initializes a typed test environment with a default mock database.

func GetEnvWithDB

func GetEnvWithDB[Env any, PT interface {
	GetInterface() requestCore.RequestCoreInterface
	GetParams() libParams.ParamInterface
	SetInterface(requestCore.RequestCoreInterface)
	SetParams(libParams.ParamInterface)
	*Env
}](db *sql.DB, defaultAPIList func() map[string]libCallApi.RemoteAPI) PT

GetEnvWithDB creates and initializes a typed test environment with the given database and API list.

func GetRequestModel added in v0.9.47

func GetRequestModel(t *testing.T) libQuery.QueryRunnerModel

GetRequestModel returns a QueryRunnerModel backed by a sqlmock database with predefined query expectations.

func GetSelectModel

func GetSelectModel(t *testing.T, m Model) libQuery.QueryRunnerModel

GetSelectModel handles selecting a single or all items in mock DB model.

func GetSingleSimulationHandler

func GetSingleSimulationHandler[Req any](core requestCore.RequestCoreInterface) gin.HandlerFunc

GetSingleSimulationHandler returns a gin handler that parses and responds with a single simulation request.

func InitSimulationHandler

func InitSimulationHandler[Req any](c context.Context, core requestCore.RequestCoreInterface) (*Req, error)

InitSimulationHandler parses a simulation request from the context and returns the typed request body.

func InitTesting

func InitTesting(t *testing.T,
	errDesc map[string]string,
	remoteApis map[string]libCallApi.RemoteAPI,
	query string,
	columns []string,
	csv string,
	module string,
) (requestCore.RequestCoreModel, libParams.ParamInterface)

InitTesting initializes the test server with the given parameters and returns the request core model and parameter interface.

func InitTestingNoDB

func InitTestingNoDB(t *testing.T,
	errDesc map[string]string,
	remoteApis map[string]libCallApi.RemoteAPI,
) (requestCore.RequestCoreModel, libParams.ParamInterface)

InitTestingNoDB initializes a test request-core model without a custom database, using a default mock DB.

func InitTestingWithDB

func InitTestingWithDB(
	errDesc map[string]string,
	remoteApis map[string]libCallApi.RemoteAPI,
	db *sql.DB,
) (requestCore.RequestCoreModel, libParams.ApplicationParams[TestingParams])

InitTestingWithDB initializes a test request-core model with the given error descriptions, remote APIs, and database.

func PrepareEnvWithDB added in v0.9.47

func PrepareEnvWithDB(env TestingEnv, db *sql.DB)

PrepareEnvWithDB initializes a TestingEnv with default error descriptions, API list, and the given database.

func SampleMockDB added in v0.10.2

func SampleMockDB(t *testing.T, mockList func(sqlmock.Sqlmock)) *sql.DB

SampleMockDB returns a mock *sql.DB with optional custom mock expectations.

func SampleQueryMock added in v0.9.20

func SampleQueryMock(t *testing.T, mockList func(sqlmock.Sqlmock)) libQuery.QueryRunnerModel

SampleQueryMock returns a QueryRunnerModel in mock mode with optional custom mock expectations.

func SampleRequestModelMock

func SampleRequestModelMock(t *testing.T, mockList func(sqlmock.Sqlmock)) libQuery.QueryRunnerModel

SampleRequestModelMock returns a QueryRunnerModel with predefined mock expectations for request handling.

func TestAPI

func TestAPI(t *testing.T, testCases []TestCase, options *TestOptions)

TestAPI tests a group of Api's test.

func TestAPIList added in v0.9.7

func TestAPIList() map[string]libCallApi.RemoteAPI

TestAPIList returns a default map of remote API configurations for API testing.

func TestDB

func TestDB(t *testing.T, tc *TestCase, options *TestOptions)

TestDB tests a single DB models.

func TestDBFiber

func TestDBFiber(t *testing.T, tc *TestCase, options *TestOptions)

TestDBFiber tests a single DB model using the Fiber framework.

func TestDMLs

func TestDMLs[Model any, PT interface {
	libQuery.DmlModel
	*Model
}](t *testing.T)

TestDMLs tests a model for equality of commands arguments, and given argumants, also it checks if given type is the same is the command.

func TestFetchData

func TestFetchData[Model any, PT interface {
	libQuery.QueryRequest
	*Model
}](t *testing.T, queryList map[string]libQuery.QueryCommand)

TestFetchData tests query models for equality of command argument counts against provided arguments.

Types

type AnyString

type AnyString string

AnyString is a sqlmock argument type that matches any driver value.

func (AnyString) Match

func (a AnyString) Match(_ driver.Value) bool

Match satisfies sqlmock.Argument interface

type CustomMockConverter added in v0.9.55

type CustomMockConverter struct{}

CustomMockConverter is a sqlmock value converter that handles common Go types.

func (CustomMockConverter) ConvertValue added in v0.9.55

func (CustomMockConverter) ConvertValue(v interface{}) (driver.Value, error)

ConvertValue converts a Go value into a driver.Value for the mock database.

type Header []KeyValuePair

Header is a slice of KeyValuePair used to set HTTP headers in test requests.

func (Header) Map

func (h Header) Map() map[string][]string

Map converts a Header to a map with dynamic header support

type KeyValuePair

type KeyValuePair struct {
	Key   string
	Value string
}

KeyValuePair holds a single key-value pair used for test headers.

type Model

type Model struct {
	Query   string
	Columns []string
	Values  [][]any
	Args    []any
	Err     error
}

Model holds the query, columns, values, and error for a mock database interaction.

type TestCase

type TestCase struct {
	Name           string
	URL            string
	Header         Header
	Model          libQuery.QueryRunnerModel
	Request        any
	Status         int
	CheckBody      []string
	CheckNotInBody []string
	CheckHeader    map[string]string
	DontReadBody   bool
	MockError      error
	Desired        any
	DesiredError   string
	DesiredResp    string
}

TestCase defines a single test case including URL, headers, expected status, and body checks.

type TestEnv

type TestEnv struct {
	Params    libParams.ParamInterface
	Interface requestCore.RequestCoreInterface
}

TestEnv holds the parameter and request-core interfaces for a test environment.

func (TestEnv) GetInterface

func (env TestEnv) GetInterface() requestCore.RequestCoreInterface

GetInterface returns the request-core interface for the test environment.

func (TestEnv) GetParams

func (env TestEnv) GetParams() libParams.ParamInterface

GetParams returns the parameter interface for the test environment.

func (*TestEnv) SetInterface

func (env *TestEnv) SetInterface(core requestCore.RequestCoreInterface)

SetInterface sets the request-core interface for the test environment.

func (*TestEnv) SetParams

func (env *TestEnv) SetParams(params libParams.ParamInterface)

SetParams sets the parameter interface for the test environment.

type TestOptions

type TestOptions struct {
	Path            string
	Name            string
	Method          string
	Handler         any
	Middleware      gin.HandlerFunc
	MiddlewareFiber fiber.Handler
	Silent          bool
}

TestOptions configures the test server including path, method, handler, and middleware.

type TestingEnv added in v0.9.47

type TestingEnv interface {
	GetInterface() requestCore.RequestCoreInterface
	GetParams() libParams.ParamInterface
	SetInterface(requestCore.RequestCoreInterface)
	SetParams(libParams.ParamInterface)
}

TestingEnv defines the interface for a test environment that holds request-core and parameter interfaces.

type TestingParams added in v0.16.0

type TestingParams struct {
}

TestingParams is the parameter type used for test application configuration.

type TestingWsParams

type TestingWsParams struct {
	RemoteAPIs  map[string]libCallApi.RemoteAPI `yaml:"remoteApis"`
	ErrorDesc   map[string]string               `yaml:"errorDesc"`
	MessageDesc map[string]string               `yaml:"messageDesc"`
	AccessRoles map[string]string               `yaml:"accessRoles"`
}

TestingWsParams holds remote API, error description, message description, and access role maps for testing.

func (*TestingWsParams) GetFonts

func (p *TestingWsParams) GetFonts() map[string]opentype.Font

GetFonts returns the font map for the test environment (always nil in tests).

func (*TestingWsParams) GetHeaderName

func (p *TestingWsParams) GetHeaderName() string

GetHeaderName returns the header name used for request identification in tests.

func (*TestingWsParams) GetImages

func (p *TestingWsParams) GetImages() map[string]image.Image

GetImages returns the image map for the test environment (always nil in tests).

func (*TestingWsParams) GetLogCompress

func (p *TestingWsParams) GetLogCompress() bool

GetLogCompress returns whether log files should be compressed in the test environment.

func (*TestingWsParams) GetLogPath

func (p *TestingWsParams) GetLogPath() string

GetLogPath returns the log file path for the test environment.

func (*TestingWsParams) GetLogSize

func (p *TestingWsParams) GetLogSize() int

GetLogSize returns the log rotation size in megabytes for the test environment.

func (*TestingWsParams) GetParams

func (p *TestingWsParams) GetParams() map[string]string

GetParams returns the parameter map for the test environment (always nil in tests).

func (*TestingWsParams) GetRoles

func (p *TestingWsParams) GetRoles() map[string]string

GetRoles returns the access role map for the test environment (always nil in tests).

func (*TestingWsParams) GetSkipPaths

func (p *TestingWsParams) GetSkipPaths() []string

GetSkipPaths returns the paths to skip in access logging for the test environment.

Jump to

Keyboard shortcuts

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