-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_cards.py
executable file
·553 lines (449 loc) · 18.4 KB
/
test_cards.py
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# encoding: utf-8
import unittest
from unittest.util import safe_repr
import redis
from django.test import TestCase
from cuesbey_main.cube_diff.models import Card, get_cards_from_names
from cuesbey_main.cube_diff import (parse_mana_cost, merge_mana_costs)
class BaseCardInserter(TestCase):
"""
Used to reset the in-memory list of what was inserted
"""
def tearDown(self):
"""
needs to reset the caches of what's inserted and what's mismatched
"""
Card.reset_names_inserted()
class SimpleTest(BaseCardInserter):
fixtures = ['mtgo_cube_og.json', 'test_names.json']
def test_create_cards_given_names(self):
for name, card_type in (('Zealous Conscripts', 'Creature'),
('Shelldock Isle', 'Land'),
('Sylvan Library', 'Enchantment')):
fetched_card = Card.get(name)
self.assertIsInstance(fetched_card, Card)
self.assertIn(card_type, fetched_card.types)
self.assertIn(fetched_card, Card.objects.all())
def test_handle_bad_name(self):
self.assertEqual([], get_cards_from_names('Lanowar Elf',
'Llanowar Elfs',
'Llan O\' War Elves'))
def test_unicode_in_weird_spots(self):
# weird unicode apostrophes from copying and pasting a cube list from
# some random website
for name, fixed_name in ((u'Moment\u2019s Peace', "Moment's Peace"),
(u'Æther Adept', u'Æther Adept'),
(u"Gideon’s Lawkeeper", "Gideon's Lawkeeper"),
(u"Night’s Whisper", "Night's Whisper")):
self.assertEqual(Card.get(name).name, fixed_name)
def test_refetching_unicode_names_searched_with_ascii(self):
# searches with ascii names sometimes return non-ascii results
# and we want to make sure that's not a problem
for ascii_name, unicode_name in [
("AEther Vial", u'Æther Vial'),
("Juzam Djinn", u'Juzám Djinn'),
("Jotun Grunt", u'Jötun Grunt')
]:
# fetch
self.assertEqual(Card.get(unicode_name).name, unicode_name)
# re-fetch
self.assertEqual(Card.get(ascii_name).name, unicode_name)
@unittest.expectedFailure # issue #14
def test_fetching_unicode_names_with_ascii_text(self):
self.assertEqual(Card.get("AEther Vial").name, u'Æther Vial')
self.assertEqual(Card.get("Juzam Djinn").name, u'Juzám Djinn')
def test_handle_apostrophe_names(self):
for name in ("Moment's Peace",
"Sensei's Divining Top"):
self.assertEqual(Card.get(name).name, name)
def assert_split_card_matches(self, search_name, name, colors):
"""
assert that the expectation matches for a given split card
:param search_name: the string to search with
:param name: the expected card name
:param colors: the expected card colors
:raise AssertionError: if the card doesn't match expectations
"""
fetched_card = Card.get(search_name)
self.assertIsInstance(fetched_card, Card)
self.assertEqual(name, fetched_card.name)
self.assertEqual(colors, fetched_card.colors)
def test_normal_split_card(self):
self.assert_split_card_matches('Fire // Ice', 'Fire // Ice',
{'Red', 'Blue'})
def test_split_card_with_different_separator(self):
self.assert_split_card_matches('Assault/Battery', 'Assault // Battery',
{'Red', 'Green'})
self.assert_split_card_matches('Fire//Ice', 'Fire // Ice',
{'Red', 'Blue'})
def test_getting_version_info(self):
self.assertIn('60', Card.get('Demonic Tutor').versions)
# split Cards take the left side
self.assertIn('27165', Card.get('Fire//Ice').versions)
@unittest.expectedFailure
def test_split_card_with_different_order(self):
self.assert_split_card_matches('Demand // Supply', 'Supply // Demand',
{'Green', 'White', 'Blue'})
def test_who_what_when_where_why(self):
wwwww = Card.get('Who/What/When/Where/Why')
self.assertIsInstance(wwwww, Card)
self.assertEqual(wwwww.name, 'Who/What/When/Where/Why')
self.assertEqual(wwwww.colors,
{'White', 'Blue', 'Black', 'Red', 'Green'})
class CardModelTest(BaseCardInserter):
fixtures = ['mtgo_cube_og.json', 'test_names.json']
def test_parse_mana_cost(self):
self.assertEqual(['5'], parse_mana_cost("{5}"))
self.assertEqual(['5'], Card.get('Batterskull').parsed_mana_cost)
self.assertEqual(['3', 'U/R', 'B'], Card.get('Slave of Bolas').parsed_mana_cost)
self.assertEqual(['3', 'U/P'], Card.get('Phyrexian Metamorph').parsed_mana_cost)
def test_activated_abilities(self):
self.assertEqual(['{1}{U}{B}'], Card.get('Creeping Tar Pit').activated_ability_mana_costs)
self.assertEqual(['{W}', '{B}'], Card.get('Stormscape Apprentice').activated_ability_mana_costs)
self.assertEqual(['{1}{G/P}'], Card.get('Birthing Pod').activated_ability_mana_costs)
self.assertEqual(['{3}', '{U}'], Card.get('Crystal Shard').activated_ability_mana_costs)
def test_handle_color_indicator(self):
self.assertEqual({'Blue'}, Card.get('Ancestral Vision').colors)
self.assertEqual({'Green'}, Card.get('Dryad Arbor').colors)
self.assertEqual({'White', 'Blue', 'Black', 'Red', 'Green'},
Card.get('Transguild Courier').colors)
@unittest.expectedFailure
def test_handle_null_color_indicator(self):
self.assertEqual(set(), Card.get('Ghostfire').colors)
class HelpersTest(TestCase):
def test_simple_cost_merge(self):
self.assertEqual(merge_mana_costs(['B'], ['B', 'B']), ['B', 'B', 'B'])
self.assertEqual(merge_mana_costs(['2', 'W'], ['R'], ['G']),
['2', 'W', 'R', 'G'])
self.assertEqual(merge_mana_costs(['X'], ['2', 'W']), ['X', '2', 'W'])
self.assertEqual(merge_mana_costs('{X}', '{2}{W}'), ['X', '2', 'W'])
class CardHeuristicTest(BaseCardInserter):
maxDiff = None
fixtures = ['mtgo_cube_og.json', 'test_names.json']
def assertHeuristicsArePresent(self, name, expected_subset,
keys_that_are_not_present=()):
actual = Card.get(name).heuristics
try:
for k in keys_that_are_not_present:
self.assertNotIn(k, actual)
self.assertDictContainsSubset(expected_subset, actual)
except AssertionError as e:
raise AssertionError(e.message + ' problem with %s' % name)
def test_handle_x_spells(self):
self.assertHeuristicsArePresent("Devil's Play", dict(
x_spells_are_infinite=dict(
converted_mana_cost=-1,
)
))
self.assertHeuristicsArePresent("Fireball", dict(
x_spells_are_infinite=dict(
converted_mana_cost=-1,
)
))
def test_handle_phyrexian(self):
self.assertHeuristicsArePresent('Phyrexian Metamorph', dict(
phyrexian_always_pays_life=dict(
mana_cost='{3}',
converted_mana_cost=3,
colors=set()
)
))
self.assertHeuristicsArePresent('Porcelain Legionnaire', dict(
phyrexian_always_pays_life=dict(
mana_cost='{2}',
converted_mana_cost=2,
colors=set()
)
))
self.assertHeuristicsArePresent('Birthing Pod', dict(
phyrexian_always_pays_life=dict(
mana_cost='{3}',
converted_mana_cost=3,
colors=set()
),
phyrexian_always_pays_life_except_for_abilities=dict(
mana_cost='{3}',
converted_mana_cost=3,
colors={'Green'}
)
))
def test_controlling_permanents_of_a_color_means_something(self):
self.assertHeuristicsArePresent('Bloodhall Ooze', dict(
caring_about_controlling_colored_permanents_affects_color=dict(
colors={'Black', 'Red', 'Green'}
)
))
self.assertHeuristicsArePresent('Thornwatch Scarecrow', dict(
caring_about_controlling_colored_permanents_affects_color=dict(
colors={'White', 'Green'}
)
))
self.assertHeuristicsArePresent('Necra Sanctuary', dict(
caring_about_controlling_colored_permanents_affects_color=dict(
colors={'White', 'Black', 'Green'}
)
))
def test_handle_color_affected_by_spells(self):
self.assertHeuristicsArePresent('Shrine of Burning Rage', dict(
caring_about_spell_colors_affects_color=dict(
colors={'Red'}
)
))
self.assertHeuristicsArePresent('Nightscape Familiar', dict(
caring_about_spell_colors_affects_color=dict(
colors={'Blue', 'Black', 'Red'}
)
))
self.assertHeuristicsArePresent('Sapphire Medallion', dict(
caring_about_spell_colors_affects_color=dict(
colors={'Blue'}
)
))
self.assertHeuristicsArePresent('Quirion Dryad', dict(
caring_about_spell_colors_affects_color=dict(
colors={'White', 'Blue', 'Black', 'Red', 'Green'}
)
))
def test_handle_off_color_flashback(self):
self.assertHeuristicsArePresent('Lingering Souls', dict(
off_color_flashback_is_gold=dict(
colors={'White', 'Black'}
)
))
def test_handle_off_color_kicker(self):
self.assertHeuristicsArePresent('Dismantling Blow', dict(
off_color_kicker_is_gold=dict(
colors={'White', 'Blue'}
)
))
self.assertHeuristicsArePresent('Thornscape Battlemage', dict(
off_color_kicker_is_gold=dict(
colors={'White', 'Red', 'Green'}
)
))
def test_handle_always_paying_kicker(self):
self.assertHeuristicsArePresent('Gatekeeper of Malakir', dict(
always_kick_creatures=dict(
mana_cost='{B}{B}{B}',
converted_mana_cost=3
),
always_kick=dict(
mana_cost='{B}{B}{B}',
converted_mana_cost=3
),
))
self.assertHeuristicsArePresent('Dismantling Blow', dict(
always_kick=dict(
mana_cost='{4}{W}{U}',
converted_mana_cost=6,
colors={'White', 'Blue'}
),
))
self.assertHeuristicsArePresent('Thornscape Battlemage', dict(
always_kick_creatures=dict(
mana_cost='{2}{W}{R}{G}',
colors={'White', 'Red', 'Green'},
converted_mana_cost=5
),
always_kick=dict(
mana_cost='{2}{W}{R}{G}',
colors={'White', 'Red', 'Green'},
converted_mana_cost=5
),
))
def test_handle_living_weapon(self):
self.assertHeuristicsArePresent('Batterskull', dict(
living_weapon_means_creature=dict(
types=["Artifact", "Creature"]
),
))
self.assertHeuristicsArePresent('Lashwrithe', dict(
living_weapon_means_creature=dict(
types=["Artifact", "Creature"]
),
))
@unittest.expectedFailure
def test_multikicker_means_x_spell(self):
self.assertHeuristicsArePresent('Everflowing Chalice', dict(
living_weapon_means_creature=dict(
mana_cost='{X}',
converted_mana_cost=-1
),
))
self.assertHeuristicsArePresent('Skitter of Lizards', dict(
multikicker_means=dict(
mana_cost='{X}{R}',
converted_mana_cost=-1
),
))
@unittest.expectedFailure
def test_morph(self):
self.assertHeuristicsArePresent('Exalted Angel', dict(
morph_cost_as_cmc=dict(
converted_mana_cost=3
),
))
self.assertHeuristicsArePresent('Bane of the Living', dict(
morph_cost_as_cmc=dict(
converted_mana_cost=3
),
))
# pay 5 life
self.assertHeuristicsArePresent('Zombie Cutthroat', dict(
morph_cost_as_cmc=dict(
converted_mana_cost=3
),
unmorph_affects_color=dict(
colors=set()
),
))
# discard a card
self.assertHeuristicsArePresent('Gathan Raiders', dict(
morph_cost_as_cmc=dict(
converted_mana_cost=3
),
unmorph_affects_color=dict(
colors=set()
),
))
# discard a zombie card
self.assertHeuristicsArePresent('Putrid Raptor', dict(
unmorph_affects_color=dict(
colors=set()
),
))
def test_token_generators_count_as_creatures(self):
self.assertHeuristicsArePresent('Lingering Souls', dict(
token_spells_are_creatures=dict(
types=['Sorcery', 'Creature']
)
))
self.assertHeuristicsArePresent('Midnight Haunting', dict(
token_spells_are_creatures=dict(
types=['Instant', 'Creature']
)
))
self.assertHeuristicsArePresent('Sarcomancy', dict(
token_spells_are_creatures=dict(
types=['Enchantment', 'Creature']
)
))
self.assertHeuristicsArePresent('Eyes in the Skies', dict(
token_spells_are_creatures=dict(
types=['Instant', 'Creature']
)
))
# things that target don't count
self.assertHeuristicsArePresent('Sundering Growth', {},
keys_that_are_not_present=['token_spells_are_creatures']
)
self.assertHeuristicsArePresent('Fists of Ironwood', {},
keys_that_are_not_present=['token_spells_are_creatures']
)
def test_cycling_as_mana_cost(self):
self.assertHeuristicsArePresent('Decree of Pain', dict(
use_cycling_cost_as_mana_cost_for_triggered_abilities=dict(
converted_mana_cost=5,
mana_cost='{3}{B}{B}'
)
))
self.assertHeuristicsArePresent('Decree of Justice', dict(
use_cycling_cost_as_mana_cost_for_triggered_abilities=dict(
mana_cost='{X}{2}{W}',
converted_mana_cost=None
)
))
# regular cycling cards don't count
self.assertHeuristicsArePresent(
'Miscalculation', {},
'use_cycling_as_mana_cost_when_there_are_effects'
)
def test_activated_abilities_affect_color(self):
self.assertHeuristicsArePresent('Kessig Wolf Run', dict(
activated_ability_costs_affect_color=dict(
colors={'Red', 'Green'}
)
))
self.assertHeuristicsArePresent('Shelldock Isle', dict(
activated_ability_costs_affect_color=dict(
colors={'Blue'}
)
))
self.assertHeuristicsArePresent('Creeping Tar Pit', dict(
activated_ability_costs_affect_color=dict(
colors={'Blue', 'Black'}
)
))
self.assertHeuristicsArePresent('Crystal Shard', dict(
activated_ability_costs_affect_color=dict(
colors={'Blue'}
)
))
expected = dict(
phyrexian_always_pays_life_except_for_abilities=dict(
colors={'Blue'}
)
)
self.assertHeuristicsArePresent(
'Spellskite', expected, [
'activated_ability_costs_affect_color',
'phyrexian_always_pays_life'
]
)
def test_mono_color_hybrids_have_modified_cmc(self):
self.assertHeuristicsArePresent('Spectral Procession', dict(
assume_on_color_cmc_for_mono_color_hybrids=dict(
converted_mana_cost=3
)
))
self.assertHeuristicsArePresent('Flame Javelin', dict(
assume_on_color_cmc_for_mono_color_hybrids=dict(
converted_mana_cost=3
)
))
def test_caring_about_land_types_affects_color(self):
self.assertHeuristicsArePresent('Wild Nacatl', dict(
caring_about_controlling_land_types_affect_color=dict(
colors={'White', 'Red', 'Green'}
)
))
self.assertHeuristicsArePresent('Crimson Muckwader', dict(
caring_about_controlling_land_types_affect_color=dict(
colors={'Black', 'Red'}
)
))
self.assertHeuristicsArePresent('Kird Ape', dict(
caring_about_controlling_land_types_affect_color=dict(
colors={'Red', 'Green'}
)
))
self.assertHeuristicsArePresent('Vedalken Shackles', dict(
caring_about_controlling_land_types_affect_color=dict(
colors={'Blue'}
)
))
def test_affinity_for_basic_land_type(self):
self.assertHeuristicsArePresent('Razor Golem', dict(
affinity_for_basic_lands_affects_mana_cost=dict(
converted_mana_cost = 3,
colors = {'White'}
)
))
self.assertHeuristicsArePresent('Dross Golem', dict(
affinity_for_basic_lands_affects_mana_cost=dict(
converted_mana_cost = 3,
colors = {'Black'}
)
))
def test_suspend_as_mana_cost(self):
self.assertHeuristicsArePresent('Greater Gargadon', dict(
suspend_as_cmc=dict(
converted_mana_cost = 1,
mana_cost = '{R}'
)
))
self.assertHeuristicsArePresent('Ancestral Vision', dict(
suspend_as_cmc=dict(
converted_mana_cost = 1,
mana_cost = '{U}')
))