tests

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package tests provides common helpers and mocks used in PocketBase application tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockMultipartData

func MockMultipartData(data map[string]string, fileFields ...string) (*bytes.Buffer, *multipart.Writer, error)

MockMultipartData creates a mocked multipart/form-data payload.

Example

data, mp, err := tests.MockMultipartData(
	map[string]string{"title": "new"},
	"file1",
	"file2",
	...
)

func MockRequestLogsData

func MockRequestLogsData(app *TestApp) error

func NewTempDataDir

func NewTempDataDir() (string, error)

NewTempDataDir creates a new temporary directory copy of the test data.

It is the caller's responsibility to call `os.RemoveAll(dir)` when the directory is no longer needed.

Types

type ApiScenario

type ApiScenario struct {
	Name           string
	Method         string
	Url            string
	Body           io.Reader
	RequestHeaders map[string]string
	// expectations
	ExpectedStatus  int
	ExpectedContent []string
	ExpectedEvents  map[string]int
	// test events
	BeforeFunc func(t *testing.T, app *TestApp, e *echo.Echo)
	AfterFunc  func(t *testing.T, app *TestApp, e *echo.Echo)
}

ApiScenario defines a single api request test case/scenario.

func (*ApiScenario) Test

func (scenario *ApiScenario) Test(t *testing.T)

Test executes the test case/scenario.

type TestApp

type TestApp struct {
	*core.BaseApp

	// EventCalls defines a map to inspect which app events
	// (and how many times) were triggered.
	EventCalls map[string]int
	TestMailer *TestMailer
}

TestApp is a wrapper app instance used for testing.

func NewTestApp

func NewTestApp() (*TestApp, error)

NewTestApp creates and initializes a full application instance for testing.

It is the caller's responsibility to call `app.Cleanup()` when the app is no longer needed.

func (*TestApp) Cleanup

func (t *TestApp) Cleanup()

Cleanup resets the test application state and removes the test app's dataDir from the filesystem.

After this call, the app instance shouldn't be used anymore.

func (*TestApp) NewMailClient

func (t *TestApp) NewMailClient() mailer.Mailer

func (*TestApp) ResetEventCalls

func (t *TestApp) ResetEventCalls()

ResetEventCalls resets the EventCalls counter.

type TestMailer

type TestMailer struct {
	TotalSend    int
	LastHtmlBody string
}

TestMailer is a mock `mailer.Mailer` implementation.

func (*TestMailer) Reset

func (m *TestMailer) Reset()

Reset clears any previously test collected data.

func (*TestMailer) Send

func (m *TestMailer) Send(fromEmail mail.Address, toEmail mail.Address, subject string, html string, attachments map[string]io.Reader) error

Send implements `mailer.Mailer` interface.

Jump to

Keyboard shortcuts

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