Fixed bug in duration parsing.
This commit is contained in:
@@ -94,6 +94,8 @@ def parse_duration(duration):
|
||||
raise Exception("Not allowed to end with 'T'")
|
||||
else:
|
||||
raise Exception("Missing T")
|
||||
elif duration[index] == "T":
|
||||
continue
|
||||
else:
|
||||
try:
|
||||
mod = duration[index:].index(code)
|
||||
|
||||
@@ -7,6 +7,7 @@ from saml2.time_util import f_quotient, modulo, parse_duration, add_duration
|
||||
from saml2.time_util import str_to_time, instant, valid, in_a_while
|
||||
from saml2.time_util import before, after, not_before, not_on_or_after
|
||||
|
||||
|
||||
def test_f_quotient():
|
||||
assert f_quotient(-1, 3) == -1
|
||||
assert f_quotient(0, 3) == 0
|
||||
@@ -15,6 +16,7 @@ def test_f_quotient():
|
||||
assert f_quotient(3, 3) == 1
|
||||
assert f_quotient(3.123, 3) == 1
|
||||
|
||||
|
||||
def test_modulo():
|
||||
assert modulo(-1, 3) == 2
|
||||
assert modulo(0, 3) == 0
|
||||
@@ -24,6 +26,7 @@ def test_modulo():
|
||||
x = 3.123
|
||||
assert modulo(3.123, 3) == x - 3
|
||||
|
||||
|
||||
def test_f_quotient_2():
|
||||
assert f_quotient(0, 1, 13) == -1
|
||||
for i in range(1, 13):
|
||||
@@ -31,6 +34,7 @@ def test_f_quotient_2():
|
||||
assert f_quotient(13, 1, 13) == 1
|
||||
assert f_quotient(13.123, 1, 13) == 1
|
||||
|
||||
|
||||
def test_modulo_2():
|
||||
assert modulo(0, 1, 13) == 12
|
||||
for i in range(1, 13):
|
||||
@@ -39,6 +43,7 @@ def test_modulo_2():
|
||||
#x = 0.123
|
||||
#assert modulo(13+x, 1, 13) == 1+x
|
||||
|
||||
|
||||
def test_parse_duration():
|
||||
(sign, d) = parse_duration("P1Y3M5DT7H10M3.3S")
|
||||
assert sign == "+"
|
||||
@@ -49,6 +54,18 @@ def test_parse_duration():
|
||||
assert d['tm_year'] == 1
|
||||
assert d['tm_min'] == 10
|
||||
|
||||
|
||||
def test_parse_duration2():
|
||||
(sign, d) = parse_duration("PT30M")
|
||||
assert sign == "+"
|
||||
assert d['tm_sec'] == 0
|
||||
assert d['tm_mon'] == 0
|
||||
assert d['tm_hour'] == 0
|
||||
assert d['tm_mday'] == 0
|
||||
assert d['tm_year'] == 0
|
||||
assert d['tm_min'] == 30
|
||||
|
||||
|
||||
def test_add_duration_1():
|
||||
#2000-01-12T12:13:14Z P1Y3M5DT7H10M3S 2001-04-17T19:23:17Z
|
||||
t = add_duration(str_to_time("2000-01-12T12:13:14Z"), "P1Y3M5DT7H10M3S")
|
||||
@@ -59,6 +76,7 @@ def test_add_duration_1():
|
||||
assert t.tm_min == 23
|
||||
assert t.tm_sec == 17
|
||||
|
||||
|
||||
def test_add_duration_2():
|
||||
#2000-01-12 PT33H 2000-01-13
|
||||
t = add_duration(str_to_time("2000-01-12T00:00:00Z"), "PT33H")
|
||||
@@ -69,6 +87,7 @@ def test_add_duration_2():
|
||||
assert t.tm_min == 0
|
||||
assert t.tm_sec == 0
|
||||
|
||||
|
||||
def test_str_to_time():
|
||||
t = calendar.timegm(str_to_time("2000-01-12T00:00:00Z"))
|
||||
#TODO: Find all instances of time.mktime(.....)
|
||||
@@ -78,12 +97,14 @@ def test_str_to_time():
|
||||
# do this as an external method in the
|
||||
assert t == 947635200
|
||||
|
||||
|
||||
def test_instant():
|
||||
inst = str_to_time(instant())
|
||||
now = time.gmtime()
|
||||
|
||||
assert now >= inst
|
||||
|
||||
|
||||
def test_valid():
|
||||
assert valid("2000-01-12T00:00:00Z") == False
|
||||
current_year = datetime.datetime.today().year
|
||||
@@ -94,6 +115,7 @@ def test_valid():
|
||||
soon = in_a_while(seconds=10)
|
||||
assert valid(soon) == True
|
||||
|
||||
|
||||
def test_timeout():
|
||||
soon = in_a_while(seconds=1)
|
||||
time.sleep(2)
|
||||
@@ -122,3 +144,6 @@ def test_not_on_or_after():
|
||||
current_year = datetime.datetime.today().year
|
||||
assert not_on_or_after("%d-01-01T00:00:00Z" % (current_year + 1)) == True
|
||||
assert not_on_or_after("%d-01-01T00:00:00Z" % (current_year - 1)) == False
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_parse_duration2()
|
||||
|
||||
Reference in New Issue
Block a user