def test_get_or_initialize_donor_new_user(): """ test adding of new donor """ mailroom = Mailroom() donor = mailroom.get_or_initialize_donor("Test User") assert isinstance(donor, Donor) assert donor.name == "Test User" assert donor in mailroom.donors
def test_send_thanks_single_donation(): """ test thank you letter """ mailroom = Mailroom() donor = Donor("Monty Python") donor.add_donation(10) assert "\n\nDear Monty Python,\nThank you for your donation of 10.\n\n" == mailroom.send_thanks( donor)
def test_mailroom_add_donation(): """ test add donation workflow """ mailroom = Mailroom() letter = mailroom.add_donation("Monty Python", 10) assert "Monty Python" == mailroom.donors[0].name assert [10] == mailroom.donors[0].get_donations() assert "\n\nDear Monty Python,\nThank you for your donation of 10.\n\n" == letter
def test_mailroom_get_menu(): """ test menu """ mailroom = Mailroom() assert { 1: "Send a Thank You", 2: "Create a Report", 3: "Quit" } == mailroom.get_menu()
def test_challenge(): mailroom = Mailroom() letter = mailroom.add_donation("Matt", 100) letter = mailroom.add_donation("Joe", 40) reports = mailroom.challenge(4, max_donation=50) assert len(reports) == 2 assert reports[0] == CHALLENGE_REPORT_ORIGINAL assert reports[1] == CHALLENGE_REPORT_MATCHING
def test_populate_donors_from_invalid_data_dict(self): mailroom = Mailroom() sample_donor_data = \ {'Mark Zuckerberg':[5000.50,1089.90,750.45], 'Jeff Bezos':[700.90,'not a number'], 'Paul Allen':[11100.80], 'Joanne K Rowling':[1199.90,9878.12]} with self.assertRaises(ValueError): mailroom.populate_donors_from_dict(sample_donor_data)
def test_mailroom_get_menu(): """ test menu """ mailroom = Mailroom() assert { 1: "Send a Thank You", 2: "Create a Report", 3: "Model Matching Contribution", 4: "Quit" } == mailroom.get_menu()
def test_model_matching_contribution(): """ test model matching contribution """ mailroom = Mailroom() donor = Donor("Test User", [1, 2, 3, 4]) new_donor = mailroom.model_matching_contribution(donor, 2, None, None) assert len(donor.donations) == len(new_donor.donations) assert donor.name == new_donor.name assert donor.donations != new_donor.donations
def setUp(self): donors = { "George Washington": [1], "John Adams": [3], "Thomas Jefferson": [3], "John Quincy Adams": [2], "James Madison": [1] } self.mailroom = Mailroom(donors)
def test_send_thanks_multiple_donation(): """ test thank you letter with multiple donations""" mailroom = Mailroom() donor = Donor("Monty Python") donor.add_donation(30) donor.add_donation(20) donor.add_donation(100) donor.add_donation(150) donor.add_donation(10) assert "\n\nDear Monty Python,\nThank you for your donation of 10.\n\n" == mailroom.send_thanks( donor)
def test_create_mailroom_populate_donors_from_dict(self): mailroom = Mailroom() sample_donor_data = \ {'Mark Zuckerberg':[5000.50,1089.90,750.45], 'Jeff Bezos':[700.90,900.99], 'Paul Allen':[11100.80], 'Joanne K Rowling':[1199.90,9878.12]} mailroom.populate_donors_from_dict(sample_donor_data) self.assertEqual(4,mailroom.donor_count) donors = ['Mark Zuckerberg','Jeff Bezos','Paul Allen','Joanne K Rowling'] for index,name in enumerate(mailroom.donor_names): self.assertEqual(name,donors[index])
def test_add_donation(): reset_data() mailroom = Mailroom() assert mailroom.donors_list.add_donation("Test1", 55.55) == Donor("Test1") assert mailroom.donors_list.add_donation("Test1", 4) == Donor("Test1") assert mailroom.donors_list.add_donation("Laura Todd", 99) == Donor("Laura Todd")
def test_num_donations(): reset_data() mailroom = Mailroom() donor = mailroom.donors_list.find_donor("Lisa Grant") assert donor.num_donations == 2 donor = mailroom.donors_list.find_donor("Frank Fred") assert donor.num_donations == 3
def test_total_donations(): reset_data() mailroom = Mailroom() donor = mailroom.donors_list.find_donor("Lisa Grant") assert donor.total_donations == 209.70 donor = mailroom.donors_list.find_donor("Frank Fred") assert donor.total_donations == 178.76
def test_name(): reset_data() mailroom = Mailroom() donor = mailroom.donors_list.find_donor("Lisa Grant") assert donor.name == "Lisa Grant" donor = mailroom.donors_list.find_donor("Frank Fred") assert donor.name == "Frank Fred"
def test_avg_donations(): reset_data() mailroom = Mailroom() donor = mailroom.donors_list.find_donor("Lisa Grant") assert donor.avg_donation == 104.85 donor = mailroom.donors_list.find_donor("Bill Ted") assert donor.avg_donation == 242.21
def test_donor_eq(): reset_data() mailroom = Mailroom() donor1 = Donor("A") donor2 = Donor("B") assert not (donor1 == donor2) donor2 = Donor("A") assert donor1 == donor2
def test_donor_lt(): reset_data() mailroom = Mailroom() donor1 = Donor("Frank Fred") donor2 = Donor("Lisa Grant") assert donor1 < donor2 donor2 = Donor("Laura Todd") assert donor1 > donor2
def test_create_thank_you(): reset_data() mailroom = Mailroom() donor = mailroom.donors_list.find_donor("Bill Ted") assert donor.create_thank_you(5) == ( "Dear Bill Ted,\nThank you for your very generous donation of $5.00. " "It \nwill go very far in supporting the Human Fund, \"Money for \nPeople.\"\n" " Sincerely\n Art Vandelay" )
def test_report(): """ test report generation """ mailroom = Mailroom() for donor, amount in INITIAL_DONORS.items(): mailroom.add_donation(donor, amount) mailroom.add_donation("Monty Python", 10) assert EXPECTED_REPORT == mailroom.report()
def test_mailroom_get_donations_for_donor(self): mailroom = Mailroom() sample_donor_data = \ {'Mark Zuckerberg':[5000.50,1089.90,750.45], 'Jeff Bezos':[700.90,900.99], 'Paul Allen':[11100.80], 'Joanne K Rowling':[1199.90,9878.12]} mailroom.populate_donors_from_dict(sample_donor_data) self.assertEqual(4,mailroom.donor_count) donors = ['Mark Zuckerberg','Jeff Bezos','Paul Allen','Joanne K Rowling'] for index,name in enumerate(mailroom.donor_names): self.assertEqual(name,donors[index]) donations_list = mailroom.donations_for_donor('Mark Zuckerberg') expected_donations = [5000.50,1089.90,750.45] self.assertEqual(len(donations_list), len(expected_donations)) for index in range(0,len(donations_list)): self.assertEqual(donations_list[index], expected_donations[index])
def test_create_report(): reset_data() mailroom = Mailroom() assert mailroom.donors_list.create_report().strip() == ( "Donor Name | Total Given | Num Gifts | Average Gift\n" "-------------------------------------------------------------\n" "Bill Ted | $ 726.63 | 3 | $ 242.21\n" "Lisa Grant | $ 209.70 | 2 | $ 104.85\n" "Frank Fred | $ 178.76 | 3 | $ 59.59\n" "Steve Lincoln | $ 165.28 | 2 | $ 82.64\n" "Laura Todd | $ 5.75 | 1 | $ 5.75")
def test_print_report(self): mailroom = Mailroom() sample_donor_data = \ {'Mark Zuckerberg':[5000.50,1089.90,750.45], 'Jeff Bezos':[700.90,900.99], 'Paul Allen':[11100.80], 'Joanne K Rowling':[1199.90,9878.12]} mailroom.populate_donors_from_dict(sample_donor_data) self.assertEqual(4,mailroom.donor_count) donors = ['Mark Zuckerberg','Jeff Bezos','Paul Allen','Joanne K Rowling'] for index,name in enumerate(mailroom.donor_names): self.assertEqual(name,donors[index]) capturedOutput = io.StringIO() # Create StringIO object sys.stdout = capturedOutput # and redirect stdout. mailroom.create_report() sys.stdout = sys.__stdout__ #Reset Redirect report_text = \ '''Donor Name | Total Given | Num Gifts | Average Gift\n------------------------------------------------------------------------\nMark Zuckerberg $ 6840.85 3 $ 2280.28\nJeff Bezos $ 1601.89 2 $ 800.94\nPaul Allen $ 11100.80 1 $ 11100.80\nJoanne K Rowling $ 11078.02 2 $ 5539.01''' self.assertEqual(capturedOutput.getvalue().strip(), report_text.strip()) #Remove all the donor name text files created for filename in os.listdir('.'): if filename.endswith('.txt'): donorname = filename[:-4] if donorname in mailroom.donor_data_dict.keys(): os.remove(filename)
def test_get_sorted_donors(): """ test sorting of donors """ mailroom = Mailroom() for donor, amount in INITIAL_DONORS.items(): mailroom.add_donation(donor, amount) mailroom.add_donation("Monty Python", 10) sorted_donors = [ donor_obj.name for donor_obj in mailroom.sort_donors_by_donation() ] assert [ "Monty Python", "John Adams", "Thomas Jefferson", "John Quincy Adams", "George Washington", "James Madison" ] == sorted_donors
def test_mail_everyone(): reset_data() mailroom = Mailroom() mailroom.donors_list.mail_everyone() dir_files = os.listdir() assert 'bill_ted.txt' in dir_files assert 'frank_fred.txt' in dir_files assert 'laura_todd.txt' in dir_files assert 'steve_lincoln.txt' in dir_files assert 'lisa_grant.txt' in dir_files with open('bill_ted.txt') as in_file: file_contents = in_file.read() assert file_contents.strip() == ( "Dear Bill Ted,\nThank you for your very generous donation of $726.63. " "It \nwill go very far in supporting the Human Fund, \"Money for \nPeople.\"\n" " Sincerely\n Art Vandelay" )
def test_get_sorted_donors_with_passed_in_list_of_donors(): """ test sorting of donor with external donor list """ mailroom = Mailroom() for donor, amount in INITIAL_DONORS.items(): mailroom.add_donation(donor, amount) mailroom.add_donation("Monty Python", 10) d1 = Donor("Test One", [1]) d2 = Donor("Test Two", [1, 2, 3]) donor_list = [d1, d2] sorted_donors = [ donor_obj.name for donor_obj in mailroom.sort_donors_by_donation(donor_list) ] assert ["Test Two", "Test One"] == sorted_donors
class TestMailroom(unittest.TestCase): """ Unit tests for mailroom """ def setUp(self): donors = { "George Washington": [1], "John Adams": [3], "Thomas Jefferson": [3], "John Quincy Adams": [2], "James Madison": [1] } self.mailroom = Mailroom(donors) def test_get_menu(self): """ test menu """ self.assertEqual( { 1: "Send a Thank You", 2: "Create a Report", 3: "Quit" }, self.mailroom.get_menu()) def test_thanks(self): """ test thank you letter """ self.assertEqual("\n\nDear Monty Python,\nThank you for your " \ "donation of 10.\n\n", self.mailroom.thanks("Monty Python", 10)) def test_report(self): """ test report generation """ self.mailroom.thanks("Monty Python", 10) self.assertEqual(EXPECTED_REPORT, self.mailroom.report()) def test_get_sorted_donors(self): """ test sorting of donors """ self.mailroom.thanks("Monty Python", 10) self.assertEqual([ "Monty Python", "John Adams", "Thomas Jefferson", "John Quincy Adams", "George Washington", "James Madison" ], self.mailroom._get_sorted_donors())
def test_send_letters_to_everyone(self): mailroom = Mailroom() sample_donor_data = \ {'Mark Zuckerberg':[5000.50,1089.90,750.45], 'Jeff Bezos':[700.90,900.99], 'Paul Allen':[11100.80], 'Joanne K Rowling':[1199.90,9878.12]} mailroom.populate_donors_from_dict(sample_donor_data) self.assertEqual(4,mailroom.donor_count) donors = ['Mark Zuckerberg','Jeff Bezos','Paul Allen','Joanne K Rowling'] for index,name in enumerate(mailroom.donor_names): self.assertEqual(name,donors[index]) mailroom.send_letters_to_everyone() for filename in os.listdir('.'): if filename.endswith('.txt'): donorname = filename[:-4] self.assertIn(donorname, mailroom.donor_data_dict.keys())
def test_get_donation_match_with_amount_gt_max(): """ test donation match with amount greater than max""" mailroom = Mailroom() amount = mailroom.get_donation_match(10, 3, None, 5) assert amount == 20
def test_get_donation_match_with_amount_lt_max(): """ test donation match with amount less than max""" mailroom = Mailroom() amount = mailroom.get_donation_match(11, 3, None, 12) assert amount == 33