Skip to content
Snippets Groups Projects
Commit 2f8bdbab authored by Maeva Pourpoint's avatar Maeva Pourpoint
Browse files

A few more unit tests

parent 08ea7a3a
No related branches found
No related tags found
2 merge requests!2Update conda recipe to build python and platform independent package,!1Update code to run under Python3 + Conda packaging
Pipeline #751 passed with stages
in 1 minute and 6 seconds
......@@ -21,9 +21,7 @@ import datetime
def usage():
"""
Prints the usage.
"""
"""Prints the usage."""
print('Usage : {} mm dd [year]'.format(sys.argv[0]))
......@@ -44,7 +42,7 @@ def cal_to_jul(argv):
calendar_date = datetime.datetime.today().strftime("%m %d %Y")
if is_date_valid(calendar_date):
print('\n Calendar Date ' + calendar_date)
print('\n Calendar Date {}'.format(calendar_date))
# Parse string to a datetime object.
d = datetime.datetime.strptime(calendar_date, "%m %d %Y")
# Format d to a julian date that can be printed.
......@@ -60,9 +58,7 @@ def cal_to_jul(argv):
def is_date_valid(calendar_date):
"""
Check if the date of the year is valid.
"""
"""Check if the date of the year is valid."""
try:
# strptime will raise an error if day, month, or year is out of range
datetime.datetime.strptime(calendar_date, "%m %d %Y")
......@@ -76,10 +72,9 @@ def main():
julday takes either no arguments, the month and day, or a month, day, and
year as arguments
"""
if len(sys.argv) == 1 or len(sys.argv) > 4:
if len(sys.argv) not in [1, 3, 4]:
usage()
sys.exit(1)
cal_to_jul(sys.argv[1:])
......
......@@ -10,7 +10,8 @@ from unittest.mock import patch
from julday.julday import is_date_valid, cal_to_jul
VALID_DATE = ['04', '02', '2020']
UNVALID_DATE = ['13', '02', '2020']
UNVALID_MONTH = ['13', '02', '2020']
UNVALID_YEAR = ['04', '02', '-2050']
class TestJulday(unittest.TestCase):
......@@ -21,19 +22,30 @@ class TestJulday(unittest.TestCase):
calendar_date_valid = "{} {} {}".format(VALID_DATE[0],
VALID_DATE[1],
VALID_DATE[2])
calendar_date_unvalid = "{} {} {}".format(UNVALID_DATE[0],
UNVALID_DATE[1],
UNVALID_DATE[2])
calendar_date_unvalid_1 = "{} {} {}".format(UNVALID_MONTH[0],
UNVALID_MONTH[1],
UNVALID_MONTH[2])
calendar_date_unvalid_2 = "{} {} {}".format(UNVALID_YEAR[0],
UNVALID_YEAR[1],
UNVALID_YEAR[2])
self.assertTrue(is_date_valid(calendar_date_valid),
'{} is not a valid date'.format(calendar_date_valid))
self.assertFalse(is_date_valid(calendar_date_unvalid),
'{} is a valid date'.format(calendar_date_unvalid))
"{} is not a valid date".format(calendar_date_valid))
self.assertFalse(is_date_valid(calendar_date_unvalid_1),
"{} is a valid date".format(calendar_date_unvalid_1))
self.assertFalse(is_date_valid(calendar_date_unvalid_2),
"{} is a valid date".format(calendar_date_unvalid_2))
@patch('julday.julday.sys.exit', autospec=True)
@patch('sys.stdout', new_callable=io.StringIO)
def test_cal_to_jul(self, mock_stdout):
def test_cal_to_jul(self, mock_stdout, mock_exit):
"""Test basic functionality of cal_to_jul function"""
cal_to_jul(VALID_DATE)
cal_to_jul(VALID_DATE[0:2])
output = '\n Calendar Date 04 02 2020\n Julian Date 093 2020\n\n'
self.assertEqual(mock_stdout.getvalue(),
output,
'Failed to properly convert date!')
self.assertEqual(mock_stdout.getvalue(), output,
"Failed to properly convert date!")
cal_to_jul(UNVALID_MONTH)
self.assertTrue(mock_exit.called, "sys.exit(1) never called - "
"cal_to_jul() not fully exercised!")
cal_to_jul(UNVALID_YEAR)
self.assertTrue(mock_exit.called, "sys.exit(1) never called - "
"cal_to_jul() not fully exercised!")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment