示例#1
0
def test_save_read_file():
    """
    verify save donors and read donors

    we create a donors object, save it, read it, then
    examine the contents which indirectly tests both save and read
    """

    test_donor_a = Donor(full_name="Joe Smith", donation=100)
    test_donor_a.add_donation(150)
    test_donor_b = Donor(full_name="Mary Jo Kline, III", donation=1000)
    test_donors = Donors(test_donor_a)
    test_donors.add_donor(test_donor_b)

    test_donor_file = "test_donor_file"

    save_donor_file(test_donors, donor_file=test_donor_file)

    return_donors = load_donor_file(donor_file=test_donor_file)

    name_matches = [x[0] for x in return_donors.full_name_index]

    print(name_matches)

    assert "Joe Smith" in name_matches
    assert "Mary Jo Kline, III" in name_matches
    assert "Joe Jo Williams" not in name_matches

    os.remove(test_donor_file)
示例#2
0
def test_match_donor():
    """ verify the match_donor search functions """
    test_donor_a = Donor(full_name="Joe Smith", donation=100)
    test_donor_a.add_donation(150)
    test_donor_b = Donor(full_name="Mary Jo Kline, III", donation=1000)
    test_donors = Donors(test_donor_a)
    test_donors.add_donor(test_donor_b)

    matches = test_donors.match_donor("Joe Smith")
    name_matches = [x[0] for x in matches]

    print(matches)

    assert "Joe Smith" in name_matches
    assert "Mary Jo Kline, III" not in name_matches
示例#3
0
def test_thank_all_donors():
    """ verify the print_thank_you() function """
    test_donor_a = Donor(full_name="Joe Smith", donation=100)
    test_donor_a.add_donation(150)
    test_donor_b = Donor(full_name="Mary Jo Kline, III", donation=1000)
    test_donors = Donors(test_donor_a)
    test_donors.add_donor(test_donor_b)

    out = io.StringIO()
    thank_all_donors(test_donors, dest_override=out)
    output = out.getvalue()
    out.close()

    assert "Dearest Mary Jo Kline, III," in output
    assert "Dearest Joe Smith," in output
示例#4
0
def test_add_donations():
    """
    test add_donations

    we test that we can add a donation as part of the constructor as well
    as via add_donation.  we also test that the donation attributes
    (number_donations, total_donations and average_donations) work
    correctly.
    """
    test_donor = Donor(full_name="Joe Smith", donation=100)
    test_donor.add_donation(150)
    test_donor.add_donation(777.77)

    assert test_donor.number_donations == 3
    assert test_donor.total_donations == 1027.77
    assert test_donor.average_donations == 342.59
示例#5
0
def test_list_donors():
    """ verify the list_donors() function """
    test_donor_a = Donor(full_name="Joe Smith", donation=100)
    test_donor_a.add_donation(150)
    test_donor_b = Donor(full_name="Mary Jo Kline, III", donation=1000)
    test_donors = Donors(test_donor_a)
    test_donors.add_donor(test_donor_b)
    out = io.StringIO()
    list_donors(test_donors, dest=out)
    output = out.getvalue()
    out.close()

    print(output)

    assert "Donor Name           | Total Given    | Num Gifts | Average Gift" in output
    assert "Joe Smith             $        250.00           2  $      125.00" in output
    assert "Mary Jo Kline, III    $      1,000.00           1  $    1,000.00" in output
示例#6
0
def test_create_donors():
    """
    verify create_donors

    verifies donor objects can be added via the constructor as
    well as via add_donor()
    """
    test_donor_a = Donor(full_name="Joe Smith", donation=100)
    test_donor_a.add_donation(150)
    test_donor_b = Donor(full_name="Mary Jo Kline, III", donation=1000)
    test_donor_c = Donor(full_name="Adam Frank")

    test_donors = Donors(test_donor_a)
    test_donors.add_donor(test_donor_b)
    test_donors.add_donor(test_donor_c)

    name_matches = [x[0] for x in test_donors.full_name_index]

    assert test_donors.number_donors == 3
    assert "Adam Frank" in name_matches
示例#7
0
def data_entry(donors):
    """ Enter new donation and send thank you. """

    menu = "\n"
    menu += "DONATION ENTRY (Donor Name)\n"
    menu += "---------------------------\n"
    menu += "\n"
    menu += "Enter the full name (first last) of the donor\n"
    menu += "for whom you would like to enter a donation,\n"
    menu += "(l)ist to see a list of the existing donors, or\n"
    menu += "(q)uit to return to the previous menu.\n"
    menu += "\n"

    while True:

        print_lines()

        print(menu)
        selection = safe_input("Donor Name, (l)ist or (q)uit: ")

        # check for a quit directive
        if selection.lower() in ["q", "quit"]:
            return

        # check for a list directive
        if selection.lower() in ["l", "list"]:
            list_donors(donors)
            continue

        # protect against no entry
        if not selection:
            continue

        # reject blatantly bad input
        if len(selection.split()) < 2:
            print("You must enter both a first and last name.")
            continue

        # find any records that match the input
        matches = donors.match_donor(selection)

        if not matches:
            # if there are no matches, go ahead and create a donor record
            donor = Donor(full_name=selection)
            donors.add_donor(donor)
            hint = "new"

        if len(matches) == 1:
            # if there is an exact match, let's use that one
            donor = donors.get_donor(matches[0][1])
            hint = "existing"

        # TODO: Add confirmation logic and allow them to ignore a current
        #       record and add a new record anyway.

        # prompt for new donation, cancel if None returned
        new_donation = get_donation_amount(donor)
        if new_donation is None:
            print_lines()
            print("Donation cancelled!")
            return

        # update the donor with the new donation
        donor.add_donation(new_donation)

        # thank the donor for the new donation
        print_thank_you(donor, hint)