79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
from ago import human
|
|
from ago import delta2human
|
|
from ago import delta2dict
|
|
|
|
from datetime import datetime
|
|
from datetime import timedelta
|
|
|
|
PRESENT = datetime.now()
|
|
PAST = PRESENT - timedelta( 492, 58711, 45 ) # days, secs, ms
|
|
FUTURE = PRESENT + timedelta( 2, 12447, 963 ) # days, secs, ms
|
|
|
|
PAST_DELTA = PRESENT - PAST
|
|
FUTURE_DELTA = PRESENT - FUTURE
|
|
|
|
def test_human_is_string():
|
|
assert 'str' in str(type(human( PAST )))
|
|
|
|
def test_delta2human_is_string():
|
|
assert 'str' in str(type(delta2human( PAST_DELTA )))
|
|
|
|
def test_delta2dict_is_dict():
|
|
assert 'dict' in str(type(delta2dict( PAST_DELTA )))
|
|
|
|
def test_ago_in_past_human():
|
|
assert 'ago' in human( PAST )
|
|
|
|
def test_in_in_future_human():
|
|
assert 'in' in human( FUTURE )
|
|
|
|
def test_no_coma_in_one_precision():
|
|
assert ',' not in human( PAST, precision = 1 )
|
|
assert ',' not in human( FUTURE, precision = 1 )
|
|
|
|
def test_coma_in_three_precision():
|
|
assert ',' in human( PAST, precision = 3 )
|
|
assert ',' in human( FUTURE, precision = 3 )
|
|
|
|
def test_one_day_singular():
|
|
assert 's' not in delta2human( timedelta(1) )
|
|
|
|
def test_two_day_plural():
|
|
assert 's' in delta2human( timedelta(2) )
|
|
|
|
def test_past_format():
|
|
output = human( PAST,
|
|
past_format = 'titanic sunk {0} ago',
|
|
future_format = 'titanic will sink in {0} from now'
|
|
)
|
|
assert 'titanic sunk' in output
|
|
|
|
def test_future_format():
|
|
output = human( FUTURE,
|
|
past_format = 'titanic sunk {0} ago',
|
|
future_format = 'titanic will sink in {0} from now'
|
|
)
|
|
assert 'titanic will sink in' in output
|
|
|
|
def test_valid_dict():
|
|
past_dict = delta2dict( PAST_DELTA )
|
|
assert past_dict['year'] == 1
|
|
assert past_dict['day'] == 127
|
|
assert past_dict['hour'] == 16
|
|
assert past_dict['minute'] == 18
|
|
|
|
|
|
def test_output():
|
|
"""Test and example usage"""
|
|
|
|
print '\nTesting past:\n'
|
|
print delta2dict( PAST_DELTA )
|
|
print 'Commented ' + delta2human( PAST_DELTA, 3 )
|
|
print human( PAST, past_format = "Commented {0} ago" )
|
|
|
|
print '\nTesting future:\n'
|
|
print delta2dict( FUTURE_DELTA )
|
|
print 'Shutdown ' + delta2human( FUTURE_DELTA, 3 )
|
|
print human( FUTURE, future_format = 'Shutdown in {0} from now' )
|
|
print ''
|
|
|