testhelper

package
v0.0.0-...-08b4e76 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context(t *testing.T) context.Context

Context returns a context that is cancelled when either:

  • the test is completed OR
  • 10 seconds elapsed

This ensures that no (sub-)test is running longer than 10 seconds.

func Database

func Database(t *testing.T) *pgxpool.Pool

Database provides a working database if a valid database connection is provided with the TEST_DATABASE_DSN environment variable. If not, the test is marked as failed.

func PartialEqual

func PartialEqual[T any](t testing.TB, expected, actual T)

PartialEqual takes a partially-initialized struct its expected (first argument) side, and compares all non-zero values on it or any substructs to the symmetrical values of actual (second argument). In other words, it can take a struct that only has values that the caller cares about, and compares just those, even if actual has other non-zero values.

Example use:

testhelper.PartialEqual(t, dbsqlc.Cluster{
    Environment: sql.NullString{String: string(dbsqlc.ClusterEnvironmentProduction), Valid: true},
    Name:        cluster.Name,
}, cluster)

WARNING: In Go, there's no difference between an explicit zero value versus an implicit one from when a field is just left out of a struct initialization, so there's no way for this helper to compare zero values set on the expected side -- they'll just be silently ignored. So watch out for use of anything like `false`, `nil`, `sql.NullString{}`, etc. as none of them will work. Recommended work around is to compare non-zero values with this assertion, and then assert on values expected to be zero with `require.Zero`. For API resources, also consider making fields pointer primitives like `*bool` or `*int` so that an explicit `ptrutil.Ptr(false)` or `ptrutil.Ptr(0)` can be checked.

This is taken and adapted from: https://brandur.org/fragments/partial-equal

func RandomString

func RandomString(n int) string

RandomString returns a random string of the given length.

Types

This section is empty.

Jump to

Keyboard shortcuts

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