Documentation
¶
Index ¶
- func AssertDatabaseCount(t testing.TB, db *lucid.DB, table string, expected int64, ...)
- func AssertDatabaseHas(t testing.TB, db *lucid.DB, table string, conditions map[string]any)
- func AssertDatabaseMissing(t testing.TB, db *lucid.DB, table string, conditions map[string]any)
- func AssertModelExists(t testing.TB, db *lucid.DB, model any)
- func AssertModelMissing(t testing.TB, db *lucid.DB, model any)
- func AssertNotSoftDeleted(t testing.TB, db *lucid.DB, table string, conditions map[string]any)
- func AssertSoftDeleted(t testing.TB, db *lucid.DB, table string, conditions map[string]any)
- func AssertStatus(w *httptest.ResponseRecorder, want int) bool
- func RefreshDatabase(t testing.TB, db *lucid.DB) *lucid.DB
- func SeedDatabase(db *lucid.DB, seeder func(db *lucid.DB) error) error
- func TruncateTables(db *lucid.DB, tables ...string) error
- type DispatchedJob
- type FakeMailer
- type FakeQueue
- type TestClient
- func (c *TestClient) Delete(path string) *TestResponse
- func (c *TestClient) Do(req *http.Request) *TestResponse
- func (c *TestClient) Get(path string) *TestResponse
- func (c *TestClient) Patch(path string, body []byte) *TestResponse
- func (c *TestClient) Post(path string, body []byte) *TestResponse
- func (c *TestClient) PostForm(path string, data url.Values) *TestResponse
- func (c *TestClient) PostJSON(path string, v any) *TestResponse
- func (c *TestClient) Put(path string, body []byte) *TestResponse
- func (c *TestClient) PutJSON(path string, v any) *TestResponse
- func (c *TestClient) WithBearerToken(token string) *TestClient
- func (c *TestClient) WithCookie(cookie *http.Cookie) *TestClient
- func (c *TestClient) WithHeader(key, value string) *TestClient
- type TestResponse
- func (r *TestResponse) AssertContains(t testing.TB, substr string) *TestResponse
- func (r *TestResponse) AssertCreated(t testing.TB) *TestResponse
- func (r *TestResponse) AssertForbidden(t testing.TB) *TestResponse
- func (r *TestResponse) AssertHeader(t testing.TB, key, want string) *TestResponse
- func (r *TestResponse) AssertJSON(t testing.TB, v any) *TestResponse
- func (r *TestResponse) AssertJSONPath(t testing.TB, key string, want any) *TestResponse
- func (r *TestResponse) AssertNoContent(t testing.TB) *TestResponse
- func (r *TestResponse) AssertNotFound(t testing.TB) *TestResponse
- func (r *TestResponse) AssertOK(t testing.TB) *TestResponse
- func (r *TestResponse) AssertRedirect(t testing.TB, location string) *TestResponse
- func (r *TestResponse) AssertStatus(t testing.TB, want int) *TestResponse
- func (r *TestResponse) AssertUnauthorized(t testing.TB) *TestResponse
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 ¶
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 ¶
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 ¶
AssertModelExists asserts that the given GORM model exists in the database.
func AssertModelMissing ¶
AssertModelMissing asserts that the given GORM model does NOT exist.
func AssertNotSoftDeleted ¶
AssertNotSoftDeleted asserts that a record exists and is NOT soft-deleted.
func AssertSoftDeleted ¶
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 ¶
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 ¶
SeedDatabase runs a seeder function within test scope. The seeder receives a *lucid.DB (which could be a transaction).
Types ¶
type DispatchedJob ¶
DispatchedJob records a job that was dispatched.
type FakeMailer ¶
FakeMailer captures sent emails for testing assertions. Implements mail.Driver.
func (*FakeMailer) HasSentTo ¶
func (f *FakeMailer) HasSentTo(addr string) bool
HasSentTo returns true if any mail was sent to the given address.
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.
type FakeQueue ¶
type FakeQueue struct {
Jobs []DispatchedJob
// contains filtered or unexported fields
}
FakeQueue captures dispatched jobs instead of processing them.
func (*FakeQueue) Dispatched ¶
func (f *FakeQueue) Dispatched() []DispatchedJob
Dispatched returns all captured jobs.
func (*FakeQueue) DispatchedCount ¶
DispatchedCount returns how many jobs were dispatched.
func (*FakeQueue) ProcessAll ¶
ProcessAll runs all captured jobs synchronously (useful for testing side effects).
func (*FakeQueue) PushToQueue ¶
PushToQueue captures the job with the specified queue name.
type TestClient ¶
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.