From 9da2de8232df0c5452d689b2e8ae58b659e144c5 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 26 May 2022 13:58:17 +0200 Subject: [PATCH] Add integration tests scaffold --- server.go | 3 --- server_integration_test.go | 40 ++++++++++++++++++++++++++++++++++ main_test.go => server_test.go | 20 +++++++++-------- 3 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 server_integration_test.go rename main_test.go => server_test.go (89%) diff --git a/server.go b/server.go index d3fade1..102372b 100644 --- a/server.go +++ b/server.go @@ -23,7 +23,6 @@ func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollec router := http.NewServeMux() router.Handle("/questions", logger.WithLogging(http.HandlerFunc(ps.questionsHandler))) - // router.Handle("/league", http.HandlerFunc(ps.leagueHandler)) ps.Handler = router @@ -31,8 +30,6 @@ func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollec } func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) { - // player := strings.TrimPrefix(r.URL.Path, "/questions") - switch r.Method { case http.MethodGet: w.Header().Set("content-type", jsonContentType) diff --git a/server_integration_test.go b/server_integration_test.go new file mode 100644 index 0000000..ba19f3e --- /dev/null +++ b/server_integration_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "bytes" + "encoding/json" + "log" + "net/http" + "net/http/httptest" + + "git.andreafazzi.eu/andrea/testhub/models" + "git.andreafazzi.eu/andrea/testhub/store" + "github.com/remogatto/prettytest" +) + +type integrationTestSuite struct { + prettytest.Suite +} + +func (t *integrationTestSuite) TestPOSTQuestionAndGETQuestion() { + server := NewTestHubCollectorServer(store.NewMemoryTestHubCollectorStore()) + + // POST a new question using a JSON payload + + question := &models.Question{ + Text: "Question 1", + } + + payload, err := json.Marshal(question) + if err != nil { + panic(err) + } + + request, _ := http.NewRequest(http.MethodPost, "/questions", bytes.NewReader(payload)) + response := httptest.NewRecorder() + + log.Println(response.Body.String()) + + server.ServeHTTP(response, request) + +} diff --git a/main_test.go b/server_test.go similarity index 89% rename from main_test.go rename to server_test.go index add4e6b..22b111a 100644 --- a/main_test.go +++ b/server_test.go @@ -15,30 +15,27 @@ import ( "github.com/remogatto/prettytest" ) -// Start of setup type testSuite struct { prettytest.Suite } -type StubTestHubCollectorStore struct{} +type StubTestHubCollectorStore struct { + questions []*models.Question +} func (store *StubTestHubCollectorStore) CreateQuestion(question *models.Question) *models.Question { return nil } func (store *StubTestHubCollectorStore) GetQuestions() []*models.Question { - return []*models.Question{ - { - Text: "Domanda 1", - AnswerIDs: []uuid.UUID{{}}, - }, - } + return store.questions } func TestRunner(t *testing.T) { prettytest.Run( t, new(testSuite), + new(integrationTestSuite), ) } @@ -54,7 +51,12 @@ func (t *testSuite) TestGETQuestions() { }, } - store := &StubTestHubCollectorStore{} + store := &StubTestHubCollectorStore{[]*models.Question{ + { + Text: "Domanda 1", + AnswerIDs: []uuid.UUID{{}}, + }, + }} server := NewTestHubCollectorServer(store)