-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalbum_test.go
More file actions
130 lines (126 loc) · 2.54 KB
/
album_test.go
File metadata and controls
130 lines (126 loc) · 2.54 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
package imgurfetch
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestAlbumMeta(t *testing.T) {
type args struct {
body []byte
status int
id string
}
type want struct {
err bool
data Album
}
tests := []struct {
name string
args args
want want
}{
{
name: "no err",
args: args{
body: []byte(`{
"data": {
"count": 2,
"images": [{
"hash": "GL7igry",
"title": "",
"description": null,
"has_sound": false,
"width": 1080,
"height": 1920,
"size": 594584,
"ext": ".png",
"animated": false,
"prefer_video": false,
"looping": false,
"datetime": "2014-11-26 15:58:34",
"edited": "0"
}, {
"hash": "tLhJOrE",
"title": "",
"description": null,
"has_sound": false,
"width": 1080,
"height": 1920,
"size": 1993181,
"ext": ".png",
"animated": false,
"prefer_video": false,
"looping": false,
"datetime": "2014-11-26 15:58:50",
"edited": "0"
}],
"include_album_ads": true
},
"success": true,
"status": 200
}`),
status: http.StatusOK,
id: "test-id",
},
want: want{
err: false,
data: Album{
Data: struct {
Images []Image `json:"images"`
}{
Images: []Image{{
Hash: "GL7igry",
Title: "",
Width: 1080,
Height: 1920,
Ext: ".png",
}, {
Hash: "tLhJOrE",
Title: "",
Width: 1080,
Height: 1920,
Ext: ".png",
}},
},
},
},
}, {
name: "http err",
args: args{
body: nil,
status: http.StatusInternalServerError,
id: "test-id",
},
want: want{
err: true,
},
},
{
name: "unmarshal err",
args: args{
body: []byte("Definitely not a valid JSON"),
status: http.StatusOK,
id: "test-id",
},
want: want{
err: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.args.status)
_, _ = w.Write(tt.args.body)
}))
got, err := AlbumMeta(srv.URL, tt.args.id)
if tt.want.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want.data, got)
}
})
}
}