Example #1
0
def test_default_args():
    with py.test.raises(TypeError):
        datetime.datetime()
    with py.test.raises(TypeError):
        datetime.datetime(10)
    with py.test.raises(TypeError):
        datetime.datetime(10, 10)
    datetime.datetime(10, 10, 10)
Example #2
0
def test_check_arg_types():
    import decimal
    class Number:
        def __init__(self, value):
            self.value = value
        def __int__(self):
            return self.value

    for xx in [10L,
               decimal.Decimal(10),
               decimal.Decimal('10.9'),
               Number(10),
               Number(10L)]:
        assert datetime.datetime(10, 10, 10, 10, 10, 10, 10) == \
               datetime.datetime(xx, xx, xx, xx, xx, xx, xx)
Example #3
0
def test_unpickle():
    e = raises(TypeError, datetime.date, '123')
    assert e.value.args[0] == 'an integer is required'
    e = raises(TypeError, datetime.time, '123')
    assert e.value.args[0] == 'an integer is required'
    e = raises(TypeError, datetime.datetime, '123')
    assert e.value.args[0] == 'an integer is required'

    datetime.time('\x01' * 6, None)
    with raises(TypeError) as e:
        datetime.time('\x01' * 6, 123)
    assert str(e.value) == "bad tzinfo state arg"

    datetime.datetime('\x01' * 10, None)
    with raises(TypeError) as e:
        datetime.datetime('\x01' * 10, 123)
    assert str(e.value) == "bad tzinfo state arg"
Example #4
0
def test_strptime():
    import time, sys
    if sys.version_info < (2, 6):
        py.test.skip("needs the _strptime module")

    string = '2004-12-01 13:02:47'
    format = '%Y-%m-%d %H:%M:%S'
    expected = datetime.datetime(*(time.strptime(string, format)[0:6]))
    got = datetime.datetime.strptime(string, format)
    assert expected == got
Example #5
0
def test_strptime():
    import time, sys
    if sys.version_info < (2, 6):
        py.test.skip("needs the _strptime module")

    string = '2004-12-01 13:02:47'
    format = '%Y-%m-%d %H:%M:%S'
    expected = datetime.datetime(*(time.strptime(string, format)[0:6]))
    got = datetime.datetime.strptime(string, format)
    assert expected == got
Example #6
0
def test_integer_args():
    with py.test.raises(TypeError):
        datetime.datetime(10, 10, 10.)
    with py.test.raises(TypeError):
        datetime.datetime(10, 10, 10, 10, 10.)
    with py.test.raises(TypeError):
        datetime.datetime(10, 10, 10, 10, 10, 10.)
Example #7
0
def test_integer_args():
    with py.test.raises(TypeError):
        datetime.datetime(10, 10, 10.)
    with py.test.raises(TypeError):
        datetime.datetime(10, 10, 10, 10, 10.)
    with py.test.raises(TypeError):
        datetime.datetime(10, 10, 10, 10, 10, 10.)
Example #8
0
def test_repr():
    print datetime
    expected = "datetime.datetime(1, 2, 3, 0, 0)"
    assert repr(datetime.datetime(1,2,3)) == expected
Example #9
0
    class Number:
        def __init__(self, value):
            self.value = value
        def __int__(self):
            return self.value

    for xx in [10L,
               decimal.Decimal(10),
               decimal.Decimal('10.9'),
               Number(10),
               Number(10L)]:
        assert datetime.datetime(10, 10, 10, 10, 10, 10, 10) == \
               datetime.datetime(xx, xx, xx, xx, xx, xx, xx)

    with py.test.raises(TypeError) as e:
        datetime.datetime(10, 10, '10')
    assert str(e.value) == 'an integer is required'

    f10 = Number(10.9)
    with py.test.raises(TypeError) as e:
        datetime.datetime(10, 10, f10)
    assert str(e.value) == '__int__ method should return an integer'

    class Float(float):
        pass
    s10 = Float(10.9)
    with py.test.raises(TypeError) as e:
        datetime.datetime(10, 10, s10)
    assert str(e.value) == 'integer argument expected, got float'

    with py.test.raises(TypeError):
Example #10
0
def test_repr():
    print datetime
    expected = "datetime.datetime(1, 2, 3, 0, 0)"
    assert repr(datetime.datetime(1, 2, 3)) == expected