from colorama import init, Fore
from duration import *
def print_pass(the_data, i):
print(f'{Fore.GREEN}Test {i} with {the_data} passed')
def print_fail(the_data, i):
print(f'''{Fore.RED}*** Test {i} with {the_data} failed.''')
def print_tests():
_test_default = Duration(60)
_test_minutes = Duration(10, "m")
_test_hours = Duration(2, "h")
_test_sec = Duration(120, "s")
print('''
Test #1 - simple declarations
-----------------------------''')
test_objects = (
_test_default,
_test_minutes,
_test_hours,
_test_sec
)
test_print_out = (
"60.00 s",
"600.00 s",
"7200.00 s",
"120.00 s"
)
test_repr_out = (
"Duration(60.00, 's')",
"Duration(10.00, 'm')",
"Duration(2.00, 'h')",
"Duration(120.00, 's')"
)
for i, test_obj in enumerate(test_objects):
try:
assert str(test_obj) == test_print_out[i], f'Expected {test_print_out[i]} received {str(test_obj)}'
print_pass(f'print of {test_obj}', i+1)
assert repr(test_obj) == test_repr_out[i], f'Expected {test_repr_out[i]} received {repr(test_obj)}'
print_pass(f'repr of {repr(test_obj)}', i+1)
except AssertionError as test_data:
print_fail(test_data, i+1)
continue
def add_tests():
print('''
Test #2 - Addition
------------------''')
_test_default = Duration(60)
_test_minutes = Duration(5, "m")
_test_hours = Duration(1, "h")
_test_list = [Duration(10), Duration(2, 'm'), Duration(1, 'h')]
_test_inc = Duration(60)
test_objects = (
_test_default + _test_minutes,
_test_default + 30,
sum(_test_list),
15 + _test_minutes
)
test_add_out = (
"Duration(360.00, 's')",
"Duration(90.00, 's')",
"Duration(3730.00, 's')",
"Duration(5.25, 'm')"
)
for i, test_obj in enumerate(test_objects):
try:
assert repr(test_obj) == test_add_out[i], f'Expected {test_add_out[i]} received {repr(test_obj)}'
print_pass(f'print of {repr(test_obj)}', i+1)
except AssertionError as test_data:
print_fail(test_data, i+1)
continue
_test_inc += 30
try:
assert repr(_test_inc) == "Duration(90.00, 's')", f'Expected Duration(90.00, s) received {repr(_test_inc)}'
print_pass(f'Incremental test of {repr(_test_inc)}', 1)
except AssertionError as test_data:
print_fail(test_data, 1)
def sub_tests():
print('''
Test #3 - Test all subtraction cases
------------------------------------''')
_test_default = Duration(120)
_test_minutes = Duration(1, "m")
_test_dec = Duration(100)
test_objects = (
_test_default - _test_minutes,
_test_default - 30
)
test_sub_out = (
"Duration(60.00, 's')",
"Duration(90.00, 's')"
)
try:
reverse_sub = 200 - Duration(60, "s")
assert repr(reverse_sub) == "Duration(140.00, 's')", \
f"Expected Duration(140.00, 's') received {repr(reverse_sub)}"
print_pass(f'reverse subtraction: {repr(reverse_sub)}', 6)
except AssertionError as test_data:
print_fail(test_data, 6)
for i, test_obj in enumerate(test_objects):
try:
assert repr(test_obj) == test_sub_out[i], f'Expected {test_sub_out[i]} received {repr(test_obj)}'
print_pass(f'print of {repr(test_obj)}', i+1)
except AssertionError as test_data:
print_fail(test_data, i+1)
continue
_test_dec -= 40
try:
assert repr(_test_dec) == "Duration(60.00, 's')", f'Expected Duration(60.00, s) received {repr(_test_dec)}'
print_pass(f'Decremental test of {repr(_test_dec)}', 1)
except AssertionError as test_data:
print_fail(test_data, 1)
def comp_tests():
print('''
Test #4 - Test comparisons between Duration objects
--------------------------''')
_test_comp1 = Duration(60)
_test_comp2 = Duration(1, "m")
_test_comp7 = Duration(100)
_test_comp8 = Duration(101)
count = 1
try:
assert _test_comp1 == _test_comp2, f'Equality tested failed for {repr(_test_comp1)} and {repr(_test_comp2)}'
print_pass(f'comparison: {repr(_test_comp1)} = {repr(_test_comp2)} ', count)
except AssertionError as test_data:
print_fail(test_data, count)
count += 1
try:
assert not (_test_comp7 > _test_comp8), f'Greater than test failed for {repr(_test_comp7)} and {repr(_test_comp8)}'
print_pass(f'comparison: {repr(_test_comp7)} not > {repr(_test_comp8)} ', count)
assert _test_comp7 < _test_comp8, f'Less than test failed for {repr(_test_comp7)} and {repr(_test_comp8)}'
print_pass(f'comparison: {repr(_test_comp7)} < {repr(_test_comp8)} ', count)
except AssertionError as test_data:
print_fail(test_data, count)
def exception_tests():
print('''
Test #5 - Test Exceptions
-------------------------''')
count = 1
try:
Duration("abc")
print_fail(f'Value error not thrown when value is string', count)
except ValueError as msg:
try:
assert str(msg) == "Value must be a number", f'Value error does not have correct message. Received {msg}'
print_pass(f'Value error thrown with message: {msg} as expected', count)
except AssertionError as test_data:
print_fail(test_data, count)
count += 1
try:
Duration(-10)
print_fail(f'Less than zero exception not thrown when value is negative', count)
except DurationLessThanZeroException as msg:
try:
assert str(msg) == "Duration cannot be negative", f'Exception does not have correct message. Received {msg}'
print_pass(f'Negative-value error thrown with message: {msg} as expected', count)
except AssertionError as test_data:
print_fail(test_data, count)
count += 1
try:
Duration(10, "abc")
print_fail(f'Incorrect unit exception not thrown', count)
except TimeUnitNotDefinedException as msg:
try:
assert str(msg) == "Unit cannot be used", f'Exception does not have correct message. Received {msg}'
print_pass(f'Unit error thrown with message: {msg} as expected', count)
except AssertionError as test_data:
print_fail(test_data, count)
if name == "main":
init(autoreset=True)
print_tests()
add_tests()
sub_tests()
comp_tests()
exception_tests()
Top comments (0)