def test_from_timestamp_with_unsupported_type_raises(unsupported_type): """ Assert that :func:`~ulid.api.from_timestamp` raises a :class:`~ValueError` when given a type it cannot compute a timestamp value from. """ with pytest.raises(ValueError) as ex: api.from_timestamp(unsupported_type()) assert ex.match(UNSUPPORTED_TIMESTAMP_TYPE_EXC_REGEX)
def test_from_timestamp_with_incorrect_size_bytes_raises(valid_bytes_128): """ Assert that :func:`~ulid.api.from_timestamp` raises a :class:`~ValueError` when given a type that cannot be represented as exactly 48 bits. """ with pytest.raises(ValueError) as ex: api.from_timestamp(valid_bytes_128) assert ex.match(TIMESTAMP_SIZE_EXC_REGEX)
def test_from_timestamp_ulid_returns_ulid_instance(valid_bytes_128): """ Assert that :func:`~ulid.api.from_timestamp` returns a new :class:`~ulid.ulid.ULID` instance from the given timestamp as a :class:`~ulid.ulid.ULID`. """ value = ulid.ULID(valid_bytes_128) instance = api.from_timestamp(value) assert isinstance(instance, ulid.ULID) assert instance.timestamp() == value.timestamp()
def test_from_timestamp_str_returns_ulid_instance(valid_bytes_48): """ Assert that :func:`~ulid.api.from_timestamp` returns a new :class:`~ulid.ulid.ULID` instance from the given timestamp as a :class:`~str`. """ value = base32.encode_timestamp(valid_bytes_48) instance = api.from_timestamp(value) assert isinstance(instance, ulid.ULID) assert instance.timestamp().str == value
def test_from_timestamp_float_returns_ulid_instance(): """ Assert that :func:`~ulid.api.from_timestamp` returns a new :class:`~ulid.ulid.ULID` instance from the given Unix time from epoch in seconds as a :class:`~float`. """ value = float(time.time()) instance = api.from_timestamp(value) assert isinstance(instance, ulid.ULID) assert int(instance.timestamp().timestamp) == int(value)
def test_from_timestamp_datetime_returns_ulid_instance(): """ Assert that :func:`~ulid.api.from_timestamp` returns a new :class:`~ulid.ulid.ULID` instance from the given Unix time from epoch in seconds as an :class:`~datetime.dateime`. """ value = datetime.datetime.now() instance = api.from_timestamp(value) assert isinstance(instance, ulid.ULID) assert int(instance.timestamp().timestamp) == int(value.timestamp())
def test_from_timestamp_bytes_returns_ulid_instance(buffer_type, valid_bytes_48): """ Assert that :func:`~ulid.api.from_timestamp` returns a new :class:`~ulid.ulid.ULID` instance from the given timestamp as an object that supports the buffer protocol. """ value = buffer_type(valid_bytes_48) instance = api.from_timestamp(value) assert isinstance(instance, ulid.ULID) assert instance.timestamp().bytes == value