-
Notifications
You must be signed in to change notification settings - Fork 11
/
card_test.go
352 lines (321 loc) · 13.7 KB
/
card_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
package scryfall
import (
"context"
"net/http"
"reflect"
"testing"
)
// TODO: Replace urls with the new
// https://scryfall.com/blog/upcoming-api-changes-to-scryfall-image-uris-and-download-uris-221
const duskDawnJSON = `{"object": "card", "id": "937dbc51-b589-4237-9fce-ea5c757f7c48", "oracle_id": "7bc3f92f-68a2-4934-afc4-89f6d0e8cf98", "multiverse_ids": [426912], "mtgo_id": 64026, "mtgo_foil_id": 64027, "name": "Dusk // Dawn", "lang": "en", "uri": "https://api.scryfall.com/cards/akh/210", "scryfall_uri": "https://scryfall.com/card/akh/210?utm_source=api", "layout": "split", "highres_image": true, "image_uris": {"small": "https://img.scryfall.com/cards/small/en/akh/210a.jpg?1519586027", "normal": "https://img.scryfall.com/cards/normal/en/akh/210a.jpg?1519586027", "large": "https://img.scryfall.com/cards/large/en/akh/210a.jpg?1519586027", "png": "https://img.scryfall.com/cards/png/en/akh/210a.png?1519586027", "art_crop": "https://img.scryfall.com/cards/art_crop/en/akh/210a.jpg?1519586027", "border_crop": "https://img.scryfall.com/cards/border_crop/en/akh/210a.jpg?1519586027"}, "cmc": 9, "mana_cost": "{2}{W}{W} // {3}{W}{W}", "colors": ["W"], "color_identity": ["W"], "card_faces": [{"object": "card_face", "name": "Dusk", "mana_cost": "{2}{W}{W}", "type_line": "Sorcery", "oracle_text": "Destroy all creatures with power 3 or greater.", "illustration_id": "f3d63aed-2784-4ef5-9676-846b1e65e040"}, {"object": "card_face", "name": "Dawn", "mana_cost": "{3}{W}{W}", "type_line": "Sorcery", "oracle_text": "Aftermath (Cast this spell only from your graveyard. Then exile it.)\nReturn all creature cards with power 2 or less from your graveyard to your hand."}], "legalities": {"standard": "legal", "modern": "legal", "pauper": "not_legal", "pioneer": "legal", "legacy": "legal", "penny": "not_legal", "vintage": "legal", "duel": "legal", "commander": "legal", "future": "legal"}, "reserved": false, "reprint": false, "set": "akh", "set_name": "Amonkhet", "set_uri": "https://api.scryfall.com/sets/akh", "set_search_uri": "https://api.scryfall.com/cards/search?order=set&q=e%3Aakh&unique=prints", "scryfall_set_uri": "https://scryfall.com/sets/akh?utm_source=api", "rulings_uri": "https://api.scryfall.com/cards/akh/210/rulings", "prints_search_uri": "https://api.scryfall.com/cards/search?order=set&q=%21%E2%80%9CDusk+%2F%2F+Dawn%E2%80%9D&unique=prints", "collector_number": "210", "digital": false, "rarity": "rare", "illustration_id": "f3d63aed-2784-4ef5-9676-846b1e65e040", "artist": "Noah Bradley", "frame": "2015", "full_art": false, "border_color": "black", "edhrec_rank": 942, "prices": {"usd": "1.02", "usd_foil": "1.79", "tix": "0.02", "eur": "0.75"}, "related_uris": {"gatherer": "http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=426912", "tcgplayer_decks": "http://decks.tcgplayer.com/magic/deck/search?contains=Dusk+%2F%2F+Dawn&page=1&partner=Scryfall", "edhrec": "http://edhrec.com/route/?cc=Dusk", "mtgtop8": "http://mtgtop8.com/search?MD_check=1&SB_check=1&cards=Dusk+%2F%2F+Dawn"}, "purchase_uris": {"tcgplayer": "https://scryfall.com/s/tcgplayer/129823", "cardmarket": "https://scryfall.com/s/mcm/296759", "cardhoarder": "https://www.cardhoarder.com/cards/64026?affiliate_id=scryfall&ref=card-profile&utm_campaign=affiliate&utm_medium=card&utm_source=scryfall"}}`
var duskDawn = Card{
ID: "937dbc51-b589-4237-9fce-ea5c757f7c48",
OracleID: "7bc3f92f-68a2-4934-afc4-89f6d0e8cf98",
MultiverseIDs: []int{426912},
MTGOID: intPointer(64026),
MTGOFoilID: intPointer(64027),
Name: "Dusk // Dawn",
Lang: LangEnglish,
URI: "https://api.scryfall.com/cards/akh/210",
ScryfallURI: "https://scryfall.com/card/akh/210?utm_source=api",
Layout: LayoutSplit,
HighresImage: true,
ImageURIs: &ImageURIs{
Small: "https://img.scryfall.com/cards/small/en/akh/210a.jpg?1519586027",
Normal: "https://img.scryfall.com/cards/normal/en/akh/210a.jpg?1519586027",
Large: "https://img.scryfall.com/cards/large/en/akh/210a.jpg?1519586027",
PNG: "https://img.scryfall.com/cards/png/en/akh/210a.png?1519586027",
ArtCrop: "https://img.scryfall.com/cards/art_crop/en/akh/210a.jpg?1519586027",
BorderCrop: "https://img.scryfall.com/cards/border_crop/en/akh/210a.jpg?1519586027",
},
CMC: 9,
ManaCost: "{2}{W}{W} // {3}{W}{W}",
Colors: []Color{ColorWhite},
ColorIdentity: []Color{ColorWhite},
CardFaces: []CardFace{
{
Name: "Dusk",
ManaCost: "{2}{W}{W}",
TypeLine: "Sorcery",
OracleText: stringPointer("Destroy all creatures with power 3 or greater."),
IllustrationID: stringPointer("f3d63aed-2784-4ef5-9676-846b1e65e040"),
},
{
Name: "Dawn",
ManaCost: "{3}{W}{W}",
TypeLine: "Sorcery",
OracleText: stringPointer("Aftermath (Cast this spell only from your graveyard. Then exile it.)\nReturn all creature cards with power 2 or less from your graveyard to your hand."),
},
},
Legalities: Legalities{
Standard: "legal",
Modern: "legal",
Pauper: "not_legal",
Pioneer: "legal",
Legacy: "legal",
Penny: "not_legal",
Vintage: "legal",
Duel: "legal",
Commander: "legal",
Future: "legal",
},
Reserved: false,
Reprint: false,
Set: "akh",
SetName: "Amonkhet",
SetURI: "https://api.scryfall.com/sets/akh",
SetSearchURI: "https://api.scryfall.com/cards/search?order=set&q=e%3Aakh&unique=prints",
ScryfallSetURI: "https://scryfall.com/sets/akh?utm_source=api",
RulingsURI: "https://api.scryfall.com/cards/akh/210/rulings",
PrintsSearchURI: "https://api.scryfall.com/cards/search?order=set&q=%21%E2%80%9CDusk+%2F%2F+Dawn%E2%80%9D&unique=prints",
CollectorNumber: "210",
Digital: false,
Rarity: "rare",
IllustrationID: stringPointer("f3d63aed-2784-4ef5-9676-846b1e65e040"),
Artist: stringPointer("Noah Bradley"),
Frame: Frame2015,
FrameEffects: nil,
FullArt: false,
BorderColor: "black",
EDHRECRank: intPointer(942),
Prices: Prices{
USD: "1.02",
USDFoil: "1.79",
EUR: "0.75",
Tix: "0.02",
},
RelatedURIs: RelatedURIs{
Gatherer: "http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=426912",
TCGPlayerDecks: "http://decks.tcgplayer.com/magic/deck/search?contains=Dusk+%2F%2F+Dawn&page=1&partner=Scryfall",
EDHREC: "http://edhrec.com/route/?cc=Dusk",
MTGTop8: "http://mtgtop8.com/search?MD_check=1&SB_check=1&cards=Dusk+%2F%2F+Dawn",
},
PurchaseURIs: PurchaseURIs{
TCGPlayer: "https://scryfall.com/s/tcgplayer/129823",
CardMarket: "https://scryfall.com/s/mcm/296759",
CardHoarder: "https://www.cardhoarder.com/cards/64026?affiliate_id=scryfall&ref=card-profile&utm_campaign=affiliate&utm_medium=card&utm_source=scryfall",
},
}
func TestSearchCards(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
q := query.Get("q")
unique := query.Get("unique")
order := query.Get("order")
dir := query.Get("dir")
includeExtras := query.Get("include_extras")
page := query.Get("page")
if q != "dusk" && unique != "cards" && order != "power" && dir != "auto" && includeExtras != "true" && page != "2" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write([]byte(`{"object": "list", "total_cards": 1000, "has_more": true, "next_page": "https://api.scryfall.com/cards?page=3", "data": [` + duskDawnJSON + `]}`))
})
client, ts, err := setupTestServer("/cards/search", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
opts := SearchCardsOptions{
Unique: UniqueModeCards,
Order: OrderPower,
Dir: DirAuto,
IncludeExtras: true,
Page: 2,
}
cards, err := client.SearchCards(ctx, "dusk", opts)
if err != nil {
t.Fatalf("Error listing cards: %v", err)
}
want := CardListResponse{
Cards: []Card{duskDawn},
HasMore: true,
NextPage: stringPointer("https://api.scryfall.com/cards?page=3"),
TotalCards: 1000,
}
if !reflect.DeepEqual(cards, want) {
t.Errorf("got: %#v want: %#v", cards, want)
}
}
func TestGetCardByName(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
set := query.Get("set")
fuzzyName := query.Get("fuzzy")
if set != "akh" && fuzzyName != "Dusk" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/named", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
opts := GetCardByNameOptions{
Set: "akh",
}
card, err := client.GetCardByName(ctx, "Dusk", false, opts)
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}
func TestAutocompleteCard(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query().Get("q")
if q != "thal" {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Write([]byte(`{"object": "catalog", "total_items": 20, "data": ["Thallid", "Thorn Thallid", "Thalakos Seer", "Thalakos Scout", "Thalia's Lancers", "Thalakos Sentry", "Thallid Devourer", "Thalakos Deceiver", "Thalakos Drifters", "Thalakos Lowlands", "Thalakos Mistfolk", "Thallid Soothsayer", "Thallid Germinator", "Thalia's Lieutenant", "Thallid Shell-Dweller", "Thalia, Heretic Cathar", "Thalakos Dreamsower", "Thalia, Guardian of Thraben", "Tukatongue Thallid", "Lethal Sting"]}`))
})
client, ts, err := setupTestServer("/cards/autocomplete", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
autocompletions, err := client.AutocompleteCard(ctx, "thal")
if err != nil {
t.Fatalf("Error auto completing card: %v", err)
}
want := []string{
"Thallid",
"Thorn Thallid",
"Thalakos Seer",
"Thalakos Scout",
"Thalia's Lancers",
"Thalakos Sentry",
"Thallid Devourer",
"Thalakos Deceiver",
"Thalakos Drifters",
"Thalakos Lowlands",
"Thalakos Mistfolk",
"Thallid Soothsayer",
"Thallid Germinator",
"Thalia's Lieutenant",
"Thallid Shell-Dweller",
"Thalia, Heretic Cathar",
"Thalakos Dreamsower",
"Thalia, Guardian of Thraben",
"Tukatongue Thallid",
"Lethal Sting",
}
if !reflect.DeepEqual(autocompletions, want) {
t.Errorf("got: %#v want: %#v", autocompletions, want)
}
}
func TestGetRandomCard(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/random", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
card, err := client.GetRandomCard(ctx)
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}
func TestGetCardByMultiverseID(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/multiverse/426912", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
card, err := client.GetCardByMultiverseID(ctx, 426912)
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}
func TestGetCardByArenaID(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/arena/67330", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
card, err := client.GetCardByArenaID(ctx, 67330)
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}
func TestGetCardByMTGOID(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/mtgo/64026", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
card, err := client.GetCardByMTGOID(ctx, 64026)
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}
func TestGetCardBySetCodeAndCollectorNumber(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/akh/210", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
card, err := client.GetCardBySetCodeAndCollectorNumber(ctx, "akh", "210")
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}
func TestGetCard(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(duskDawnJSON))
})
client, ts, err := setupTestServer("/cards/937dbc51-b589-4237-9fce-ea5c757f7c48", handler)
if err != nil {
t.Fatalf("Error setting up test server: %v", err)
}
defer ts.Close()
ctx := context.Background()
card, err := client.GetCard(ctx, "937dbc51-b589-4237-9fce-ea5c757f7c48")
if err != nil {
t.Fatalf("Error getting card: %v", err)
}
if !reflect.DeepEqual(card, duskDawn) {
t.Errorf("got: %#v want: %#v", card, duskDawn)
}
}