Fix #202: Postgres error when an endpoint has a second page but others do not

This commit is contained in:
TwiN 2021-11-16 22:56:16 -05:00
parent 31bf2aeb80
commit 2af3425b9e
2 changed files with 37 additions and 5 deletions

View File

@ -568,6 +568,10 @@ func (s *Store) getEndpointResultsByEndpointID(tx *sql.Tx, endpointID int64, pag
results = append([]*core.Result{result}, results...)
idResultMap[id] = result
}
if len(idResultMap) == 0 {
// If there's no result, we'll just return an empty/nil slice
return
}
// Get condition results
args := make([]interface{}, 0, len(idResultMap))
query := `SELECT endpoint_result_id, condition, success

View File

@ -184,13 +184,10 @@ func TestStore_GetEndpointStatusForMissingStatusReturnsNil(t *testing.T) {
func TestStore_GetAllEndpointStatuses(t *testing.T) {
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetAllEndpointStatuses")
defer cleanUp(scenarios)
firstResult := testSuccessfulResult
secondResult := testUnsuccessfulResult
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
scenario.Store.Insert(&testEndpoint, &firstResult)
scenario.Store.Insert(&testEndpoint, &secondResult)
// Can't be bothered dealing with timezone issues on the worker that runs the automated tests
scenario.Store.Insert(&testEndpoint, &testSuccessfulResult)
scenario.Store.Insert(&testEndpoint, &testUnsuccessfulResult)
endpointStatuses, err := scenario.Store.GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(1, 20))
if err != nil {
t.Error("shouldn't have returned an error, got", err.Error())
@ -210,6 +207,37 @@ func TestStore_GetAllEndpointStatuses(t *testing.T) {
}
scenario.Store.Clear()
})
t.Run(scenario.Name+"-page-2", func(t *testing.T) {
otherEndpoint := testEndpoint
otherEndpoint.Name = testEndpoint.Name + "-other"
scenario.Store.Insert(&testEndpoint, &testSuccessfulResult)
scenario.Store.Insert(&otherEndpoint, &testSuccessfulResult)
scenario.Store.Insert(&otherEndpoint, &testSuccessfulResult)
scenario.Store.Insert(&otherEndpoint, &testSuccessfulResult)
endpointStatuses, err := scenario.Store.GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(2, 2))
if err != nil {
t.Error("shouldn't have returned an error, got", err.Error())
}
if len(endpointStatuses) != 2 {
t.Fatal("expected 2 endpoint statuses")
}
if endpointStatuses[0] == nil || endpointStatuses[1] == nil {
t.Fatal("expected endpoint status to exist")
}
if len(endpointStatuses[0].Results) != 0 {
t.Error("expected 0 results on the first endpoint, got", len(endpointStatuses[0].Results))
}
if len(endpointStatuses[1].Results) != 1 {
t.Error("expected 1 result on the second endpoint, got", len(endpointStatuses[1].Results))
}
if len(endpointStatuses[0].Events) != 0 {
t.Error("expected 0 events on the first endpoint, got", len(endpointStatuses[0].Events))
}
if len(endpointStatuses[1].Events) != 0 {
t.Error("expected 0 events on the second endpoint, got", len(endpointStatuses[1].Events))
}
scenario.Store.Clear()
})
}
}