choktest

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package choktest is what an application's tests need from chok: a throwaway database of the lane's dialect, a store on it, an App served in process, and a JSON client for that App.

func TestPosts(t *testing.T) {
	c := choktest.App(t, chok.Base{}, func(app *chok.App) {
		d := choktest.DB(t, db.Table(&Post{}))
		chok.List(app.Router(), "/posts", store.New[Post](d))
	})
	var page chok.Page[Post]
	if code := c.GetJSON("/posts", &page); code != http.StatusOK {
		t.Fatal(code)
	}
}

The database follows CHOK_TEST_DRIVER: SQLite in a temporary file by default, or a database of its own on the server CHOK_TEST_PG_DSN or CHOK_TEST_MYSQL_DSN names, dropped when the test ends.

A test binary that imports choktest hashes identity's passwords at bcrypt's minimum cost, so tests that register users stay fast; production binaries never import it.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DB added in v0.2.0

func DB(t testing.TB, specs ...db.TableSpec) *db.DB

DB opens a fresh database with the given tables, migrated, and closes and drops it when the test ends. It follows CHOK_TEST_DRIVER (sqlite, postgres or mysql); a server lane without its DSN fails the test.

func Principal added in v0.2.0

func Principal(ctx context.Context, p chok.Principal) context.Context

Principal returns ctx carrying p, for calling stores directly as an authenticated user would.

Example

A store test acts as a user through Principal.

package main

import (
	"context"
	"testing"

	"github.com/zynthara/chok"
	"github.com/zynthara/chok/choktest"
	"github.com/zynthara/chok/db"
)

type Post struct {
	db.OwnedModel
	Title string `json:"title" store:"query,update" gorm:"size:200;not null"`
}

func main() {
	_ = func(t *testing.T) {
		posts := choktest.Store[Post](t)
		alice := choktest.Principal(context.Background(), chok.Principal{UserID: "alice"})
		if err := posts.Create(alice, &Post{Title: "hello"}); err != nil {
			t.Fatal(err)
		}
	}
}

func Store added in v0.2.0

func Store[T db.Modeler](t testing.TB, opts ...store.Option) *store.Store[T]

Store returns the store of T on a fresh database holding T's table, as store.New would with opts. Declare Unique and SoftUnique indexes with DB and store.New instead.

Types

type Client added in v0.2.0

type Client struct {
	// contains filtered or unexported fields
}

Client sends requests to an App in process. A request that cannot be encoded, or a response that does not decode into the value asked for, fails the test.

func App added in v0.2.0

func App(t testing.TB, base chok.Base, setup func(app *chok.App)) *Client

App builds an App from base — a zero Base means every default — runs setup on it to register components and routes, and runs its start phase: Starters start and every Healther must report healthy. The App serves in process through the returned Client and never listens on a port. When the test ends the stop phase runs, without the drain delay, and a failure to stop fails the test. The App logs warnings and errors to the test's output.

Example

An application's test: a database of the lane's dialect, the routes served in process, a JSON client.

package main

import (
	"net/http"
	"testing"

	"github.com/zynthara/chok"
	"github.com/zynthara/chok/choktest"
	"github.com/zynthara/chok/db"
	"github.com/zynthara/chok/store"
)

type Post struct {
	db.OwnedModel
	Title string `json:"title" store:"query,update" gorm:"size:200;not null"`
}

func main() {
	_ = func(t *testing.T) {
		posts := store.New[Post](choktest.DB(t, db.Table(&Post{})))
		c := choktest.App(t, chok.Base{}, func(app *chok.App) {
			chok.List(app.Router(), "/posts", posts)
		})
		var page chok.Page[Post]
		if code := c.GetJSON("/posts", &page); code != http.StatusOK {
			t.Fatalf("GET /posts = %d", code)
		}
	}
}

func (*Client) Do added in v0.2.0

func (c *Client) Do(req *http.Request) *http.Response

Do serves req and returns the response, with its body read to the end and replayable. A relative URL is served as is.

func (*Client) GetJSON added in v0.2.0

func (c *Client) GetJSON(path string, out any, headers ...string) int

GetJSON is JSON(http.MethodGet, path, nil, out, headers...).

func (*Client) JSON added in v0.2.0

func (c *Client) JSON(method, path string, in, out any, headers ...string) int

JSON sends in as a JSON body (none when nil), decodes the response into out whatever its status — an error envelope too — unless out is nil or the body empty, and returns the status code. headers are key-value pairs: "X-Request-Id", "abc".

func (*Client) PostJSON added in v0.2.0

func (c *Client) PostJSON(path string, in, out any, headers ...string) int

PostJSON is JSON(http.MethodPost, path, in, out, headers...).

func (*Client) WithBearer added in v0.2.0

func (c *Client) WithBearer(token string) *Client

WithBearer returns a client that sends Authorization: Bearer token with every request, unless the request sets its own.

Jump to

Keyboard shortcuts

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