Skip to content

Commit 6e40a6c

Browse files
authored
Add unit tests for News List API and News API (#60)
1 parent 029d33a commit 6e40a6c

File tree

8 files changed

+3427
-58
lines changed

8 files changed

+3427
-58
lines changed

src/TibiaNewsV3.go

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,31 @@ import (
1111
"github.com/gin-gonic/gin"
1212
)
1313

14+
// Child of JSONData
15+
type News struct {
16+
ID int `json:"id"`
17+
Date string `json:"date"`
18+
Title string `json:"title,omitempty"`
19+
Category string `json:"category"`
20+
Type string `json:"type,omitempty"`
21+
TibiaURL string `json:"url"`
22+
Content string `json:"content"`
23+
ContentHTML string `json:"content_html"`
24+
}
25+
26+
//
27+
// The base
28+
type NewsResponse struct {
29+
News News `json:"news"`
30+
Information Information `json:"information"`
31+
}
32+
1433
var (
1534
martelRegex = regexp.MustCompile(`<img src=\"https:\/\/static\.tibia\.com\/images\/global\/letters\/letter_martel_(.)\.gif\" ([^\/>]+..)`)
1635
)
1736

1837
// TibiaNewsV3 func
1938
func TibiaNewsV3(c *gin.Context) {
20-
2139
// getting params from URL
2240
NewsID := TibiadataStringToIntegerV3(c.Param("news_id"))
2341

@@ -27,32 +45,6 @@ func TibiaNewsV3(c *gin.Context) {
2745
return
2846
}
2947

30-
// Child of JSONData
31-
type News struct {
32-
ID int `json:"id"`
33-
Date string `json:"date"`
34-
Title string `json:"title,omitempty"`
35-
Category string `json:"category"`
36-
Type string `json:"type,omitempty"`
37-
TibiaURL string `json:"url"`
38-
Content string `json:"content"`
39-
ContentHTML string `json:"content_html"`
40-
}
41-
42-
//
43-
// The base
44-
type JSONData struct {
45-
News News `json:"news"`
46-
Information Information `json:"information"`
47-
}
48-
49-
// Declaring vars for later use..
50-
var (
51-
NewsData News
52-
tmp1 *goquery.Selection
53-
tmp2 string
54-
)
55-
5648
TibiadataRequest.URL = "https://www.tibia.com/news/?subtopic=newsarchive&id=" + strconv.Itoa(NewsID)
5749

5850
// Getting data with TibiadataHTMLDataCollectorV3
@@ -64,14 +56,27 @@ func TibiaNewsV3(c *gin.Context) {
6456
return
6557
}
6658

59+
jsonData := TibiaNewsV3Impl(NewsID, TibiadataRequest.URL, BoxContentHTML)
60+
61+
TibiaDataAPIHandleSuccessResponse(c, "TibiaNewsV3", jsonData)
62+
}
63+
64+
func TibiaNewsV3Impl(NewsID int, rawUrl string, BoxContentHTML string) NewsResponse {
65+
// Declaring vars for later use..
66+
var (
67+
NewsData News
68+
tmp1 *goquery.Selection
69+
tmp2 string
70+
)
71+
6772
// Loading HTML data into ReaderHTML for goquery with NewReader
6873
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
6974
if err != nil {
7075
log.Fatal(err)
7176
}
7277

7378
NewsData.ID = NewsID
74-
NewsData.TibiaURL = TibiadataRequest.URL
79+
NewsData.TibiaURL = rawUrl
7580

7681
ReaderHTML.Find(".NewsHeadline").Each(func(index int, s *goquery.Selection) {
7782

@@ -121,13 +126,11 @@ func TibiaNewsV3(c *gin.Context) {
121126

122127
//
123128
// Build the data-blob
124-
jsonData := JSONData{
129+
return NewsResponse{
125130
NewsData,
126131
Information{
127132
APIVersion: TibiadataAPIversion,
128133
Timestamp: TibiadataDatetimeV3(""),
129134
},
130135
}
131-
132-
TibiaDataAPIHandleSuccessResponse(c, "TibiaNewsV3", jsonData)
133136
}

src/TibiaNewsV3_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewsById(t *testing.T) {
11+
data, err := ioutil.ReadFile("../testdata/news/archive/6529.html")
12+
if err != nil {
13+
t.Errorf("File reading error: %s", err)
14+
return
15+
}
16+
17+
newsArticleJson := TibiaNewsV3Impl(6529, "https://www.tibia.com/news/?subtopic=newsarchive&id=6529", string(data))
18+
assert := assert.New(t)
19+
20+
assert.Equal(6529, newsArticleJson.News.ID)
21+
assert.Equal("2022-01-12", newsArticleJson.News.Date)
22+
assert.Equal("", newsArticleJson.News.Title)
23+
assert.Equal("development", newsArticleJson.News.Category)
24+
assert.Equal("ticker", newsArticleJson.News.Type)
25+
assert.Equal("https://www.tibia.com/news/?subtopic=newsarchive&id=6529", newsArticleJson.News.TibiaURL)
26+
assert.Equal("A number of issues related to the 25 years activities have been fixed, including the following: Dragon pinatas that were stuck in the inbox have been changed into dragon pinata kits. The wind-up loco can now be taken even after defeating Lord Retro. The baby seals can now be painted even when the quest The Ice Islands is active. The weight of wallpapers and fairy lights has been increased and their market category has been changed to decoration. A number of typos and map issues have been fixed as well.", newsArticleJson.News.Content)
27+
assert.Equal("A number of issues related to the 25 years activities have been fixed, including the following: Dragon pinatas that were stuck in the inbox have been changed into dragon pinata kits. The wind-up loco can now be taken even after defeating Lord Retro. The baby seals can now be painted even when the quest The Ice Islands is active. The weight of wallpapers and fairy lights has been increased and their market category has been changed to decoration. A number of typos and map issues have been fixed as well.", newsArticleJson.News.ContentHTML)
28+
}
29+
30+
func TestNews6512(t *testing.T) {
31+
data, err := ioutil.ReadFile("../testdata/news/archive/6512.html")
32+
if err != nil {
33+
t.Errorf("File reading error: %s", err)
34+
return
35+
}
36+
37+
newsArticleJson := TibiaNewsV3Impl(6512, "https://www.tibia.com/news/?subtopic=newsarchive&id=6512", string(data))
38+
assert := assert.New(t)
39+
40+
assert.Equal(6512, newsArticleJson.News.ID)
41+
assert.Equal("2022-01-04", newsArticleJson.News.Date)
42+
assert.Equal("", newsArticleJson.News.Title)
43+
assert.Equal("community", newsArticleJson.News.Category)
44+
assert.Equal("ticker", newsArticleJson.News.Type)
45+
assert.Equal("https://www.tibia.com/news/?subtopic=newsarchive&id=6512", newsArticleJson.News.TibiaURL)
46+
assert.Equal("TibiaData.com has some news to share! First of all, they invite you to participate in their Discord Server. Further, they are now present on GitHub. They are working on their v3, which is still in beta. If you are interested in such things, head on over there to see what is cooking.", newsArticleJson.News.Content)
47+
assert.Equal("<a href=\"https://tibiadata.com\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">TibiaData.com</a> has some news to share! First of all, they invite you to participate in their <a href=\"https://tibiadata.com/2021/07/join-tibiadata-on-discord/\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">Discord Server</a>. Further, they are now present on <a href=\"https://tibiadata.com/2021/12/tibiadata-has-gone-open-source/\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">GitHub</a>. They are working on their <a href=\"https://tibiadata.com/doc-api-v3/v3-beta/\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">v3</a>, which is still in beta. If you are interested in such things, head on over there to see what is cooking.", newsArticleJson.News.ContentHTML)
48+
}
49+
50+
func TestNews6481(t *testing.T) {
51+
data, err := ioutil.ReadFile("../testdata/news/archive/6481.html")
52+
if err != nil {
53+
t.Errorf("File reading error: %s", err)
54+
return
55+
}
56+
57+
newsArticleJson := TibiaNewsV3Impl(6481, "https://www.tibia.com/news/?subtopic=newsarchive&id=6481", string(data))
58+
assert := assert.New(t)
59+
60+
assert.Equal(6481, newsArticleJson.News.ID)
61+
assert.Equal("2021-12-22", newsArticleJson.News.Date)
62+
assert.Equal("New Mounts", newsArticleJson.News.Title)
63+
assert.Equal("development", newsArticleJson.News.Category)
64+
assert.Equal("", newsArticleJson.News.Type)
65+
assert.Equal("https://www.tibia.com/news/?subtopic=newsarchive&id=6481", newsArticleJson.News.TibiaURL)
66+
assert.Equal("New mounts have been added to the Store today!\nThe origins of the Emerald Raven, Mystic Raven, and Radiant Raven are shrouded in darkness, as no written record nor tale told by even the most knowing storytellers mentions but a trace of them. Superstition surrounds them, as some see these gigantic birds as an echo of a long forgotten past, while others believe them to herald hitherto unknown events. What is clear is that they are highly intelligent beings which make great companions if they deem somebody worthy.\n\nClick on image to enlarge.\n\nOnce bought, your character can use the mount ingame anytime, no matter if you are a free account or a Premium account.\nGet yourself a corvid companion!Your Community Managers ", newsArticleJson.News.Content)
67+
assert.Equal("<p>New mounts have been added to the Store today!</p>\n<p>The origins of the <strong>Emerald Raven</strong>, <strong>Mystic Raven</strong>, and <strong>Radiant Raven</strong> are shrouded in darkness, as no written record nor tale told by even the most knowing storytellers mentions but a trace of them. Superstition surrounds them, as some see these gigantic birds as an echo of a long forgotten past, while others believe them to herald hitherto unknown events. What is clear is that they are highly intelligent beings which make great companions if they deem somebody worthy.</p>\n<figure><center><img style=\"cursor: pointer;\" src=\"https://static.tibia.com/images/news/emeraldraven_small.jpg\" onclick=\"ImageInNewWindow(&#39;https://static.tibia.com/images/news/emeraldraven.jpg&#39;)\"/></center>\n<figcaption><center><em>Click on image to enlarge.</em></center></figcaption>\n</figure>\n<p>Once bought, your character can use the mount ingame anytime, no matter if you are a free account or a Premium account.</p>\n<p>Get yourself a corvid companion!<br/>Your Community Managers</p> ", newsArticleJson.News.ContentHTML)
68+
}

src/TibiaNewslistV3.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,32 @@ import (
1212
"github.com/gin-gonic/gin"
1313
)
1414

15+
// Child of JSONData
16+
type NewsItem struct {
17+
ID int `json:"id"`
18+
Date string `json:"date"`
19+
News string `json:"news"`
20+
Category string `json:"category"`
21+
Type string `json:"type"`
22+
TibiaURL string `json:"url"`
23+
ApiURL string `json:"url_api,omitempty"`
24+
}
25+
26+
//
27+
// The base
28+
type NewsListResponse struct {
29+
News []NewsItem `json:"news"`
30+
Information Information `json:"information"`
31+
}
32+
1533
// TibiaNewslistV3 func
1634
func TibiaNewslistV3(c *gin.Context) {
17-
1835
// getting params from URL
1936
days := TibiadataStringToIntegerV3(c.Param("days"))
2037
if days == 0 {
2138
days = 90 // default for recent posts
2239
}
2340

24-
// Child of JSONData
25-
type News struct {
26-
ID int `json:"id"`
27-
Date string `json:"date"`
28-
News string `json:"news"`
29-
Category string `json:"category"`
30-
Type string `json:"type"`
31-
TibiaURL string `json:"url"`
32-
ApiURL string `json:"url_api,omitempty"`
33-
}
34-
35-
//
36-
// The base
37-
type JSONData struct {
38-
News []News `json:"news"`
39-
Information Information `json:"information"`
40-
}
41-
42-
// Declaring vars for later use..
43-
var NewsListData []News
44-
4541
// generating dates to pass to FormData
4642
DateBegin := time.Now().AddDate(0, 0, -days)
4743
DateEnd := time.Now()
@@ -84,14 +80,23 @@ func TibiaNewslistV3(c *gin.Context) {
8480
return
8581
}
8682

83+
jsonData := TibiaNewslistV3Impl(days, BoxContentHTML)
84+
85+
TibiaDataAPIHandleSuccessResponse(c, "TibiaNewslistV3", jsonData)
86+
}
87+
88+
func TibiaNewslistV3Impl(days int, BoxContentHTML string) NewsListResponse {
89+
// Declaring vars for later use..
90+
var NewsListData []NewsItem
91+
8792
// Loading HTML data into ReaderHTML for goquery with NewReader
8893
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
8994
if err != nil {
9095
log.Fatal(err)
9196
}
9297

9398
ReaderHTML.Find(".Odd,.Even").Each(func(index int, s *goquery.Selection) {
94-
var OneNews News
99+
var OneNews NewsItem
95100

96101
// getting category by image src
97102
CategoryImg, _ := s.Find("img").Attr("src")
@@ -114,7 +119,7 @@ func TibiaNewslistV3(c *gin.Context) {
114119
OneNews.TibiaURL = NewsSplit[0] + NewsID
115120

116121
if TibiadataHost != "" {
117-
OneNews.ApiURL = "http://api.tibiadata.com/v3/news/id/" + NewsID
122+
OneNews.ApiURL = "https://" + TibiadataHost + "/v3/news/id/" + NewsID
118123
}
119124

120125
// add to NewsListData for response
@@ -123,13 +128,11 @@ func TibiaNewslistV3(c *gin.Context) {
123128

124129
//
125130
// Build the data-blob
126-
jsonData := JSONData{
131+
return NewsListResponse{
127132
NewsListData,
128133
Information{
129134
APIVersion: TibiadataAPIversion,
130135
Timestamp: TibiadataDatetimeV3(""),
131136
},
132137
}
133-
134-
TibiaDataAPIHandleSuccessResponse(c, "TibiaNewslistV3", jsonData)
135138
}

src/TibiaNewslistV3_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewsList(t *testing.T) {
11+
data, err := ioutil.ReadFile("../testdata/news/newslist.html")
12+
if err != nil {
13+
t.Errorf("File reading error: %s", err)
14+
return
15+
}
16+
17+
TibiadataHost = "unittest.example.com"
18+
19+
newsListJson := TibiaNewslistV3Impl(90, string(data))
20+
assert := assert.New(t)
21+
22+
assert.Equal(50, len(newsListJson.News))
23+
24+
firstArticle := newsListJson.News[0]
25+
assert.Equal(6529, firstArticle.ID)
26+
assert.Equal("2022-01-12", firstArticle.Date)
27+
assert.Equal("A number of issues related to the 25 years activities have been fixed,...", firstArticle.News)
28+
assert.Equal("development", firstArticle.Category)
29+
assert.Equal("ticker", firstArticle.Type)
30+
assert.Equal("https://www.tibia.com/news/?subtopic=newsarchive&id=6529", firstArticle.TibiaURL)
31+
assert.Equal("https://unittest.example.com/v3/news/id/6529", firstArticle.ApiURL)
32+
}

0 commit comments

Comments
 (0)