From 62562272f181ab050641be3a4ddbfd57c2d0bc64 Mon Sep 17 00:00:00 2001 From: dylan Date: Fri, 17 Jan 2025 14:55:54 +0100 Subject: [PATCH] Add tests for GetElection --- cmd/web/handlers_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index 242cfac..2b39bbc 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -851,3 +851,44 @@ func TestGetElectionResults_NotFound(t *testing.T) { t.Fatal(err) } } + +func TestGetElection_Found(t *testing.T) { + app := newTestApplication(t) + server := newTestServer(t, app.routes()) + defer server.Close() + + mockElections := app.elections.(*mockElectionModel) + mockElections. + On("GetById", mock.Anything). + Return(&models.Election{ + ID: 1, + Name: "Guy of the year", + Tokens: 100, + AreVotersKnown: false, + MaxVoters: 10, + CreatedAt: time.UnixMilli(1), + ExpiresAt: time.UnixMilli(100000), + Choices: []string{"Gandhi", "Buddha"}, + }, nil) + + path := baseUri + "election/1" + code, _, _ := server.get(t, path) + + assert.Equal(t, http.StatusOK, code) +} + +func TestGetElection_NotFound(t *testing.T) { + app := newTestApplication(t) + server := newTestServer(t, app.routes()) + defer server.Close() + + mockElections := app.elections.(*mockElectionModel) + mockElections. + On("GetById", mock.Anything). + Return((*models.Election)(nil), sql.ErrNoRows) + + path := baseUri + "election/1" + code, _, _ := server.get(t, path) + + assert.Equal(t, http.StatusNotFound, code) +}