Add some more tests and implement getElection
This commit is contained in:
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