Insert election with uuid instead of auto-generated id
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"net/http"
|
||||
@ -21,10 +22,11 @@ func TestCreateElection(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
id, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(1, nil)
|
||||
Return(id.String(), nil)
|
||||
|
||||
path := baseUri + "election"
|
||||
|
||||
@ -197,10 +199,11 @@ func TestCreateElection_KnownVoters(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
electionId, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("Insert", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(1, nil)
|
||||
Return(electionId.String(), nil)
|
||||
|
||||
mockVoters := app.voters.(*mockVoterModel)
|
||||
mockVoters.
|
||||
@ -270,11 +273,12 @@ func TestCreateVotes_UnknownVotersElection(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
id, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("GetById", mock.Anything).
|
||||
Return(&models.Election{
|
||||
ID: 1,
|
||||
ID: id.String(),
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: false,
|
||||
@ -373,11 +377,12 @@ func TestCreateVotes_KnownVotersElection(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
id, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("GetById", mock.Anything).
|
||||
Return(&models.Election{
|
||||
ID: 1,
|
||||
ID: id.String(),
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: true,
|
||||
@ -534,7 +539,7 @@ func TestCreateVotes_NonExistingElection(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNotFound, code)
|
||||
}
|
||||
|
||||
func TestCreateVotes_NonNumberElectionID(t *testing.T) {
|
||||
func TestCreateVotes_NonUuidElectionID(t *testing.T) {
|
||||
app := newTestApplication(t)
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
@ -562,7 +567,7 @@ func TestCreateVotes_NonNumberElectionID(t *testing.T) {
|
||||
|
||||
code, _, _ := server.post(t, path, bytes.NewReader(requestBodyJson))
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, code)
|
||||
assert.Equal(t, http.StatusNotFound, code)
|
||||
}
|
||||
|
||||
func TestCreateVotes_AlreadyVoted(t *testing.T) {
|
||||
@ -570,8 +575,9 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
unknownVotersElectionId, _ := uuid.NewV7()
|
||||
unknownVotersElection := models.Election{
|
||||
ID: 1,
|
||||
ID: unknownVotersElectionId.String(),
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: false,
|
||||
@ -580,15 +586,18 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
|
||||
ExpiresAt: time.Now().Add(24 * time.Hour),
|
||||
Choices: []string{"Gandhi", "Buddha"},
|
||||
}
|
||||
|
||||
knownVotersElection := unknownVotersElection
|
||||
knownVotersElectionId, _ := uuid.NewV7()
|
||||
knownVotersElection.ID = knownVotersElectionId.String()
|
||||
knownVotersElection.AreVotersKnown = true
|
||||
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("GetById", 1).
|
||||
On("GetById", knownVotersElectionId.String()).
|
||||
Return(&knownVotersElection, nil)
|
||||
mockElections.
|
||||
On("GetById", 2).
|
||||
On("GetById", unknownVotersElectionId.String()).
|
||||
Return(&unknownVotersElection, nil)
|
||||
|
||||
mockVotes := app.votes.(*mockVoteModel)
|
||||
@ -601,8 +610,9 @@ func TestCreateVotes_AlreadyVoted(t *testing.T) {
|
||||
On("Exists", mock.Anything, mock.Anything).
|
||||
Return(true, nil)
|
||||
|
||||
knownVotersElectionPath := baseUri + "election/1/votes"
|
||||
unknownVotersElectionPath := baseUri + "election/2/votes"
|
||||
layout := baseUri + "election/%v/votes"
|
||||
knownVotersElectionPath := fmt.Sprintf(layout, unknownVotersElectionId)
|
||||
unknownVotersElectionPath := fmt.Sprintf(layout, knownVotersElectionId)
|
||||
voterIdentity := "anything"
|
||||
|
||||
tests := []struct {
|
||||
@ -661,11 +671,12 @@ func TestCreateVotes_UnknownVotersElectionMaxVotersReached(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
id, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("GetById", mock.Anything).
|
||||
Return(&models.Election{
|
||||
ID: 1,
|
||||
ID: id.String(),
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: false,
|
||||
@ -712,11 +723,12 @@ func TestCreateVotes_ExpiredElection(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
id, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("GetById", mock.Anything).
|
||||
Return(&models.Election{
|
||||
ID: 1,
|
||||
ID: id.String(),
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: false,
|
||||
@ -763,38 +775,39 @@ func TestGetElectionResults(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
electionID, _ := uuid.NewV7()
|
||||
votes := []models.Vote{
|
||||
{
|
||||
VoterIdentity: "Voter1",
|
||||
ElectionID: 1,
|
||||
ElectionID: electionID.String(),
|
||||
ChoiceText: "Choice1",
|
||||
Tokens: 2,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
VoterIdentity: "Voter2",
|
||||
ElectionID: 1,
|
||||
ElectionID: electionID.String(),
|
||||
ChoiceText: "Choice2",
|
||||
Tokens: 4,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
VoterIdentity: "Voter3",
|
||||
ElectionID: 1,
|
||||
ElectionID: electionID.String(),
|
||||
ChoiceText: "Choice3",
|
||||
Tokens: 6,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
VoterIdentity: "Voter4",
|
||||
ElectionID: 1,
|
||||
ElectionID: electionID.String(),
|
||||
ChoiceText: "Choice1",
|
||||
Tokens: 8,
|
||||
CreatedAt: time.Now(),
|
||||
},
|
||||
{
|
||||
VoterIdentity: "Voter5",
|
||||
ElectionID: 1,
|
||||
ElectionID: electionID.String(),
|
||||
ChoiceText: "Choice2",
|
||||
Tokens: 10,
|
||||
CreatedAt: time.Now(),
|
||||
@ -802,10 +815,10 @@ func TestGetElectionResults(t *testing.T) {
|
||||
}
|
||||
mockVotes := app.votes.(*mockVoteModel)
|
||||
mockVotes.
|
||||
On("GetByElection", mock.Anything).
|
||||
On("GetByElection", electionID.String()).
|
||||
Return(&votes, nil)
|
||||
|
||||
path := baseUri + "election/1/results"
|
||||
path := baseUri + fmt.Sprintf("election/%v/results", electionID)
|
||||
code, _, body := server.get(t, path)
|
||||
|
||||
assert.Equal(t, http.StatusOK, code)
|
||||
@ -857,11 +870,12 @@ func TestGetElection_Found(t *testing.T) {
|
||||
server := newTestServer(t, app.routes())
|
||||
defer server.Close()
|
||||
|
||||
id, _ := uuid.NewV7()
|
||||
mockElections := app.elections.(*mockElectionModel)
|
||||
mockElections.
|
||||
On("GetById", mock.Anything).
|
||||
Return(&models.Election{
|
||||
ID: 1,
|
||||
ID: id.String(),
|
||||
Name: "Guy of the year",
|
||||
Tokens: 100,
|
||||
AreVotersKnown: false,
|
||||
|
Reference in New Issue
Block a user