testutil

package
v1.14.4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	YT        = "3V8rpJDpbKg"                          // YTID is an example YouTube video ID
	UID       = "123e4567-e89b-12d3-a456-426614174000" // UUID is a generic Universal Unique ID
	UID4      = "bb2310e1-93aa-475e-8b88-59eb1fb984a4" // UID4 is a UUID version 4
	SCREENPNG = 328_468                                // SCREENPNG is the byte file size of testdata/SCREEN.PNG
	// RTF is an example of Rich-Text-Format encoded text.
	RTF = `{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
 This is some {\b bold} text.\par
}`
)

Variables

View Source
var Content1 = [...]string{
	"file1.nfo",
	"file1.txt",
	"file1.unp",
	"file1.doc",
}
View Source
var Content2 = [...]string{
	"file.diz",
	"file.asc",
	"file.1st",
	"group2.dox",
}
View Source
var Content3 = [...]string{
	"file3.nfo",
	"file.txt",
	"file30.unp",
	"file3x.doc",
	"filex3.diz",
	"file3.asc",
	"file3.1st",
	"file3.dox",
}

Functions

func CopyPNG

func CopyPNG(tb testing.TB, dest string)

CopyPNG copies a sample PNG image to the target destination.

func CopyTXT

func CopyTXT(tb testing.TB, dest string)

CopyTXT copies a sample textfile to the target destination.

func Count

func Count() int

Count returns the number of TestData entries.

func CountTest

func CountTest() int

CountTest returns the number of TestData entries that have a filename prefix of "TEST." (test plus a dot).

func DB

func DB(tb testing.TB) *sql.DB

DB returns a shared Postgres database connection for use with tests. If no connection can be made, the test is skipped.

func DBConns

func DBConns(tb testing.TB)

DBConns logs the active and maximum Postgres database connections.

func DOSBIN

func DOSBIN(tb testing.TB, s string) [4000]byte

DOSBIN converts the string into a single page, MS-DOS BIN binary text. If s is left empty, "hello world" will be used. Long strings will be cropped to fit the 80 column * 25 row per page limit.

func EmbedFS

func EmbedFS(tb testing.TB) fs.FS

EmbedFS returns the embed file system of the testdata subdirectory.

func Middleware

func Middleware(tb testing.TB, e *echo.Echo, target string) *echo.Context

Middleware returns an echo response using the target url. If no target is provided, it is set to root "/".

The echo.Echo can be provided using echo.New.

func MiddlewareStatus

func MiddlewareStatus(tb testing.TB, e *echo.Echo, target string, status int) *echo.Context

MiddlewareStatus returns an echo response using http request using the target url and http status.

The echo.Echo can be provided using echo.New.

func MkDOSBIN

func MkDOSBIN(tb testing.TB, name, s string) (string, error)

MkDOSBIN converts the string into a single line, MS-DOS BIN file saved to the named file and placed in a temporary test directory. The file location is returned.

See DOSBIN.

func NewContext

func NewContext(tb testing.TB, target string) *echo.Context

NewContext creates a new instance of Echo and returns a response using the target url. If no target is provided, it is set to root "/". Query params can also be provided using target, for example:

target="/myurl?id=1&name=hello"

func NewFile

func NewFile(t *testing.T, target, fieldname, filename string) *echo.Context

NewFile creates a new Echo instance with an embedded textfile upload.

  • fieldname is the named input of the form containing the upload.
  • filename is the original filename of the file upload.
  • The upload contains the following string, "Hello world!".

func NewFileInputs

func NewFileInputs(t *testing.T, target, fieldname, filename string, formInputs Input,
) *echo.Context

NewFileInputs functions the same as NewFile but also allows for additional form inputs using Input, for example:

Input["id"] = 1
Input["uuid"] = 123e4567-e89b-12d3-a456-426614174000

func NewInput

func NewInput(tb testing.TB, target, key, value string) *echo.Context

NewInput creates a new instance of Echo and returns a response using the target url. It sets the key and value that are mapped as both query parameters and form values. If no target is provided, it is set to root "/".

func NewInputs

func NewInputs(t *testing.T, target string, formInputs Input) *echo.Context

NewInputs creates a new Echo instance and returns a response using the target url. It accepts an Input map using keys and values that are treated as both query parameters and form values.

func NewInputsPath

func NewInputsPath(t *testing.T, target string, formInputs Input, pathValues echo.PathValues) *echo.Context

NewInputsPath functions as both NewInputs and NewPath.

func NewModel

func NewModel(tb testing.TB) *models.File

NewModel returns a new models.File entry with the following boilerplate:

  • ID: 1
  • UUID: bb2310e1-93aa-475e-8b88-59eb1fb984a4
  • Filename: filename.txt
  • Filesize: 9876

func NewPath

func NewPath(t *testing.T, target string, pathValues echo.PathValues) *echo.Context

NewPath creates a new Echo instance with a collection of echo.PathValues, that allow the provision path parameters using Name and Value pairs.

func OpenFS

func OpenFS(tb testing.TB) fs.FS

OpenFS opens the root directory of the testutil package and closes on cleanup.

func OpenRoot

func OpenRoot(tb testing.TB) *os.Root

OpenRoot opens the root directory of the testutil package and closes on cleanup.

func Project

func Project() string

Project returns the absolute representation to the root directory for this project repository.

func ProjectFS

func ProjectFS(tb testing.TB) fs.FS

ProjectFS opens the root directory of this Go project and closes on cleanup.

func Testdata

func Testdata() string

Testdata returns the absolute representation to the "testdata/" subdirectory found in this testutil package.

func Tx

func Tx(tb testing.TB) *sql.Tx

Tx returns a shared Postgres database connection for use with tests. The transaction with any database edits get rolled back during the test cleanup. If no connection can be made, the test is skipped.

Note: Only one Tx should be used per-test otherwise the test may never resolve.

Types

type ANSITest

type ANSITest struct {
	Name string
	Data []byte
}

func ANSITests

func ANSITests(tb testing.TB) []ANSITest

ANSITests generates a set of testing data containing randomized data embedded with ANSI escape codes. Being intended for use in tests requiring detection and removal.

type Input

type Input map[string]string

type Logger

type Logger struct {
	Log *slog.Logger
	// contains filtered or unexported fields
}

func Buffer

func Buffer(tb testing.TB) *Logger

Buffer returns a custom debug logger that writes to Logger.

func (*Logger) Contains

func (l *Logger) Contains(substr string) bool

Contains returns true if the substr is found in the content of the logger.

func (*Logger) Reset

func (l *Logger) Reset()

Reset empties the content of the logger.

func (*Logger) String

func (l *Logger) String() string

String returns the content of the logger.

type TestData

type TestData = map[string]Testfile // TestData is a map of the filenames found in the "testdata/" subdirectory of testutil.

func Files

func Files() TestData

Files returns a map of the TestData.

type Testfile

type Testfile string // Testfile is the key used to reference TestData.

func FileData

func FileData(key string) Testfile

FileData returns the filename of the key testdata.

For example:

FileData("logotxt") = "LOGO.TXT"

func (Testfile) Abs

func (tf Testfile) Abs() string

Abs returns an absolute representation of the testfile path.

func (Testfile) Dir

func (tf Testfile) Dir() string

Dir returns the directory path of the testfile.

Jump to

Keyboard shortcuts

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