testing

package
v1.3.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertDatabaseCount

func AssertDatabaseCount(t testing.TB, db *lucid.DB, table string, expected int64, conditions map[string]any)

AssertDatabaseCount asserts the table has exactly n records (optionally matching conditions).

nimbustesting.AssertDatabaseCount(t, db, "users", 5, nil)
nimbustesting.AssertDatabaseCount(t, db, "users", 2, map[string]any{"role": "admin"})

func AssertDatabaseHas

func AssertDatabaseHas(t testing.TB, db *lucid.DB, table string, conditions map[string]any)

AssertDatabaseHas asserts that a record matching the given conditions exists in the specified table. Conditions is a map[string]any of column=value pairs.

nimbustesting.AssertDatabaseHas(t, db, "users", map[string]any{"email": "alice@example.com"})

func AssertDatabaseMissing

func AssertDatabaseMissing(t testing.TB, db *lucid.DB, table string, conditions map[string]any)

AssertDatabaseMissing asserts that no record matching the given conditions exists in the specified table.

nimbustesting.AssertDatabaseMissing(t, db, "users", map[string]any{"email": "deleted@example.com"})

func AssertModelExists

func AssertModelExists(t testing.TB, db *lucid.DB, model any)

AssertModelExists asserts that the given GORM model exists in the database.

func AssertModelMissing

func AssertModelMissing(t testing.TB, db *lucid.DB, model any)

AssertModelMissing asserts that the given GORM model does NOT exist.

func AssertNotSoftDeleted

func AssertNotSoftDeleted(t testing.TB, db *lucid.DB, table string, conditions map[string]any)

AssertNotSoftDeleted asserts that a record exists and is NOT soft-deleted.

func AssertSoftDeleted

func AssertSoftDeleted(t testing.TB, db *lucid.DB, table string, conditions map[string]any)

AssertSoftDeleted asserts that a soft-deleted record exists in the table (i.e., deleted_at IS NOT NULL).

func AssertStatus

func AssertStatus(w *httptest.ResponseRecorder, want int) bool

AssertStatus is a legacy helper (use TestResponse assertions instead).

func RefreshDatabase

func RefreshDatabase(t testing.TB, db *lucid.DB) *lucid.DB

RefreshDatabase wraps a test in a database transaction that is rolled back after the test completes. This ensures each test starts with a clean database state without running migrations.

Usage:

func TestCreateUser(t *testing.T) {
    tx := nimbustesting.RefreshDatabase(t, db)
    // use tx instead of db in your test
    tx.Create(&User{Name: "Alice"})
    // transaction is automatically rolled back when test ends
}

func SeedDatabase

func SeedDatabase(db *lucid.DB, seeder func(db *lucid.DB) error) error

SeedDatabase runs a seeder function within test scope. The seeder receives a *lucid.DB (which could be a transaction).

func TruncateTables

func TruncateTables(db *lucid.DB, tables ...string) error

TruncateTables truncates the given table names. Useful for test setup when RefreshDatabase (transaction-per-test) is not suitable.

Types

type DispatchedJob

type DispatchedJob struct {
	Job   queue.Job
	Queue string
}

DispatchedJob records a job that was dispatched.

type FakeMailer

type FakeMailer struct {
	Mails []*mail.Message
	// contains filtered or unexported fields
}

FakeMailer captures sent emails for testing assertions. Implements mail.Driver.

func NewFakeMailer

func NewFakeMailer() *FakeMailer

NewFakeMailer returns a fresh fake mailer.

func (*FakeMailer) HasSentTo

func (f *FakeMailer) HasSentTo(addr string) bool

HasSentTo returns true if any mail was sent to the given address.

func (*FakeMailer) Reset

func (f *FakeMailer) Reset()

Reset clears all captured messages.

func (*FakeMailer) Send

func (f *FakeMailer) Send(m *mail.Message) error

Send captures the message instead of sending it.

func (*FakeMailer) Sent

func (f *FakeMailer) Sent() []*mail.Message

Sent returns all captured messages.

func (*FakeMailer) SentCount

func (f *FakeMailer) SentCount() int

SentCount returns how many emails were sent.

func (*FakeMailer) SentTo

func (f *FakeMailer) SentTo(addr string) []*mail.Message

SentTo returns all messages sent to the given email address.

type FakeQueue

type FakeQueue struct {
	Jobs []DispatchedJob
	// contains filtered or unexported fields
}

FakeQueue captures dispatched jobs instead of processing them.

func NewFakeQueue

func NewFakeQueue() *FakeQueue

NewFakeQueue returns a fresh fake queue.

func (*FakeQueue) Dispatched

func (f *FakeQueue) Dispatched() []DispatchedJob

Dispatched returns all captured jobs.

func (*FakeQueue) DispatchedCount

func (f *FakeQueue) DispatchedCount() int

DispatchedCount returns how many jobs were dispatched.

func (*FakeQueue) ProcessAll

func (f *FakeQueue) ProcessAll(ctx context.Context) []error

ProcessAll runs all captured jobs synchronously (useful for testing side effects).

