Documentation ¶
Index ¶
- Constants
- Variables
- func CreateTestFiles(fs fs.FS, paths ...string) ([]fsutil.File, error)
- func FindRootDirectory() string
- func NewTestRequest(method, uri string, body io.Reader) *goyave.Request
- func NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)
- func ReadJSONBody[T any](body io.Reader) (T, error)
- func ToJSON(data any) *bytes.Reader
- func WriteMultipartFile(writer *multipart.Writer, filesystem fs.FS, path, fieldName, fileName string) (err error)
- type LogWriter
- type Session
- func (s *Session) Begin(ctx context.Context) (session.Session, error)
- func (s *Session) Children() []*Session
- func (s *Session) Commit() error
- func (s *Session) Context() context.Context
- func (s *Session) Rollback() error
- func (s *Session) Status() uint32
- func (s *Session) Transaction(ctx context.Context, f func(context.Context) error) error
- type TestServer
- func (s *TestServer) CloseDB()
- func (s *TestServer) NewTestRequest(method, uri string, body io.Reader) *goyave.Request
- func (s *TestServer) NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)
- func (s *TestServer) TestMiddleware(middleware goyave.Middleware, request *goyave.Request, ...) *http.Response
- func (s *TestServer) TestRequest(request *http.Request) *http.Response
Constants ¶
const ( SessionCreated uint32 = iota SessionCommitted SessionRolledBack )
Variables ¶
var ( ErrSessionEnded = stderrors.New("testutil.Session: session already ended") ErrEndRootSession = stderrors.New("testutil.Session: cannot commit/rollback root session") ErrChildRunning = stderrors.New("testutil.Session: cannot commit/rollback if a child session is still running") ErrNotParentContext = stderrors.New("testutil.Session: cannot create a child session with an unrelated context. Parent context should be the context or a child context of the parent session.") )
Functions ¶
func CreateTestFiles ¶
CreateTestFiles create a slice of "fsutil.File" from the given FS. To reproduce the way the files are obtained in real scenarios, files are first encoded in a multipart form, then decoded with a multipart form reader.
Paths are relative to the caller, not relative to the project's root directory.
func FindRootDirectory ¶
func FindRootDirectory() string
FindRootDirectory find relative path to the project's root directory based on the existence of a `go.mod` file. The returned path is a rooted path. Returns an empty string if not found.
func NewTestRequest ¶
NewTestRequest create a new `goyave.Request` with an underlying HTTP request created usin the `httptest` package.
func NewTestResponse ¶
func NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)
NewTestResponse create a new `goyave.Response` with an underlying HTTP response recorder created using the `httptest` package. This function uses a temporary `goyave.Server` with all defaults values loaded so all functions of `*goyave.Response` can be used safely.
func ReadJSONBody ¶
ReadJSONBody decodes the given body reader into a new variable of type `*T`.
Types ¶
type LogWriter ¶
type LogWriter struct { T interface { Log(args ...any) } }
LogWriter implementation of `io.Writer` redirecting the logs to `testing.T.Log()`
type Session ¶ added in v5.5.0
type Session struct {
// contains filtered or unexported fields
}
Session is an advanced mock for the `session.Session` interface. This implementation is designed to provide a realistic, observable transaction system and help identify incorrect usage.
Each transaction created with this implementation has a cancellable context created from its parent. The context is canceled when the session is committed or rolled back. This helps detecting cases where your code tries to use a terminated transaction.
A transaction cannot be committed or rolled back several times. It cannot be committed after being rolled back or the other way around.
For nested transactions, all child sessions should be ended (committed or rolled back) before the parent can be ended. Moreover, the context given on `Begin()` should be the context or a child context of the parent session.
A child session cannot be created or committed if its parent context is done.
The root transaction cannot be committed or rolledback. This helps detecting cases where your codes uses the root session without creating a child session.
This implementation is not meant to be used concurrently. You should create a new instance for each test.
func NewTestSession ¶ added in v5.5.0
func NewTestSession() *Session
NewTestSession create a new root session with the `context.Background()`.
func (*Session) Children ¶ added in v5.5.0
Children returns the direct child sessions. You can use the returned values for your test assertions. The returned values are sorted in the order in which the child transactions were started.
To access nested transactions, call `Children()` on the returned values.
This method always returns a non-nil value but can return an empty slice.
func (*Session) Commit ¶ added in v5.5.0
Commit the transaction. For this test utility, it only sets the sessions status to `SessionCommitted`. If the session status is not `testutil.SessionCreated`, returns `testutil.ErrSessionEnded`.
It is not possible to commit the root session. In this case, `testutil.ErrEndRootSession` is returned.
This action is final.
func (*Session) Rollback ¶ added in v5.5.0
Rollback the transaction. For this test utility, it only sets the sessions status to `SessionRollbedBack`. If the session status is not `testutil.SessionCreated`, returns `testutil.ErrSessionEnded`.
It is not possible to roll back the root session. In this case, `testutil.ErrEndRootSession` is returned.
This action is final.
func (*Session) Status ¶ added in v5.5.0
Status returns the session status. The value will be equal to `testutil.SessionCreated`, `testutil.SessionCommitted` or `testutil.SessionRolledBack`.
func (*Session) Transaction ¶ added in v5.5.0
Transaction executes a transaction. If the given function returns an error, the transaction is rolled back. Otherwise it is automatically committed before `Transaction()` returns. The underlying transaction mechanism is injected into the context as a value.
type TestServer ¶
type TestServer struct {
*goyave.Server
}
TestServer extension of `goyave.Server` providing useful functions for testing.
func NewTestServer ¶
func NewTestServer(t *testing.T, configFileName string) *TestServer
NewTestServer creates a new server using the given config file. The config path is relative to the project's directory. If not nil, the given `routeRegistrer` function is called to register routes without starting the server.
A default logger redirecting the output to `testing.T.Log()` is used.
Automatically closes the DB connection (if there is one) using a test `Cleanup` function.
func NewTestServerWithOptions ¶
func NewTestServerWithOptions(t *testing.T, opts goyave.Options) *TestServer
NewTestServerWithOptions creates a new server using the given options. If not nil, the given `routeRegistrer` function is called to register routes without starting the server.
By default, if no `Logger` is given in the options, a default logger redirecting the output to `io.Discard` is used.
Automatically closes the DB connection (if there is one) using a test `Cleanup` function.
func (*TestServer) CloseDB ¶
func (s *TestServer) CloseDB()
CloseDB close the server DB if one is open. It is a good practice to always call this in a test `Cleanup` function when using a database.
func (*TestServer) NewTestRequest ¶
func (s *TestServer) NewTestRequest(method, uri string, body io.Reader) *goyave.Request
NewTestRequest create a new `goyave.Request` with an underlying HTTP request created usin the `httptest` package. This function sets the request language using the default language of the server.
func (*TestServer) NewTestResponse ¶
func (s *TestServer) NewTestResponse(request *goyave.Request) (*goyave.Response, *httptest.ResponseRecorder)
NewTestResponse create a new `goyave.Response` with an underlying HTTP response recorder created using the `httptest` package.
func (*TestServer) TestMiddleware ¶
func (s *TestServer) TestMiddleware(middleware goyave.Middleware, request *goyave.Request, procedure goyave.Handler) *http.Response
TestMiddleware executes with the given request and returns the response. The `procedure` parameter is the `next` handler passed to the middleware and can be used to make assertions. Keep in mind that this procedure won't be executed if your middleware is blocking.
The request will go through the entire lifecycle like a regular request.
The given request is cloned. If the middleware alters the request object, these changes won't be reflected on the input request. You can do your assertions inside the `procedure`.
func (*TestServer) TestRequest ¶
func (s *TestServer) TestRequest(request *http.Request) *http.Response
TestRequest execute a request by calling the root Router's `ServeHTTP()` implementation.