Add integration tests scaffold

This commit is contained in:
Andrea Fazzi 2022-05-26 13:58:17 +02:00
parent bdfbb7ddea
commit 9da2de8232
3 changed files with 51 additions and 12 deletions

View file

@ -23,7 +23,6 @@ func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollec
router := http.NewServeMux() router := http.NewServeMux()
router.Handle("/questions", logger.WithLogging(http.HandlerFunc(ps.questionsHandler))) router.Handle("/questions", logger.WithLogging(http.HandlerFunc(ps.questionsHandler)))
// router.Handle("/league", http.HandlerFunc(ps.leagueHandler))
ps.Handler = router ps.Handler = router
@ -31,8 +30,6 @@ func NewTestHubCollectorServer(store store.TestHubCollectorStore) *TestHubCollec
} }
func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) { func (ps *TestHubCollectorServer) questionsHandler(w http.ResponseWriter, r *http.Request) {
// player := strings.TrimPrefix(r.URL.Path, "/questions")
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
w.Header().Set("content-type", jsonContentType) w.Header().Set("content-type", jsonContentType)

View file

@ -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)
}

View file

@ -15,30 +15,27 @@ import (
"github.com/remogatto/prettytest" "github.com/remogatto/prettytest"
) )
// Start of setup
type testSuite struct { type testSuite struct {
prettytest.Suite prettytest.Suite
} }
type StubTestHubCollectorStore struct{} type StubTestHubCollectorStore struct {
questions []*models.Question
}
func (store *StubTestHubCollectorStore) CreateQuestion(question *models.Question) *models.Question { func (store *StubTestHubCollectorStore) CreateQuestion(question *models.Question) *models.Question {
return nil return nil
} }
func (store *StubTestHubCollectorStore) GetQuestions() []*models.Question { func (store *StubTestHubCollectorStore) GetQuestions() []*models.Question {
return []*models.Question{ return store.questions
{
Text: "Domanda 1",
AnswerIDs: []uuid.UUID{{}},
},
}
} }
func TestRunner(t *testing.T) { func TestRunner(t *testing.T) {
prettytest.Run( prettytest.Run(
t, t,
new(testSuite), 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) server := NewTestHubCollectorServer(store)