From e3edc4e2f8dcb5c7ff6b43154a0062aae02501c9 Mon Sep 17 00:00:00 2001 From: Roland Hedberg Date: Tue, 8 Apr 2014 16:29:38 +0200 Subject: Fixed bug in duration parsing. --- tests/test_10_time_util.py | 67 +++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 21 deletions(-) (limited to 'tests/test_10_time_util.py') diff --git a/tests/test_10_time_util.py b/tests/test_10_time_util.py index 555ee15d..d86f1081 100644 --- a/tests/test_10_time_util.py +++ b/tests/test_10_time_util.py @@ -7,38 +7,43 @@ 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 - assert f_quotient(1,3) == 0 - assert f_quotient(2,3) == 0 - assert f_quotient(3,3) == 1 - assert f_quotient(3.123,3) == 1 + assert f_quotient(-1, 3) == -1 + assert f_quotient(0, 3) == 0 + assert f_quotient(1, 3) == 0 + assert f_quotient(2, 3) == 0 + 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 - assert modulo(1,3) == 1 - assert modulo(2,3) == 2 - assert modulo(3,3) == 0 + assert modulo(-1, 3) == 2 + assert modulo(0, 3) == 0 + assert modulo(1, 3) == 1 + assert modulo(2, 3) == 2 + assert modulo(3, 3) == 0 x = 3.123 - assert modulo(3.123,3) == x - 3 + 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): + for i in range(1, 13): assert f_quotient(i, 1, 13) == 0 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): + for i in range(1, 13): assert modulo(i, 1, 13) == i assert modulo(13, 1, 13) == 1 #x = 0.123 #assert modulo(13+x, 1, 13) == 1+x + def test_parse_duration(): (sign, d) = parse_duration("P1Y3M5DT7H10M3.3S") assert sign == "+" @@ -48,7 +53,19 @@ def test_parse_duration(): assert d['tm_mday'] == 5 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") @@ -58,17 +75,19 @@ def test_add_duration_1(): assert t.tm_hour == 19 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") + t = add_duration(str_to_time("2000-01-12T00:00:00Z"), "PT33H") assert t.tm_year == 2000 assert t.tm_mon == 1 assert t.tm_mday == 14 assert t.tm_hour == 9 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,22 +97,25 @@ 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 assert valid("%d-01-12T00:00:00Z" % (current_year + 1)) == True this_instance = instant() time.sleep(1) - assert valid(this_instance) == False # unless on a very fast machine :-) + assert valid(this_instance) == False # unless on a very fast machine :-) 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() -- cgit v1.2.1