Skip to content

Commit e3b2cfc

Browse files
authored
Add unit tests for Highscores API (#61)
1 parent 14c0680 commit e3b2cfc

File tree

3 files changed

+895
-32
lines changed

3 files changed

+895
-32
lines changed

src/TibiaHighscoresV3.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ import (
1010
"github.com/gin-gonic/gin"
1111
)
1212

13+
// Child of Highscores
14+
type Highscore struct {
15+
Rank int `json:"rank"` // Rank column
16+
Name string `json:"name"` // Name column
17+
Vocation string `json:"vocation"` // Vocation column
18+
World string `json:"world"` // World column
19+
Level int `json:"level"` // Level column
20+
Value int `json:"value"` // Points/SkillLevel column
21+
Title string `json:"title,omitempty"` // Title column (when category: loyalty)
22+
}
23+
24+
// Child of JSONData
25+
type Highscores struct {
26+
World string `json:"world"`
27+
Category string `json:"category"`
28+
Vocation string `json:"vocation"`
29+
HighscoreAge int `json:"highscore_age"`
30+
HighscoreList []Highscore `json:"highscore_list"`
31+
}
32+
33+
//
34+
// The base includes two levels: Highscores and Information
35+
type HighscoresResponse struct {
36+
Highscores Highscores `json:"highscores"`
37+
Information Information `json:"information"`
38+
}
39+
1340
var (
1441
HighscoresAgeRegex = regexp.MustCompile(`.*<div class="Text">Highscores.*Last Update: ([0-9]+) minutes ago.*`)
1542
SevenColumnRegex = regexp.MustCompile(`<td>.*<\/td><td.*">(.*)<\/a><\/td><td.*>(.*)<\/td><td.*>(.*)<\/td><td>(.*)<\/td><td.*>(.*)<\/td><td.*>(.*)<\/td>`)
@@ -18,7 +45,6 @@ var (
1845

1946
// TibiaHighscoresV3 func
2047
func TibiaHighscoresV3(c *gin.Context) {
21-
2248
// getting params from URL
2349
world := c.Param("world")
2450
category := c.Param("category")
@@ -27,33 +53,6 @@ func TibiaHighscoresV3(c *gin.Context) {
2753
// do some validation of category and vocation
2854
// maybe return error on faulty value?!
2955

30-
// Child of Highscores
31-
type Highscore struct {
32-
Rank int `json:"rank"` // Rank column
33-
Name string `json:"name"` // Name column
34-
Vocation string `json:"vocation"` // Vocation column
35-
World string `json:"world"` // World column
36-
Level int `json:"level"` // Level column
37-
Value int `json:"value"` // Points/SkillLevel column
38-
Title string `json:"title,omitempty"` // Title column (when category: loyalty)
39-
}
40-
41-
// Child of JSONData
42-
type Highscores struct {
43-
World string `json:"world"`
44-
Category string `json:"category"`
45-
Vocation string `json:"vocation"`
46-
HighscoreAge int `json:"highscore_age"`
47-
HighscoreList []Highscore `json:"highscore_list"`
48-
}
49-
50-
//
51-
// The base includes two levels: Highscores and Information
52-
type JSONData struct {
53-
Highscores Highscores `json:"highscores"`
54-
Information Information `json:"information"`
55-
}
56-
5756
// Adding fix for First letter to be upper and rest lower
5857
if strings.EqualFold(world, "all") {
5958
world = ""
@@ -125,6 +124,13 @@ func TibiaHighscoresV3(c *gin.Context) {
125124
return
126125
}
127126

127+
jsonData := TibiaHighscoresV3Impl(world, category, vocationName, BoxContentHTML)
128+
129+
// return jsonData
130+
TibiaDataAPIHandleSuccessResponse(c, "TibiaHighscoresV3", jsonData)
131+
}
132+
133+
func TibiaHighscoresV3Impl(world string, category string, vocationName string, BoxContentHTML string) HighscoresResponse {
128134
// Loading HTML data into ReaderHTML for goquery with NewReader
129135
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
130136
if err != nil {
@@ -226,7 +232,7 @@ func TibiaHighscoresV3(c *gin.Context) {
226232

227233
//
228234
// Build the data-blob
229-
jsonData := JSONData{
235+
return HighscoresResponse{
230236
Highscores{
231237
World: strings.Title(strings.ToLower(world)),
232238
Category: category,
@@ -239,7 +245,4 @@ func TibiaHighscoresV3(c *gin.Context) {
239245
Timestamp: TibiadataDatetimeV3(""),
240246
},
241247
}
242-
243-
// return jsonData
244-
TibiaDataAPIHandleSuccessResponse(c, "TibiaHighscoresV3", jsonData)
245248
}

src/TibiaHighscoresV3_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestHighscoresAll(t *testing.T) {
11+
data, err := ioutil.ReadFile("../testdata/highscores/all.html")
12+
if err != nil {
13+
t.Errorf("File reading error: %s", err)
14+
return
15+
}
16+
17+
highscoresJson := TibiaHighscoresV3Impl("", "experience", "all", string(data))
18+
assert := assert.New(t)
19+
20+
assert.Equal("", highscoresJson.Highscores.World)
21+
assert.Equal("experience", highscoresJson.Highscores.Category)
22+
assert.Equal("all", highscoresJson.Highscores.Vocation)
23+
assert.Equal(30, highscoresJson.Highscores.HighscoreAge)
24+
25+
assert.Equal(50, len(highscoresJson.Highscores.HighscoreList))
26+
27+
firstHighscore := highscoresJson.Highscores.HighscoreList[0]
28+
assert.Equal(1, firstHighscore.Rank)
29+
assert.Equal("Bobeek", firstHighscore.Name)
30+
assert.Equal("Elder Druid", firstHighscore.Vocation)
31+
assert.Equal("Bona", firstHighscore.World)
32+
assert.Equal(2015, firstHighscore.Level)
33+
assert.Equal(136026206904, firstHighscore.Value)
34+
assert.Equal("", firstHighscore.Title)
35+
36+
lastHighscore := highscoresJson.Highscores.HighscoreList[49]
37+
assert.Equal(50, lastHighscore.Rank)
38+
assert.Equal("Kewhyx Mythus", lastHighscore.Name)
39+
assert.Equal("Royal Paladin", lastHighscore.Vocation)
40+
assert.Equal("Celebra", lastHighscore.World)
41+
assert.Equal(1575, lastHighscore.Level)
42+
assert.Equal(64869293274, lastHighscore.Value)
43+
assert.Equal("", lastHighscore.Title)
44+
}

0 commit comments

Comments
 (0)