Documentation
¶
Overview ¶
Package tests provides common helpers and mocks used in PocketBase application tests.
Index ¶
- func MockMultipartData(data map[string]string, fileFields ...string) (*bytes.Buffer, *multipart.Writer, error)
- func StubLogsData(app *TestApp) error
- func StubMFARecords(app core.App) error
- func StubOTPRecords(app core.App) error
- func TempDirClone(dirToClone string) (string, error)
- func TestValidationErrors(t *testing.T, rawErrors error, expectedErrors []string)
- type ApiScenario
- type TestApp
- type TestMailer
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 StubLogsData ¶ added in v0.23.0
func StubMFARecords ¶ added in v0.23.0
func StubOTPRecords ¶ added in v0.23.0
func TempDirClone ¶ added in v0.7.0
TempDirClone creates a new temporary directory copy from the provided directory path.
It is the caller's responsibility to call `os.RemoveAll(tempDir)` when the directory is no longer needed!
func TestValidationErrors ¶ added in v0.23.0
TestValidationErrors checks whether the provided rawErrors are instance of validation.Errors and contains the expectedErrors keys.
Types ¶
type ApiScenario ¶
type ApiScenario struct { // Name is the test name. Name string // Method is the HTTP method of the test request to use. Method string // URL is the url/path of the endpoint you want to test. URL string // Body specifies the body to send with the request. // // For example: // // strings.NewReader(`{"title":"abc"}`) Body io.Reader // Headers specifies the headers to send with the request (e.g. "Authorization": "abc") Headers map[string]string // Delay adds a delay before checking the expectations usually // to ensure that all fired non-awaited go routines have finished Delay time.Duration // Timeout specifies how long to wait before cancelling the request context. // // A zero or negative value means that there will be no timeout. Timeout time.Duration // ExpectedStatus specifies the expected response HTTP status code. ExpectedStatus int // List of keywords that MUST exist in the response body. // // Either ExpectedContent or NotExpectedContent must be set if the response body is non-empty. // Leave both fields empty if you want to ensure that the response didn't have any body (e.g. 204). ExpectedContent []string // List of keywords that MUST NOT exist in the response body. // // Either ExpectedContent or NotExpectedContent must be set if the response body is non-empty. // Leave both fields empty if you want to ensure that the response didn't have any body (e.g. 204). NotExpectedContent []string // List of hook events to check whether they were fired or not. // // You can use the wildcard "*" event key if you want to ensure // that no other hook events except those listed have been fired. // // For example: // // map[string]int{ "*": 0 } // no hook events were fired // map[string]int{ "*": 0, "EventA": 2 } // no hook events, except EventA were fired // map[string]int{ EventA": 2, "EventB": 0 } // ensures that EventA was fired exactly 2 times and EventB exactly 0 times. ExpectedEvents map[string]int TestAppFactory func(t testing.TB) *TestApp BeforeTestFunc func(t testing.TB, app *TestApp, e *core.ServeEvent) AfterTestFunc func(t testing.TB, app *TestApp, res *http.Response) }
ApiScenario defines a single api request test case/scenario.
func (*ApiScenario) Benchmark ¶ added in v0.23.0
func (scenario *ApiScenario) Benchmark(b *testing.B)
Benchmark benchmarks the test scenario.
Example:
func BenchmarkListExample(b *testing.B) { scenario := tests.ApiScenario{ Name: "list example collection", Method: http.MethodGet, URL: "/api/collections/example/records", ExpectedStatus: 200, ExpectedContent: []string{ `"totalItems":3`, `"id":"0yxhwia2amd8gec"`, `"id":"achvryl401bhse3"`, `"id":"llvuca81nly1qls"`, }, ExpectedEvents: map[string]int{ "OnRecordsListRequest": 1, "OnRecordEnrich": 3, }, } scenario.Benchmark(b) }
func (*ApiScenario) Test ¶
func (scenario *ApiScenario) Test(t *testing.T)
Test executes the test scenario.
Example:
func TestListExample(t *testing.T) { scenario := tests.ApiScenario{ Name: "list example collection", Method: http.MethodGet, URL: "/api/collections/example/records", ExpectedStatus: 200, ExpectedContent: []string{ `"totalItems":3`, `"id":"0yxhwia2amd8gec"`, `"id":"achvryl401bhse3"`, `"id":"llvuca81nly1qls"`, }, ExpectedEvents: map[string]int{ "OnRecordsListRequest": 1, "OnRecordEnrich": 3, }, } scenario.Test(t) }
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 // contains filtered or unexported fields }
TestApp is a wrapper app instance used for testing.
func NewTestApp ¶
NewTestApp creates and initializes a test application instance.
It is the caller's responsibility to call app.Cleanup() when the app is no longer needed.
func NewTestAppWithConfig ¶ added in v0.24.0
func NewTestAppWithConfig(config core.BaseAppConfig) (*TestApp, error)
NewTestAppWithConfig creates and initializes a test application instance from the provided config.
If config.DataDir is not set it fallbacks to the default internal test data directory.
config.DataDir is cloned for each new test application instance.
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) ResetEventCalls ¶
func (t *TestApp) ResetEventCalls()
ResetEventCalls resets the EventCalls counter.
type TestMailer ¶
type TestMailer struct {
// contains filtered or unexported fields
}
TestMailer is a mock mailer.Mailer implementation.
func (*TestMailer) FirstMessage ¶ added in v0.23.0
func (tm *TestMailer) FirstMessage() mailer.Message
FirstMessage returns a shallow copy of the first sent message.
Returns an empty mailer.Message struct if there are no sent messages.
func (*TestMailer) LastMessage ¶ added in v0.9.0
func (tm *TestMailer) LastMessage() mailer.Message
LastMessage returns a shallow copy of the last sent message.
Returns an empty mailer.Message struct if there are no sent messages.
func (*TestMailer) Messages ¶ added in v0.23.0
func (tm *TestMailer) Messages() []*mailer.Message
Messages returns a shallow copy of all of the collected test messages.
func (*TestMailer) Reset ¶
func (tm *TestMailer) Reset()
Reset clears any previously test collected data.
func (*TestMailer) Send ¶
func (tm *TestMailer) Send(m *mailer.Message) error
Send implements mailer.Mailer interface.
func (*TestMailer) TotalSend ¶
func (tm *TestMailer) TotalSend() int
TotalSend returns the total number of sent messages.