diff --git a/cmd/web/handlers_test.go b/cmd/web/handlers_test.go index 37ec9bf..c7cc86a 100644 --- a/cmd/web/handlers_test.go +++ b/cmd/web/handlers_test.go @@ -4,7 +4,9 @@ import ( "bytes" "code.dlmw.ch/dlmw/qv/internal" "encoding/json" + "fmt" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "net/http" "testing" "time" @@ -15,6 +17,11 @@ func TestCreateElection(t *testing.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) + path := "/election" tests := []struct { @@ -180,3 +187,32 @@ func TestCreateElection(t *testing.T) { }) } } + +func TestCreateElection_ServerError(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(0, fmt.Errorf("")) + + path := "/election" + requestBody := api.CreateElectionRequest{ + Choices: []string{"宮本武蔵", "伊東一刀斎"}, + ExpiresAt: time.Now().Add(24 * time.Hour), + AreVotersKnown: false, + MaxVoters: 0, + Name: "強", + Tokens: 100, + } + requestBodyJson, err := json.Marshal(requestBody) + if err != nil { + t.Fatal(err) + } + + code, _, _ := server.post(t, path, bytes.NewReader(requestBodyJson)) + + assert.Equal(t, 500, code) +} diff --git a/cmd/web/testutils_test.go b/cmd/web/testutils_test.go index b5aebb4..d197f10 100644 --- a/cmd/web/testutils_test.go +++ b/cmd/web/testutils_test.go @@ -2,6 +2,7 @@ package main import ( "code.dlmw.ch/dlmw/qv/internal/models" + "github.com/stretchr/testify/mock" "io" "log/slog" "net/http" @@ -44,10 +45,12 @@ func (ts *testServer) post(t *testing.T, urlPath string, body io.Reader) (int, h } type mockElectionModel struct { + mock.Mock } func (e *mockElectionModel) Insert(name string, tokens int, areVotersKnown bool, maxVoters int, choices []string, expiresAt time.Time) (int, error) { - return 1, nil + args := e.Called(name, tokens, areVotersKnown, maxVoters, choices, expiresAt) + return args.Int(0), args.Error(1) } func (e *mockElectionModel) GetById(id int) (*models.Election, error) { @@ -78,10 +81,12 @@ func (v *mockVoterModel) Exists(voterIdentity string, electionID int) (bool, err } type mockVoteModel struct { + mock.Mock } func (v *mockVoteModel) Insert(voterIdentity string, electionId int, choiceText string, tokens int) (int, error) { - return 1, nil + args := v.Called(voterIdentity, electionId, choiceText, tokens) + return args.Int(0), args.Error(1) } func (v *mockVoteModel) Exists(voterIdentity string, electionID int) (bool, error) { diff --git a/go.mod b/go.mod index 6b0dfbe..25643b3 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/tidwall/pretty v1.2.0 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect go.uber.org/atomic v1.7.0 // indirect diff --git a/go.sum b/go.sum index d456a44..3bfa790 100644 --- a/go.sum +++ b/go.sum @@ -74,6 +74,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=