class Test(unittest.TestCase): def setUp(self): self.card_1 = Card("Geography", "What is the capital of Alaska?", "Juneau") self.card_2 = Card( "STEM", "The Viking spacecraft sent back to Earth photographs and reports about the surface of which planet?", "Mars") self.card_3 = Card( "STEM", "Describe in words the exact direction that is 697.5° clockwise from due north?", "North north west") self.deck = Deck([self.card_1, self.card_2, self.card_3]) def test_it_has_attributes(self): self.assertEqual(self.deck.cards, [self.card_1, self.card_2, self.card_3]) def test_add_card(self): self.assertEqual(self.deck.cards, [self.card_1, self.card_2, self.card_3]) self.card4 = Card("Geography", "What is it?", "I don't know!") expected = [self.card_1, self.card_2, self.card_3, self.card4] self.deck.add_card(self.card4) self.assertEqual(self.deck.cards, expected) def test_it_can_get_all_categories(self): expected = ['STEM', 'Geography'] self.assertEqual(self.deck.all_categories(), expected)
class TestDeck(unittest.TestCase): def setUp(self): self.card_one = Card("Heart", "5", 5) self.card_two = Card("Diamond", "Jack", 11) self.card_three = Card("Spade", "7", 7) self.card_four = Card("Spade", "Ace", 14) cards = [self.card_one, self.card_two, self.card_three, self.card_four] self.deck = Deck(cards) def test_it_exists(self): assert type(self.deck) == Deck assert type(self.deck.cards[0]) == Card def test_value_of_card_at(self): assert self.deck.value_of_card_at(0) == 5 assert self.deck.value_of_card_at(1) == 11 assert self.deck.value_of_card_at(2) == 7 def test_high_ranking_cards(self): assert self.deck.high_ranking_cards() == [ self.card_two, self.card_four ] def test_percent_high_ranking(self): assert self.deck.percent_high_ranking() == 0.5 def test_remove_card(self): self.deck.remove_card() assert len(self.deck.cards) == 3 def test_add_card(self): new_card = Card("Heart", "King", 13) self.deck.add_card(new_card) assert len(self.deck.cards) == 5
def test_add_card(self): """add_card() interface tests.""" deck_name = 'test deck' cards = ['card 0', 'card 1', 'card 2'] deck = Deck(deck_name) for idx, c in enumerate(cards, 1): deck.add_card(c) self.assertEqual(len(deck), idx)
def test_equality(self): """__eq__() tests.""" other_deck = Deck('other deck') for c in self.cards: other_deck.add_card(c) other_other_deck = Deck(self.deck_name) self.assertEqual(self.deck, self.deck) self.assertNotEqual(self.deck, other_deck) self.assertNotEqual(self.deck, other_other_deck) self.assertNotEqual(self.deck, 'not a deck')
def swap(args): """Swap-create a new flashcard deck. Create a new flashcard deck by swapping questions and answers. Args: args (argparse.Namespace): command line arguments. """ print('Swapping questions and answers from {} and saving to {}.'.format( args.deck, args.dest)) src = Deck.load(args.deck) dest = Deck(src.name) for c in src: dest.add_card(Flashcard(c.answer, c.question)) dest.save(args.dest)
def create(args): """Create a new flashcard deck. Args: args (argparse.Namespace): command line arguments. """ _logger.info('Creating deck {}.'.format(args.deck)) if os.path.exists(args.deck): raise ValueError('{} already exists.'.format(args.deck)) name = input('Deck Name: ') deck = Deck(name) print('Enter an empty question to finish.') for idx in itertools.count(1): q = input('Question #{}: '.format(idx)) if not q: break a = input('Answer #{}: '.format(idx)) deck.add_card(Flashcard(q, a)) deck.save(args.deck)
class DeckManipulationTestCase(unittest.TestCase): def setUp(self): self.deck_name = 'test deck' self.deck = Deck(self.deck_name) self.cards = [ Flashcard('q0', 'a0', 0, 0, str(datetime.datetime.utcnow())), Flashcard('q1', 'a1', 0, 0, str(datetime.datetime.utcnow())), Flashcard('q2', 'a2', 0, 0, str(datetime.datetime.utcnow())) ] for c in self.cards: self.deck.add_card(c) def test_iterate(self): """Iteration tests.""" self.assertEqual( frozenset(c for c in self.deck), frozenset(self.cards) ) def test_len(self): """__len__() tests.""" self.assertEqual(len(self.cards), len(self.deck)) def test_equality(self): """__eq__() tests.""" other_deck = Deck('other deck') for c in self.cards: other_deck.add_card(c) other_other_deck = Deck(self.deck_name) self.assertEqual(self.deck, self.deck) self.assertNotEqual(self.deck, other_deck) self.assertNotEqual(self.deck, other_other_deck) self.assertNotEqual(self.deck, 'not a deck') def test_load_and_save(self): """load() and save() interface tests.""" # Save and load the deck. with tempfile.NamedTemporaryFile(mode='w', newline='') as tf: self.deck.save(tf.name, overwrite=True) saved_deck = Deck.load(tf.name) # Test. self.assertEqual(saved_deck, self.deck) def test_sample_deck_file(self): """Load sample deck from README.md test.""" sample = """Name: Sample Deck Quiz: Question,Answer,Attempts,Correct,Last Shown 1 + 1, 2 1 + 2, 3 1 + 3, 4 1 + 4, 5 1 + 5, 6 1 + 6, 7 1 + 7, 8 1 + 8, 9 1 + 9, 10 """ with tempfile.NamedTemporaryFile(mode='w', newline='') as tf: with open(tf.name, 'w') as f: f.write(sample) saved_deck = Deck.load(tf.name) self.assertEqual(saved_deck.name, 'Sample Deck') self.assertEqual(len(saved_deck), 9) def test_answers(self): """answers() interface tests.""" expected_answers = frozenset([c.answer for c in self.cards]) self.assertEqual(frozenset(self.deck.answers()), expected_answers)