Skip to content

Commit 88d71e5

Browse files
committed
Mailroom with full test coverage!
1 parent efb4f09 commit 88d71e5

File tree

2 files changed

+97
-88
lines changed

2 files changed

+97
-88
lines changed

students/eowyn/session06/mailroom/mailroom.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import sys
44

5-
''' --------------------------------------------------------------
6-
GLOBAL VARIABLE: DONORS is the database of donor names, amounts
7-
-------------------------------------------------------------------'''
5+
# --------------------------------------------------------------
6+
# GLOBAL VARIABLE: DONORS is the database of donor names, amounts
7+
# -------------------------------------------------------------------
8+
89
donor_names = ["Margaret Atwood", "Fred Armisen",
910
"Heinz the Baron Krauss von Espy"]
1011
donations = [[300, 555], [240, 422, 1000], [1500, 2300]]
@@ -13,9 +14,9 @@
1314
del donations
1415

1516

16-
''' --------------------------------------------------------------
17-
Following are helper functions to control program flow
18-
-------------------------------------------------------------------'''
17+
# --------------------------------------------------------------
18+
# Following are helper functions to control program flow
19+
# -------------------------------------------------------------------
1920

2021

2122
def safe_input():
@@ -31,9 +32,9 @@ def return_to_menu():
3132
return True
3233

3334

34-
''' --------------------------------------------------------------
35-
Following are helper functions for generating reports
36-
-------------------------------------------------------------------'''
35+
# --------------------------------------------------------------
36+
# Following are helper functions for generating reports
37+
# -------------------------------------------------------------------
3738

3839

3940
def generate_report_data():
@@ -80,10 +81,10 @@ def print_table(list_data):
8081
print(setup_body(list_data))
8182

8283

83-
''' --------------------------------------------------------------
84-
Following are helper functions for adding donations to DONORS and
85-
sending thank you notes to donors
86-
-------------------------------------------------------------------'''
84+
# --------------------------------------------------------------
85+
# Following are helper functions for adding donations to DONORS and
86+
# sending thank you notes to donors
87+
# -------------------------------------------------------------------
8788

8889

8990
def collect_donor_input():
@@ -117,8 +118,13 @@ def update_donors():
117118

118119
def list_donors():
119120
''' List all DONORS '''
120-
print("All Donors:")
121-
[print(x) for x in DONORS]
121+
print(make_donor_list())
122+
123+
124+
def make_donor_list():
125+
''' generate string of donor names '''
126+
outputstr = ""
127+
return ("Donor Names:" + "".join([outputstr + '\n' + x for x in DONORS]))
122128

123129

124130
def generate_letter(donor_name):
@@ -130,10 +136,10 @@ def generate_letter(donor_name):
130136
return fs.format(donor_name, DONORS[donor_name][-1])
131137

132138

133-
''' --------------------------------------------------------------
134-
Following are helper functions for accepting and responding to
135-
keyboard input
136-
-------------------------------------------------------------------'''
139+
# --------------------------------------------------------------
140+
# Following are helper functions for accepting and responding to
141+
# keyboard input
142+
# -------------------------------------------------------------------
137143

138144

139145
def remove_inputquotes(a_string):
@@ -156,14 +162,14 @@ def select_action(arg_dict, answer):
156162
''' Execute an action from arg_dict that corresponds to answer.
157163
Return None if action was executed and False if an error occurs'''
158164
try:
159-
return arg_dict.get(answer)()
160-
except (KeyError, TypeError):
165+
return arg_dict[answer]()
166+
except (KeyError):
161167
return False
162168

163169

164-
''' --------------------------------------------------------------
165-
Following are primary actions called by MAINLOOP
166-
-------------------------------------------------------------------'''
170+
# --------------------------------------------------------------
171+
# Following are primary actions called by MAINLOOP
172+
# --------------------------------------------------------------
167173

168174

169175
def send_letters():
@@ -175,6 +181,15 @@ def send_letters():
175181
print("Successfully saved letters for each donor.")
176182

177183

