def test_donors_add_donations_name_in_db(): db = Donors({"A Name": [1], "B Name": [2]}) db.add_donation("A Name", 3) expected = " A Name\n B Name" assert db.list_donors() == expected assert db["A Name"].donations == [1, 3] assert db["B Name"].donations == [2]
def test_donors_add_donations_empty_value_list(): db = Donors({"A Name": [], "B Name": []}) db.add_donation("A Name", 3) expected = " A Name\n B Name" assert db.list_donors() == expected assert db["A Name"].donations == [3] assert db["B Name"].donations == []
def test_donors_add_donations_empty_db(): db = Donors() db.add_donation("A Name", 1) expected = " A Name" assert db.list_donors() == expected assert db["A Name"].name == "A Name" assert db["A Name"].donations == [1]
def test_donor_names(): donors = Donors() d1 = Donor("Mark", 50) d2 = Donor("Mary", 80) donors.add_donor(d1) donors.add_donor(d2) assert set(donors.names) == set(["Mark", "Mary"])
def test_donors_summary_report_multiple_names_multiple_values_sorting(): db = Donors({ "A Name": [1, 2, 3, 4, 5], "B Name": [6.75, 7.75, 8.50], "C Name": [20.6789] }) expected = "DONOR NAME TOTAL DONATED NUM DONATIONS AVG DONATION\n" \ "B Name $ 23.00 3 $ 7.67\n" \ "C Name $ 20.68 1 $ 20.68\n" \ "A Name $ 15.00 5 $ 3.00\n" assert db.summary_report() == expected
def test_challenge(): donors = Donors() d1 = Donor("Mark", 50) d1.add_donation(90) d1.add_donation(110) d2 = Donor("Mary", 80) d2.add_donation(95) d2.add_donation(115) donors.add_donor(d1) donors.add_donor(d2) copy = donors.challenge(3, min_amt=85, max_amt=100) assert copy.get_donor("Mark").donations == (270, ) assert copy.get_donor("Mary").donations == (285, )
def run_projection(db): """Run projection showing total contribution of challenge scenario""" while True: factor = input("\nChallenge factor ('q' to quit): ") if factor in ('q', 'quit'): return min_filter = input("Minimum donation to challenge (<return> for none, 'q' to quit): ") if min_filter in ('q', 'quit'): return max_filter = input("Maximum donation to challenge (<return> for none, 'q' to quit): ") if max_filter in ('q', 'quit'): return try: scenario = Donors.challenge(db, float(factor), float(min_filter) if min_filter else None, float(max_filter) if max_filter else None) except ValueError: print('Invalid inputs, try again') else: break print() for d in scenario.values(): print(f" {d.name}: ${sum(d.donations):,.2f} = {factor} * " f"("+' + '.join(list(map(lambda x: f"${x:,.2f}", d.donations)))+')') print(f"\n Total contribution required: ${scenario.total_value():,.2f}\n")
def test_insert(): D = Donors() rec = Record("C00629618|||||||PEREZ, JOHN A|||90017|||01032017|40||||||") D.insert(rec) assert(D._donors == {'90017': {'PEREZ, JOHN A': [2017]}}) rec = Record("C00629618|||||||PEREZ, JOHN A|||90017|||01022012|40||||||") D.insert(rec) assert (D._donors == {'90017': {'PEREZ, JOHN A': [2017, 2012]}}) rec = Record("xyz|||||||CHANDAN, DEEPAK|||90017|||01022011|40||||||") D.insert(rec) assert (D._donors == {'90017': {'PEREZ, JOHN A': [2017, 2012], 'CHANDAN, DEEPAK': [2011]}}) rec = Record("xyz|||||||someone|||34567|||01022018|40||||||") D.insert(rec) assert (D._donors == {'90017': {'PEREZ, JOHN A': [2017, 2012], 'CHANDAN, DEEPAK': [2011]}, '34567': {'someone': [2018]}})
def __init__(self, ofname, perc, verbosity=1): """ Constructor :param ofname: output file name :param perc: percentile value (> 0 and <= 100) :param verbosity: how much diagnostics to produce """ self._ofname = ofname self._perc = perc if (self._perc <= 0) or (self._perc > 100): raise ValueError("Invalid percentile value") if not isinstance(ofname, str): raise ValueError("Output filename must be a string") # A data structure containing (some) information about all donors self._all_donors = Donors() # A data structure containing (some) information about all repeat donations self._repeats = RepeatDonations() if not isinstance(verbosity, int): raise ValueError("Verbosity must be an integer") self._verbosity = verbosity if self._verbosity >= 1: print("Configuration:") print(" Output file: ", self._ofname) print(" Percentile : ", self._perc) print(" Verbosity : ", self._verbosity) self._ofile = open(self._ofname, "w") # to count how many records processed self._ctr = 0
TY_MENU = ("Send a Thank You", "", "At the prompt you can either:", "\tEnter a donor name,", "\tType 'list' to display all existing donors,", "\tType 'q' to go back to main menu.") NOT_UNDERSTOOD = "Input not understood. Please try again." PROMPT = "Your selection: " MIN_AMT_PR = "Enter a minimum amount, or press Enter to skip: " MAX_AMT_PR = "Enter a maximum amount, or press Enter to skip: " donors = Donors() formatter = Formatter(donors) def display(lines): print('-' * 80) if isinstance(lines, str): print(lines) else: for line in lines: print(line) print("\n") def display_donors(): display(["Existing Donors:", ""] + donors.names)
def test_donors_add_donations_name_not_in_db(): db = Donors({"A Name": [1]}) db.add_donation("B Name", 2) expected = " A Name\n B Name" assert db.list_donors() == expected
def test_donors_total_value_empty_db(): db = Donors() assert db.total_value() == 0
def test_donors_add_donor_w_multiple_donations(): db = Donors() db.add_donor(Donor("Bill", [1, 2, 3])) assert db["Bill"].name == "Bill" assert db["Bill"].donations == [1, 2, 3]
def test_donors_summary_report_one_name_multiple_values(): db = Donors({"A Name": [1, 2, 3, 4, 5]}) expected = "DONOR NAME TOTAL DONATED NUM DONATIONS AVG DONATION\n" \ "A Name $ 15.00 5 $ 3.00\n" assert db.summary_report() == expected
#!/usr/bin/python3 """ updated mailroom program for Lesson 10 assignment (functional programming) (1) updated donors class with challenge and total_value methods (2) added tests for donors challenge and total_value methods (3) added menu option for running projections """ from donors import Donors DONOR_DB = Donors({ "William Gates, III": [653772.32, 12.17], "Jeff Bezos": [877.33], "Paul Allen": [663.23, 43.87, 1.32], "Mark Zuckerberg": [1663.23, 4300.87, 10432.0], "Colleen Kaku": [50000, 1000000, 1000000] }) def send_thank_you(db): """Add a donor/donation and print out a thank you letter""" # loop for user input: donor name, or list, or quit while True: name = input("\nDonor Full Name (type 'list' for donor list or 'q' to quit): ") if name in ('q', 'quit'): return if name == 'list': print(db.list_donors()) continue # if name == donor name: # loop for user to input valid donation amount
def test_donors_total_value(): db = Donors({"A Name": [1, 2, 3], "B Name": [4, 5, 6]}) assert db.total_value() == 21
def test_donors_list_donors_one_item_in_db(): db = Donors({"one": [1, 2, 3]}) expected = " one" assert db.list_donors() == expected
def test_donors_get_second_does_not_exist(): elem = [1] assert Donors.get_second(elem) is None
def test_donors_init(): db = Donors() assert db == {}
def test_donors_get_second_from_dict(): db = {"A Name": [1, 2, 3], "B Name": [4, 5, 6]} assert Donors.get_second(db["B Name"]) == 5
def test_donors_get_second_from_list(): elem = ["A Name", 1, 2, 3] assert Donors.get_second(elem) == 1
def test_donors_summary_report_empty_db(): db = Donors() expected = "DONOR NAME TOTAL DONATED NUM DONATIONS AVG DONATION\n" assert db.summary_report() == expected
def test_get_donor(): donors = Donors() d = Donor("Mark", 50) donors.add_donor(d) assert donors.get_donor("Mark") == d assert donors.get_donor("Mary") is None
def test_donors_list_donors_multiple_items_in_db(): db = Donors({"one": [1, 2, 3], "two": [4, 5, 6], "three": [7, 8, 9]}) expected = " one\n two\n three" assert db.list_donors() == expected
def test_add_donation(): donors = Donors() donors.add_donation("Mark", 10) assert donors.get_donor("Mark") == Donor("Mark", 10) donors.add_donation("Mark", 20) assert donors.get_donor("Mark").donations == (10, 20) donors.add_donation("Mary", 30) assert donors.get_donor("Mary") == Donor("Mary", 30) assert set(donors.names) == set(["Mark", "Mary"])
def test_donors_list_donors_empty_value_lists(): db = Donors({"one": [], "two": [], "three": []}) expected = " one\n two\n three" assert db.list_donors() == expected
def test_donors_challenge_min_and_max_donation(): db = Donors({"A Name": [1, 2, 3], "B Name": [4, 5, 6]}) db2 = Donors.challenge(db, 2, min_donation=2, max_donation=5) assert db2["A Name"].donations == [4, 6] assert db2["B Name"].donations == [8, 10]
def test_donors_add_donor(): db = Donors() db.add_donor(Donor("Bill", 100)) assert db["Bill"].name == "Bill" assert db["Bill"].donations == [100]
def test_donors_list_donors_empty_db(): db = Donors() expected = "" assert db.list_donors() == expected
def test_donors_summary_report_one_name_one_value(): db = Donors({"A Name": [0]}) expected = "DONOR NAME TOTAL DONATED NUM DONATIONS AVG DONATION\n" \ "A Name $ 0.00 1 $ 0.00\n" assert db.summary_report() == expected