test

package
v0.0.0-...-c05fae0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package test contains utilities used for testing Karman. This package should only be imported by test files packages.

This package contains a subpackage github.com/Karaoke-Manager/karman/test/data which provides an interface for generating test data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIError

func APIError(h http.Handler, method string, path string, status int, problemType string) func(t *testing.T)

APIError returns a test that runs a request against h and asserts that the response describes an error of type problemType.

func AssertPagination

func AssertPagination(t *testing.T, resp *http.Response, offset, limit, count int, total int64)

AssertPagination validates that the response provides the expected pagination values.

func AssertProblemDetails

func AssertProblemDetails(t *testing.T, resp *http.Response, code int, errType string, fields map[string]any)

AssertProblemDetails validates that resp encodes a problem details instance with the specified values. Any fields will be checked for presence in the custom fields of the response. This assertion will NOT fail if additional fields are present in the response.

func AssertValidationError

func AssertValidationError(t *testing.T, resp *http.Response, errors map[string]string)

AssertValidationError validates that resp encodes a problem details instance, indicating a 422 Unprocessable Entity error. It is validated that the error contains the specified error messages. errors maps from expected JSON pointers to their error messages.

func DoRequest

func DoRequest(h http.Handler, r *http.Request) *http.Response

DoRequest executes r against h, records the response and returns it.

func HTTPError

func HTTPError(h http.Handler, method string, path string, status int) func(t *testing.T)

HTTPError is a convenience function for APIError where we do not expect a non-default problem type.

func InvalidContentType

func InvalidContentType(h http.Handler, method string, path string, invalid string, allowed ...any) func(t *testing.T)

InvalidContentType returns a test that runs a request against h with the specified invalid Content-Type header and validates that the response indicates as much. The variadic argument lets you specify the expected allowed content types for this endpoint.

func InvalidPagination

func InvalidPagination(h http.Handler, method string, path string) func(t *testing.T)

InvalidPagination returns a test that runs a request against h with invalid pagination request parameters and asserts an appropriate error response.

func InvalidUUID

func InvalidUUID(h http.Handler, method string, path string) func(t *testing.T)

InvalidUUID returns a test that runs a request against h and asserts that the response indicates an invalid UUID.

func MissingContentType

func MissingContentType(h http.Handler, method string, path string, allowed ...any) func(t *testing.T)

MissingContentType returns a test that runs a request against h without the Content-Type header and validates that the response indicates as much. The variadic argument lets you specify the expected allowed content types for this endpoint.

func MustOpen

func MustOpen(t *testing.T, name string) *os.File

MustOpen opens the named file. The file is closed automatically at the end of the test. If the file cannot be opened the test is aborted.

func NewDB

func NewDB(t *testing.T) pgxutil.DB

NewDB creates a new database connection for a single test. Depending on the environment this method behaves in one of two ways:

If the PGHOST environment variable is set, a database connection to an existing database is created. The connection uses the usual environment variables for postgres connections (PGPORT, PGUSER, etc.). If PGUSER or PGPASSWORD is empty, a default value of "postgres" will be used for either.

If PGHOST is not present in the environment, this functions uses testcontainers to create a testing database. You can control the postgres version using the POSTGRES_VERSION variable or specify a custom image using POSTGRES_IMAGE. Only a single database container is created for multiple tests (for each version/image). The database container will not be terminated automatically, so that it can be reused by multiple tests. You should rely on Reaper/Ryuk to automatically remove these containers.

In both cases this function creates a new database using CREATE DATABASE. NewDB runs all migrations on the database and then returns a connection. When the test is over the database is dropped automatically. The -pg-user must have the appropriate permissions on the database. When using testcontainers this is the case by default.

Types

This section is empty.

Directories

Path Synopsis
Package testdata provides utility functions for filling a database with test data.
Package testdata provides utility functions for filling a database with test data.

Jump to

Keyboard shortcuts

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