testutil

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthedEchoCtx added in v1.6.0

func AuthedEchoCtx(method, target, body string) (echo.Context, *httptest.ResponseRecorder)

AuthedEchoCtx creates an authenticated Echo context for use in HTTP handler tests. It is a convenience wrapper around EchoCtx that injects a "user_id" of "actor_1".

Parameters:

  • method: HTTP method (e.g., http.MethodGet, http.MethodPost)
  • target: request URL or path (e.g., "/api/users")
  • body: JSON request body as a string; pass "" for requests with no body

Returns the Echo context (with user_id set to "actor_1") and a response recorder.

func AuthedEchoCtxWithPolicy added in v1.6.0

func AuthedEchoCtxWithPolicy(method, target, body string, policy any) (echo.Context, *httptest.ResponseRecorder)

AuthedEchoCtxWithPolicy creates an authenticated Echo context with a field-level RBAC policy attached, for use in HTTP handler tests that require both authentication and authorization. It is a convenience wrapper around EchoCtx that injects a "user_id" of "actor_1" and grants access to all fields via rbac.AllFields().

Parameters:

  • method: HTTP method (e.g., http.MethodGet, http.MethodPost)
  • target: request URL or path (e.g., "/api/users")
  • body: JSON request body as a string; pass "" for requests with no body

Returns the Echo context (with user_id and rbac_field_policy set) and a response recorder.

func EchoCtx added in v1.6.0

func EchoCtx(method, target, body string, opts ...CtxOption) (echo.Context, *httptest.ResponseRecorder)

EchoCtx creates a new Echo context and response recorder for use in HTTP handler tests. It constructs an HTTP request with the given method and target URL. If body is non-empty, the request body is set to the provided JSON string and the Content-Type header is set to application/json. If body is empty, the request is created with no body.

Optional CtxOption functions can be passed to mutate the context after construction, e.g. to inject authentication or authorization state.

Parameters:

  • method: HTTP method (e.g., http.MethodGet, http.MethodPost)
  • target: request URL or path (e.g., "/api/users")
  • body: JSON request body as a string; pass "" for requests with no body
  • opts: zero or more CtxOption functions applied to the context in order

Returns the Echo context and a response recorder that captures the handler's response.

func NewMockDB

func NewMockDB(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock)

NewMockDB creates a sqlmock-backed *sqlx.DB using the "sqlmock" driver name. This is suitable for generic unit tests that only need to verify query structure, argument binding, and row scanning — without any PostgreSQL-specific behavior.

Because sqlx treats the "sqlmock" driver as an unknown dialect, it uses the QUESTION bind type (?) for placeholder rebinding. Use this when:

  • Your queries use raw SQL with no placeholder rebinding via sqlx.Rebind
  • You want a lightweight mock that is not coupled to any real database dialect
  • You are testing non-Postgres code paths (e.g. generic repositories)

Example:

func TestGetUser(t *testing.T) {
    db, mock := testutil.NewMockDB(t)

    rows := sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "Alice")
    mock.ExpectQuery(`SELECT \* FROM users WHERE id = ?`).
        WithArgs(1).
        WillReturnRows(rows)

    user, err := repo.GetUser(db, 1)
    require.NoError(t, err)
    require.Equal(t, "Alice", user.Name)
}

func NewMockDBWithMatcher added in v1.5.0

func NewMockDBWithMatcher(t *testing.T, table string) (*sqlx.DB, sqlmock.Sqlmock)

NewMockDBWithMatcher creates a sqlmock database with a custom query matcher for use in unit tests that exercise code paths involving *sqlx.DB (projectors, pg repositories). The underlying *sql.DB is closed automatically via t.Cleanup.

func NewMockPostgresDB added in v1.5.0

func NewMockPostgresDB(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock)

NewMockPostgresDB creates a sqlmock-backed *sqlx.DB using the "postgres" driver name. This is the correct choice when testing code that targets PostgreSQL specifically, because sqlx will apply the DOLLAR bind type ($1, $2, ...) to match Postgres placeholder syntax. Use this when:

  • Your queries or repository layer use $1-style placeholders
  • You call sqlx.Rebind or rely on sqlx dialect-aware helpers (e.g. In, NamedQuery)
  • You are testing pg-specific repositories, projectors, or query builders

Example:

func TestCreateOrder(t *testing.T) {
    db, mock := testutil.NewMockPostgresDB(t)

    mock.ExpectExec(`INSERT INTO orders \(user_id, total\) VALUES \(\$1, \$2\)`).
        WithArgs(42, 99.99).
        WillReturnResult(sqlmock.NewResult(1, 1))

    err := repo.CreateOrder(db, 42, 99.99)
    require.NoError(t, err)
}

func SetupTestDB

func SetupTestDB(t *testing.T) *sqlx.DB

func SetupTestValkey

func SetupTestValkey(t *testing.T) valkey.Client

Types

type CtxOption added in v1.6.0

type CtxOption func(echo.Context)

CtxOption is a functional option that mutates an Echo context after construction.

func WithFieldPolicy added in v1.6.0

func WithFieldPolicy(policy any) CtxOption

WithFieldPolicy injects any value as the RBAC field policy. The caller supplies the value, so testutil stays import-free.

func WithUserID added in v1.6.0

func WithUserID(id string) CtxOption

WithUserID injects the given user ID into the Echo context under the "user_id" key.

Jump to

Keyboard shortcuts

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