DRY GetReactions test
Ryan Carver
8 years ago
140 | 140 | } |
141 | 141 | } |
142 | 142 | |
143 | func TestSlack_GetReaction_ToMessage(t *testing.T) { | |
143 | func TestSlack_GetReactions(t *testing.T) { | |
144 | 144 | once.Do(startServer) |
145 | 145 | SLACK_API = "http://" + serverAddr + "/" |
146 | 146 | api := New("testing-token") |
147 | getReactionRes = `{"ok": true, | |
147 | tests := []struct { | |
148 | params GetReactionParameters | |
149 | wantParams map[string]string | |
150 | json string | |
151 | wantReactions []ItemReaction | |
152 | }{ | |
153 | { | |
154 | ||
155 | GetReactionParameters{ItemRef: NewRefToMessage("ChannelID", "123")}, | |
156 | map[string]string{ | |
157 | "channel": "ChannelID", | |
158 | "timestamp": "123", | |
159 | }, | |
160 | `{"ok": true, | |
148 | 161 | "message": { |
149 | 162 | "type": "message", |
150 | 163 | "message": { |
161 | 174 | } |
162 | 175 | ] |
163 | 176 | } |
164 | }}` | |
165 | want := []ItemReaction{ | |
166 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, | |
167 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, | |
168 | } | |
169 | wantParams := map[string]string{ | |
170 | "channel": "ChannelID", | |
171 | "timestamp": "123", | |
172 | } | |
173 | gotParams = map[string]string{} | |
174 | params := NewGetReactionParameters(NewRefToMessage("ChannelID", "123")) | |
175 | got, err := api.GetReactions(params) | |
176 | if err != nil { | |
177 | t.Fatalf("Unexpected error: %s", err) | |
178 | } | |
179 | if !reflect.DeepEqual(got, want) { | |
180 | t.Errorf("Got reaction %#v, want %#v", got, want) | |
181 | } | |
182 | if !reflect.DeepEqual(gotParams, wantParams) { | |
183 | t.Errorf("Got params %#v, want %#v", gotParams, wantParams) | |
184 | } | |
185 | } | |
186 | ||
187 | func TestSlack_GetReaction_ToFile(t *testing.T) { | |
188 | once.Do(startServer) | |
189 | SLACK_API = "http://" + serverAddr + "/" | |
190 | api := New("testing-token") | |
191 | getReactionRes = `{"ok": true, | |
177 | }}`, | |
178 | []ItemReaction{ | |
179 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, | |
180 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, | |
181 | }, | |
182 | }, | |
183 | { | |
184 | GetReactionParameters{ItemRef: NewRefToFile("FileID"), Full: true}, | |
185 | map[string]string{ | |
186 | "file": "FileID", | |
187 | "full": "true", | |
188 | }, | |
189 | `{"ok": true, | |
192 | 190 | "message": { |
193 | 191 | "type": "file", |
194 | 192 | "file": { |
205 | 203 | } |
206 | 204 | ] |
207 | 205 | } |
208 | }}` | |
209 | want := []ItemReaction{ | |
210 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, | |
211 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, | |
212 | } | |
213 | wantParams := map[string]string{ | |
214 | "file": "FileID", | |
215 | "full": "true", | |
216 | } | |
217 | gotParams = map[string]string{} | |
218 | params := NewGetReactionParameters(NewRefToFile("FileID")) | |
219 | params.Full = true | |
220 | got, err := api.GetReactions(params) | |
221 | if err != nil { | |
222 | t.Fatalf("Unexpected error: %s", err) | |
223 | } | |
224 | if !reflect.DeepEqual(got, want) { | |
225 | t.Errorf("Got reaction %#v, want %#v", got, want) | |
226 | } | |
227 | if !reflect.DeepEqual(gotParams, wantParams) { | |
228 | t.Errorf("Got params %#v, want %#v", gotParams, wantParams) | |
229 | } | |
230 | } | |
231 | ||
232 | func TestSlack_GetReaction_ToFileComment(t *testing.T) { | |
233 | once.Do(startServer) | |
234 | SLACK_API = "http://" + serverAddr + "/" | |
235 | api := New("testing-token") | |
236 | getReactionRes = `{"ok": true, | |
206 | }}`, | |
207 | []ItemReaction{ | |
208 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, | |
209 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, | |
210 | }, | |
211 | }, | |
212 | { | |
213 | ||
214 | GetReactionParameters{ItemRef: NewRefToFileComment("FileCommentID")}, | |
215 | map[string]string{ | |
216 | "file_comment": "FileCommentID", | |
217 | }, | |
218 | `{"ok": true, | |
237 | 219 | "message": { |
238 | 220 | "type": "file_comment", |
239 | 221 | "file_comment": { |
252 | 234 | ] |
253 | 235 | } |
254 | 236 | } |
255 | }}` | |
256 | want := []ItemReaction{ | |
257 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, | |
258 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, | |
259 | } | |
260 | wantParams := map[string]string{ | |
261 | "file_comment": "FileCommentID", | |
262 | } | |
263 | gotParams = map[string]string{} | |
264 | params := NewGetReactionParameters(NewRefToFileComment("FileCommentID")) | |
265 | got, err := api.GetReactions(params) | |
266 | if err != nil { | |
267 | t.Fatalf("Unexpected error: %s", err) | |
268 | } | |
269 | if !reflect.DeepEqual(got, want) { | |
270 | t.Errorf("Got reaction %#v, want %#v", got, want) | |
271 | } | |
272 | if !reflect.DeepEqual(gotParams, wantParams) { | |
273 | t.Errorf("Got params %#v, want %#v", gotParams, wantParams) | |
237 | }}`, | |
238 | []ItemReaction{ | |
239 | ItemReaction{Name: "astonished", Count: 3, Users: []string{"U1", "U2", "U3"}}, | |
240 | ItemReaction{Name: "clock1", Count: 3, Users: []string{"U1", "U2"}}, | |
241 | }, | |
242 | }, | |
243 | } | |
244 | for i, test := range tests { | |
245 | gotParams = map[string]string{} | |
246 | getReactionRes = test.json | |
247 | got, err := api.GetReactions(test.params) | |
248 | if err != nil { | |
249 | t.Fatalf("%d: Unexpected error: %s", i, err) | |
250 | } | |
251 | if !reflect.DeepEqual(got, test.wantReactions) { | |
252 | t.Errorf("%d: Got reaction %#v, want %#v", i, got, test.wantReactions) | |
253 | } | |
254 | if !reflect.DeepEqual(gotParams, test.wantParams) { | |
255 | t.Errorf("%d: Got params %#v, want %#v", i, gotParams, test.wantParams) | |
256 | } | |
274 | 257 | } |
275 | 258 | } |
276 | 259 |