Refactor test setup/cleanup
This commit is contained in:
@ -82,21 +82,21 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStore_GetServiceStatusByKey(t *testing.T) {
|
type Scenario struct {
|
||||||
|
Name string
|
||||||
|
Store Store
|
||||||
|
}
|
||||||
|
|
||||||
|
func initStoresAndBaseScenarios(t *testing.T, testName string) []*Scenario {
|
||||||
memoryStore, err := memory.NewStore("")
|
memoryStore, err := memory.NewStore("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to create store:", err.Error())
|
t.Fatal("failed to create store:", err.Error())
|
||||||
}
|
}
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusByKey.db")
|
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/"+testName+".db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to create store:", err.Error())
|
t.Fatal("failed to create store:", err.Error())
|
||||||
}
|
}
|
||||||
defer databaseStore.Close()
|
return []*Scenario{
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
{
|
||||||
Name: "memory",
|
Name: "memory",
|
||||||
Store: memoryStore,
|
Store: memoryStore,
|
||||||
@ -106,6 +106,17 @@ func TestStore_GetServiceStatusByKey(t *testing.T) {
|
|||||||
Store: databaseStore,
|
Store: databaseStore,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUp(scenarios []*Scenario) {
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
scenario.Store.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStore_GetServiceStatusByKey(t *testing.T) {
|
||||||
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetServiceStatusByKey")
|
||||||
|
defer cleanUp(scenarios)
|
||||||
firstResult := testSuccessfulResult
|
firstResult := testSuccessfulResult
|
||||||
firstResult.Timestamp = now.Add(-time.Minute)
|
firstResult.Timestamp = now.Add(-time.Minute)
|
||||||
secondResult := testUnsuccessfulResult
|
secondResult := testUnsuccessfulResult
|
||||||
@ -149,29 +160,8 @@ func TestStore_GetServiceStatusByKey(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T) {
|
func TestStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T) {
|
||||||
memoryStore, err := memory.NewStore("")
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetServiceStatusForMissingStatusReturnsNil")
|
||||||
if err != nil {
|
defer cleanUp(scenarios)
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusForMissingStatusReturnsNil.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
defer databaseStore.Close()
|
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
|
||||||
Name: "memory",
|
|
||||||
Store: memoryStore,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "database",
|
|
||||||
Store: databaseStore,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
t.Run(scenario.Name, func(t *testing.T) {
|
t.Run(scenario.Name, func(t *testing.T) {
|
||||||
scenario.Store.Insert(&testService, &testSuccessfulResult)
|
scenario.Store.Insert(&testService, &testSuccessfulResult)
|
||||||
@ -192,29 +182,8 @@ func TestStore_GetServiceStatusForMissingStatusReturnsNil(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStore_GetAllServiceStatuses(t *testing.T) {
|
func TestStore_GetAllServiceStatuses(t *testing.T) {
|
||||||
memoryStore, err := memory.NewStore("")
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetAllServiceStatuses")
|
||||||
if err != nil {
|
defer cleanUp(scenarios)
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_GetAllServiceStatuses.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
defer databaseStore.Close()
|
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
|
||||||
Name: "memory",
|
|
||||||
Store: memoryStore,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "database",
|
|
||||||
Store: databaseStore,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
firstResult := testSuccessfulResult
|
firstResult := testSuccessfulResult
|
||||||
secondResult := testUnsuccessfulResult
|
secondResult := testUnsuccessfulResult
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
@ -242,29 +211,8 @@ func TestStore_GetAllServiceStatuses(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStore_GetAllServiceStatusesWithResultsAndEvents(t *testing.T) {
|
func TestStore_GetAllServiceStatusesWithResultsAndEvents(t *testing.T) {
|
||||||
memoryStore, err := memory.NewStore("")
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetAllServiceStatusesWithResultsAndEvents")
|
||||||
if err != nil {
|
defer cleanUp(scenarios)
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_GetAllServiceStatusesWithResultsAndEvents.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
defer databaseStore.Close()
|
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
|
||||||
Name: "memory",
|
|
||||||
Store: memoryStore,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "database",
|
|
||||||
Store: databaseStore,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
firstResult := testSuccessfulResult
|
firstResult := testSuccessfulResult
|
||||||
secondResult := testUnsuccessfulResult
|
secondResult := testUnsuccessfulResult
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
@ -292,29 +240,8 @@ func TestStore_GetAllServiceStatusesWithResultsAndEvents(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2(t *testing.T) {
|
func TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2(t *testing.T) {
|
||||||
memoryStore, err := memory.NewStore("")
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2")
|
||||||
if err != nil {
|
defer cleanUp(scenarios)
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
defer databaseStore.Close()
|
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
|
||||||
Name: "memory",
|
|
||||||
Store: memoryStore,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "database",
|
|
||||||
Store: databaseStore,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
firstResult := testSuccessfulResult
|
firstResult := testSuccessfulResult
|
||||||
firstResult.Timestamp = now.Add(-time.Minute)
|
firstResult.Timestamp = now.Add(-time.Minute)
|
||||||
secondResult := testUnsuccessfulResult
|
secondResult := testUnsuccessfulResult
|
||||||
@ -347,29 +274,8 @@ func TestStore_GetServiceStatusPage1IsHasMoreRecentResultsThanPage2(t *testing.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStore_Insert(t *testing.T) {
|
func TestStore_Insert(t *testing.T) {
|
||||||
memoryStore, err := memory.NewStore("")
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_Insert")
|
||||||
if err != nil {
|
defer cleanUp(scenarios)
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_Insert.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
defer databaseStore.Close()
|
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
|
||||||
Name: "memory",
|
|
||||||
Store: memoryStore,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "database",
|
|
||||||
Store: databaseStore,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
firstResult := testSuccessfulResult
|
firstResult := testSuccessfulResult
|
||||||
firstResult.Timestamp = now.Add(-time.Minute)
|
firstResult.Timestamp = now.Add(-time.Minute)
|
||||||
secondResult := testUnsuccessfulResult
|
secondResult := testUnsuccessfulResult
|
||||||
@ -427,29 +333,8 @@ func TestStore_Insert(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStore_DeleteAllServiceStatusesNotInKeys(t *testing.T) {
|
func TestStore_DeleteAllServiceStatusesNotInKeys(t *testing.T) {
|
||||||
memoryStore, err := memory.NewStore("")
|
scenarios := initStoresAndBaseScenarios(t, "TestStore_DeleteAllServiceStatusesNotInKeys")
|
||||||
if err != nil {
|
defer cleanUp(scenarios)
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
databaseStore, err := database.NewStore("sqlite", t.TempDir()+"/TestStore_DeleteAllServiceStatusesNotInKeys.db")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed to create store:", err.Error())
|
|
||||||
}
|
|
||||||
defer databaseStore.Close()
|
|
||||||
type Scenario struct {
|
|
||||||
Name string
|
|
||||||
Store Store
|
|
||||||
}
|
|
||||||
scenarios := []Scenario{
|
|
||||||
{
|
|
||||||
Name: "memory",
|
|
||||||
Store: memoryStore,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "database",
|
|
||||||
Store: databaseStore,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
firstService := core.Service{Name: "service-1", Group: "group"}
|
firstService := core.Service{Name: "service-1", Group: "group"}
|
||||||
secondService := core.Service{Name: "service-2", Group: "group"}
|
secondService := core.Service{Name: "service-2", Group: "group"}
|
||||||
result := &testSuccessfulResult
|
result := &testSuccessfulResult
|
||||||
|
Reference in New Issue
Block a user