diff --git a/tw/twdataloader/helpers.go b/tw/twdataloader/helpers.go index bcfb10f..f650940 100644 --- a/tw/twdataloader/helpers.go +++ b/tw/twdataloader/helpers.go @@ -33,6 +33,7 @@ type handlers struct { killAllTribe http.HandlerFunc killAttTribe http.HandlerFunc killDefTribe http.HandlerFunc + getPlayers http.HandlerFunc } func (h *handlers) init() { @@ -63,6 +64,9 @@ func (h *handlers) init() { if h.killDefTribe == nil { h.killDefTribe = noop } + if h.getPlayers == nil { + h.getPlayers = noop + } } func prepareTestServer(h *handlers) *httptest.Server { @@ -72,6 +76,10 @@ func prepareTestServer(h *handlers) *httptest.Server { h.init() return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusNotFound) + return + } switch r.URL.Path { case EndpointGetServers: h.getServers(w, r) @@ -97,6 +105,9 @@ func prepareTestServer(h *handlers) *httptest.Server { case EndpointKillDefTribe: h.killDefTribe(w, r) return + case EndpointPlayer: + h.getPlayers(w, r) + return default: w.WriteHeader(http.StatusNotFound) } diff --git a/tw/twdataloader/server_data_loader_test.go b/tw/twdataloader/server_data_loader_test.go index 7e9227a..2d62786 100644 --- a/tw/twdataloader/server_data_loader_test.go +++ b/tw/twdataloader/server_data_loader_test.go @@ -45,6 +45,33 @@ func TestLoadOD(t *testing.T) { expectedErrMsg: "invalid line format (should be rank,id,score)", tribe: true, }, + { + respKillAll: "1,1,asd", + expectedErrMsg: "parsedODLine.Score: strconv.Atoi: parsing \"asd\"", + }, + { + respKillAll: "1,asd,1", + expectedErrMsg: "parsedODLine.ID: strconv.Atoi: parsing \"asd\":", + }, + { + respKillAll: "asd,1,1", + expectedErrMsg: "parsedODLine.Rank: strconv.Atoi: parsing \"asd\":", + }, + { + respKillAllTribe: "1,1,asd", + expectedErrMsg: "parsedODLine.Score: strconv.Atoi: parsing \"asd\"", + tribe: true, + }, + { + respKillAllTribe: "1,asd,1", + expectedErrMsg: "parsedODLine.ID: strconv.Atoi: parsing \"asd\":", + tribe: true, + }, + { + respKillAllTribe: "asd,1,1", + expectedErrMsg: "parsedODLine.Rank: strconv.Atoi: parsing \"asd\":", + tribe: true, + }, { respKillAll: "1,1,1\n2,2,2\n3,3,3", respKillAtt: "1,1,1\n2,2,2\n3,3,3", @@ -154,3 +181,61 @@ func TestLoadOD(t *testing.T) { ts.Close() } } + +func TestLoadPlayers(t *testing.T) { + type scenario struct { + resp string + expectedResult []*twmodel.Player + expectedErrMsg string + } + + scenarios := []scenario{ + { + resp: "1,1,1,1", + expectedErrMsg: "invalid line format (should be id,name,tribeid,villages,points,rank)", + }, + { + resp: "1,name,1,500,500", + expectedErrMsg: "invalid line format (should be id,name,tribeid,villages,points,rank)", + }, + } + + for _, scenario := range scenarios { + ts := prepareTestServer(&handlers{ + getPlayers: createWriteCompressedStringHandler(scenario.resp), + }) + + dl := NewServerDataLoader(&ServerDataLoaderConfig{ + BaseURL: ts.URL, + Client: ts.Client(), + }) + + res, err := dl.LoadPlayers() + if scenario.expectedErrMsg != "" { + assert.NotNil(t, err) + assert.Contains(t, err.Error(), scenario.expectedErrMsg) + } else { + assert.Nil(t, err) + } + + if scenario.expectedResult != nil { + assert.Len(t, res, len(scenario.expectedResult)) + for _, singleResult := range res { + found := false + var player *twmodel.Player + for _, expected := range scenario.expectedResult { + if expected.ID == singleResult.ID { + found = true + player = expected + break + } + } + assert.True(t, found) + assert.NotNil(t, player) + assert.EqualValues(t, player, singleResult) + } + } + + ts.Close() + } +}