Ejemplo n.º 1
0
def test_encode_raises_on_bytes_length_mismatch(invalid_bytes_48_80_128):
    """
    Assert that :func:`~ulid.base32.encode` raises a :class:`~ValueError` when given a :class:`~bytes`
    instance that is not exactly 48, 80, or 128 bits in length.
    """
    with pytest.raises(ValueError) as ex:
        base32.encode(invalid_bytes_48_80_128)
    assert ex.match(ENCODE_BYTE_SIZE_EXC_REGEX)
Ejemplo n.º 2
0
def test_encode_handles_randomness_and_returns_16_char_string(valid_bytes_80):
    """
    Assert that :func:`~ulid.base32.encode` encodes a valid 80 bit bytes object into a :class:`~str`
    that is 16 characters long.
    """
    encoded = base32.encode(valid_bytes_80)
    assert isinstance(encoded, str)
    assert len(encoded) == 16
Ejemplo n.º 3
0
def test_encode_handles_timestamp_and_returns_10_char_string(valid_bytes_48):
    """
    Assert that :func:`~ulid.base32.encode` encodes a valid 48 bit bytes object into a :class:`~str`
    that is 10 characters long.
    """
    encoded = base32.encode(valid_bytes_48)
    assert isinstance(encoded, str)
    assert len(encoded) == 10
Ejemplo n.º 4
0
def test_parse_returns_ulid_instance_from_ulid_str(valid_bytes_128):
    """
    Assert that :func:`~ulid.api.parse` returns a new :class:`~ulid.ulid.ULID` instance
    from the given :class:`~str` instance that represents a fill ULID.
    """
    value = base32.encode(valid_bytes_128)
    instance = api.parse(value)
    assert isinstance(instance, ulid.ULID)
    assert instance.bytes == valid_bytes_128
Ejemplo n.º 5
0
def test_from_str_raises_when_not_128_bits(valid_bytes_48):
    """
    Assert that :func:`~ulid.api.from_str` raises a :class:`~ValueError` when given bytes
    that is not 128 bit in length.
    """
    value = base32.encode(valid_bytes_48)
    with pytest.raises(ValueError) as ex:
        api.from_str(value)
    assert ex.match(STR_SIZE_EXC_REGEX)
Ejemplo n.º 6
0
def test_from_str_returns_ulid_instance(valid_bytes_128):
    """
    Assert that :func:`~ulid.api.from_str` returns a new :class:`~ulid.ulid.ULID` instance
    from the given bytes.
    """
    value = base32.encode(valid_bytes_128)
    instance = api.from_str(value)
    assert isinstance(instance, ulid.ULID)
    assert instance.bytes == valid_bytes_128
Ejemplo n.º 7
0
def test_model_supports_ge_with_expected_types(model_with_ordered_bytes):
    """
    Assert that any of the model types support "greater than or equal" comparisons against expected types.
    """
    model_type, less_than_bytes, greater_than_bytes = model_with_ordered_bytes

    model = model_type(greater_than_bytes)
    assert model >= ulid.MemoryView(greater_than_bytes)
    assert model >= bytes(greater_than_bytes)
    assert model >= bytearray(greater_than_bytes)
    assert model >= memoryview(greater_than_bytes)
    assert model >= int.from_bytes(greater_than_bytes, byteorder='big')
    assert model >= base32.encode(greater_than_bytes)

    assert model >= ulid.MemoryView(less_than_bytes)
    assert model >= bytes(less_than_bytes)
    assert model >= bytearray(less_than_bytes)
    assert model >= memoryview(less_than_bytes)
    assert model >= int.from_bytes(less_than_bytes, byteorder='big')
    assert model >= base32.encode(less_than_bytes)
Ejemplo n.º 8
0
def test_model_supports_ne_with_expected_types(model_with_ne_bytes):
    """
    Assert that any of the model types supports "not equal" comparisons against expected types.
    """
    model_type, equal_bytes, not_equal_bytes = model_with_ne_bytes

    model = model_type(equal_bytes)
    assert model != ulid.MemoryView(not_equal_bytes)
    assert model != bytes(not_equal_bytes)
    assert model != bytearray(not_equal_bytes)
    assert model != memoryview(not_equal_bytes)
    assert model != int.from_bytes(not_equal_bytes, byteorder='big')
    assert model != base32.encode(not_equal_bytes)
Ejemplo n.º 9
0
def test_model_supports_eq_with_expected_types(model_with_eq_bytes):
    """
    Assert that any of the model types support "equal" comparisons against expected types.
    """
    model_type, equal_bytes = model_with_eq_bytes

    model = model_type(equal_bytes)
    assert model == model_type(equal_bytes)
    assert model == bytes(equal_bytes)
    assert model == bytearray(equal_bytes)
    assert model == memoryview(equal_bytes)
    assert model == int.from_bytes(equal_bytes, byteorder='big')
    assert model == base32.encode(equal_bytes)
Ejemplo n.º 10
0
def test_model_supports_lt_with_expected_types(model_with_ordered_bytes):
    """
    Assert that any of the model types support "less than" comparisons against expected types.
    """
    model_type, less_than_bytes, greater_than_bytes = model_with_ordered_bytes

    model = model_type(less_than_bytes)
    assert model < ulid.MemoryView(greater_than_bytes)
    assert model < bytes(greater_than_bytes)
    assert model < bytearray(greater_than_bytes)
    assert model < memoryview(greater_than_bytes)
    assert model < int.from_bytes(greater_than_bytes, byteorder='big')
    assert model < float(int.from_bytes(greater_than_bytes, byteorder='big'))
    assert model < base32.encode(greater_than_bytes)
Ejemplo n.º 11
0
 def __str__(self) -> str:
     """Encode this object as a 26 character string sequence."""
     return base32.encode(self.bytes)