From b09c8fae85f3ccbfedebc1d10a6a64d4d4ee9736 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 26 May 2022 07:59:55 +0200 Subject: [PATCH] Add Question model --- go.mod | 1 + go.sum | 2 ++ main_test.go | 84 ++++++++++------------------------------------ models/question.go | 17 ++++++++++ store/store.go | 7 ++-- 5 files changed, 41 insertions(+), 70 deletions(-) create mode 100644 models/question.go diff --git a/go.mod b/go.mod index d81f395..90b56a7 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 84250d7..8e63524 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main_test.go b/main_test.go index c4ed684..1f6c894 100644 --- a/main_test.go +++ b/main_test.go @@ -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{ diff --git a/models/question.go b/models/question.go new file mode 100644 index 0000000..4f78f36 --- /dev/null +++ b/models/question.go @@ -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 +} diff --git a/store/store.go b/store/store.go index d3f254b..46c31c9 100644 --- a/store/store.go +++ b/store/store.go @@ -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 }