def test_get_thankyou_message(self): d = mr.DonorDB() name = "Adam Lee" amt = 1.23 d = mr.DonorDB() d.add_donor(name, amt) expected_message = '''Dear Adam Lee, Thank you so much for your generosity with your most recent donation of $1.23. It will be put to very good use. Sincerely.''' self.assertEqual(d.get_thankyou_message(d.find_donor(name)), expected_message)
def test_empty_db(): """ tests that you can initilize an empty DB """ db = mailroom.DonorDB() assert len(db.donors) == 0
def test_print_report(self): name = "Adam Lee" amt = 1.23 d = mr.DonorDB() d.add_donor(name, amt) name2 = "Henry Chung" amt2 = 100.55 d.add_donor(name2, amt2) out = io.StringIO() with redirect_stdout(out): d.print_report() output = out.getvalue().strip() self.assertIn( "--------------------------------------------------------------------", output) self.assertIn( "Donor Name | Total Given | Num Gifts | Average Gift", output) self.assertIn( "Adam Lee $ 1.23 1 $ 1.23", output) self.assertIn( "Henry Chung $ 100.55 1 $ 100.55", output) out.close()
def test_add_donor(): name = "Fred Flintstone " db = mailroom.DonorDB() donor = db.add_donor(name) donor.make_donation(300) assert donor.name == "Fred Flintstone" assert donor.last_donation == 300
def test_print_donor_list(self): d = mr.DonorDB() name1 = "Adam Lee" amt1 = 1.23 d = mr.DonorDB() d.add_donor(name1, amt1) name2 = "Henry Chung" amt2 = 123.456 d.add_donor(name2, amt2) out = io.StringIO() with redirect_stdout(out): d.print_donor_list() output = out.getvalue().strip() self.assertIn("Below are the existing donors:", output) self.assertIn("- Adam Lee [1.23]", output) self.assertIn("- Henry Chung [123.456]", output) out.close()
def test_list_donors(): # create a clean one to make sure everything is there. sample_db = mailroom.DonorDB(mailroom.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_add_donors(self): name1 = "Adam Lee" amt1 = 1.23 d = mr.DonorDB() d.add_donor(name1, amt1) self.assertEqual(d.get_donor(name1).name, name1) self.assertEqual(d.donors_container[0].latest_donation, 1.23) name2 = "Henry Chung" amt2 = 123.456 d.add_donor(name2, amt2) self.assertEqual(d.get_donor(name2).name, name2) self.assertEqual(d.donors_container[1].latest_donation, amt2)
def test_write_letters_to_file(self): name = "Adam Lee" amt = 1.23 d = mr.DonorDB() d.add_donor(name, amt) name2 = "Henry Chung" amt2 = 100.55 d.add_donor(name2, amt2) # first delete all existing thank you files in current directory [os.remove(f) for f in os.listdir(".") if f.endswith(".rpt")] num_file = len(fnmatch.filter(os.listdir("."), '*.rpt')) self.assertEqual(num_file, 0) # write thank_you letter files to current directory d.write_letters_to_file() # test thank_you letter files get generated self.assertTrue(os.path.isfile('Adam_Lee.rpt')) self.assertTrue(os.path.isfile('Henry_Chung.rpt')) num_file = len(fnmatch.filter(os.listdir("."), '*.rpt')) self.assertEqual(num_file, 2)
from random import randint def rand_name(): return "".join([chr(randint(97, 122)) for i in range(randint(5, 10))]) def gen_name(): name = " ".join((rand_name().capitalize(), chr(randint(65, 90)) + ".", rand_name().capitalize())) return name def make_lots_of_donors(db, n=100): for i in range(n): name = gen_name() donor = db.add_donor(name) # Add a bunch of random donations num_don = randint(100, 200) donor.donations = [randint(10, 30) * 100 for i in range(num_don)] return db if __name__ == "__main__": import mailroom db = mailroom.DonorDB() make_lots_of_donors(db, 100) print(db.generate_donor_report())
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 import mailroom # creates a sample database for the tests to use sample_db = mailroom.DonorDB(mailroom.get_sample_data()) def test_empty_db(): """ tests that you can initilize an empty DB """ db = mailroom.DonorDB() assert len(db.donors) == 0 # donor_list = db.list_donors() # print(donor_list) # # no donors # assert donor_list.strip() == "Donor list:"