forked from tjhowse/cacheodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeocaching_test.go
More file actions
314 lines (300 loc) · 9.64 KB
/
Geocaching_test.go
File metadata and controls
314 lines (300 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"os"
"testing"
"time"
log "github.com/sirupsen/logrus"
)
type mockGeocachingApi struct {
caches []Geocache
logs []GeocacheLog
}
// Populate some dummy data into the struct
func (m *mockGeocachingApi) populate() {
m.caches = []Geocache{
{
ID: 123456,
Name: "Secret Hideout",
Code: "GC1234",
PremiumOnly: false,
FavoritePoints: 5,
GeocacheType: 1,
ContainerType: 2,
Difficulty: 3.5,
Terrain: 2.5,
CacheStatus: 1,
PostedCoordinates: GocachePostedCoordinates{
Latitude: 37.7749,
Longitude: -122.4194,
},
DetailsURL: "/geocache/GC1234",
HasGeotour: false,
PlacedDate: "2020-01-01T00:00:00",
Owner: GeocacheOwner{
Code: "ABC123",
Username: "JimblyBimbly",
},
LastFoundDate: "2022-05-01T12:00:00",
TrackableCount: 2,
Region: "California",
Country: "United States",
Attributes: []GeocacheAttributes{
{ID: 24, Name: "Wheelchair accessible", IsApplicable: false},
{ID: 8, Name: "Scenic view", IsApplicable: true},
},
Distance: "2.3mi",
Bearing: "NW",
LastFoundTime: time.Now(),
GUID: "a8cf16ab-5a5d-42a2-9a8e-2b33d431c758",
},
{
ID: 789123,
Name: "Bingo Hall",
Code: "GC456798",
PremiumOnly: false,
FavoritePoints: 5,
GeocacheType: 1,
ContainerType: 2,
Difficulty: 3.5,
Terrain: 2.5,
CacheStatus: 1,
PostedCoordinates: GocachePostedCoordinates{
Latitude: 37.7749,
Longitude: -122.4194,
},
DetailsURL: "/geocache/GC456798",
HasGeotour: false,
PlacedDate: "2020-02-03T10:00:00",
Owner: GeocacheOwner{
Code: "ABC123",
Username: "PrinceOfBingo",
},
LastFoundDate: "2022-07-11T12:12:34",
TrackableCount: 2,
Region: "California",
Country: "United States",
Attributes: []GeocacheAttributes{
{ID: 24, Name: "Wheelchair accessible", IsApplicable: false},
{ID: 8, Name: "Scenic view", IsApplicable: true},
},
Distance: "2.3mi",
Bearing: "NW",
LastFoundTime: time.Now(),
GUID: "a8cf16ab-5a5d-42a2-9a8e-2b33d431c758",
},
}
m.logs = []GeocacheLog{
{
LogID: 2150129950,
CacheID: 123456,
LogGUID: "fc59d67c-ccda-45a7-ad5c-f9a09f040d60",
Latitude: 37.7749,
Longitude: -122.4194,
LatLonString: "37.7749,-122.4194",
LogTypeID: 1,
LogType: "Found it",
LogTypeImage: "1.png",
LogText: `Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!
Had a great time at this event. Thanks for hosting!`,
Created: "3/16/2023",
Visited: "3/16/2023",
UserName: "Amy",
MembershipLevel: 3,
AccountID: 16998345,
AccountGUID: "8fa1b05c-d5f5-4f9c-8f0b-634b88017772",
Email: "amy@example.com",
AvatarImage: "https://www.example.com/avatar.jpg",
GeocacheFindCount: 1123,
GeocacheHideCount: 5,
ChallengesCompleted: 2,
IsEncoded: true,
Creator: struct {
GroupTitle string `json:"GroupTitle"`
GroupImageURL string `json:"GroupImageUrl"`
}{
GroupTitle: "Premium Member",
GroupImageURL: "/images/icons/prem_user.gif",
},
Images: []any{},
},
{
LogID: 2150129950,
CacheID: 789123,
LogGUID: "fc59d67c-ccda-45a7-ad5c-f9a09f040d60",
Latitude: 37.7749,
Longitude: -122.4194,
LatLonString: "37.7749,-122.4194",
LogTypeID: 1,
LogType: "Found it",
LogTypeImage: "1.png",
LogText: "dogs dogs dogs!\n",
Created: "3/16/2023",
Visited: "3/16/2023",
UserName: "Beepo",
MembershipLevel: 3,
AccountID: 16998345,
AccountGUID: "8fa1b05c-d5f5-4f9c-8f0b-634b88017772",
Email: "amy@example.com",
AvatarImage: "https://www.example.com/avatar.jpg",
GeocacheFindCount: 1123,
GeocacheHideCount: 5,
ChallengesCompleted: 2,
IsEncoded: true,
Creator: struct {
GroupTitle string `json:"GroupTitle"`
GroupImageURL string `json:"GroupImageUrl"`
}{
GroupTitle: "Premium Member",
GroupImageURL: "/images/icons/prem_user.gif",
},
Images: []any{},
},
}
}
// Advance the last found date on the stored cache
func (m *mockGeocachingApi) advanceLastFoundDate(index int) {
m.caches[index].LastFoundTime = m.caches[index].LastFoundTime.Add(time.Hour * 24)
}
func (m *mockGeocachingApi) Auth(clientID, clientSecret string) error {
return nil
}
func (m *mockGeocachingApi) Search(st searchTerms) ([]Geocache, error) {
return m.caches, nil
}
func (m *mockGeocachingApi) GetLogs(geocache *Geocache) ([]GeocacheLog, error) {
var logs []GeocacheLog
for _, log := range m.logs {
if log.CacheID == geocache.ID {
logs = append(logs, log)
}
}
return logs, nil
}
func TestUpdate(t *testing.T) {
var err error
tempdir := t.TempDir()
conf := configStore{
SearchTerms: searchTerms{
AreaName: "Blerpville",
},
DBFilename: tempdir + "/test.sqlite3",
}
var g *Geocaching
api := &mockGeocachingApi{}
api.populate()
if g, err = NewGeocaching(conf, api); err != nil {
log.Fatal(err)
os.Exit(1)
}
defer g.Close()
var logs []postDetails
logs, err = g.Update()
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if want, got := 2, len(logs); want != got {
t.Errorf("Expected %d logs, got %d", want, got)
}
// Check that this cache showed up as a new one
if !logs[0].NewCache {
t.Errorf("Expected this to be a new cache")
}
// Check that it has the owner's details, not the owner's
if want, got := "JimblyBimbly", logs[0].UserName; want != got {
t.Errorf("Expected the owner to be %s, got %s", want, got)
}
// Advance the last found date on the mock cache
api.advanceLastFoundDate(0)
logs, err = g.Update()
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if want, got := 1, len(logs); want != got {
t.Errorf("Expected %d logs, got %d", want, got)
}
// Check that it is no longer a new cache
if logs[0].NewCache {
t.Errorf("Expected this to not be a new cache")
}
// Check that it has the finder's details, not the owner's
if want, got := "Amy", logs[0].UserName; want != got {
t.Errorf("Expected the finder to be %s, got %s", want, got)
}
// Check the very long message was truncated properly.
if want, got := 500, len(logs[0].toString()); want != got {
t.Errorf("Expected the log to be %d characters, got %d", want, got)
}
// Check the truncated string ends with "" #geocaching"
if want, got := `" #geocaching`, logs[0].toString()[len(logs[0].toString())-13:]; want != got {
t.Errorf("Expected the log to end with %q, got %q", want, got)
}
// TODO Check that we get the "That's their second find for the day!" thing.
api.advanceLastFoundDate(0)
logs, err = g.Update()
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if want, got := 1, len(logs); want != got {
t.Errorf("Expected %d logs, got %d", want, got)
}
// Check that it has the finder's details, not the owner's
if want, got := "Amy", logs[0].UserName; want != got {
t.Errorf("Expected the finder to be %s, got %s", want, got)
}
if want, got := 2, logs[0].UsersFindsToday; want != got {
t.Errorf("Expected the finder to have found %d caches, got %d", want, got)
}
logs, err = g.Update()
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if want, got := 0, len(logs); want != got {
t.Errorf("Expected %d logs, got %d", want, got)
}
api.advanceLastFoundDate(1)
logs, err = g.Update()
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if want, got := 1, len(logs); want != got {
t.Errorf("Expected %d logs, got %d", want, got)
}
// Check that it has the finder's details, not the owner's
if want, got := "Beepo", logs[0].UserName; want != got {
t.Errorf("Expected the finder to be %s, got %s", want, got)
}
api.advanceLastFoundDate(0)
api.advanceLastFoundDate(1)
logs, err = g.Update()
if err != nil {
t.Errorf("Expected no error, got %s", err)
}
if want, got := 2, len(logs); want != got {
t.Errorf("Expected %d logs, got %d", want, got)
}
}