def __init__(self, storage_path): self.storage = ZODB.FileStorage.FileStorage(storage_path) self.database = ZODB.DB(self.storage) self.connection = self.database.open() self.root = self.connection.root try: self.root.scryfall_cards except (AttributeError, KeyError): self.root.scryfall_cards = PCardList() try: self.root.scryfall_sets except (AttributeError, KeyError): self.root.scryfall_sets = PSetList() try: self.root.mtgio_cards except (AttributeError, KeyError): self.root.mtgio_cards = PCardList() try: self.root.mtgio_sets except (AttributeError, KeyError): self.root.mtgio_sets = PSetList()
def format_and_pack(self): """Formats the database. After this operation, the old objects are still available in 'mydata.fs.old' storage. """ answer = input('Attempting to format the whole database. Continue (y/n)?') if answer == 'y' or answer == 'yes': self.root.scryfall_sets = PSetList() self.root.scryfall_cards = PCardList() self.root.mtgio_sets = PSetList() self.root.mtgio_cards = PCardList() transaction.commit() self.database.pack()
def test_update_changed_sets(self): clean_db = MtgDB.MtgDB("clean_db.fs") c_set = clean_db.root.scryfall_sets[0] c_cards = PCardList(c_set._cards) c1_l = len(clean_db.root.scryfall_cards) s1_l = len(clean_db.root.scryfall_sets) c_set_code = c_set.code c_set_name = c_set.name c_set.code = 'xxxyyyy' c_set.name = 'xxxyyyy' self.assertTrue(c_set_code not in [pset.code for pset in clean_db.root.scryfall_sets]) self.assertTrue(c_set_name not in [pset.name for pset in clean_db.root.scryfall_sets]) self.assertTrue( 'xxxyyyy' in [pset.code for pset in clean_db.root.scryfall_sets]) self.assertTrue( 'xxxyyyy' in [pset.name for pset in clean_db.root.scryfall_sets]) clean_db.commit() clean_db.scryfall_update() self.assertTrue('xxxyyyy' not in [pset.code for pset in clean_db.root.scryfall_sets]) self.assertTrue('xxxyyyy' not in [pset.name for pset in clean_db.root.scryfall_sets]) self.assertTrue( c_set_code in [pset.code for pset in clean_db.root.scryfall_sets]) self.assertTrue( c_set_name in [pset.name for pset in clean_db.root.scryfall_sets]) self.assertEqual(c1_l, len(clean_db.root.scryfall_cards)) self.assertEqual(s1_l, len(clean_db.root.scryfall_sets)) self.assertTrue( clean_db.root.scryfall_sets.where_exactly( code=c_set_code)[0].has_all(c_cards)) self.assertEqual( len(clean_db.root.scryfall_sets.where_exactly(code=c_set_code)[0]), len(c_cards)) self.assertEqual( len(clean_db.root.scryfall_cards), sum([len(pset.cards) for pset in clean_db.root.scryfall_sets])) for pset in clean_db.root.scryfall_sets: self.assertEqual(len(pset.cards), pset.card_count) for card in pset.cards: self.assertEqual(card.set, pset.code) clean_db.verify_scryfall_integrity() clean_db.close()
def test_search(self): self.assertEqual(len(testlist.where(type_line='creature')), 54) self.assertEqual(len(testlist.where(type_line='arti')), 12) self.assertEqual(len(testlist.where(type_line='enchant')), 24) self.assertEqual(len(testlist.where(colors='R')), 24) self.assertEqual( len(testlist.where(type_line='enchant').where(colors='R')), 3) self.assertEqual( len(testlist.where(type_line='enchant').where(colors=['R'])), 3) self.assertEqual(len(creatures.where_exactly(colors='R')), 9) self.assertEqual(len(creatures.where_exactly(colors=['R'])), 9) self.assertEqual( len( non_creature_spells.where_exactly( type_line='enchantment').where(colors=['G'])), 3) self.assertEqual( len( non_creature_spells.where_exactly( type_line='enchantment').where(colors='G')), 3) self.assertEqual(len(creatures.where(colors='G')), 12) self.assertEqual(len(creatures.where_exactly(colors='G')), 6) self.assertEqual(len(creatures.where(colors=['G', 'B'])), 18) self.assertEqual( len(creatures.where_exactly(power='2', toughness='2')), 12) self.assertEqual(len(creatures.where_exactly(power='0')), 3) self.assertEqual( len( creatures.where_exactly(search_all_faces=True, power='5', toughness='1')), 3) self.assertEqual( len(creatures.where_exactly(power='0', toughness='1')), 3) self.assertEqual( len( creatures.where(search_all_faces=True, power='5').where(search_all_faces=True, toughness='1')), 3) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") testlist.where(colorrrrs='R') testlist.where(colors=None) testlist.where(colorrrrs=None) PCardList().where(colors=None) self.assertEqual(len(w), 4)
def test_groups(self): tgst = testlist.grouped_by_simple_type() tgstcards = tgst['creatures'] + tgst['noncreatures'] + tgst['lands'] self.assertEqual(len(tgstcards), len(testlist)) self.assertTrue(tgstcards.has_all(testlist)) self.assertTrue(tgst['creatures'].has_all(creatures)) self.assertTrue(tgst['noncreatures'].has_all(non_creature_spells + planeswalkers + other)) self.assertTrue(tgst['lands'].has_all(basic_lands + non_basic_lands)) self.assertFalse(tgst['creatures'].has_any(non_creature_spells)) self.assertFalse(tgst['creatures'].has_any(non_basic_lands)) tgcmc = testlist.grouped_by_converted_mana_cost() tgcmccards = PCardList( list(card for sublist in tgcmc.values() for card in sublist)) self.assertEqual(len(tgcmccards), len(testlist)) self.assertTrue(tgcmccards.has_all(testlist)) self.assertTrue( testlist.where_exactly( name="Ajani Steadfast Emblem")[0] in tgcmc[0]) self.assertTrue(testlist.where_exactly(name="Ashnod")[0] in tgcmc[0]) self.assertTrue(testlist.where_exactly(name="Clue")[0] in tgcmc[0]) self.assertTrue( testlist.where_exactly(name="Blazing Torch")[0] in tgcmc[1]) self.assertTrue( testlist.where_exactly(name="Wild Mongrel")[0] in tgcmc[2]) self.assertTrue( testlist.where_exactly(name="Appeal // Authority")[0] in tgcmc[3]) self.assertTrue( testlist.where_exactly(name="Ajani Vengeant")[0] in tgcmc[4]) self.assertTrue( testlist.where_exactly(name="Trapjaw Tyrant")[0] in tgcmc[5]) tgci = testlist.grouped_by_color_identity() tgciards = PCardList( list(card for sublist in tgci.values() for card in sublist)) self.assertEqual(len(tgciards), len(testlist)) self.assertTrue(tgciards.has_all(testlist)) self.assertTrue( testlist.where_exactly( name="Ajani Steadfast Emblem")[0] in tgci['']) self.assertTrue(testlist.where_exactly(name="Ashnod")[0] in tgci['']) self.assertTrue(testlist.where_exactly(name="Clue")[0] in tgci['']) self.assertTrue( testlist.where_exactly(name="Blazing Torch")[0] in tgci['']) self.assertTrue( testlist.where_exactly(name="Wild Mongrel")[0] in tgci['G']) self.assertTrue( testlist.where_exactly( name="Appeal // Authority")[0] in tgci['GW']) self.assertTrue( testlist.where_exactly(name="Ajani Vengeant")[0] in tgci['RW']) self.assertTrue(testlist.where_exactly(name="Angel")[0] in tgci['W']) self.assertTrue( testlist.where_exactly(name="Dryad Arbor")[0] in tgci['G']) self.assertTrue(testlist.where_exactly(name="Forest")[0] in tgci['G']) self.assertTrue( cards.where_exactly(name="Izzet Signet")[0] in tgci['RU']) tgc = testlist.grouped_by_color() tgcards = PCardList( list(card for sublist in tgc.values() for card in sublist)) self.assertEqual(len(tgcards), len(testlist)) self.assertTrue(tgcards.has_all(testlist)) self.assertTrue( testlist.where_exactly( name="Ajani Steadfast Emblem")[0] in tgc['']) self.assertTrue(testlist.where_exactly(name="Ashnod")[0] in tgc['']) self.assertTrue(testlist.where_exactly(name="Clue")[0] in tgc['']) self.assertTrue( testlist.where_exactly(name="Blazing Torch")[0] in tgc['']) self.assertTrue( testlist.where_exactly(name="Wild Mongrel")[0] in tgc['G']) self.assertTrue( testlist.where_exactly(name="Appeal // Authority")[0] in tgc['GW']) self.assertTrue( testlist.where_exactly(name="Ajani Vengeant")[0] in tgc['RW']) self.assertTrue(testlist.where_exactly(name="Angel")[0] in tgc['W']) self.assertTrue( testlist.where_exactly(name="Dryad Arbor")[0] in tgc['G']) self.assertTrue(testlist.where_exactly(name="Forest")[0] in tgc['']) self.assertTrue( testlist.where_exactly(name="Izzet Signet")[0] in tgc['']) cards.grouped_by_color() cards.grouped_by_color_identity() cards.grouped_by_converted_mana_cost() cards.grouped_by_simple_type() cards.mana_symbol_counts()
def test_basic(self): self.assertEqual(len(tokens + other), 24) self.assertEqual(len(planeswalkers + other), 21) self.assertEqual(len(basic_lands + non_basic_lands), 27) self.assertEqual(len(testlist), 144) self.assertEqual(len(basic_lands), 15) self.assertEqual(len(non_basic_lands), 12) self.assertEqual(len(creatures), 48) self.assertEqual(len(non_creature_spells), 36) self.assertEqual(len(planeswalkers), 9) self.assertEqual(len(tokens), 12) self.assertEqual(len(other), 12) self.assertEqual(len(testlist.basic_lands()), 15) self.assertEqual(len(testlist.creatures()), 54) self.assertEqual(len(testlist.lands()), 30) self.assertEqual(len(testlist.noncreatures()), 90) self.assertEqual(len(testlist.unique_cards()), 48) self.assertEqual(len(testlist.unique_names()), 48) self.assertEqual(len(testlist.normal_playable_cards()), 120) self.assertEqual(len(testlist - testlist.lands()), 114) self.assertEqual(len(testlist - testlist.creatures()), 90) self.assertEqual(len(testlist + testlist.lands()), 174) self.assertEqual(len(testlist + testlist.creatures()), 198) self.assertEqual(len(testlist * 2), 288) self.assertEqual(len(2 * testlist), 288) self.assertEqual(len(5 * testlist[0:1]), 5) self.assertTrue(testlist[0] in 3 * testlist[0:1]) self.assertTrue(testlist.has_all(creatures)) self.assertTrue(testlist.has_any(creatures)) self.assertTrue(testlist.has_all(planeswalkers)) self.assertTrue(testlist.has_any(planeswalkers)) self.assertTrue(testlist.has_all(other)) self.assertTrue(testlist.has_any(other)) self.assertTrue(testlist.has_all(non_creature_spells)) self.assertTrue(testlist.has_any(non_creature_spells)) self.assertTrue(testlist.has_all(non_basic_lands)) self.assertTrue(testlist.has_any(non_basic_lands)) self.assertTrue(testlist.has_all(basic_lands)) self.assertTrue(testlist.has_any(basic_lands)) self.assertTrue(testlist.has_all(creatures + creatures[0])) self.assertTrue(testlist.has_any(creatures + creatures[0])) self.assertTrue(testlist.has_all(planeswalkers + creatures[0])) self.assertTrue(testlist.has_any(planeswalkers + creatures[0])) self.assertTrue(testlist.has_all(other + creatures[0])) self.assertTrue(testlist.has_any(other + creatures[0])) self.assertTrue(testlist.has_all(non_creature_spells + creatures[0])) self.assertTrue(testlist.has_any(non_creature_spells + creatures[0])) self.assertTrue(testlist.has_all(non_basic_lands + creatures[0])) self.assertTrue(testlist.has_any(non_basic_lands + creatures[0])) self.assertTrue(testlist.has_all(basic_lands + creatures[0])) self.assertTrue(testlist.has_any(basic_lands + creatures[0])) self.assertFalse(cards.where_exactly(name='forest')[1] in basic_lands) self.assertFalse( cards.where_exactly(name='bayou')[1] in non_basic_lands) self.assertFalse( cards.where_exactly(name='island')[0] in non_basic_lands) self.assertTrue(cards.where_exactly(name='forest')[0] in basic_lands) self.assertTrue( cards.where_exactly(name='bayou')[0] in non_basic_lands) self.assertTrue( cards.where_exactly(name='Dryad Arbor')[0] in creatures) list1 = PCardList(creatures[0:3]) list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) list1.append(creatures[7]) self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 3) list2.append(creatures[7]) self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 4) list1 = PCardList([creatures[0], creatures[1], creatures[2]]) list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) list1.append(creatures[7]) self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 3) list2.append(creatures[7]) self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 4) list1 = PCardList(creatures[0:3]) list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) list1 += creatures[7] self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 3) list2 += creatures[7] self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 4) list1 = PCardList(creatures[0:3]) list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) list1 += list2 self.assertEqual(len(list1), 6) self.assertEqual(len(list2), 3) list2 += list1 self.assertEqual(len(list1), 6) self.assertEqual(len(list2), 9) list1 = creatures[0:3] list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) list1 += list2 self.assertEqual(len(list1), 6) self.assertEqual(len(list2), 3) list2 += list1 self.assertEqual(len(list1), 6) self.assertEqual(len(list2), 9) list1 = [creatures[0], creatures[1], creatures[2]] list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) self.assertEqual(len(list2 + list1), 6) self.assertEqual(len(list1 + list2), 6) self.assertEqual(len(list2 + list1[0]), 4) self.assertEqual(len(list1[0] + list2), 4) list2 += list1 self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 6) list2 += list1[0] self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 7) list1 = PCardList() + creatures[0] + creatures[1] + creatures[2] list2 = PCardList(list1) self.assertEqual(len(list1), 3) self.assertEqual(len(list2), 3) list1.append(creatures[7]) self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 3) list2.append(creatures[7]) self.assertEqual(len(list1), 4) self.assertEqual(len(list2), 4)