Skip to content

Commit

Permalink
start to implement scrape test using mock lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein committed Dec 28, 2023
1 parent c1cbce3 commit ecd9ff5
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/PuerkitoBio/goquery v1.5.0 // indirect
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/jarcoal/httpmock v1.3.1 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6ODgOub/6LCI=
Expand Down
2 changes: 1 addition & 1 deletion scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func Scrape(c *Config, uri string) error {
// extract slug and id from uri
uriparts := strings.Split(uri, "/")
if len(uriparts) < 6 {
return errors.New("invalid uri")
return errors.New("invalid uri: " + uri)
}
ad.Slug = uriparts[4]
ad.Id = uriparts[5]
Expand Down
89 changes: 89 additions & 0 deletions scrape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright © 2023 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
"fmt"
"io/ioutil"

Check failure on line 22 in scrape_test.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"testing"

"github.com/jarcoal/httpmock"
)

type Adsource struct {
uri string
content string
}

func IoRead(file string) string {
content, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}

return string(content)
}

func InitSources(conf *Config) []Adsource {
ads := []Adsource{
{
uri: fmt.Sprintf("%s%s?userId=%d", Baseuri, Listuri, conf.User),
content: IoRead("t/adlist1.html"),
},
{
uri: fmt.Sprintf("%s%s?userId=%d&pageNum=2", Baseuri, Listuri, conf.User),
content: IoRead("t/adlist2.html"),
},
{
uri: fmt.Sprintf("%s%s?userId=%d&pageNum=3", Baseuri, Listuri, conf.User),
content: IoRead("t/adlist3.html"),
},
}

for n := 1; n < 10; n++ {
ads = append(ads, Adsource{
uri: fmt.Sprintf("%s/s-anzeige/ad%d/%d", Baseuri, n, n),
content: IoRead(fmt.Sprintf("t/ad%d.html", n)), // FIXME: add actual ad?.html files
})
}

return ads
}

func SetIntercept(conf *Config) {
ads := InitSources(conf)

for _, ad := range ads {
httpmock.RegisterResponder("GET", ad.uri,
httpmock.NewStringResponder(200, ad.content))
}

}

func TestStart(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

conf := &Config{User: 1, Outdir: "t/out"}

SetIntercept(conf)

if err := Start(conf); err != nil {
t.Errorf("failed: %s", err.Error())
}
}
32 changes: 32 additions & 0 deletions t/adlist1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="de" >
<head>
<title>Ad Listing</title>
</head>
<body>

<h2 class="text-module-begin">
<a class="ellipsis"
href="/s-anzeige/first-ad/1">First Ad</a>
</h2>
<h2 class="text-module-begin">
<a class="ellipsis"
href="/s-anzeige/first-ad/2">Second Ad</a>
</h2>

<h2 class="text-module-begin">
<a class="ellipsis"
href="/s-anzeige/first-ad/3">Third Ad</a>
</h2>

<h2 class="text-module-begin">
<a class="ellipsis" name="2625911449"
href="/s-anzeige/first-ad/4">Fourth Ad</a>
</h2>

<h2 class="text-module-begin">
<a class="ellipsis" name="2624220233"
href="/s-anzeige/first-ad/5">Fitth Ad</a>
</h2>
</body>
</html>
26 changes: 26 additions & 0 deletions t/adlist2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="de" >
<head>
<title>Ad Listing</title>
</head>
<body>
<h2 class="text-module-begin">
<a class="ellipsis"
href="/s-anzeige/first-ad/6">Sixth Ad</a>
</h2>
<h2 class="text-module-begin">
<a class="ellipsis"
href="/s-anzeige/first-ad/7">Seventh Ad</a>
</h2>

<h2 class="text-module-begin">
<a class="ellipsis"
href="/s-anzeige/first-ad/8">Eighth Ad</a>
</h2>

<h2 class="text-module-begin">
<a class="ellipsis" name="2625911449"
href="/s-anzeige/first-ad/9">Ninth Ad</a>
</h2>
</body>
</html>
9 changes: 9 additions & 0 deletions t/adlist3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="de" >
<head>
<title>Ad Listing</title>
</head>
<body>
<!-- empty -->>
</body>
</html>

0 comments on commit ecd9ff5

Please sign in to comment.