2022-05-25 12:04:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-25 18:09:54 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-05-25 12:04:53 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-05-25 18:09:54 +02:00
|
|
|
"reflect"
|
2022-05-25 12:04:53 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-05-25 19:24:12 +02:00
|
|
|
"git.andreafazzi.eu/andrea/testhub/models"
|
2022-05-25 12:04:53 +02:00
|
|
|
"github.com/remogatto/prettytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Start of setup
|
|
|
|
type testSuite struct {
|
|
|
|
prettytest.Suite
|
|
|
|
}
|
|
|
|
|
2022-05-26 07:59:55 +02:00
|
|
|
type StubQuestionsStore struct {
|
|
|
|
questions map[uint]*models.Question
|
|
|
|
answers map[uint]*models.Answer
|
|
|
|
lastQuestionID uint
|
|
|
|
lastAnswerID uint
|
2022-05-25 12:04:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 07:59:55 +02:00
|
|
|
func (store *StubQuestionsStore) CreateQuestion(question *models.Question) *models.Question {
|
|
|
|
id := store.lastQuestionID++
|
|
|
|
|
|
|
|
for _, answer := range question.Answers {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:04:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 07:59:55 +02:00
|
|
|
func (store *StubQuestionsStore) GetQuestions() []*models.Question {
|
|
|
|
return store.questions
|
2022-05-25 18:09:54 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 12:04:53 +02:00
|
|
|
func TestRunner(t *testing.T) {
|
|
|
|
prettytest.Run(
|
|
|
|
t,
|
|
|
|
new(testSuite),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-26 07:59:55 +02:00
|
|
|
func (t *testSuite) TestGETQuestions() {
|
|
|
|
expectedResult := []*models.Question{
|
|
|
|
&{Name: "Cleo", Wins: 20},
|
|
|
|
&{Name: "Chris", Wins: 10},
|
2022-05-25 18:09:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:24:12 +02:00
|
|
|
func getLeagueFromResponse(body io.Reader) (league []models.Player) {
|
2022-05-25 18:09:54 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:24:12 +02:00
|
|
|
func leaguesAreEqual(got, want []models.Player) bool {
|
2022-05-25 18:09:54 +02:00
|
|
|
return reflect.DeepEqual(got, want)
|
|
|
|
}
|