Skip to content

Commit 51ff27e

Browse files
author
Matthew Shirtliffe
committed
validating inputs with tests
1 parent d64cc84 commit 51ff27e

File tree

12 files changed

+127
-0
lines changed

12 files changed

+127
-0
lines changed
File renamed without changes.
File renamed without changes.

validating_inputs/app.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import re
2+
3+
def print_header():
4+
""" Print header
5+
"""
6+
header_text = ''
7+
TEXT = f' Validating Inputs \n'
8+
line = '-' * len(TEXT)
9+
line += '\n'
10+
11+
header_text += line
12+
header_text += TEXT
13+
header_text += line
14+
print(header_text)
15+
16+
17+
def get_first_name():
18+
19+
while True:
20+
first_name = input('Enter your first name: ')
21+
if len(first_name) >=2:
22+
return first_name
23+
else:
24+
print('A valid input is required')
25+
26+
27+
def get_last_name():
28+
29+
while True:
30+
last_name = input('Enter your last name: ')
31+
if len(last_name) >=2:
32+
return last_name
33+
else:
34+
print('A valid input is required')
35+
36+
def get_employee_id():
37+
regex = '^[A-Z]{2}-[0-9]{3,4}'
38+
while True:
39+
employee_id = input('Enter your employee id: ')
40+
if re.match(regex, employee_id):
41+
return employee_id
42+
else:
43+
print('A valid input is required')
44+
45+
def get_zip_code():
46+
regex = '^[A-Z]{5}'
47+
while True:
48+
zip_code = input('Enter your zip code: ')
49+
if re.match(regex, zip_code):
50+
return zip_code
51+
else:
52+
print('A valid input is required')
53+
54+
55+
if __name__ == "__main__":
56+
57+
print_header()
58+
first_name = get_first_name()
59+
last_name = get_last_name()
60+
zip_code = get_zip_code()
61+
employee_id = get_employee_id()
62+
if all([first_name, last_name, zip_code, employee_id]):
63+
print('There were no errors found')

validating_inputs/tests/__init__.py

Whitespace-only changes.

validating_inputs/tests/system/__init__.py

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
import app
4+
5+
class AppTest(TestCase):
6+
7+
def test_print_header(self):
8+
9+
expected = '--------------------\n Validating Inputs \n--------------------\n'
10+
with patch('builtins.print') as mocked_print:
11+
app.print_header()
12+
mocked_print.assert_called_with(expected)
13+
14+
def test_get_first_name(self):
15+
16+
expected = 'Enter your first name: '
17+
with patch('builtins.input') as mocked_input:
18+
with patch('builtins.print') as mocked_print:
19+
mocked_input.side_effect = ('' , 'Matthew')
20+
first_name = app.get_first_name()
21+
mocked_print.assert_called_with('A valid input is required')
22+
mocked_input.assert_called_with(expected)
23+
self.assertEqual(first_name, 'Matthew')
24+
25+
def test_get_last_name(self):
26+
27+
expected = 'Enter your last name: '
28+
with patch('builtins.input') as mocked_input:
29+
with patch('builtins.print') as mocked_print:
30+
mocked_input.side_effect = ('' , 'Shirtliffe')
31+
last_name = app.get_last_name()
32+
mocked_print.assert_called_with('A valid input is required')
33+
mocked_input.assert_called_with(expected)
34+
self.assertEqual(last_name, 'Shirtliffe')
35+
36+
def test_get_employee_id(self):
37+
38+
expected = 'Enter your employee id: '
39+
with patch('builtins.input') as mocked_input:
40+
with patch('builtins.print') as mocked_print:
41+
mocked_input.side_effect = ('' , 'AA-1234')
42+
employee_id = app.get_employee_id()
43+
mocked_print.assert_called_with('A valid input is required')
44+
mocked_input.assert_called_with(expected)
45+
self.assertEqual(employee_id, 'AA-1234')
46+
47+
def test_get_zip_code(self):
48+
49+
expected = 'Enter your zip code: '
50+
with patch('builtins.input') as mocked_input:
51+
with patch('builtins.print') as mocked_print:
52+
mocked_input.side_effect = ('' , 'ABCDE')
53+
zip_code = app.get_zip_code()
54+
mocked_print.assert_called_with('A valid input is required')
55+
mocked_input.assert_called_with(expected)
56+
self.assertEqual(zip_code, 'ABCDE')

validating_inputs/tests/unit/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from unittest import TestCase
2+
import app
3+
4+
class PasswordStrengthIndicatorTest(TestCase):
5+
pass
6+
# def test_is_anagram(self):
7+
# is_anagram = app.is_anagram('tone', 'note')
8+
# self.assertTrue(is_anagram)

0 commit comments

Comments
 (0)