[#396] updated tests helpers

This commit is contained in:
Gani Georgiev
2022-09-07 20:31:05 +03:00
parent b79a7982bf
commit 4bc28abac4
21 changed files with 117 additions and 101 deletions
+16 -7
View File
@@ -36,13 +36,22 @@ type ApiScenario struct {
// test hooks
// ---
BeforeFunc func(t *testing.T, app *TestApp, e *echo.Echo)
AfterFunc func(t *testing.T, app *TestApp, e *echo.Echo)
TestAppFactory func() *TestApp
BeforeTestFunc func(t *testing.T, app *TestApp, e *echo.Echo)
AfterTestFunc func(t *testing.T, app *TestApp, e *echo.Echo)
}
// Test executes the test case/scenario.
func (scenario *ApiScenario) Test(t *testing.T) {
testApp, _ := NewTestApp()
var testApp *TestApp
if scenario.TestAppFactory != nil {
testApp = scenario.TestAppFactory()
} else {
testApp, _ = NewTestApp()
}
if testApp == nil {
t.Fatal("Failed to initialize the test app instance.")
}
defer testApp.Cleanup()
e, err := apis.InitApi(testApp)
@@ -50,8 +59,8 @@ func (scenario *ApiScenario) Test(t *testing.T) {
t.Fatal(err)
}
if scenario.BeforeFunc != nil {
scenario.BeforeFunc(t, testApp, e)
if scenario.BeforeTestFunc != nil {
scenario.BeforeTestFunc(t, testApp, e)
}
recorder := httptest.NewRecorder()
@@ -135,7 +144,7 @@ func (scenario *ApiScenario) Test(t *testing.T) {
}
}
if scenario.AfterFunc != nil {
scenario.AfterFunc(t, testApp, e)
if scenario.AfterTestFunc != nil {
scenario.AfterTestFunc(t, testApp, e)
}
}
+18 -11
View File
@@ -45,12 +45,21 @@ func (t *TestApp) ResetEventCalls() {
t.EventCalls = make(map[string]int)
}
// NewTestApp creates and initializes a full application instance for testing.
// 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 NewTestApp() (*TestApp, error) {
tempDir, err := NewTempDataDir()
func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
var testDataDir string
if len(optTestDataDir) == 0 || optTestDataDir[0] == "" {
// fallback to the default test data directory
_, currentFile, _, _ := runtime.Caller(0)
testDataDir = filepath.Join(path.Dir(currentFile), "data")
} else {
testDataDir = optTestDataDir[0]
}
tempDir, err := CloneIntoTempDir(testDataDir)
if err != nil {
return nil, err
}
@@ -381,21 +390,19 @@ func NewTestApp() (*TestApp, error) {
return t, nil
}
// NewTempDataDir creates a new temporary directory copy of the test data.
// CloneIntoTempDir creates a new temporary directory copy from the
// provided directory path.
//
// It is the caller's responsibility to call `os.RemoveAll(dir)`
// when the directory is no longer needed.
func NewTempDataDir() (string, error) {
// It is the caller's responsibility to call `os.RemoveAll(tempDir)`
// when the directory is no longer needed!
func CloneIntoTempDir(dirToClone string) (string, error) {
tempDir, err := os.MkdirTemp("", "pb_test_*")
if err != nil {
return "", err
}
_, currentFile, _, _ := runtime.Caller(0)
testDataDir := filepath.Join(path.Dir(currentFile), "data")
// copy everything from testDataDir to tempDir
if err := copyDir(testDataDir, tempDir); err != nil {
if err := copyDir(dirToClone, tempDir); err != nil {
return "", err
}