예제 #1
0
def test_decode_timestamp_with_incorrect_size_bytes_raises(valid_bytes_128):
    """
    Assert that :func:`~ulid.codec.decode_timestamp` raises a :class:`~ValueError` when given
    a type that cannot be represented as exactly 48 bits.
    """
    with pytest.raises(ValueError) as ex:
        codec.decode_timestamp(valid_bytes_128)
    assert ex.match(TIMESTAMP_SIZE_EXC_REGEX)
예제 #2
0
def test_decode_timestamp_with_unsupported_type_raises(unsupported_type):
    """
    Assert that :func:`~ulid.codec.decode_timestamp` raises a :class:`~ValueError` when given
    a type it cannot compute a timestamp value from.
    """
    with pytest.raises(ValueError) as ex:
        codec.decode_timestamp(unsupported_type())
    assert ex.match(UNSUPPORTED_TIMESTAMP_TYPE_EXC_REGEX)
예제 #3
0
def test_decode_timestamp_timestamp_returns_timestamp_instance(valid_bytes_48):
    """
    Assert that :func:`~ulid.codec.decode_timestamp` returns a new :class:`~ulid.ulid.Timestamp` instance
    from the given timestamp as a :class:`~ulid.ulid.Timestamp`.
    """
    value = ulid.Timestamp(valid_bytes_48)
    instance = codec.decode_timestamp(value)
    assert isinstance(instance, ulid.Timestamp)
    assert instance == value
예제 #4
0
def test_decode_timestamp_float_returns_timestamp_instance():
    """
    Assert that :func:`~ulid.codec.decode_timestamp` returns a new :class:`~ulid.ulid.Timestamp` instance
    from the given Unix time from epoch in seconds as a :class:`~float`.
    """
    value = float(time.time())
    instance = codec.decode_timestamp(value)
    assert isinstance(instance, ulid.Timestamp)
    assert int(instance.timestamp) == int(value)
예제 #5
0
def test_decode_timestamp_datetime_returns_timestamp_instance():
    """
    Assert that :func:`~ulid.codec.decode_timestamp` returns a new :class:`~ulid.ulid.Timestamp` instance
    from the given Unix time from epoch in seconds as an :class:`~datetime.datetime`.
    """
    value = datetime.datetime.now()
    instance = codec.decode_timestamp(value)
    assert isinstance(instance, ulid.Timestamp)
    assert int(instance.timestamp) == int(value.timestamp())
예제 #6
0
def test_decode_timestamp_bytes_returns_timestamp_instance(
        buffer_type, valid_bytes_48):
    """
    Assert that :func:`~ulid.codec.decode_timestamp` returns a new :class:`~ulid.ulid.Timestamp` instance
    from the given timestamp as an object that supports the buffer protocol.
    """
    value = buffer_type(valid_bytes_48)
    instance = codec.decode_timestamp(value)
    assert isinstance(instance, ulid.Timestamp)
    assert instance.bytes == value