From 2af3425b9ec96ec0fc9a6d63997ba40d621b3c28 Mon Sep 17 00:00:00 2001 From: TwiN Date: Tue, 16 Nov 2021 22:56:16 -0500 Subject: [PATCH] Fix #202: Postgres error when an endpoint has a second page but others do not --- storage/store/sql/sql.go | 4 ++++ storage/store/store_test.go | 38 ++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/storage/store/sql/sql.go b/storage/store/sql/sql.go index 3543efd8..b6c91f3f 100644 --- a/storage/store/sql/sql.go +++ b/storage/store/sql/sql.go @@ -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 diff --git a/storage/store/store_test.go b/storage/store/store_test.go index 6bfe24c5..bb5fd037 100644 --- a/storage/store/store_test.go +++ b/storage/store/store_test.go @@ -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() + }) } }