Write test for getElectionResults
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user