-
Notifications
You must be signed in to change notification settings - Fork 0
/
author_test.go
225 lines (200 loc) · 6.29 KB
/
author_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
package goodreads
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_GetOneAuthor(t *testing.T) {
var ctx = context.TODO()
t.Run("returns the parsed author", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("fixtures/get_one_author.xml")
_, _ = fmt.Fprintln(w, string(content))
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
author, err := client.GetOneAuthor(ctx, 15388346)
assert.NoError(t, err)
assert.Equal(t, Author{
ID: 15388346,
Name: "Nicholas Eames",
About: "",
ImageURL: "https://images.gr-assets.com/authors/1468878466p5/15388346.jpg",
SmallImageURL: "https://images.gr-assets.com/authors/1468878466p2/15388346.jpg",
LargeImageURL: "https://images.gr-assets.com/authors/1468878466p7/15388346.jpg",
WorkCount: 8,
Gender: "male",
Hometown: "",
BornDate: "",
DiedAt: "",
}, author)
})
t.Run("returns error if the called failed", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "wtf", http.StatusInternalServerError)
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
author, err := client.GetOneAuthor(ctx, 15388346)
assert.EqualError(t, err, "failed to get the author #15388346: request failed for '//author/show': 500 Internal Server Error")
assert.Equal(t, Author{}, author)
})
}
func TestClient_GetAuthorBooks(t *testing.T) {
var ctx = context.TODO()
t.Run("returns the author and his books where there's results", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("fixtures/get_author_books_with_results.xml")
_, _ = fmt.Fprintln(w, string(content))
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
author, err := client.GetAuthorBooks(ctx, 15388346, 1)
assert.NoError(t, err)
assert.Equal(t, AuthorWithBooks{
Author: Author{
ID: 15388346,
Name: "Nicholas Eames",
About: "",
ImageURL: "",
SmallImageURL: "",
LargeImageURL: "",
WorkCount: 0,
Gender: "",
Hometown: "",
BornDate: "",
DiedAt: "",
},
Books: []Book{
{
ID: 30841984,
Title: "Kings of the Wyld (The Band, #1)",
Description: "good description",
ImageURL: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1477027207l/30841984._SX98_.jpg",
SmallImageURL: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1477027207l/30841984._SY75_.jpg",
NumPage: 502,
Format: "Paperback",
EditionInformation: "",
Publisher: "Orbit",
Work: Work{
WorkID: 51246585,
},
Authors: []Author{
{
ID: 15388346,
Name: "Nicholas Eames",
About: "",
ImageURL: "https://images.gr-assets.com/authors/1468878466p5/15388346.jpg",
SmallImageURL: "https://images.gr-assets.com/authors/1468878466p2/15388346.jpg",
LargeImageURL: "",
WorkCount: 0,
Gender: "",
Hometown: "",
BornDate: "",
DiedAt: "",
},
},
PublicationDate: PublicationDate{
Year: 2017,
Month: 2,
Day: 21,
},
},
{
ID: 35052265,
Title: "Bloody Rose (The Band, #2)",
Description: "good description 2",
ImageURL: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1509483649l/35052265._SX98_.jpg",
SmallImageURL: "https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1509483649l/35052265._SY75_.jpg",
NumPage: 544,
Format: "Paperback",
EditionInformation: "",
Publisher: "Orbit",
Work: Work{
WorkID: 56340013,
},
Authors: []Author{
{
ID: 15388346,
Name: "Nicholas Eames",
About: "",
ImageURL: "https://images.gr-assets.com/authors/1468878466p5/15388346.jpg",
SmallImageURL: "https://images.gr-assets.com/authors/1468878466p2/15388346.jpg",
LargeImageURL: "",
WorkCount: 0,
Gender: "",
Hometown: "",
BornDate: "",
DiedAt: "",
},
},
PublicationDate: PublicationDate{
Year: 2018,
Month: 8,
Day: 30,
},
},
},
}, author)
})
t.Run("returns the author and his empty books if no results", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("fixtures/get_author_books_with_no_results.xml")
_, _ = fmt.Fprintln(w, string(content))
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
author, err := client.GetAuthorBooks(ctx, 15388346, 1)
assert.NoError(t, err)
assert.Equal(t, AuthorWithBooks{
Author: Author{
ID: 15388346,
Name: "Nicholas Eames",
About: "",
ImageURL: "",
SmallImageURL: "",
LargeImageURL: "",
WorkCount: 0,
Gender: "",
Hometown: "",
BornDate: "",
DiedAt: "",
},
Books: []Book{},
}, author)
})
t.Run("returns an error if called failed", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "wtf", http.StatusInternalServerError)
}))
defer ts.Close()
client := client{
APIKey: "123",
domain: ts.URL,
http: ts.Client(),
}
author, err := client.GetAuthorBooks(ctx, 15388346, 1)
assert.EqualError(t, err, "failed to get the books for the author #15388346 in page #1: request failed for '//author/list': 500 Internal Server Error")
assert.Equal(t, AuthorWithBooks{}, author)
})
}