Skip to content

Commit d98c0cd

Browse files
authored
Add unit tests for Guild Overview and Guild API (#58)
1 parent 39c0a6d commit d98c0cd

File tree

7 files changed

+2170
-144
lines changed

7 files changed

+2170
-144
lines changed

src/TibiaCharactersCharacterV3.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@ type Houses struct {
2020
}
2121

2222
// Child of Character
23-
type Guild struct {
23+
type CharacterGuild struct {
2424
GuildName string `json:"name,omitempty"`
2525
Rank string `json:"rank,omitempty"`
2626
}
2727

2828
// Child of Characters
2929
type Character struct {
30-
Name string `json:"name"`
31-
FormerNames []string `json:"former_names,omitempty"`
32-
Traded bool `json:"traded,omitempty"`
33-
DeletionDate string `json:"deletion_date,omitempty"`
34-
Sex string `json:"sex"`
35-
Title string `json:"title"`
36-
UnlockedTitles int `json:"unlocked_titles"`
37-
Vocation string `json:"vocation"`
38-
Level int `json:"level"`
39-
AchievementPoints int `json:"achievement_points"`
40-
World string `json:"world"`
41-
FormerWorlds []string `json:"former_worlds,omitempty"`
42-
Residence string `json:"residence"`
43-
MarriedTo string `json:"married_to,omitempty"`
44-
Houses []Houses `json:"houses,omitempty"`
45-
Guild Guild `json:"guild"`
46-
LastLogin string `json:"last_login,omitempty"`
47-
AccountStatus string `json:"account_status"`
48-
Comment string `json:"comment,omitempty"`
30+
Name string `json:"name"`
31+
FormerNames []string `json:"former_names,omitempty"`
32+
Traded bool `json:"traded,omitempty"`
33+
DeletionDate string `json:"deletion_date,omitempty"`
34+
Sex string `json:"sex"`
35+
Title string `json:"title"`
36+
UnlockedTitles int `json:"unlocked_titles"`
37+
Vocation string `json:"vocation"`
38+
Level int `json:"level"`
39+
AchievementPoints int `json:"achievement_points"`
40+
World string `json:"world"`
41+
FormerWorlds []string `json:"former_worlds,omitempty"`
42+
Residence string `json:"residence"`
43+
MarriedTo string `json:"married_to,omitempty"`
44+
Houses []Houses `json:"houses,omitempty"`
45+
Guild CharacterGuild `json:"guild"`
46+
LastLogin string `json:"last_login,omitempty"`
47+
AccountStatus string `json:"account_status"`
48+
Comment string `json:"comment,omitempty"`
4949
}
5050

5151
// Child of Characters

src/TibiaGuildsGuildV3.go

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

13+
// Child of Guild
14+
type Guildhall struct {
15+
Name string `json:"name"`
16+
World string `json:"world"` // Maybe duplicate info? Guild can only be on one world..
17+
/*
18+
Town string `json:"town"` // We can collect that from cached info?
19+
Status string `json:"status"` // rented (but maybe also auctioned)
20+
Owner string `json:"owner"` // We can collect that from cached info?
21+
HouseID int `json:"houseid"` // We can collect that from cached info?
22+
*/
23+
PaidUntil string `json:"paid_until"` // Paid until date
24+
}
25+
26+
// Child of Guild
27+
type GuildMember struct {
28+
Name string `json:"name"`
29+
Title string `json:"title"`
30+
Rank string `json:"rank"`
31+
Vocation string `json:"vocation"`
32+
Level int `json:"level"`
33+
Joined string `json:"joined"`
34+
Status string `json:"status"`
35+
}
36+
37+
// Child of Guild
38+
type InvitedGuildMember struct {
39+
Name string `json:"name"`
40+
Date string `json:"date"`
41+
}
42+
43+
// Child of Guilds
44+
type Guild struct {
45+
Name string `json:"name"`
46+
World string `json:"world"`
47+
LogoURL string `json:"logo_url"`
48+
Description string `json:"description"`
49+
Guildhalls []Guildhall `json:"guildhalls"`
50+
Active bool `json:"active"`
51+
Founded string `json:"founded"`
52+
Applications bool `json:"open_applications"`
53+
Homepage string `json:"homepage"`
54+
InWar bool `json:"in_war"`
55+
DisbandedDate string `json:"disband_date"`
56+
DisbandedCondition string `json:"disband_condition"`
57+
PlayersOnline int `json:"players_online"`
58+
PlayersOffline int `json:"players_offline"`
59+
MembersTotal int `json:"members_total"`
60+
MembersInvited int `json:"members_invited"`
61+
Members []GuildMember `json:"members"`
62+
Invited []InvitedGuildMember `json:"invites"`
63+
}
64+
65+
// Child of JSONData
66+
type Guilds struct {
67+
Guild Guild `json:"guild"`
68+
}
69+
70+
//
71+
// The base includes two levels: Guild and Information
72+
type GuildResponse struct {
73+
Guilds Guilds `json:"guilds"`
74+
Information Information `json:"information"`
75+
}
76+
1377
var (
1478
GuildLogoRegex = regexp.MustCompile(`.*img src="(.*)" width=.*`)
1579
GuildWorldAndFoundationRegex = regexp.MustCompile(`The guild was founded on (.*) on (.*).<br/>`)
@@ -22,99 +86,44 @@ var (
2286

2387
// TibiaGuildsGuildV3 func
2488
func TibiaGuildsGuildV3(c *gin.Context) {
25-
2689
// getting params from URL
2790
guild := c.Param("guild")
2891

29-
// Child of Guild
30-
type Guildhall struct {
31-
Name string `json:"name"`
32-
World string `json:"world"` // Maybe duplicate info? Guild can only be on one world..
33-
/*
34-
Town string `json:"town"` // We can collect that from cached info?
35-
Status string `json:"status"` // rented (but maybe also auctioned)
36-
Owner string `json:"owner"` // We can collect that from cached info?
37-
HouseID int `json:"houseid"` // We can collect that from cached info?
38-
*/
39-
PaidUntil string `json:"paid_until"` // Paid until date
40-
}
41-
42-
// Child of Guild
43-
type Members struct {
44-
Name string `json:"name"`
45-
Title string `json:"title"`
46-
Rank string `json:"rank"`
47-
Vocation string `json:"vocation"`
48-
Level int `json:"level"`
49-
Joined string `json:"joined"`
50-
Status string `json:"status"`
51-
}
52-
// Child of Guild
53-
type Invited struct {
54-
Name string `json:"name"`
55-
Date string `json:"date"`
56-
}
92+
// Getting data with TibiadataHTMLDataCollectorV3
93+
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=" + TibiadataQueryEscapeStringV3(guild)
94+
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)
5795

58-
// Child of Guilds
59-
type Guild struct {
60-
Name string `json:"name"`
61-
World string `json:"world"`
62-
LogoURL string `json:"logo_url"`
63-
Description string `json:"description"`
64-
Guildhalls []Guildhall `json:"guildhalls"`
65-
Active bool `json:"active"`
66-
Founded string `json:"founded"`
67-
Applications bool `json:"open_applications"`
68-
Homepage string `json:"homepage"`
69-
InWar bool `json:"in_war"`
70-
DisbandedDate string `json:"disband_date"`
71-
DisbandedCondition string `json:"disband_condition"`
72-
PlayersOnline int `json:"players_online"`
73-
PlayersOffline int `json:"players_offline"`
74-
MembersTotal int `json:"members_total"`
75-
MembersInvited int `json:"members_invited"`
76-
Members []Members `json:"members"`
77-
Invited []Invited `json:"invites"`
96+
// return error (e.g. for maintenance mode)
97+
if err != nil {
98+
TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaGuildsGuildV3", gin.H{"error": err.Error()})
99+
return
78100
}
79101

80-
// Child of JSONData
81-
type Guilds struct {
82-
Guild Guild `json:"guild"`
83-
}
102+
jsonData := TibiaGuildsGuildV3Impl(guild, BoxContentHTML)
84103

85-
//
86-
// The base includes two levels: Guild and Information
87-
type JSONData struct {
88-
Guilds Guilds `json:"guilds"`
89-
Information Information `json:"information"`
90-
}
104+
// return jsonData
105+
TibiaDataAPIHandleSuccessResponse(c, "TibiaGuildsGuildV3", jsonData)
106+
}
91107

108+
func TibiaGuildsGuildV3Impl(guild string, BoxContentHTML string) GuildResponse {
92109
// Creating empty vars
93110
var (
94-
MembersData []Members
95-
InvitedData []Invited
111+
MembersData []GuildMember
112+
InvitedData []InvitedGuildMember
96113
GuildGuildhallData []Guildhall
97114
MembersRank, MembersTitle, MembersStatus, GuildDescription, GuildDisbandedDate, GuildDisbandedCondition, GuildHomepage, GuildWorld, GuildLogoURL, GuildFounded string
98-
GuildActive, GuildApplications, GuildInWar, GuildNameDetected, GuildDescriptionFinished bool
115+
GuildActive, GuildApplications, GuildInWar, GuildDescriptionFinished bool
99116
MembersCountOnline, MembersCountOffline, MembersCountInvited int
100117
)
101118

102-
// Getting data with TibiadataHTMLDataCollectorV3
103-
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=" + TibiadataQueryEscapeStringV3(guild)
104-
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)
105-
106-
// return error (e.g. for maintenance mode)
107-
if err != nil {
108-
TibiaDataAPIHandleOtherResponse(c, http.StatusBadGateway, "TibiaGuildsGuildV3", gin.H{"error": err.Error()})
109-
return
110-
}
111-
112119
// Loading HTML data into ReaderHTML for goquery with NewReader
113120
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
114121
if err != nil {
115122
log.Fatal(err)
116123
}
117124

125+
guildName, _ := ReaderHTML.Find("h1").First().Html()
126+
118127
// Getting data from div.InnerTableContainer and then first p
119128
InnerTableContainerTMPA, err := ReaderHTML.Find(".BoxContent table").Html()
120129
if err != nil {
@@ -130,13 +139,6 @@ func TibiaGuildsGuildV3(c *gin.Context) {
130139
}
131140

132141
for _, line := range strings.Split(strings.TrimSuffix(InnerTableContainerTMPB, "\n"), "\n") {
133-
134-
// setting guild name based on html
135-
if !GuildNameDetected {
136-
guild = strings.TrimSpace(RemoveHtmlTag(line))
137-
GuildNameDetected = true
138-
}
139-
140142
// Guild information
141143
if !GuildDescriptionFinished {
142144
// First line is the description..
@@ -199,7 +201,6 @@ func TibiaGuildsGuildV3(c *gin.Context) {
199201

200202
// Running query over each div
201203
ReaderHTML.Find(".TableContentContainer .TableContent tbody tr").Each(func(index int, s *goquery.Selection) {
202-
203204
// Storing HTML into GuildsDivHTML
204205
GuildsDivHTML, err := s.Html()
205206
if err != nil {
@@ -230,24 +231,22 @@ func TibiaGuildsGuildV3(c *gin.Context) {
230231
MembersCountOffline++
231232
}
232233

233-
MembersData = append(MembersData, Members{
234-
Name: TibiaDataSanitizeEscapedString(subma1[0][2]),
234+
MembersData = append(MembersData, GuildMember{
235+
Name: TibiaDataSanitizeNbspSpaceString(subma1[0][2]),
235236
Title: MembersTitle,
236237
Rank: MembersRank,
237238
Vocation: subma1[0][4],
238239
Level: TibiadataStringToIntegerV3(subma1[0][5]),
239240
Joined: TibiadataDateV3(subma1[0][6]),
240241
Status: MembersStatus,
241242
})
242-
243243
} else {
244-
245244
// Regex to get data for record values
246245
subma2 := GuildMemberInvitesInformationRegex.FindAllStringSubmatch(GuildsDivHTML, -1)
247246

248247
if len(subma2) > 0 {
249248
MembersCountInvited++
250-
InvitedData = append(InvitedData, Invited{
249+
InvitedData = append(InvitedData, InvitedGuildMember{
251250
Name: subma2[0][1],
252251
Date: subma2[0][2],
253252
})
@@ -257,10 +256,10 @@ func TibiaGuildsGuildV3(c *gin.Context) {
257256

258257
//
259258
// Build the data-blob
260-
jsonData := JSONData{
259+
return GuildResponse{
261260
Guilds{
262261
Guild{
263-
Name: guild,
262+
Name: guildName,
264263
World: GuildWorld,
265264
LogoURL: GuildLogoURL,
266265
Description: GuildDescription,
@@ -286,7 +285,4 @@ func TibiaGuildsGuildV3(c *gin.Context) {
286285
Timestamp: TibiadataDatetimeV3(""),
287286
},
288287
}
289-
290-
// return jsonData
291-
TibiaDataAPIHandleSuccessResponse(c, "TibiaGuildsGuildV3", jsonData)
292288
}

src/TibiaGuildsGuildV3_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestOrderofGlory(t *testing.T) {
11+
data, err := ioutil.ReadFile("../testdata/guilds/guild/Order of Glory.html")
12+
if err != nil {
13+
t.Errorf("File reading error: %s", err)
14+
return
15+
}
16+
17+
orderOfGloryJson := TibiaGuildsGuildV3Impl("Order of Glory", string(data))
18+
assert := assert.New(t)
19+
20+
assert.Equal("Order of Glory", orderOfGloryJson.Guilds.Guild.Name)
21+
assert.Equal("Premia", orderOfGloryJson.Guilds.Guild.World)
22+
assert.Equal("https://static.tibia.com/images/guildlogos/Order_of_Glory.gif", orderOfGloryJson.Guilds.Guild.LogoURL)
23+
assert.Equal("We are an English speaking guild of friends and allies from around the world who seek only peaceful questing, exploring, team hunts and a chill place to hang out. Message any of our leaders for an invitation. Contact Zyb with any problems.", orderOfGloryJson.Guilds.Guild.Description)
24+
assert.Nil(orderOfGloryJson.Guilds.Guild.Guildhalls)
25+
assert.True(orderOfGloryJson.Guilds.Guild.Active)
26+
assert.Equal("2020-06-27", orderOfGloryJson.Guilds.Guild.Founded)
27+
assert.True(orderOfGloryJson.Guilds.Guild.Applications)
28+
assert.Equal("", orderOfGloryJson.Guilds.Guild.Homepage)
29+
assert.False(orderOfGloryJson.Guilds.Guild.InWar)
30+
assert.Equal("", orderOfGloryJson.Guilds.Guild.DisbandedDate)
31+
assert.Equal("", orderOfGloryJson.Guilds.Guild.DisbandedCondition)
32+
assert.Equal(1, orderOfGloryJson.Guilds.Guild.PlayersOnline)
33+
assert.Equal(32, orderOfGloryJson.Guilds.Guild.PlayersOffline)
34+
assert.Equal(33, orderOfGloryJson.Guilds.Guild.MembersTotal)
35+
assert.Equal(0, orderOfGloryJson.Guilds.Guild.MembersInvited)
36+
assert.Equal(33, len(orderOfGloryJson.Guilds.Guild.Members))
37+
38+
guildLeader := orderOfGloryJson.Guilds.Guild.Members[0]
39+
assert.Equal("Zyb the Warrior", guildLeader.Name)
40+
assert.Equal("", guildLeader.Title)
41+
assert.Equal("Leader", guildLeader.Rank)
42+
assert.Equal("Elite Knight", guildLeader.Vocation)
43+
assert.Equal(385, guildLeader.Level)
44+
assert.Equal("2020-10-13", guildLeader.Joined)
45+
assert.Equal("online", guildLeader.Status)
46+
47+
assert.Nil(orderOfGloryJson.Guilds.Guild.Invited)
48+
}

0 commit comments

Comments
 (0)