Add more test cases and add UniqueValues check
This commit is contained in:
@ -69,6 +69,11 @@ func isRequestValid(r *createElectionRequestWithValidator) bool {
|
||||
r.CheckField(validator.GreaterThan(r.Tokens, 0), "tokens", "must be greater than 0")
|
||||
r.CheckField(validator.After(r.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date")
|
||||
r.CheckField(validator.GreaterThan(len(r.Choices), 1), "choices", "there must be more than 1 choice")
|
||||
r.CheckField(validator.UniqueValues(r.Choices), "choices", "must not contain duplicate values")
|
||||
|
||||
for _, choice := range r.Choices {
|
||||
r.CheckField(validator.NotBlank(choice), "choice", "must not be blank")
|
||||
}
|
||||
|
||||
if r.AreVotersKnown {
|
||||
r.CheckField(
|
||||
|
@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
api "code.dlmw.ch/dlmw/qv/internal"
|
||||
"code.dlmw.ch/dlmw/qv/internal"
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
@ -47,6 +47,19 @@ func TestCreateElection(t *testing.T) {
|
||||
},
|
||||
expectedCode: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Valid request (with 3 choices)",
|
||||
urlPath: "/election",
|
||||
body: api.CreateElectionRequest{
|
||||
Choices: []string{"Gandhi", "Buddha", "You"},
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
AreVotersKnown: false,
|
||||
MaxVoters: 0,
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
},
|
||||
expectedCode: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "Valid request (voters unknown with max voters)",
|
||||
urlPath: "/election",
|
||||
@ -125,6 +138,32 @@ func TestCreateElection(t *testing.T) {
|
||||
},
|
||||
expectedCode: http.StatusUnprocessableEntity,
|
||||
},
|
||||
{
|
||||
name: "Invalid request (choices are not unique)",
|
||||
urlPath: "/election",
|
||||
body: api.CreateElectionRequest{
|
||||
Choices: []string{"Gandhi", "Gandhi"},
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
AreVotersKnown: false,
|
||||
MaxVoters: 0,
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
},
|
||||
expectedCode: http.StatusUnprocessableEntity,
|
||||
},
|
||||
{
|
||||
name: "Invalid request (choices contain blank entries)",
|
||||
urlPath: "/election",
|
||||
body: api.CreateElectionRequest{
|
||||
Choices: []string{"Gandhi", "Buddha", ""},
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
AreVotersKnown: false,
|
||||
MaxVoters: 0,
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
},
|
||||
expectedCode: http.StatusUnprocessableEntity,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
@ -67,3 +67,16 @@ func GreaterThanOrEquals(value int, n int) bool {
|
||||
func After(value time.Time, n time.Time) bool {
|
||||
return value.After(n)
|
||||
}
|
||||
|
||||
func UniqueValues[T comparable](values []T) bool {
|
||||
seen := make(map[T]bool)
|
||||
|
||||
for _, value := range values {
|
||||
if seen[value] {
|
||||
return false
|
||||
}
|
||||
seen[value] = true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
Reference in New Issue
Block a user