184+
def run_interactive_loop(arg_dict, prompt_string):
185+
while True:
186+
answer = get_user_input(prompt_string)
187+
if answer:
188+
result = select_action(arg_dict, answer)
189+
if result:
190+
return
191+
192+
178193
def thank_you_loop():
179194
''' Primary loop to update and thank DONORS
180195
update DONORS, print donor names, or return to main menu
@@ -184,12 +199,7 @@ def thank_you_loop():
184199
(1) Update donor and send thank-you\n
185200
(2) List all existing DONORS\n
186201
(3) Return to main menu\n >"""
187-
while True:
188-
answer = get_user_input(prompt_string)
189-
if answer:
190-
result = select_action(arg_dict, answer)
191-
if result:
192-
return
202+
run_interactive_loop(arg_dict, prompt_string)
193203

194204

195205
def print_report_loop():
@@ -200,17 +210,12 @@ def print_report_loop():
200210
prompt_string = """Select one:\n
201211
(1) Generate a summary report\n
202212
(2) Return to the main menu\n"""
203-
while True:
204-
answer = get_user_input(prompt_string)
205-
if answer:
206-
result = select_action(arg_dict, answer)
207-
if result:
208-
return
213+
run_interactive_loop(arg_dict, prompt_string)
209214

210215

211-
''' --------------------------------------------------------------
212-
The MAINLOOP to control the entire program
213-
-------------------------------------------------------------------'''
216+
# --------------------------------------------------------------
217+
# The MAINLOOP to control the entire program
218+
# -------------------------------------------------------------------
214219

215220

216221
def mainloop():
@@ -224,12 +229,7 @@ def mainloop():
224229
(2) Create a Report\n
225230
(3) Send letters to everyone\n
226231
(4) Quit\n>"""
227-
while True:
228-
answer = get_user_input(prompt_string)
229-
if answer:
230-
result = select_action(arg_dict, answer)
231-
if result:
232-
continue
232+
run_interactive_loop(arg_dict, prompt_string)
233233

234234

235235
if __name__ == "__main__":
Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22

33
import mailroom
4-
import pytest
54
import os.path
65

76

@@ -15,53 +14,49 @@ def test_return_to_menu():
1514
assert result is True
1615

1716

18-
def test_remove_inputquotes():
19-
astring = "'hello'"
20-
output = 'hello'
21-
assert mailroom.remove_inputquotes(astring) == output
22-
23-
2417
def test_generate_letter():
2518
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
2619
'Heinz the Baron Krauss von Espy': [1500, 2300],
2720
'Margaret Atwood': [300, 555]}
28-
output = 'Thank you, Margaret Atwood, for your generosity and recent gift of $555.'
29-
assert mailroom.generate_letter('Margaret Atwood') == output
21+
returnval = mailroom.generate_letter('Margaret Atwood')
22+
assert "Margaret Atwood" in returnval
23+
assert "$555" in returnval
3024

3125

3226
def test_generate_report_data():
3327
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
3428
'Heinz the Baron Krauss von Espy': [1500, 2300],
3529
'Margaret Atwood': [300, 555]}
36-
output = [('Heinz the Baron Krauss von Espy', 3800, 2, 1900),
37-
('Fred Armisen', 1662, 3, 554),
38-
('Margaret Atwood', 855, 2, 428)]
39-
assert mailroom.generate_report_data() == output
30+
returnval = set(mailroom.generate_report_data())
31+
assert len(mailroom.DONORS) == len(returnval)
32+
assert ('Fred Armisen', 1662, 3, 554) in returnval
4033

4134

4235
def test_setup_table():
43-
''' this is not working'''
44-
output = """'Donor Name |
45-
Total Given |Num Gifts |Average Gift'\n,
46-
'----------------------------------------
47-
-------------------------------------'"""
48-
assert mailroom.setup_table() == output
36+
returnval = mailroom.setup_table()
37+
assert returnval.startswith("Donor Name")
38+
assert "Total Given" in returnval
39+
assert "Num Gifts" in returnval
40+
assert "Average Gift" in returnval
4941

5042

5143
def test_setup_body():
52-
''' this is not working'''
44+
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
45+
'Heinz the Baron Krauss von Espy': [1500, 2300],
46+
'Margaret Atwood': [300, 555]}
5347
list_data = mailroom.generate_report_data()
54-
output = """'Heinz the Baron Krauss von Espy $ 3800.00 2 $ 1900.00 \nFred Armisen $ 1662.00
55-
3 $ 554.00 \nMargaret Atwood $ 855.00 2 $ 428.00 \n'
56-
"""
57-
assert mailroom.setup_body(list_data) == output
48+
returnval = mailroom.setup_body(list_data)
49+
nlines = returnval.count('\n')
50+
assert "Margaret Atwood" in returnval
51+
assert "Fred Armisen" in returnval
52+
assert nlines == len(mailroom.DONORS)
53+
5854

5955
def test_remove_inputquotes():
6056
output = 'hello'
6157
assert mailroom.remove_inputquotes("hello") == output
6258

6359

64-
6560
def test_retrieve_donor_exists():
6661
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
6762
'Heinz the Baron Krauss von Espy': [1500, 2300],
@@ -70,13 +65,12 @@ def test_retrieve_donor_exists():
7065
output = [300, 555]
7166
assert mailroom.retrieve_donations(name) == output
7267

68+
7369
def test_retrieve_donations_new():
7470
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
7571
'Heinz the Baron Krauss von Espy': [1500, 2300],
7672
'Margaret Atwood': [300, 555]}
77-
name = "Paul Simon"
78-
output = None
79-
assert mailroom.retrieve_donations(name) == output
73+
assert mailroom.retrieve_donations("Paul Simon") is None
8074

8175

8276
def test_add_donation_new():
@@ -96,27 +90,42 @@ def test_add_donation_existing():
9690
name = "Fred Armisen"
9791
amount = 500
9892
mailroom.add_donation(name, amount)
99-
assert mailroom.retrieve_donations(name) == [240, 422, 1000, 500]
100-
101-
102-
def test_select_action1():
103-
''' not sure how to test if correct action was completed
104-
something called "mock" has this capability?'''
105-
pass
93+
assert mailroom.retrieve_donations(name)[-1] == amount
10694

10795

10896
def test_file_existance():
10997
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
11098
'Heinz the Baron Krauss von Espy': [1500, 2300],
11199
'Margaret Atwood': [300, 555]}
112100
mailroom.send_letters()
113-
c1 = os.path.isfile("Margaret_Atwood.txt")
114-
c2 = os.path.isfile("Fred_Armisen.txt")
115-
c3 = os.path.isfile("Heinz_the_Baron_Krauss_von_Espy.txt")
116-
output = c1 and c2 and c3
117-
assert output is True
101+
assert os.path.isfile("Margaret_Atwood.txt")
102+
assert os.path.isfile("Fred_Armisen.txt")
103+
assert os.path.isfile("Heinz_the_Baron_Krauss_von_Espy.txt")
104+
105+
106+
def test_select_action_bad():
107+
arg_dict = {"1": mailroom.update_donors,
108+
"2": mailroom.send_letters}
109+
assert mailroom.select_action(arg_dict, "5") is False
110+
111+
112+
def test_donor_list():
113+
mailroom.DONORS = {'Fred Armisen': [240, 422, 1000],
114+
'Heinz the Baron Krauss von Espy': [1500, 2300],
115+
'Margaret Atwood': [300, 555]}
116+
returnval = mailroom.make_donor_list()
117+
nlines = returnval.count('\n')
118+
assert returnval.startswith("Donor Names:")
119+
assert "Margaret Atwood" in returnval
120+
assert "Fred Armisen" in returnval
121+
assert nlines == len(mailroom.DONORS)
122+
123+
124+
def test_elect_action_good():
125+
arg_dict = {"1": some_function, "2": mailroom.send_letters}
126+
x = mailroom.select_action(arg_dict, "1")
127+
assert x == 'this worked'
118128

119129

120-
def test_select_actionbad():
121-
arg_dict = {"1": "dummy1", "2": "dummy2", "3": "dummy3"}
122-
assert mailroom.select_action(arg_dict,"5") == False
130+
def some_function():
131+
return "this worked"

0 commit comments

Comments
 (0)