Implement mocking and update tests for createElection

This commit is contained in:
2025-01-08 17:18:53 +01:00
parent 49a1df06d2
commit bf3368b736
4 changed files with 46 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"code.dlmw.ch/dlmw/qv/internal/models"
"github.com/stretchr/testify/mock"
"io"
"log/slog"
"net/http"
@ -44,10 +45,12 @@ func (ts *testServer) post(t *testing.T, urlPath string, body io.Reader) (int, h
}
type mockElectionModel struct {
mock.Mock
}
func (e *mockElectionModel) Insert(name string, tokens int, areVotersKnown bool, maxVoters int, choices []string, expiresAt time.Time) (int, error) {
return 1, nil
args := e.Called(name, tokens, areVotersKnown, maxVoters, choices, expiresAt)
return args.Int(0), args.Error(1)
}
func (e *mockElectionModel) GetById(id int) (*models.Election, error) {
@ -78,10 +81,12 @@ func (v *mockVoterModel) Exists(voterIdentity string, electionID int) (bool, err
}
type mockVoteModel struct {
mock.Mock
}
func (v *mockVoteModel) Insert(voterIdentity string, electionId int, choiceText string, tokens int) (int, error) {
return 1, nil
args := v.Called(voterIdentity, electionId, choiceText, tokens)
return args.Int(0), args.Error(1)
}
func (v *mockVoteModel) Exists(voterIdentity string, electionID int) (bool, error) {