Documentation
¶
Index ¶
- Constants
- func Assert(it *IntegrationTest, t *Data, res *HandlerResult)
- func AssertAll(it *IntegrationTest, tt []Data)
- func LoadMocks(it *IntegrationTest, t *Data)
- func Request(it *IntegrationTest, method string, params Params) (echo.Context, *httptest.ResponseRecorder)
- func RunWithPostgres(m *testing.M) int
- type BigqueryEmulatorContainer
- type Data
- type FileUpload
- type Fixtures
- func (f Fixtures) ExcelToMap(content []byte) (map[string][][]string, error)
- func (f Fixtures) ReadCsvFile(s string) string
- func (f Fixtures) ReadExcelFile(s string) map[string][][]string
- func (f Fixtures) ReadFileBytes(filename string) []byte
- func (f Fixtures) ReadFixture(filename, dir string) string
- func (f Fixtures) ReadRequestBody(s string) string
- func (f Fixtures) ReadResponse(s string) string
- type Form
- type HandlerResult
- type IntegrationTest
- type IntegrationTestOption
- type IntegrationTestWithBigQuery
- type IntegrationTestWithMocks
- type IntegrationTestWithPostgres
- type Mock
- type MockCall
- type MockConfig
- type Params
- type PostgresDBContainer
Constants ¶
const ( Excel = "xlsx" CSV = "csv" )
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
func Assert(it *IntegrationTest, t *Data, res *HandlerResult)
Assert asserts the result of a handler. Leveraging the HandlerResult struct allows us to assert the response of a request by calling the handler function outside the assertion method. Thus, we can pass a custom context when calling the handler.
Example:
for _, t := range tt {
ctx, response := Request(it, t.Method, t.Params)
sctx := &CustomContext{
Context: ctx,
Clock: clock.NewMock(),
}
err := t.Handler(sctx)
if err != nil {
it.T.Log(err.Error())
}
t.Assert(it, &HandlerResult{
err: err,
response: response,
})
}
func AssertAll ¶
func AssertAll(it *IntegrationTest, tt []Data)
AssertAll runs the given tests and asserts their result. The handler function is called inside the assertion method.
func LoadMocks ¶
func LoadMocks(it *IntegrationTest, t *Data)
LoadMocks loads the mocks for a given test case.
func Request ¶
func Request(it *IntegrationTest, method string, params Params) (echo.Context, *httptest.ResponseRecorder)
Request creates a new request and a new test service context to which it passes the required parameters.
func RunWithPostgres ¶ added in v1.5.0
RunWithPostgres runs the tests of a package and stops the shared Postgres containers afterwards. Call it from TestMain. It turns on container sharing even when Ryuk is disabled, because it stops the containers itself.
Types ¶
type BigqueryEmulatorContainer ¶
type BigqueryEmulatorContainer struct {
testcontainers.Container
BqHost string
BqRestPort int
BqGrpcPort int
}
type Data ¶
type Data struct {
Name string
Method string
Params Params
Handler func(ctx echo.Context) error
Mocks []MockCall
ExpectResponse string
ExpectErrResponse bool
ExpectCode int
ExpectResponseType string
}
Data is a helper struct to define the parameters of a request for a test case.
type FileUpload ¶ added in v1.3.0
FileUpload defines the file to be uploaded in a multipart form request.
type Fixtures ¶
type Fixtures struct{}
Fixtures is a helper for reading fixtures.
func (Fixtures) ExcelToMap ¶
func (Fixtures) ReadCsvFile ¶ added in v1.0.0
ReadExcelFile reads an excel file with xlsx extension.
func (Fixtures) ReadExcelFile ¶
ReadExcelFile reads an excel file with xlsx extension.
func (Fixtures) ReadFileBytes ¶ added in v1.3.0
ReadFileBytes reads a file from the uploads fixtures directory and returns the raw bytes.
func (Fixtures) ReadFixture ¶
ReadFixture reads a fixture from a file.
func (Fixtures) ReadRequestBody ¶
ReadRequestBody reads the request body from a file.
func (Fixtures) ReadResponse ¶
ReadResponse reads the response from a file.
type Form ¶ added in v1.3.0
type Form struct {
File *FileUpload // file to upload in a multipart form request
Fields map[string]string
}
Form defines the parameters for a multipart form request.
type HandlerResult ¶
type HandlerResult struct {
Err error
Response *httptest.ResponseRecorder
}
HandlerResult holds the result of a handler, the error that possibly was returned and the response recorder.
type IntegrationTest ¶
type IntegrationTest struct {
T *testing.T
Db interface{}
Echo *echo.Echo
Fixtures *Fixtures
Container *PostgresDBContainer
BqContainer *BigqueryEmulatorContainer
Mock *Mock
// contains filtered or unexported fields
}
IntegrationTest is a struct that holds all the necessary information for integration testing.
func NewIntegrationTest ¶
func NewIntegrationTest(t *testing.T, opts ...IntegrationTestOption) *IntegrationTest
NewIntegrationTest prepares database for integration testing.
func (*IntegrationTest) TearDown ¶
func (it *IntegrationTest) TearDown()
TearDown cleans up the database after integration testing.
type IntegrationTestOption ¶
type IntegrationTestOption interface {
// contains filtered or unexported methods
}
IntegrationTestOption is an interface for integration test options.
type IntegrationTestWithBigQuery ¶
IntegrationTestWithBigQuery is an option for integration testing that sets up a BigQuery database test container.
type IntegrationTestWithMocks ¶
type IntegrationTestWithMocks struct {
BaseURL string
}
IntegrationTestWithMocks is an option for integration testing that allows mocking The mocks should be placed in a 'mocks' directory where the _test.go file is located.
type IntegrationTestWithPostgres ¶
IntegrationTestWithPostgres is an option for integration testing that sets up a postgres database test container. In the InitSQLScript a SQL script filename can be passed to initialize the database. The script should be located under a 'fixtures' directory where the _test.go file is located. An optional gorm config can also be passed.
When Ryuk is enabled, or the package runs its tests through RunWithPostgres, tests share one container per Version. Each test then gets its own database, copied from a template database per InitSQLScript, and the database is dropped when the test finishes.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock is the struct that gives access to all the mocks
func (*Mock) Debug ¶
func (m *Mock) Debug()
Debug is used to print the request URL and the mock returned for that particular request
func (*Mock) MockRequest ¶
func (m *Mock) MockRequest(config *MockConfig)
func (*Mock) SetHttpClient ¶ added in v1.1.0
type MockCall ¶
type MockCall struct {
Function func(config *MockConfig)
Config *MockConfig
}
MockCall represents a mocked API call used in tests
type MockConfig ¶
MockConfig configures a Mock
type PostgresDBContainer ¶
type PostgresDBContainer struct {
testcontainers.Container
DBHost string
DBPort int
DBName string
DBUsername string
DBPassword string
// contains filtered or unexported fields
}
PostgresDBContainer holds all the necessary information for postgres database test container.