Add Question model

This commit is contained in:
Andrea Fazzi 2022-05-26 07:59:55 +02:00
parent 5915efbeb3
commit b09c8fae85
5 changed files with 41 additions and 70 deletions

1
go.mod
View file

@ -3,6 +3,7 @@ module git.andreafazzi.eu/andrea/testhub
go 1.17
require (
github.com/google/uuid v1.3.0 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/remogatto/prettytest v0.0.0-20200211072524-6d385e11dcb8 // indirect

2
go.sum
View file

@ -1,3 +1,5 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

View file

@ -18,22 +18,24 @@ type testSuite struct {
prettytest.Suite
}
type StubPlayerStore struct {
scores map[string]int
winCalls []string
league []models.Player
type StubQuestionsStore struct {
questions map[uint]*models.Question
answers map[uint]*models.Answer
lastQuestionID uint
lastAnswerID uint
}
func (store *StubPlayerStore) GetPlayerScore(player string) int {
return store.scores[player]
func (store *StubQuestionsStore) CreateQuestion(question *models.Question) *models.Question {
id := store.lastQuestionID++
for _, answer := range question.Answers {
}
}
func (s *StubPlayerStore) RecordWin(name string) {
s.winCalls = append(s.winCalls, name)
}
func (store *StubPlayerStore) GetLeague() []models.Player {
return store.league
func (store *StubQuestionsStore) GetQuestions() []*models.Question {
return store.questions
}
func TestRunner(t *testing.T) {
@ -43,60 +45,10 @@ func TestRunner(t *testing.T) {
)
}
func (t *testSuite) TestGETPlayers() {
tests := []struct {
name string
expectedScore string
expectedHTTPStatus int
}{
{"Pepper", "20", http.StatusOK},
{"Floyd", "10", http.StatusOK},
{"Apollo", "0", http.StatusNotFound},
}
store := &StubPlayerStore{
map[string]int{
"Pepper": 20,
"Floyd": 10,
},
nil,
[]models.Player{},
}
server := NewPlayerServer(store)
for _, test := range tests {
request, _ := http.NewRequest(http.MethodGet, "/players/"+test.name, nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
t.Equal(test.expectedScore, response.Body.String())
t.Equal(test.expectedHTTPStatus, response.Code)
}
}
func (t *testSuite) TestPOSTPlayers() {
store := &StubPlayerStore{
map[string]int{},
nil,
[]models.Player{},
}
server := NewPlayerServer(store)
request, _ := http.NewRequest(http.MethodPost, "/players/Pepper", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
t.Equal(1, len(store.winCalls))
t.Equal(http.StatusAccepted, response.Code)
}
func (t *testSuite) TestGETLeague() {
expectedResult := []models.Player{
{Name: "Cleo", Wins: 20},
{Name: "Chris", Wins: 10},
func (t *testSuite) TestGETQuestions() {
expectedResult := []*models.Question{
&{Name: "Cleo", Wins: 20},
&{Name: "Chris", Wins: 10},
}
store := &StubPlayerStore{

17
models/question.go Normal file
View file

@ -0,0 +1,17 @@
package models
import (
"github.com/google/uuid"
)
type Question struct {
ID uuid.UUID
Text string
AnswerIDs []uuid.UUID
}
type Answers struct {
ID uuid.UUID
Text string
Correct bool
}

View file

@ -4,8 +4,7 @@ import (
"git.andreafazzi.eu/andrea/testhub/models"
)
type PlayerStore interface {
GetPlayerScore(player string) int
RecordWin(player string)
GetLeague() []models.Player
type QuestionsStore interface {
GetQuestions() []*models.Question
CreateQuestion(question *models.Question) *models.Question
}