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
|
|
|
|
}
|
|
|
|
|
|
|
|
type StubPlayerStore struct {
|
|
|
|
scores map[string]int
|
|
|
|
winCalls []string
|
2022-05-25 19:24:12 +02:00
|
|
|
league []models.Player
|
2022-05-25 12:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *StubPlayerStore) GetPlayerScore(player string) int {
|
|
|
|
return store.scores[player]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StubPlayerStore) RecordWin(name string) {
|
|
|
|
s.winCalls = append(s.winCalls, name)
|
|
|
|
}
|
|
|
|
|
2022-05-25 19:24:12 +02:00
|
|
|
func (store *StubPlayerStore) GetLeague() []models.Player {
|
2022-05-25 18:09:54 +02:00
|
|
|
return store.league
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:04:53 +02:00
|
|
|
func TestRunner(t *testing.T) {
|
|
|
|
prettytest.Run(
|
|
|
|
t,
|
|
|
|
new(testSuite),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:09:54 +02:00
|
|
|
func (t *testSuite) TestGETPlayers() {
|
2022-05-25 12:04:53 +02:00
|
|
|
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,
|
2022-05-25 19:24:12 +02:00
|
|
|
[]models.Player{},
|
2022-05-25 12:04:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:09:54 +02:00
|
|
|
server := NewPlayerServer(store)
|
2022-05-25 12:04:53 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:09:54 +02:00
|
|
|
func (t *testSuite) TestPOSTPlayers() {
|
|
|
|
store := &StubPlayerStore{
|
2022-05-25 12:04:53 +02:00
|
|
|
map[string]int{},
|
|
|
|
nil,
|
2022-05-25 19:24:12 +02:00
|
|
|
[]models.Player{},
|
2022-05-25 12:04:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 18:09:54 +02:00
|
|
|
server := NewPlayerServer(store)
|
2022-05-25 12:04:53 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2022-05-25 18:09:54 +02:00
|
|
|
|
|
|
|
func (t *testSuite) TestGETLeague() {
|
2022-05-25 19:24:12 +02:00
|
|
|
expectedResult := []models.Player{
|
2022-05-25 18:09:54 +02:00
|
|
|
{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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|