# Test the PARSE DURATION module

# imports
import traceback
import types

from parse_duration import *

# test routines follow!

def test_duration_string_to_msec( chatty):
    print 'test duration_string_to_msec()',

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)
    
    tsts = [
        [ None, 1], [ '', 1], # None defaults to msec
        [ 'msec', 1], [ 'MSEC', 1],
        [ 'junk', None], [ 'JUNK', None], # garbage defaults to error/throw
        [ 'sec', SEC], [ 'SEC', SEC],
        [ 'min', MIN], [ 'MIN', MIN],
        [ 'hr', HRS], [ 'HR', HRS],
        [ 'day', DAY], [ 'DAY', DAY],
        ]

    for tst in tsts:
        st = tst[0]
        expect = tst[1]

        rtn = duration_string_to_msec( st, False)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'
    return True

def test_adjust_msec( chatty):

    base = 'msec'
    print 'test adjust_time_duration() from %s to X as INT ' % base,

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)
    
    tsts = [
        [ 1, None, None, 1], # None defaults to msec
        [ 1, '', '', 1],     # None defaults to msec
        
        [ 1, base, 'msec', 1],
        [ 1, base, 'sec', 0],
        [ 1, base, 'min', 0],
        [ 1, base, 'hr', 0],
        [ 1, base, 'day', 0],

        [ SEC, base, 'msec', SEC],
        [ SEC, base, 'sec', 1],
        [ SEC, base, 'min', 0],
        [ SEC, base, 'hr', 0],
        [ SEC, base, 'day', 0],

        [ MIN, base, 'msec', MIN],
        [ MIN, base, 'sec', MIN/SEC],
        [ MIN, base, 'min', 1],
        [ MIN, base, 'hr', 0],
        [ MIN, base, 'day', 0],

        [ HRS, base, 'msec', HRS],
        [ HRS, base, 'sec', HRS/SEC],
        [ HRS, base, 'min', HRS/MIN],
        [ HRS, base, 'hr', 1],
        [ HRS, base, 'day', 0],

        [ DAY, base, 'msec', DAY],
        [ DAY, base, 'sec', DAY/SEC],
        [ DAY, base, 'min', DAY/MIN],
        [ DAY, base, 'hr', DAY/HRS],
        [ DAY, base, 'day', 1],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    print 'test adjust_time_duration() from %s to X as FLOAT ' % base,

    SEC = float(1000)
    MIN = float(60*1000)
    HRS = float(60*60*1000)
    DAY = float(24*60*60*1000)
    
    tsts = [

        [ 1.0, base, 'msec', 1],
        [ 1.0, base, 'sec', 1/SEC],
        [ 1.0, base, 'min', 1/MIN],
        [ 1.0, base, 'hr', 1/HRS],
        [ 1.0, base, 'day', 1/DAY],

        [ SEC, base, 'msec', SEC],
        [ SEC, base, 'sec', 1],
        [ SEC, base, 'min', SEC/MIN],
        [ SEC, base, 'hr', SEC/HRS],
        [ SEC, base, 'day', SEC/DAY],

        [ MIN, base, 'msec', MIN],
        [ MIN, base, 'sec', MIN/SEC],
        [ MIN, base, 'min', 1],
        [ MIN, base, 'hr', MIN/HRS],
        [ MIN, base, 'day', MIN/DAY],

        [ HRS, base, 'msec', HRS],
        [ HRS, base, 'sec', HRS/SEC],
        [ HRS, base, 'min', HRS/MIN],
        [ HRS, base, 'hr', 1],
        [ HRS, base, 'day', HRS/DAY],

        [ DAY, base, 'msec', DAY],
        [ DAY, base, 'sec', DAY/SEC],
        [ DAY, base, 'min', DAY/MIN],
        [ DAY, base, 'hr', DAY/HRS],
        [ DAY, base, 'day', 1],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    return True

def test_adjust_sec( chatty):

    base = 'sec'
    print 'test adjust_time_duration() from %s to X as INT ' % base,

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)
    
    tsts = [
        [ 1, None, None, 1], # None defaults to msec
        [ 1, '', '', 1],     # None defaults to msec
        
        [ 1, base, 'msec', SEC],
        [ 1, base, 'sec', 1],
        [ 1, base, 'min', 0],
        [ 1, base, 'hr', 0],
        [ 1, base, 'day', 0],

        [ SEC, base, 'msec', SEC*1000],
        [ SEC, base, 'sec', SEC],
        [ SEC, base, 'min', SEC/60],
        [ SEC, base, 'hr', 0],
        [ SEC, base, 'day', 0],

        [ MIN, base, 'msec', MIN*1000],
        [ MIN, base, 'sec', MIN],
        [ MIN, base, 'min', MIN/60],
        [ MIN, base, 'hr', MIN/3600],
        [ MIN, base, 'day', 0],

        [ HRS, base, 'msec', HRS*1000],
        [ HRS, base, 'sec', HRS],
        [ HRS, base, 'min', HRS/60],
        [ HRS, base, 'hr', HRS/3600],
        [ HRS, base, 'day', HRS/(60*60*24)],

        [ DAY, base, 'msec', DAY*1000],
        [ DAY, base, 'sec', DAY],
        [ DAY, base, 'min', DAY/60],
        [ DAY, base, 'hr', DAY/3600],
        [ DAY, base, 'day', DAY/(60*60*24)],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    print 'test adjust_time_duration() from %s to X as FLOAT ' % base,

    SEC = float(1000)
    MIN = float(60*1000)
    HRS = float(60*60*1000)
    DAY = float(24*60*60*1000)
    
    tsts = [

        [ 1.0, base, 'msec', SEC],
        [ 1.0, base, 'sec', SEC/SEC],
        [ 1.0, base, 'min', SEC/MIN],
        [ 1.0, base, 'hr', SEC/HRS],
        [ 1.0, base, 'day', SEC/DAY],

        [ SEC, base, 'msec', SEC*1000],
        [ SEC, base, 'sec', SEC],
        [ SEC, base, 'min', SEC/60],
        [ SEC, base, 'hr', SEC/3600],
        [ SEC, base, 'day', SEC/(60*60*24)],

        [ MIN, base, 'msec', MIN*1000],
        [ MIN, base, 'sec', MIN],
        [ MIN, base, 'min', MIN/60],
        [ MIN, base, 'hr', MIN/3600],
        [ MIN, base, 'day', MIN/(60*60*24)],

        [ HRS, base, 'msec', HRS*1000],
        [ HRS, base, 'sec', HRS],
        [ HRS, base, 'min', HRS/60],
        [ HRS, base, 'hr', HRS/3600],
        [ HRS, base, 'day', HRS/(60*60*24)],

        [ DAY, base, 'msec', DAY*1000],
        [ DAY, base, 'sec', DAY],
        [ DAY, base, 'min', DAY/60],
        [ DAY, base, 'hr', DAY/3600],
        [ DAY, base, 'day', DAY/(60*60*24)],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    return True

def test_adjust_min( chatty):

    base = 'min'
    print 'test adjust_time_duration() from %s to X as INT ' % base,

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)
    
    tsts = [
        [ 1, None, None, 1], # None defaults to msec
        [ 1, '', '', 1],     # None defaults to msec
        
        [ 1, base, 'msec', MIN],
        [ 1, base, 'sec', MIN/SEC],
        [ 1, base, 'min', MIN/MIN],
        [ 1, base, 'hr', MIN/HRS],
        [ 1, base, 'day', 0],

        [ SEC, base, 'msec', MIN*SEC],
        [ SEC, base, 'sec', MIN],
        [ SEC, base, 'min', MIN/60],
        [ SEC, base, 'hr', MIN/(60*60)],
        [ SEC, base, 'day', MIN/(60*60*24)],

        [ MIN, base, 'msec', MIN*(60*1000)],
        [ MIN, base, 'sec', MIN*60],
        [ MIN, base, 'min', MIN],
        [ MIN, base, 'hr', MIN/60],
        [ MIN, base, 'day', MIN/(60*24)],

        [ HRS, base, 'msec', HRS*(60*1000)],
        [ HRS, base, 'sec', HRS*60],
        [ HRS, base, 'min', HRS],
        [ HRS, base, 'hr', HRS/60],
        [ HRS, base, 'day', HRS/(60*24)],

        [ DAY, base, 'msec', DAY*(60*1000)],
        [ DAY, base, 'sec', DAY*60],
        [ DAY, base, 'min', DAY],
        [ DAY, base, 'hr', DAY/60],
        [ DAY, base, 'day', DAY/(60*24)],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    print 'test adjust_time_duration() from %s to X as FLOAT ' % base,

    SEC = float(1000)
    MIN = float(60*1000)
    HRS = float(60*60*1000)
    DAY = float(24*60*60*1000)
    
    tsts = [

        [ 1.0, base, 'msec', MIN],
        [ 1.0, base, 'sec', MIN/SEC],
        [ 1.0, base, 'min', MIN/MIN],
        [ 1.0, base, 'hr', MIN/HRS],
        [ 1.0, base, 'day', MIN/DAY],

        [ SEC, base, 'msec', MIN*SEC],
        [ SEC, base, 'sec', MIN],
        [ SEC, base, 'min', MIN/60],
        [ SEC, base, 'hr', MIN/(60*60)],
        [ SEC, base, 'day', MIN/(60*60*24)],

        [ MIN, base, 'msec', MIN*(60*1000)],
        [ MIN, base, 'sec', MIN*60],
        [ MIN, base, 'min', MIN],
        [ MIN, base, 'hr', MIN/60],
        [ MIN, base, 'day', MIN/(60*24)],

        [ HRS, base, 'msec', HRS*(60*1000)],
        [ HRS, base, 'sec', HRS*60],
        [ HRS, base, 'min', HRS],
        [ HRS, base, 'hr', HRS/60],
        [ HRS, base, 'day', HRS/(60*24)],

        [ DAY, base, 'msec', DAY*(60*1000)],
        [ DAY, base, 'sec', DAY*60],
        [ DAY, base, 'min', DAY],
        [ DAY, base, 'hr', DAY/60],
        [ DAY, base, 'day', DAY/(60*24)],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    return True

def test_adjust_hr( chatty):

    base = 'hr'
    print 'test adjust_time_duration() from %s to X as INT ' % base,

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)
    
    tsts = [
        [ 1, None, None, 1], # None defaults to msec
        [ 1, '', '', 1],     # None defaults to msec
        
        [ 1, base, 'msec', HRS],
        [ 1, base, 'sec', HRS/SEC],
        [ 1, base, 'min', HRS/MIN],
        [ 1, base, 'hr', HRS/HRS],
        [ 1, base, 'day', 0],

        [ 10, base, 'msec', HRS*10],
        [ 10, base, 'sec', HRS*10/SEC],
        [ 10, base, 'min', HRS*10/MIN],
        [ 10, base, 'hr', HRS*10/HRS],
        [ 10, base, 'day', HRS*10/DAY],

        [ 100, base, 'msec', HRS*100],
        [ 100, base, 'sec', HRS*100/SEC],
        [ 100, base, 'min', HRS*100/MIN],
        [ 100, base, 'hr', HRS*100/HRS],
        [ 100, base, 'day', HRS*100/DAY],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    print 'test adjust_time_duration() from %s to X as FLOAT ' % base,

    SEC = float(1000)
    MIN = float(60*1000)
    HRS = float(60*60*1000)
    DAY = float(24*60*60*1000)
    
    tsts = [

        [ 1.0, base, 'msec', HRS],
        [ 1.0, base, 'sec', HRS/SEC],
        [ 1.0, base, 'min', HRS/MIN],
        [ 1.0, base, 'hr', HRS/HRS],
        [ 1.0, base, 'day', HRS/DAY],

        [ 10.0, base, 'msec', HRS*10],
        [ 10.0, base, 'sec', HRS*10/SEC],
        [ 10.0, base, 'min', HRS*10/MIN],
        [ 10.0, base, 'hr', HRS*10/HRS],
        [ 10.0, base, 'day', HRS*10/DAY],

        [ 100.0, base, 'msec', HRS*100],
        [ 100.0, base, 'sec', HRS*100/SEC],
        [ 100.0, base, 'min', HRS*100/MIN],
        [ 100.0, base, 'hr', HRS*100/HRS],
        [ 100.0, base, 'day', HRS*100/DAY],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    return True

def test_adjust_day( chatty):

    base = 'day'
    print 'test adjust_time_duration() from %s to X as INT ' % base,

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)
    
    tsts = [
        [ 1, None, None, 1], # None defaults to msec
        [ 1, '', '', 1],     # None defaults to msec
        
        [ 1, base, 'msec', DAY],
        [ 1, base, 'sec', DAY/SEC],
        [ 1, base, 'min', DAY/MIN],
        [ 1, base, 'hr', DAY/HRS],
        [ 1, base, 'day', DAY/DAY],

        [ 10, base, 'msec', DAY*10],
        [ 10, base, 'sec', DAY*10/SEC],
        [ 10, base, 'min', DAY*10/MIN],
        [ 10, base, 'hr', DAY*10/HRS],
        [ 10, base, 'day', DAY*10/DAY],

        [ 100, base, 'msec', DAY*100],
        [ 100, base, 'sec', DAY*100/SEC],
        [ 100, base, 'min', DAY*100/MIN],
        [ 100, base, 'hr', DAY*100/HRS],
        [ 100, base, 'day', DAY*100/DAY],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    print 'test adjust_time_duration() from %s to X as FLOAT ' % base,

    SEC = float(1000)
    MIN = float(60*1000)
    HRS = float(60*60*1000)
    DAY = float(24*60*60*1000)
    
    tsts = [

        [ 1.0, base, 'msec', DAY],
        [ 1.0, base, 'sec', DAY/SEC],
        [ 1.0, base, 'min', DAY/MIN],
        [ 1.0, base, 'hr', DAY/HRS],
        [ 1.0, base, 'day', DAY/DAY],

        [ 10.0, base, 'msec', DAY*10],
        [ 10.0, base, 'sec', DAY*10/SEC],
        [ 10.0, base, 'min', DAY*10/MIN],
        [ 10.0, base, 'hr', DAY*10/HRS],
        [ 10.0, base, 'day', DAY*10/DAY],

        [ 100.0, base, 'msec', DAY*100],
        [ 100.0, base, 'sec', DAY*100/SEC],
        [ 100.0, base, 'min', DAY*100/MIN],
        [ 100.0, base, 'hr', DAY*100/HRS],
        [ 100.0, base, 'day', DAY*100/DAY],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        rtn = adjust_time_duration( n, in_ty, ot_ty)
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    return True

def test_parse_time_duration( chatty):

    print 'test parse_time_duration() with INTs ',

    SEC = (1000)
    MIN = (60*1000)
    HRS = (60*60*1000)
    DAY = (24*60*60*1000)

    # order is IN, then OUT; use None to force default parameters;
    # this test assumes if IN is None, then OUT is also none
    tsts = [
        [ 1, None, None, 1], # None defaults to msec
        [ 1, '', None, 1],   # None defaults to msec
        [ 1, '', '', 1],     # None defaults to msec
        
        [ 1, 'msec', None, 1],
        [ 1, 'sec', None, SEC],
        [ 1, 'min', None, MIN],
        [ 1, 'hr', None, HRS],
        [ 1, 'day', None, DAY],

        [ '1', None, None, 1],
        [ '01', None, None, 1],
        [ '0x01', None, None, 1],

        [ '1 ms', None, None, 1],
        [ '1 msec', None, None, 1],
        [ '1 sec', None, None, SEC],
        [ '1 min', None, None, MIN],
        [ '1 hr', None, None, HRS],
        [ '1 day', None, None, DAY],

        [ '60 sec', None, None, MIN],
        [ '60 min', None, None, HRS],
        [ '12 hr', None, None, DAY/2],
        [ '24 hr', None, None, DAY],

        [ '1 min 30 sec', None, None, 90000],

        ]

    for tst in tsts:
        n = tst[0]
        in_ty = tst[1]
        ot_ty = tst[2]
        expect = tst[3]

        if( in_ty == None):
            # print 'assume both in/out are None, leave as default'
            rtn = parse_time_duration( n)
        elif( ot_ty == None):
            # print 'assume only in is None'
            rtn = parse_time_duration( n, in_ty)
        else:
            rtn = parse_time_duration( n, in_ty, ot_ty)

        if( type(rtn) != int):
            print "ERROR: type of rtn:%s (%s) didn't match expected INT in test:%s" % (rtn, type(rtn), tst)
            return False
        
        if( rtn != expect):
            print "ERROR: rtn:%s didn't match expected:%s in test:%s" % (rtn, expect, tst)
            return False
        
    print '... Okay!'

    return True

if __name__ == '__main__':

    test_all = False
    chatty = True

    if( False or test_all):
        test_duration_string_to_msec( chatty)

    if( False or test_all):
        test_adjust_msec( chatty)

    if( False or test_all):
        test_adjust_sec( chatty)

    if( False or test_all):
        test_adjust_min( chatty)

    if( False or test_all):
        test_adjust_hr( chatty)

    if( False or test_all):
        test_adjust_day( chatty)

    if( True or test_all):
        test_parse_time_duration( chatty)


