From 1428534dc3f24fc929fc855359a4025d3936666a Mon Sep 17 00:00:00 2001 From: dylan Date: Tue, 14 Jan 2025 17:28:45 +0100 Subject: [PATCH] Write test for getElectionResults --- cmd/web/handlers_test.go | 72 +++++++++++++++++++++++++++++++++++++++ cmd/web/testutils_test.go | 19 ++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index 2f459fe..1e5e65a 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -711,3 +711,75 @@ func TestCreateVotes_ExpiredElection(t *testing.T) { assert.Equal(t, http.StatusUnprocessableEntity, code) } + +func TestGetElectionResults(t *testing.T) { + app := newTestApplication(t) + server := newTestServer(t, app.routes()) + defer server.Close() + + votes := []models.Vote{ + { + VoterIdentity: "Voter1", + ElectionID: 1, + ChoiceText: "Choice1", + Tokens: 2, + CreatedAt: time.Now(), + }, + { + VoterIdentity: "Voter2", + ElectionID: 1, + ChoiceText: "Choice2", + Tokens: 4, + CreatedAt: time.Now(), + }, + { + VoterIdentity: "Voter3", + ElectionID: 1, + ChoiceText: "Choice3", + Tokens: 6, + CreatedAt: time.Now(), + }, + { + VoterIdentity: "Voter4", + ElectionID: 1, + ChoiceText: "Choice1", + Tokens: 8, + CreatedAt: time.Now(), + }, + { + VoterIdentity: "Voter5", + ElectionID: 1, + ChoiceText: "Choice2", + Tokens: 10, + CreatedAt: time.Now(), + }, + } + mockVotes := app.votes.(*mockVoteModel) + mockVotes. + On("GetByElection", mock.Anything). + Return(&votes, nil) + + path := "/api/election/1/results" + code, _, body := server.get(t, path) + + assert.Equal(t, http.StatusOK, code) + + var response api.ElectionResultsResponse + err := json.Unmarshal([]byte(body), &response) + if err != nil { + t.Fatal(err) + } + + for _, result := range *response.Results { + switch result.Choice { + case "Choice1": + assert.Equal(t, 3, result.Votes) + case "Choice2": + assert.Equal(t, 5, result.Votes) + case "Choice3": + assert.Equal(t, 2, result.Votes) + default: + t.Fatalf("Unexpected choice: %s", result.Choice) + } + } +} diff --git a/cmd/web/testutils_test.go b/cmd/web/testutils_test.go index 9ef1fa6..9f86ccd 100644 --- a/cmd/web/testutils_test.go +++ b/cmd/web/testutils_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "code.dlmw.ch/dlmw/qv/internal/models" "github.com/stretchr/testify/mock" "io" @@ -29,6 +30,22 @@ func newTestServer(t *testing.T, h http.Handler) *testServer { return &testServer{server} } +func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, string) { + rs, err := ts.Client().Get(ts.URL + urlPath) + if err != nil { + t.Fatal(err) + } + defer rs.Body.Close() + + body, err := io.ReadAll(rs.Body) + if err != nil { + t.Fatal(err) + } + body = bytes.TrimSpace(body) + + return rs.StatusCode, rs.Header, string(body) +} + func (ts *testServer) post(t *testing.T, urlPath string, body io.Reader) (int, http.Header, string) { res, err := ts.Client().Post(ts.URL+urlPath, "application/json", body) if err != nil { @@ -93,5 +110,5 @@ func (v *mockVoteModel) Exists(voterIdentity string, electionID int) (bool, erro func (v *mockVoteModel) GetByElection(electionID int) (*[]models.Vote, error) { args := v.Called(electionID) - return args.Get(0).(*[]models.Vote), args.Error(2) + return args.Get(0).(*[]models.Vote), args.Error(1) }