Add some more tests and implement getElection
This commit is contained in:
@ -41,6 +41,18 @@ type CreateVotesRequest struct {
|
||||
VoterIdentity *string `json:"voterIdentity,omitempty"`
|
||||
}
|
||||
|
||||
// Election defines model for Election.
|
||||
type Election struct {
|
||||
AreVotersKnown bool `json:"areVotersKnown"`
|
||||
Choices []string `json:"choices"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
ExpiresAt string `json:"expiresAt"`
|
||||
Id int `json:"id"`
|
||||
MaxVoters int `json:"maxVoters"`
|
||||
Name string `json:"name"`
|
||||
Tokens int `json:"tokens"`
|
||||
}
|
||||
|
||||
// ElectionResultsResponse defines model for ElectionResultsResponse.
|
||||
type ElectionResultsResponse struct {
|
||||
Results *[]VotesForChoice `json:"results,omitempty"`
|
||||
@ -75,6 +87,9 @@ type ServerInterface interface {
|
||||
// Create a new election
|
||||
// (POST /election)
|
||||
CreateElection(w http.ResponseWriter, r *http.Request)
|
||||
// Get an election
|
||||
// (GET /election/{id})
|
||||
GetElection(w http.ResponseWriter, r *http.Request, id int)
|
||||
// Get the results of an election
|
||||
// (GET /election/{id}/results)
|
||||
GetElectionResults(w http.ResponseWriter, r *http.Request, id int)
|
||||
@ -107,6 +122,32 @@ func (siw *ServerInterfaceWrapper) CreateElection(w http.ResponseWriter, r *http
|
||||
handler.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
|
||||
// GetElection operation middleware
|
||||
func (siw *ServerInterfaceWrapper) GetElection(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var err error
|
||||
|
||||
// ------------- Path parameter "id" -------------
|
||||
var id int
|
||||
|
||||
err = runtime.BindStyledParameterWithOptions("simple", "id", r.PathValue("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
|
||||
if err != nil {
|
||||
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err})
|
||||
return
|
||||
}
|
||||
|
||||
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
siw.Handler.GetElection(w, r, id)
|
||||
}))
|
||||
|
||||
for _, middleware := range siw.HandlerMiddlewares {
|
||||
handler = middleware(handler)
|
||||
}
|
||||
|
||||
handler.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
|
||||
// GetElectionResults operation middleware
|
||||
func (siw *ServerInterfaceWrapper) GetElectionResults(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
@ -274,6 +315,7 @@ func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.H
|
||||
}
|
||||
|
||||
m.HandleFunc("POST "+options.BaseURL+"/election", wrapper.CreateElection)
|
||||
m.HandleFunc("GET "+options.BaseURL+"/election/{id}", wrapper.GetElection)
|
||||
m.HandleFunc("GET "+options.BaseURL+"/election/{id}/results", wrapper.GetElectionResults)
|
||||
m.HandleFunc("POST "+options.BaseURL+"/election/{id}/votes", wrapper.CreateVotes)
|
||||
|
||||
|
19
internal/mappers/elections.go
Normal file
19
internal/mappers/elections.go
Normal file
@ -0,0 +1,19 @@
|
||||
package mappers
|
||||
|
||||
import (
|
||||
api "code.dlmw.ch/dlmw/qv/internal"
|
||||
"code.dlmw.ch/dlmw/qv/internal/models"
|
||||
)
|
||||
|
||||
func ElectionResponse(election *models.Election) *api.Election {
|
||||
return &api.Election{
|
||||
Id: election.ID,
|
||||
Name: election.Name,
|
||||
Tokens: election.Tokens,
|
||||
AreVotersKnown: election.AreVotersKnown,
|
||||
MaxVoters: election.MaxVoters,
|
||||
CreatedAt: election.CreatedAt.String(),
|
||||
ExpiresAt: election.ExpiresAt.String(),
|
||||
Choices: election.Choices,
|
||||
}
|
||||
}
|
46
internal/mappers/elections_test.go
Normal file
46
internal/mappers/elections_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package mappers
|
||||
|
||||
import (
|
||||
"code.dlmw.ch/dlmw/qv/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestElectionResponse(t *testing.T) {
|
||||
layout := "2006-01-02 15:04:05 -0700"
|
||||
parsedCreatedAtTime, err := time.Parse(layout, "2025-01-14 15:13:37 +0000")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
parsedExpiresAtTime, err := time.Parse(layout, "2025-01-15 15:13:37 +0000")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
election := models.Election{
|
||||
ID: 15,
|
||||
Name: "The best",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: true,
|
||||
MaxVoters: 150,
|
||||
CreatedAt: parsedCreatedAtTime.In(time.UTC),
|
||||
ExpiresAt: parsedExpiresAtTime.In(time.UTC),
|
||||
Choices: []string{"You", "Me"},
|
||||
}
|
||||
|
||||
response := ElectionResponse(&election)
|
||||
|
||||
assert.Equal(t, 15, response.Id)
|
||||
assert.Equal(t, "The best", response.Name)
|
||||
assert.Equal(t, 100, response.Tokens)
|
||||
assert.Equal(t, true, response.AreVotersKnown)
|
||||
assert.Equal(t, 150, response.MaxVoters)
|
||||
assert.Equal(t, "2025-01-14 15:13:37 +0000 UTC", response.CreatedAt)
|
||||
assert.Equal(t, "2025-01-15 15:13:37 +0000 UTC", response.ExpiresAt)
|
||||
|
||||
for _, choice := range election.Choices {
|
||||
assert.Contains(t, election.Choices, choice)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user