unittest

package
v1.27.1 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const NonexistentID = int64(math.MaxInt64)

NonexistentID an ID that will never exist

Variables

This section is empty.

Functions

func AssertCount

func AssertCount(t TestingT, bean, expected any) bool

AssertCount assert the count of a bean

func AssertCountByCond

func AssertCountByCond(t TestingT, tableName string, cond builder.Cond, expected int) bool

AssertCountByCond test the count of database entries matching bean

func AssertExistsAndLoadBean

func AssertExistsAndLoadBean[T any](t TestingT, bean T, conditions ...any) T

AssertExistsAndLoadBean assert that a bean exists and load it from the test database

func AssertExistsAndLoadMap

func AssertExistsAndLoadMap(t TestingT, table string, conditions ...any) map[string]string

AssertExistsAndLoadMap assert that a row exists and load it from the test database

func AssertInt64InRange

func AssertInt64InRange(t assert.TestingT, low, high, value int64)

AssertInt64InRange assert value is in range [low, high]

func AssertNotExistsBean

func AssertNotExistsBean(t TestingT, bean any, conditions ...any)

AssertNotExistsBean assert that a bean does not exist in the test database

func CheckConsistencyFor

func CheckConsistencyFor(t TestingT, beansToCheck ...any)

CheckConsistencyFor test that all matching database entries are consistent

func Cond

func Cond(query any, args ...any) any

Cond create a condition with arguments for a test

func CreateTestEngine

func CreateTestEngine(testSQLiteFile string, opts FixturesOptions) error

CreateTestEngine creates a test database and loads the fixture data from fixturesDir

func DumpQueryResult

func DumpQueryResult(t require.TestingT, sqlOrBean any, sqlArgs ...any)

DumpQueryResult dumps the result of a query for debugging purpose

func FixturesFileFullPaths

func FixturesFileFullPaths(dir string, files []string) (map[string]*FixtureItem, error)

func GetBean

func GetBean[T any](t TestingT, bean T, conditions ...any) (ret T)

func GetCount

func GetCount(t TestingT, bean any, conditions ...any) int

GetCount get the count of a bean

func GetCountByCond

func GetCountByCond(t TestingT, tableName string, cond builder.Cond) int64

GetCountByCond get the count of database entries matching bean

func GetXORMEngine

func GetXORMEngine() (x *xorm.Engine)

GetXORMEngine gets the XORM engine

func InitFixtures

func InitFixtures(opts FixturesOptions) (err error)

InitFixtures initialize test fixtures for a test database

func LoadFixtures

func LoadFixtures() error

LoadFixtures load fixtures for a test database

func MainTest

func MainTest(m *testing.M, testOptsArg ...*TestOptions)

MainTest a reusable TestMain(..) function for unit tests that need to use a test database. Creates the test database, and sets necessary settings.

func NewMockWebServer

func NewMockWebServer(t *testing.T, liveServerBaseURL, testDataDir string, liveMode bool, opts ...MockServerOptions) *httptest.Server

NewMockWebServer returns a test HTTP server that records upstream responses on demand and replays them from disk on subsequent runs.

  • liveMode=true: requests are forwarded to liveServerBaseURL and responses written as fixture files under testDataDir.
  • liveMode=false: responses come from existing fixture files.

Fixture format: header lines ("Name: value"), a blank line, then the body. Before replay, occurrences of liveServerBaseURL in the body are swapped for the mock URL.

The typical switch is an env var holding an API token; fixtures ship committed so the default run (no token) works offline.

token := os.Getenv("GITEA_TOKEN")
mock := NewMockWebServer(t, "https://gitea.com", fixtureDir, token != "")

func OrderBy

func OrderBy(orderBy string) any

OrderBy creates "ORDER BY" a test query

func PrepareTestDatabase

func PrepareTestDatabase() error

PrepareTestDatabase load test fixtures into test database

func PrepareTestEnv

func PrepareTestEnv(t testing.TB)

PrepareTestEnv prepares the environment for unit tests. Can only be called by tests that use the above MainTest(..) function.

func ResetTestDatabase

func ResetTestDatabase() (cleanup func(), err error)

func SyncDirs

func SyncDirs(srcPath, destPath string) error

SyncDirs synchronizes files recursively from source to target directory. It returns error when error occurs in underlying functions.

func SyncFile

func SyncFile(srcPath, destPath string) error

SyncFile synchronizes the two files. This is skipped if both files exist and the size, modtime, and mode match.

Types

type FixtureItem

type FixtureItem struct {
	// contains filtered or unexported fields
}

type FixturesLoader

type FixturesLoader interface {
	Load() error
	MarkTableChanged(tableName string)
}

func NewFixturesLoader

func NewFixturesLoader(x *xorm.Engine, opts FixturesOptions) (FixturesLoader, error)

type FixturesOptions

type FixturesOptions struct {
	Dir   string
	Files []string
}

FixturesOptions fixtures needs to be loaded options

type MockServerOptions

type MockServerOptions struct {
	// Routes installs extra handlers on the mux before the fixture fallback;
	// more specific patterns win.
	Routes func(mux *http.ServeMux)
	// StripPrefix is trimmed from the request path before forwarding upstream,
	// useful when the client prepends a prefix the real upstream does not use
	// (e.g. go-github prepends "/api/v3").
	StripPrefix string
}

MockServerOptions tweaks NewMockWebServer behavior.

type TestOptions

type TestOptions struct {
	FixtureFiles []string
	SetUp        func() error // SetUp will be executed before all tests in this package
	TearDown     func() error // TearDown will be executed after all tests in this package
}

TestOptions represents test options

type TestingT

type TestingT interface {
	require.TestingT
	assert.TestingT
	Context() context.Context
}

Jump to

Keyboard shortcuts

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