11package aklapi
22
33import (
4+ _ "embed"
45 "encoding/json"
56 "io"
67 "net/http"
@@ -13,6 +14,19 @@ import (
1314 "github.com/stretchr/testify/assert"
1415)
1516
17+ //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
18+ //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
19+
20+ // Test data, run go:generate to update, then update dates in tests
21+ // accordingly.
22+ var (
23+ //go:embed "test_assets/1-luanda-drive.html"
24+ taRsd1LuandaDrive string
25+
26+ //go:embed "test_assets/500-queen-street.html"
27+ taCom500QueenStreet string
28+ )
29+
1630func Test_parse (t * testing.T ) {
1731 type args struct {
1832 r io.Reader
@@ -23,37 +37,48 @@ func Test_parse(t *testing.T) {
2337 want * CollectionDayDetailResult
2438 wantErr bool
2539 }{
26- {"ok " ,
27- args {strings .NewReader (testHTML )},
40+ {"1 Luanda Drive, Ranui " ,
41+ args {strings .NewReader (taRsd1LuandaDrive )},
2842 & CollectionDayDetailResult {
2943 Collections : []RubbishCollection {
30- {Day : "Sunday 21 April" ,
31- Date : adjustYear (time .Date (0 , 04 , 21 , 0 , 0 , 0 , 0 , defaultLoc )),
44+ {
45+ Day : "Tuesday 27 August" ,
46+ Date : adjustYear (time .Date (0 , 8 , 27 , 0 , 0 , 0 , 0 , defaultLoc )),
3247 Rubbish : true ,
3348 Recycle : false ,
34- FoodScraps : false },
35- {Day : "Sunday 21 April" ,
36- Date : adjustYear (time .Date (0 , 04 , 21 , 0 , 0 , 0 , 0 , defaultLoc )),
49+ FoodScraps : false ,
50+ },
51+ {
52+ Day : "Tuesday 27 August" ,
53+ Date : adjustYear (time .Date (0 , 8 , 27 , 0 , 0 , 0 , 0 , defaultLoc )),
54+ Rubbish : false ,
55+ Recycle : false ,
56+ FoodScraps : true ,
57+ },
58+ {
59+ Day : "Tuesday 3 September" ,
60+ Date : adjustYear (time .Date (0 , 9 , 3 , 0 , 0 , 0 , 0 , defaultLoc )),
3761 Rubbish : false ,
3862 Recycle : true ,
39- FoodScraps : false },
63+ FoodScraps : false ,
64+ },
4065 },
4166 Address : nil ,
4267 },
4368 false },
44- {"500 queen ok " ,
45- args {strings .NewReader (testHTMLcommercial )},
69+ {"500 Queen Street, CBD " ,
70+ args {strings .NewReader (taCom500QueenStreet )},
4671 & CollectionDayDetailResult {
4772 Collections : []RubbishCollection {
4873 {
49- Day : "Sunday 21 April " ,
50- Date : adjustYear (time .Date (0 , 04 , 21 , 0 , 0 , 0 , 0 , defaultLoc )),
74+ Day : "Thursday 22 August " ,
75+ Date : adjustYear (time .Date (0 , 8 , 22 , 0 , 0 , 0 , 0 , defaultLoc )),
5176 Rubbish : true ,
5277 Recycle : false ,
5378 },
5479 {
55- Day : "Sunday 21 April " ,
56- Date : adjustYear (time .Date (0 , 04 , 21 , 0 , 0 , 0 , 0 , defaultLoc )),
80+ Day : "Thursday 22 August " ,
81+ Date : adjustYear (time .Date (0 , 8 , 22 , 0 , 0 , 0 , 0 , defaultLoc )),
5782 Rubbish : false ,
5883 Recycle : true ,
5984 },
@@ -74,6 +99,71 @@ func Test_parse(t *testing.T) {
7499 }
75100}
76101
102+ func TestCollectionDayDetail (t * testing.T ) {
103+ type args struct {
104+ addr string
105+ }
106+ tests := []struct {
107+ name string
108+ testSrv * httptest.Server
109+ args args
110+ want * CollectionDayDetailResult
111+ wantErr bool
112+ }{
113+ {"main branch" ,
114+ httptest .NewServer (testMux ()),
115+ args {addr : "xxx" },
116+ & CollectionDayDetailResult {
117+ Collections : []RubbishCollection {
118+ {
119+ Day : "Tuesday 27 August" ,
120+ Date : adjustYear (time .Date (0 , 8 , 27 , 0 , 0 , 0 , 0 , defaultLoc )),
121+ Rubbish : true ,
122+ Recycle : false ,
123+ FoodScraps : false ,
124+ },
125+ {
126+ Day : "Tuesday 27 August" ,
127+ Date : adjustYear (time .Date (0 , 8 , 27 , 0 , 0 , 0 , 0 , defaultLoc )),
128+ Rubbish : false ,
129+ Recycle : false ,
130+ FoodScraps : true ,
131+ },
132+ {
133+ Day : "Tuesday 3 September" ,
134+ Date : adjustYear (time .Date (0 , 9 , 3 , 0 , 0 , 0 , 0 , defaultLoc )),
135+ Rubbish : false ,
136+ Recycle : true ,
137+ FoodScraps : false ,
138+ },
139+ },
140+ Address : & Address {
141+ ACRateAccountKey : "42" ,
142+ Address : "Red Square" ,
143+ Suggestion : "Red Square" ,
144+ },
145+ },
146+ false ,
147+ },
148+ }
149+ for _ , tt := range tests {
150+ t .Run (tt .name , func (t * testing.T ) {
151+ defer tt .testSrv .Close ()
152+ oldAddrURI := addrURI
153+ oldcollectionDayURI := collectionDayURI
154+ defer func () { addrURI = oldAddrURI ; collectionDayURI = oldcollectionDayURI }()
155+ addrURI = tt .testSrv .URL + "/addr/"
156+ collectionDayURI = tt .testSrv .URL + "/rubbish/?an=%s"
157+ got , err := CollectionDayDetail (tt .args .addr )
158+ if (err != nil ) != tt .wantErr {
159+ t .Errorf ("CollectionDayDetail() error = %v, wantErr %v" , err , tt .wantErr )
160+ return
161+ }
162+ assert .Equal (t , tt .want , got )
163+ })
164+ }
165+ }
166+
77167func TestCollectionDayDetailResult_NextRubbish (t * testing.T ) {
78168 type fields struct {
79169 Collections []RubbishCollection
@@ -192,64 +282,6 @@ func TestRubbishCollection_parseDate(t *testing.T) {
192282 }
193283}
194284
195- func TestCollectionDayDetail (t * testing.T ) {
196- type args struct {
197- addr string
198- }
199- tests := []struct {
200- name string
201- testSrv * httptest.Server
202- args args
203- want * CollectionDayDetailResult
204- wantErr bool
205- }{
206- {"main branch" ,
207- httptest .NewServer (testMux ()),
208- args {addr : "xxx" },
209- & CollectionDayDetailResult {
210- Collections : []RubbishCollection {
211- {
212- Day : "Sunday 21 April" ,
213- Date : adjustYear (time .Date (0 , 4 , 21 , 0 , 0 , 0 , 0 , defaultLoc )),
214- Rubbish : true ,
215- Recycle : false ,
216- FoodScraps : false ,
217- },
218- {
219- Day : "Sunday 21 April" ,
220- Date : adjustYear (time .Date (0 , 4 , 21 , 0 , 0 , 0 , 0 , defaultLoc )),
221- Rubbish : false ,
222- Recycle : true ,
223- FoodScraps : false ,
224- },
225- },
226- Address : & Address {
227- ACRateAccountKey : "42" ,
228- Address : "Red Square" ,
229- Suggestion : "Red Square" ,
230- },
231- },
232- false ,
233- },
234- }
235- for _ , tt := range tests {
236- t .Run (tt .name , func (t * testing.T ) {
237- defer tt .testSrv .Close ()
238- oldAddrURI := addrURI
239- oldcollectionDayURI := collectionDayURI
240- defer func () { addrURI = oldAddrURI ; collectionDayURI = oldcollectionDayURI }()
241- addrURI = tt .testSrv .URL + "/addr/"
242- collectionDayURI = tt .testSrv .URL + "/rubbish/?an=%s"
243- got , err := CollectionDayDetail (tt .args .addr )
244- if (err != nil ) != tt .wantErr {
245- t .Errorf ("CollectionDayDetail() error = %v, wantErr %v" , err , tt .wantErr )
246- return
247- }
248- assert .Equal (t , tt .want , got )
249- })
250- }
251- }
252-
253285func testMux () http.Handler {
254286 mux := http .NewServeMux ()
255287 mux .HandleFunc ("/addr/" , func (w http.ResponseWriter , r * http.Request ) {
@@ -260,7 +292,7 @@ func testMux() http.Handler {
260292 w .Write (data )
261293 })
262294 mux .HandleFunc ("/rubbish/" , func (w http.ResponseWriter , r * http.Request ) {
263- w .Write ([]byte (testHTML ))
295+ w .Write ([]byte (taRsd1LuandaDrive ))
264296 })
265297
266298 return mux
0 commit comments