Skip to content

Commit e181e84

Browse files
committed
email/email test suite
1 parent 26c1d85 commit e181e84

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: Testing/errors/emails.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
3+
import csv
4+
import sys
5+
6+
7+
def populate_dictionary(filename):
8+
"""Populate a dictionary with name/email pairs for easy lookup."""
9+
email_dict = {}
10+
with open(filename) as csvfile:
11+
lines = csv.reader(csvfile, delimiter=',')
12+
for row in lines:
13+
name = str(row[0].lower())
14+
email_dict[name] = row[1]
15+
return email_dict
16+
17+
18+
def find_email(argv):
19+
""" Return an email address based on the username given."""
20+
# Create the username based on the command line input.
21+
try:
22+
fullname = str(argv[1] + " " + argv[2])
23+
# Preprocess the data
24+
email_dict = populate_dictionary(
25+
'/home/{{ username }}/data/user_emails.csv')
26+
# If email exists, print it
27+
if email_dict.get(fullname.lower()):
28+
return email_dict.get(fullname.lower())
29+
else:
30+
return "No email address found"
31+
except IndexError:
32+
return "Missing parameters"
33+
34+
35+
def main():
36+
print(find_email(sys.argv))
37+
38+
39+
if __name__ == "__main__":
40+
main()

Diff for: Testing/errors/emails_test.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
3+
import unittest
4+
from emails import find_email
5+
6+
7+
class EmailsTest(unittest.TestCase):
8+
def test_basic(self):
9+
testcase = [None, "Bree", "Campbell"]
10+
expected = "[email protected]"
11+
self.assertEqual(find_email(testcase), expected)
12+
13+
def test_one_name(self):
14+
testcase = [None, "John"]
15+
expected = "Missing parameters"
16+
self.assertEqual(find_email(testcase), expected)
17+
18+
def test_two_name(self):
19+
testcase = [None, "Roy", "Cooper"]
20+
expected = "No email address found"
21+
self.assertEqual(find_email(testcase), expected)
22+
23+
24+
if __name__ == '__main__':
25+
unittest.main()

0 commit comments

Comments
 (0)