Add some more tests and implement getElection
This commit is contained in:
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
api "code.dlmw.ch/dlmw/qv/internal"
|
||||
"code.dlmw.ch/dlmw/qv/internal/mappers"
|
||||
"code.dlmw.ch/dlmw/qv/internal/models"
|
||||
"code.dlmw.ch/dlmw/qv/internal/validator"
|
||||
"code.dlmw.ch/dlmw/qv/ui"
|
||||
@ -312,3 +313,23 @@ func getResultsFromVotes(votes *[]models.Vote) []api.VotesForChoice {
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
func (app *application) GetElection(w http.ResponseWriter, r *http.Request, id int) {
|
||||
election, err := app.elections.GetById(id)
|
||||
if err != nil {
|
||||
if errors.Is(sql.ErrNoRows, err) {
|
||||
app.clientError(w, http.StatusNotFound, fmt.Sprintf("couldn't find election with id %v", id))
|
||||
return
|
||||
}
|
||||
app.serverError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := json.Marshal(mappers.ElectionResponse(election))
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(response)
|
||||
}
|
||||
|
@ -192,6 +192,50 @@ func TestCreateElection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateElection_KnownVoters(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(1, nil)
|
||||
|
||||
mockVoters := app.voters.(*mockVoterModel)
|
||||
mockVoters.
|
||||
On("InsertMultiple", mock.Anything, mock.Anything).
|
||||
Return([]int{1}, nil)
|
||||
|
||||
path := baseUri + "election"
|
||||
requestBody := api.CreateElectionRequest{
|
||||
Choices: []string{"宮本武蔵", "伊東一刀斎"},
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
AreVotersKnown: true,
|
||||
MaxVoters: 100,
|
||||
Name: "強",
|
||||
Tokens: 100,
|
||||
}
|
||||
requestBodyJson, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
code, _, body := server.post(t, path, bytes.NewReader(requestBodyJson))
|
||||
|
||||
assert.Equal(t, http.StatusOK, code)
|
||||
|
||||
var voterIdentities struct {
|
||||
VoterIdentities []string
|
||||
}
|
||||
err = json.Unmarshal([]byte(body), &voterIdentities)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.Len(t, voterIdentities.VoterIdentities, 100)
|
||||
}
|
||||
|
||||
func TestCreateElection_ServerError(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
server := newTestServer(t, app.routes())
|
||||
|
@ -22,6 +22,38 @@ tags:
|
||||
description: Retrieve data related to votes
|
||||
|
||||
paths:
|
||||
/election/{id}:
|
||||
get:
|
||||
tags:
|
||||
- election
|
||||
summary: Get an election
|
||||
operationId: getElection
|
||||
parameters:
|
||||
- name: id
|
||||
in: path
|
||||
required: true
|
||||
description: The ID of the election
|
||||
schema:
|
||||
type: integer
|
||||
responses:
|
||||
200:
|
||||
description: Election returned
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Election"
|
||||
400:
|
||||
description: Request malformed
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/ErrorResponse"
|
||||
404:
|
||||
description: Election not found
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/ErrorResponse"
|
||||
/election:
|
||||
post:
|
||||
tags:
|
||||
@ -77,7 +109,7 @@ paths:
|
||||
schema:
|
||||
$ref: "#/components/schemas/CreateVotesRequest"
|
||||
responses:
|
||||
200:
|
||||
201:
|
||||
description: Votes cast
|
||||
400:
|
||||
description: Request malformed
|
||||
@ -145,6 +177,37 @@ paths:
|
||||
|
||||
components:
|
||||
schemas:
|
||||
Election:
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
- tokens
|
||||
- areVotersKnown
|
||||
- maxVoters
|
||||
- createdAt
|
||||
- expiresAt
|
||||
- choices
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
name:
|
||||
type: string
|
||||
tokens:
|
||||
type: integer
|
||||
areVotersKnown:
|
||||
type: boolean
|
||||
maxVoters:
|
||||
type: integer
|
||||
createdAt:
|
||||
type: string
|
||||
expiresAt:
|
||||
type: string
|
||||
choices:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
|
||||
CreateElectionRequest:
|
||||
type: object
|
||||
required:
|
||||
|
Reference in New Issue
Block a user