Skip to content

Commit

Permalink
fix parsing for v1.2.24215.01 of the page
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Aug 22, 2024
1 parent 6f64cfc commit 6aa19da
Show file tree
Hide file tree
Showing 5 changed files with 1,035 additions and 158 deletions.
15 changes: 0 additions & 15 deletions mocks_test.go

This file was deleted.

27 changes: 24 additions & 3 deletions rubbish.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ type RubbishCollection struct {
FoodScraps bool
}

func (r *RubbishCollection) String() string {
return fmt.Sprintf("%s: %s", r.Day, r.Type())
}

func (r *RubbishCollection) Type() string {
switch {
case r.Rubbish:
return "Rubbish"
case r.Recycle:
return "Recycle"
case r.FoodScraps:
return "Food Scraps"
default:
return "Unknown"
}
}

// CollectionDayDetailResult contains the information about Rubbish and
// Recycling collection.
type CollectionDayDetailResult struct {
Expand Down Expand Up @@ -155,11 +172,15 @@ func (p *refuseParser) parseLinks(el int, sel *goquery.Selection) {
sel.Children().Children().Each(func(n int, sel *goquery.Selection) {
switch n {
case 0:
if sel.Text() == "Rubbish" {
attr, found := sel.Attr("class")
if !found {
return
}
if attr == "icon-rubbish" {
p.detail[el].Rubbish = true
} else if sel.Text() == "Food scraps" {
} else if attr == "icon-food-waste" {
p.detail[el].FoodScraps = true
} else if sel.Text() == "Recycle" {
} else if attr == "icon-recycle" {
p.detail[el].Recycle = true
} else {
p.Err = fmt.Errorf("parse error: sel.Text = %q, el = %d, n = %d", sel.Text(), el, n)
Expand Down
55 changes: 40 additions & 15 deletions rubbish_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aklapi

import (
_ "embed"
"encoding/json"
"io"
"net/http"
Expand All @@ -13,6 +14,19 @@ import (
"github.com/stretchr/testify/assert"
)

//go:generate curl -L https://www.aucklandcouncil.govt.nz/rubbish-recycling/rubbish-recycling-collections/Pages/collection-day-detail.aspx?an=12342478585 -o test_assets/500-queen-street.html
//go:generate curl -L https://www.aucklandcouncil.govt.nz/rubbish-recycling/rubbish-recycling-collections/Pages/collection-day-detail.aspx?an=12341511281 -o test_assets/1-luanda-drive.html

// Test data, run go:generate to update, then update dates in tests
// accordingly.
var (
//go:embed "test_assets/1-luanda-drive.html"
taRsd1LuandaDrive string

//go:embed "test_assets/500-queen-street.html"
taCom500QueenStreet string
)

func Test_parse(t *testing.T) {
type args struct {
r io.Reader
Expand All @@ -23,37 +37,48 @@ func Test_parse(t *testing.T) {
want *CollectionDayDetailResult
wantErr bool
}{
{"ok",
args{strings.NewReader(testHTML)},
{"1 Luanda Drive, Ranui",
args{strings.NewReader(taRsd1LuandaDrive)},
&CollectionDayDetailResult{
Collections: []RubbishCollection{
{Day: "Sunday 21 April",
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
{
Day: "Tuesday 27 August",
Date: adjustYear(time.Date(0, 8, 27, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: false,
FoodScraps: false},
{Day: "Sunday 21 April",
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
FoodScraps: false,
},
{
Day: "Tuesday 27 August",
Date: adjustYear(time.Date(0, 8, 27, 0, 0, 0, 0, defaultLoc)),
Rubbish: false,
Recycle: false,
FoodScraps: true,
},
{
Day: "Tuesday 3 September",
Date: adjustYear(time.Date(0, 9, 3, 0, 0, 0, 0, defaultLoc)),
Rubbish: false,
Recycle: true,
FoodScraps: false},
FoodScraps: false,
},
},
Address: nil,
},
false},
{"500 queen ok",
args{strings.NewReader(testHTMLcommercial)},
{"500 Queen Street, CBD",
args{strings.NewReader(taCom500QueenStreet)},
&CollectionDayDetailResult{
Collections: []RubbishCollection{
{
Day: "Sunday 21 April",
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
Day: "Thursday 22 August",
Date: adjustYear(time.Date(0, 8, 22, 0, 0, 0, 0, defaultLoc)),
Rubbish: true,
Recycle: false,
},
{
Day: "Sunday 21 April",
Date: adjustYear(time.Date(0, 04, 21, 0, 0, 0, 0, defaultLoc)),
Day: "Thursday 22 August",
Date: adjustYear(time.Date(0, 8, 22, 0, 0, 0, 0, defaultLoc)),
Rubbish: false,
Recycle: true,
},
Expand Down Expand Up @@ -260,7 +285,7 @@ func testMux() http.Handler {
w.Write(data)
})
mux.HandleFunc("/rubbish/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(testHTML))
w.Write([]byte(taRsd1LuandaDrive))
})

return mux
Expand Down
841 changes: 841 additions & 0 deletions test_assets/1-luanda-drive.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 6aa19da

Please sign in to comment.