-
Notifications
You must be signed in to change notification settings - Fork 6
/
posts_test.go
360 lines (297 loc) · 6.9 KB
/
posts_test.go
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package pinboard
import (
"fmt"
"log"
"testing"
"time"
)
// Test TestPostsAdd first as almost all subsequent tests rely on
// adding a post.
//
// go test -v -failfast
func TestPostsAdd(t *testing.T) {
err := PostsAdd(optAdd)
if err != nil {
t.Error(err)
}
err = PostsAdd(&PostsAddOptions{
Description: "This should fail",
})
if err == nil {
t.Error("error: expected missing url error")
}
err = PostsAdd(&PostsAddOptions{
URL: "https://github.com/imwally/pinboard",
})
if err == nil {
t.Error("error: expected must provide title error")
}
}
func ExamplePostsAdd() {
opt := &PostsAddOptions{
URL: "https://github.com/imwally/pinboard",
Description: "Testing Pinboard Go Package",
}
err := PostsAdd(opt)
if err != nil {
log.Println("error adding post:", err)
}
}
// Next, make sure PostsDelete works so we can remove test posts
// created in subsequent tests. This will delete the post that was
// created with TestPostsAdd.
func TestPostsDelete(t *testing.T) {
err := PostsDelete(optAdd.URL)
if err != nil {
t.Errorf("error: failed to delete test post: %s", err)
}
url := "https://thisisjustatest.com"
err = PostsDelete(url)
if err == nil {
t.Error("error: expected item not found error")
}
}
func TestPostsUpdate(t *testing.T) {
timeBeforeAdd := time.Now()
err := PostsAdd(optAdd)
if err != nil {
t.Errorf("error: failed to create test post: %s", err)
}
timeAfterAdd := time.Now()
timeUpdate, err := PostsUpdate()
if err != nil {
t.Error(err)
}
if timeUpdate.After(timeAfterAdd) && timeUpdate.Before(timeBeforeAdd) {
t.Errorf("error: expected %s to be between %s and %s",
timeUpdate,
timeBeforeAdd,
timeAfterAdd,
)
}
// Clean up by removing test post
err = PostsDelete(optAdd.URL)
if err != nil {
t.Errorf("error: clean up failed: %s", err)
}
}
func TestPostsGet(t *testing.T) {
err := PostsAdd(optAdd)
if err != nil {
t.Errorf("error: failed to create test post: %s", err)
}
// Test PostsGet by URL
posts, err := PostsGet(&PostsGetOptions{
URL: optAdd.URL,
})
if err != nil {
t.Error(err)
}
if len(posts) > 1 {
t.Error("error: PostsGet: expected only 1 post")
}
// Test PostsGet by tag
dt, _ := time.Parse("2006-01-02", "2010-12-11")
posts, err = PostsGet(&PostsGetOptions{
Dt: dt,
Tag: []string{"pinboard", "testing"},
})
if err != nil {
t.Error(err)
}
if len(posts) != 1 {
t.Error("error: PostsGet: expected only 1 post")
}
// Test PostsGet by Dt
posts, err = PostsGet(&PostsGetOptions{
Dt: dt,
})
if err != nil {
t.Error(err)
}
if len(posts) != 1 {
t.Error("error: PostsGet: expected only 1 post")
}
// Did we get the meta signature?
if posts[0].Meta == nil {
t.Error("error: PostsGet: expected meta signature")
}
posts, err = PostsGet(&PostsGetOptions{
Tag: []string{"sadklfjsldkfjsdlkfj"},
})
if err != nil {
t.Error(err)
}
if len(posts) != 0 {
t.Error("error: PostsGet: expected zero posts")
}
// Clean up by removing test post
err = PostsDelete(optAdd.URL)
if err != nil {
t.Errorf("error: clean up failed: %s", err)
}
}
func ExamplePostsGet() {
dt, err := time.Parse("2006-01-02", "2010-12-11")
if err != nil {
log.Println(err)
}
posts, err := PostsGet(&PostsGetOptions{Dt: dt})
if err != nil {
log.Println("error getting posts:", err)
}
for _, post := range posts {
fmt.Println(post.Description)
fmt.Println(post.Href)
fmt.Println(post.Time)
}
// Output:
// Testing Pinboard Go Package
// https://github.com/imwally/pinboard
// 2010-12-11 19:48:02 +0000 UTC
}
func TestPostsRecent(t *testing.T) {
// Test Count
posts, err := PostsRecent(&PostsRecentOptions{
Count: 100,
})
if err != nil {
t.Error(err)
}
if len(posts) != 100 {
t.Error("error: expected 100 posts")
}
err = PostsAdd(optAdd)
if err != nil {
t.Errorf("error: failed to create test post: %s", err)
}
posts, err = PostsRecent(&PostsRecentOptions{
Tag: []string{"pinboard_testing"},
})
if err != nil {
t.Error(err)
}
if len(posts) != 1 {
t.Error("error: expected 1 post")
}
// Clean up by removing test post
err = PostsDelete(optAdd.URL)
if err != nil {
t.Errorf("error: clean up failed: %s", err)
}
}
func TestPostsDates(t *testing.T) {
err := PostsAdd(optAdd)
if err != nil {
t.Errorf("error: failed to create test post: %s", err)
}
dates, err := PostsDates(nil)
if err != nil {
t.Error(err)
}
expected := "2010-12-11"
if _, ok := dates[expected]; !ok {
t.Errorf("error: expected at least 1 post on %s", expected)
}
// Test with tags
dates, err = PostsDates(&PostsDatesOptions{
Tag: []string{"pinboard_testing"},
})
if err != nil {
t.Error(err)
}
if len(dates) != 1 {
t.Error("error: expected only 1 post")
}
if _, ok := dates[expected]; !ok {
t.Errorf("error: expected at least 1 post on %s", expected)
}
// Test count
if dates[expected] != 1 {
t.Errorf("error: expected count to be 1, got %v", dates[expected])
}
// Clean up by removing test post
err = PostsDelete(optAdd.URL)
if err != nil {
t.Errorf("error: clean up failed: %s", err)
}
// Make sure that date no longer appears
dates, err = PostsDates(nil)
if err != nil {
t.Error(err)
}
if _, ok := dates[expected]; ok {
t.Errorf("error: expected no posts on %s", expected)
}
}
func TestPostsAll(t *testing.T) {
err := PostsAdd(optAdd)
if err != nil {
t.Errorf("error: failed to create test post: %s", err)
}
posts, err := PostsAll(&PostsAllOptions{
Results: 1,
Fromdt: optAdd.Dt.Add(time.Duration(-1) * time.Second),
Todt: optAdd.Dt.Add(time.Second),
Tag: []string{"pinboard", "testing"},
})
if err != nil {
t.Error(err)
}
if len(posts) != 1 {
t.Errorf("error: PostsAll: expected only 1 post")
}
posts, err = PostsAll(&PostsAllOptions{
Results: 1,
Fromdt: optAdd.Dt.Add(time.Duration(-1) * time.Second),
Todt: optAdd.Dt.Add(time.Second),
Tag: []string{"this should fail"},
})
if err != nil {
t.Error(err)
}
if len(posts) != 0 {
t.Errorf("error: PostsAll: expected 0 posts")
}
posts, err = PostsAll(nil)
if err != nil {
t.Error(err)
}
if len(posts) < 200 {
t.Errorf("error: PostsAll: expected more than 200 posts")
}
// Clean up by removing test post
err = PostsDelete(optAdd.URL)
if err != nil {
t.Errorf("error: clean up failed: %s", err)
}
}
// The following two tests don't require posting a bookmark to get
// suggested tags.
func TestPostsSuggestPopular(t *testing.T) {
got, err := PostsSuggestPopular(optAdd.URL)
if err != nil {
t.Error(err)
}
if len(got) > 1 || got[0] != "code" {
t.Error("error: expected single tag code")
}
}
func TestPostsSuggestRecommended(t *testing.T) {
got, err := PostsSuggestRecommended(optAdd.URL)
if err != nil {
t.Error(err)
}
if len(got) != 3 {
t.Error("error: expected 3 tags")
}
if got[0] != "code" {
t.Error("error: expected code tag")
}
if got[1] != "github" {
t.Error("error: expected github tag")
}
if got[2] != "IFTTT" {
t.Error("error: expected IFTTT tag")
}
}