def test_find_or_create_donor_not_there():
    "test one that's not there"
    sample_db = DonorDB(get_sample_data())
    donor = sample_db.find_or_create_donor("Bob Jones")

    assert donor.name == "Bob Jones"
    assert donor.last_donation == None
def test_find_or_create_donor():
    """ checks a donor that is there, but with odd case and spaces"""
    sample_db = DonorDB(get_sample_data())
    donor = sample_db.find_or_create_donor("jefF beZos ")

    assert donor.name == "Jeff Bezos"
    assert donor.last_donation == 877.33
def test_add_donation():
    # fixme: there should be a better way to get an arbitrary donor
    sample_db = DonorDB(get_sample_data())
    donor = sample_db.donor_data.popitem()[1]

    donor.add_donation(3000)

    assert donor.last_donation == 3000
def test_add_donor():
    name = "Fred Flintstone  "

    sample_db = DonorDB(get_sample_data())
    donor = sample_db.add_donor(name)
    donor.add_donation(300)
    assert donor.name == "Fred Flintstone"
    assert donor.last_donation == 300
    assert sample_db.find_donor(name) is donor
def test_database_save():
    db = model.DonorDB(model.get_sample_data())

    json_dict = db.to_json_compat()

    db2 = model.DonorDB.from_json_dict(json_dict)

    print(db2)

    assert db2 == db
    assert db2.donor_data == db.donor_data
def test_add_donation_negative():
    # fixme: there should be a better way to get an arbitrary donor
    sample_db = DonorDB(get_sample_data())

    donor = sample_db.donor_data.popitem()[1]

    with pytest.raises(ValueError):
        donor.add_donation(-100)

    with pytest.raises(ValueError):
        donor.add_donation(0.0)
def test_list_donors():
    # create a clean one to make sure everything is there.
    sample_db = DonorDB(get_sample_data())
    listing = sample_db.list_donors()

    # hard to test this throughly -- better not to hard code the entire
    # thing. But check for a few aspects -- this will catch the likely
    # errors
    assert listing.startswith("Donor list:\n")
    assert "Jeff Bezos" in listing
    assert "William Gates III" in listing
    assert len(listing.split('\n')) == 5
def test_generate_donor_report():
    sample_db = DonorDB(get_sample_data())
    report = sample_db.generate_donor_report()

    print(report)  # printing so you can see it if it fails
    # this is pretty tough to test
    # these are not great, because they will fail if unimportant parts of the
    # report are changed.
    # but at least you know that code's working now.
    assert report.startswith(
        "Donor Name                | Total Given | Num Gifts | Average Gift")

    assert "Jeff Bezos                  $    877.33           1   $     877.33" in report

    # check if it's in the right order
    lines = report.split("\n")
    assert "Paul Allen" in lines[2]
    assert "William Gates III" in lines[5]
def test_donor_round_trip():
    # get an arbitrary
    donor = model.get_sample_data()[0]

    json_dict = donor.to_json_compat()

    donor2 = model.Donor.from_json_dict(json_dict)

    print(json_dict)

    print(donor)
    print(donor2)

    assert donor2 == donor

    # in case equality isn't right...
    assert donor.name == donor2.name
    assert donor.norm_name == donor2.norm_name
    assert donor.donations == donor2.donations
def test_save_letters_to_disk():
    """
    This only tests that the files get created, but that's a start

    Note that the contents of the letter was already
    tested with test_gen_letter
    """

    # FIXME: this should create a temp dir to save to.
    sample_db = DonorDB(get_sample_data())

    sample_db.save_letters_to_disk()

    assert os.path.isfile('Jeff_Bezos.txt')
    assert os.path.isfile('William_Gates_III.txt')
    # check that it's not empty:
    with open('William_Gates_III.txt') as f:
        size = len(f.read())
    assert size > 0
The only code in here should deal with the command line interface.

Nothing to do with the logic code, etc that does the real work.
"""

import sys
import math

# handy utility to make pretty printing easier
from textwrap import dedent

from mailroom import model

# create a DB with the sample data
db = model.DonorDB(model.get_sample_data())


def main_menu_selection():
    """
    Print out the main application menu and then read the user input.
    """
    action = input(dedent('''
      Choose an action:

      1 - Send a Thank You
      2 - Create a Report
      3 - Send letters to everyone
      4 - Quit

      > '''))
def sample_db():
    """
    creates a clean sample database for the tests to use
    """
    return model.DonorDB(model.get_sample_data())
def test_save_load_file():
    db = model.DonorDB(model.get_sample_data())
    db.save_to_file("test.json")
    db2 = model.DonorDB.load_from_file("test.json")

    assert db == db2
def test_find_donor_not():
    "test one that's not there"
    sample_db = DonorDB(get_sample_data())
    donor = sample_db.find_donor("Jeff Bzos")

    assert donor is None
NOTE: when I first ran it, I got 97% coverage -- it was missing tests
      of creating a Donor and DonorDB empty.

      This prompted me to write tests for these, and then I discoverd
      that I got an error when you tried to get the last_donation from
      a Donor that did not have any donations.

      A win for testing!
"""

import os
import pytest
from mailroom.model import Donor, DonorDB, get_sample_data

# creates a sample database for the tests to use
sample_db = DonorDB(get_sample_data())


def test_empty_db():
    """
    tests that you can initilize an empty DB
    """
    db = DonorDB()

    assert len(db.donors) == 0

    # donor_list = db.list_donors()
    # print(donor_list)
    # # no donors
    # assert donor_list.strip() == "Donor list:"