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)
- type BigqueryEmulatorContainer
- type Data
- 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) ReadFixture(filename, dir string) string
- func (f Fixtures) ReadRequestBody(s string) string
- func (f Fixtures) ReadResponse(s string) string
- 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.
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 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) 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 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 ¶
type IntegrationTestWithBigQuery struct {
DataPath string
}
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.
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
}
PostgresDBContainer holds all the necessary information for postgres database test container.