func (*FakeQueue) Push

func (f *FakeQueue) Push(job queue.Job)

Push captures the job with an empty queue name.

func (*FakeQueue) PushToQueue

func (f *FakeQueue) PushToQueue(queueName string, job queue.Job)

PushToQueue captures the job with the specified queue name.

func (*FakeQueue) Reset

func (f *FakeQueue) Reset()

Reset clears all captured jobs.

type TestClient

type TestClient struct {
	Router *router.Router
	// contains filtered or unexported fields
}

TestClient performs HTTP requests against a Nimbus router.

func NewAppTestClient

func NewAppTestClient(setup func(app *nimbus.App)) (*nimbus.App, *TestClient)

NewAppTestClient creates a minimal Nimbus application with a fresh router and returns both the app and an HTTP TestClient bound to its router.

func NewTestClient

func NewTestClient(r *router.Router) *TestClient

NewTestClient returns a client that sends requests to the given router.

func (*TestClient) Delete

func (c *TestClient) Delete(path string) *TestResponse

Delete performs a DELETE request.

func (*TestClient) Do

func (c *TestClient) Do(req *http.Request) *TestResponse

Do executes a raw *http.Request and returns a TestResponse.

func (*TestClient) Get

func (c *TestClient) Get(path string) *TestResponse

Get performs a GET request and returns a TestResponse.

func (*TestClient) Patch

func (c *TestClient) Patch(path string, body []byte) *TestResponse

Patch performs a PATCH request.

func (*TestClient) Post

func (c *TestClient) Post(path string, body []byte) *TestResponse

Post performs a POST request with an optional JSON body.

func (*TestClient) PostForm

func (c *TestClient) PostForm(path string, data url.Values) *TestResponse

PostForm posts form data.

func (*TestClient) PostJSON

func (c *TestClient) PostJSON(path string, v any) *TestResponse

PostJSON posts a JSON-serializable value.

func (*TestClient) Put

func (c *TestClient) Put(path string, body []byte) *TestResponse

Put performs a PUT request.

func (*TestClient) PutJSON

func (c *TestClient) PutJSON(path string, v any) *TestResponse

PutJSON puts a JSON-serializable value.

func (*TestClient) WithBearerToken

func (c *TestClient) WithBearerToken(token string) *TestClient

WithBearerToken sets the Authorization header.

func (*TestClient) WithCookie

func (c *TestClient) WithCookie(cookie *http.Cookie) *TestClient

WithCookie adds a cookie for all subsequent requests.

func (*TestClient) WithHeader

func (c *TestClient) WithHeader(key, value string) *TestClient

WithHeader sets a header for all subsequent requests.

type TestResponse

type TestResponse struct {
	*httptest.ResponseRecorder
}

TestResponse wraps httptest.ResponseRecorder with assertion helpers.

func (*TestResponse) AssertContains

func (r *TestResponse) AssertContains(t testing.TB, substr string) *TestResponse

AssertContains fails t if body does not contain substr.

func (*TestResponse) AssertCreated

func (r *TestResponse) AssertCreated(t testing.TB) *TestResponse

AssertCreated asserts 201.

func (*TestResponse) AssertForbidden

func (r *TestResponse) AssertForbidden(t testing.TB) *TestResponse

AssertForbidden asserts 403.

func (*TestResponse) AssertHeader

func (r *TestResponse) AssertHeader(t testing.TB, key, want string) *TestResponse

AssertHeader asserts a response header value.

func (*TestResponse) AssertJSON

func (r *TestResponse) AssertJSON(t testing.TB, v any) *TestResponse

AssertJSON decodes the response body as JSON into v.

func (*TestResponse) AssertJSONPath

func (r *TestResponse) AssertJSONPath(t testing.TB, key string, want any) *TestResponse

AssertJSONPath asserts that a JSON object has the given key with the given value.

func (*TestResponse) AssertNoContent

func (r *TestResponse) AssertNoContent(t testing.TB) *TestResponse

AssertNoContent asserts 204.

func (*TestResponse) AssertNotFound

func (r *TestResponse) AssertNotFound(t testing.TB) *TestResponse

AssertNotFound asserts 404.

func (*TestResponse) AssertOK

func (r *TestResponse) AssertOK(t testing.TB) *TestResponse

AssertOK asserts 200.

func (*TestResponse) AssertRedirect

func (r *TestResponse) AssertRedirect(t testing.TB, location string) *TestResponse

AssertRedirect asserts a 3xx response with the given Location header.

func (*TestResponse) AssertStatus

func (r *TestResponse) AssertStatus(t testing.TB, want int) *TestResponse

AssertStatus fails t if the status code does not match.

func (*TestResponse) AssertUnauthorized

func (r *TestResponse) AssertUnauthorized(t testing.TB) *TestResponse

AssertUnauthorized asserts 401.

Directories

Path Synopsis
Package browser is a Dusk-style end-to-end testing harness for Nimbus.
Package browser is a Dusk-style end-to-end testing harness for Nimbus.

Jump to

Keyboard shortcuts

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