Write test for getElectionResults

This commit is contained in:
2025-01-14 17:28:45 +01:00
parent 66cdd0086c
commit 1428534dc3
2 changed files with 90 additions and 1 deletions

View File

@ -711,3 +711,75 @@ func TestCreateVotes_ExpiredElection(t *testing.T) {
assert.Equal(t, http.StatusUnprocessableEntity, code) 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)
}
}
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"code.dlmw.ch/dlmw/qv/internal/models" "code.dlmw.ch/dlmw/qv/internal/models"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
"io" "io"
@ -29,6 +30,22 @@ func newTestServer(t *testing.T, h http.Handler) *testServer {
return &testServer{server} 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) { 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) res, err := ts.Client().Post(ts.URL+urlPath, "application/json", body)
if err != nil { 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) { func (v *mockVoteModel) GetByElection(electionID int) (*[]models.Vote, error) {
args := v.Called(electionID) args := v.Called(electionID)
return args.Get(0).(*[]models.Vote), args.Error(2) return args.Get(0).(*[]models.Vote), args.Error(1)
} }