def test_iso_datetimes(): "Test the iso_dates rule will generate encoders and decoders for datetimes using ISO8601." decoder = std.iso_dates(verb=JSON2PY, typ=datetime, ctx=Fail()) assert decoder("6666-06-06T12:12:12.987654") == datetime( 6666, 6, 6, 12, 12, 12, 987654 ) encoder = std.iso_dates(verb=PY2JSON, typ=datetime, ctx=Fail()) assert ( encoder(datetime(6666, 6, 6, 12, 12, 12, 987654)) == "6666-06-06T12:12:12.987654" ) inspect = std.iso_dates(verb=INSP_PY, typ=datetime, ctx=Fail()) assert not inspect(date(1776, 7, 4)) assert inspect(datetime(1776, 7, 4, 3, 3)) assert not inspect("2000-01-01") assert not inspect("2000-01-01T03:03:03") assert not inspect("string") inspect = std.iso_dates(verb=INSP_JSON, typ=datetime, ctx=Fail()) assert not inspect(date(1776, 7, 4)) assert not inspect(datetime(1776, 7, 4, 3, 3)) assert inspect("2000-01-01") assert inspect("2000-01-01T03:03:03") assert not inspect("string")
def test_iso_dates_disregard(): "Test the iso_dates rule will disregard unknown types and verbs." assert std.iso_dates(verb="unknown", typ=date, ctx=Rules()) is None assert std.iso_dates(verb=JSON2PY, typ=Mystery, ctx=Rules()) is None assert std.iso_dates(verb=PY2JSON, typ=Mystery, ctx=Rules()) is None assert std.iso_dates(verb=INSP_JSON, typ=Mystery, ctx=Rules()) is None assert std.iso_dates(verb=INSP_PY, typ=Mystery, ctx=Rules()) is None
def test_iso_dates_picklable(): "Test that actions generated by the iso_dates rule can be pickled." actions = [ std.iso_dates(verb=verb, typ=typ, ctx=Rules()) for verb in [JSON2PY, PY2JSON] for typ in [date, datetime] ] assert None not in actions dumps(actions)
def test_iso_dates(): "Test the iso_dates rule handles dates using ISO8601 and rejects datetimes." decoder = std.iso_dates(verb=JSON2PY, typ=date, ctx=Rules()) assert decoder("1776-07-04") == date(1776, 7, 4) with pytest.raises(ValueError): decoder("6543-02-01T09:09:09") encoder = std.iso_dates(verb=PY2JSON, typ=date, ctx=Rules()) assert encoder(date(1776, 7, 4)) == "1776-07-04" inspect = std.iso_dates(verb=INSP_PY, typ=date, ctx=Rules()) assert inspect(date(1776, 7, 4)) assert not inspect(datetime(1776, 7, 4, 3, 3)) assert not inspect("2000-01-01") assert not inspect("2000-01-01T03:03:03") assert not inspect("string") inspect = std.iso_dates(verb=INSP_JSON, typ=date, ctx=Rules()) assert not inspect(date(1776, 7, 4)) assert not inspect(datetime(1776, 7, 4, 3, 3)) assert inspect("2000-01-01") assert not inspect("2000-01-01T03:03:03") assert not inspect("string")