add more scenarios to TestLoadOD, add one more test - TestLoadPlayers
This commit is contained in:
parent
f30ef110da
commit
25c3a4f2bc
|
@ -33,6 +33,7 @@ type handlers struct {
|
||||||
killAllTribe http.HandlerFunc
|
killAllTribe http.HandlerFunc
|
||||||
killAttTribe http.HandlerFunc
|
killAttTribe http.HandlerFunc
|
||||||
killDefTribe http.HandlerFunc
|
killDefTribe http.HandlerFunc
|
||||||
|
getPlayers http.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handlers) init() {
|
func (h *handlers) init() {
|
||||||
|
@ -63,6 +64,9 @@ func (h *handlers) init() {
|
||||||
if h.killDefTribe == nil {
|
if h.killDefTribe == nil {
|
||||||
h.killDefTribe = noop
|
h.killDefTribe = noop
|
||||||
}
|
}
|
||||||
|
if h.getPlayers == nil {
|
||||||
|
h.getPlayers = noop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareTestServer(h *handlers) *httptest.Server {
|
func prepareTestServer(h *handlers) *httptest.Server {
|
||||||
|
@ -72,6 +76,10 @@ func prepareTestServer(h *handlers) *httptest.Server {
|
||||||
h.init()
|
h.init()
|
||||||
|
|
||||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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 {
|
switch r.URL.Path {
|
||||||
case EndpointGetServers:
|
case EndpointGetServers:
|
||||||
h.getServers(w, r)
|
h.getServers(w, r)
|
||||||
|
@ -97,6 +105,9 @@ func prepareTestServer(h *handlers) *httptest.Server {
|
||||||
case EndpointKillDefTribe:
|
case EndpointKillDefTribe:
|
||||||
h.killDefTribe(w, r)
|
h.killDefTribe(w, r)
|
||||||
return
|
return
|
||||||
|
case EndpointPlayer:
|
||||||
|
h.getPlayers(w, r)
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,33 @@ func TestLoadOD(t *testing.T) {
|
||||||
expectedErrMsg: "invalid line format (should be rank,id,score)",
|
expectedErrMsg: "invalid line format (should be rank,id,score)",
|
||||||
tribe: true,
|
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",
|
respKillAll: "1,1,1\n2,2,2\n3,3,3",
|
||||||
respKillAtt: "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()
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue