testhub/main_test.go
2022-05-26 07:59:55 +02:00

85 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"git.andreafazzi.eu/andrea/testhub/models"
"github.com/remogatto/prettytest"
)
// Start of setup
type testSuite struct {
prettytest.Suite
}
type StubQuestionsStore struct {
questions map[uint]*models.Question
answers map[uint]*models.Answer
lastQuestionID uint
lastAnswerID uint
}
func (store *StubQuestionsStore) CreateQuestion(question *models.Question) *models.Question {
id := store.lastQuestionID++
for _, answer := range question.Answers {
}
}
func (store *StubQuestionsStore) GetQuestions() []*models.Question {
return store.questions
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
func (t *testSuite) TestGETQuestions() {
expectedResult := []*models.Question{
&{Name: "Cleo", Wins: 20},
&{Name: "Chris", Wins: 10},
}
store := &StubPlayerStore{
nil,
nil,
expectedResult,
}
server := NewPlayerServer(store)
request, _ := http.NewRequest(http.MethodGet, "/league", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
result := getLeagueFromResponse(response.Body)
t.True(leaguesAreEqual(expectedResult, result))
t.Equal(http.StatusOK, response.Code)
}
func getLeagueFromResponse(body io.Reader) (league []models.Player) {
err := json.NewDecoder(body).Decode(&league)
if err != nil {
panic(fmt.Errorf("Unable to parse response from server %q into slice of Player, '%v'", body, err))
}
return
}
func leaguesAreEqual(got, want []models.Player) bool {
return reflect.DeepEqual(got, want)
}