Ejemplo n.º 1
0
def test_memoryview_defines_hash(valid_bytes_128):
    """
    Assert that the `hash` representation of a :class:`~ulid.ulid.MemoryView` is equal to the
    hash result of the underlying :class:`~memoryview.`
    """
    mv = ulid.MemoryView(valid_bytes_128)
    assert hash(mv) == hash(mv.memory)
Ejemplo n.º 2
0
def test_memoryview_supports_getstate(valid_bytes_128):
    """
    Assert that the `__getstate__` representation of a :class:`~ulid.ulid.MemoryView` is equal to the
    str result of the underlying :class:`~memoryview.`
    """
    mv = ulid.MemoryView(valid_bytes_128)
    assert mv.__getstate__() == mv.str
Ejemplo n.º 3
0
def test_memoryview_supports_float(valid_bytes_128):
    """
    Assert that the `float` representation of a :class:`~ulid.ulid.MemoryView` is equal to the
    result of the :meth:`~ulid.ulid.MemoryView.float` method.
    """
    mv = ulid.MemoryView(valid_bytes_128)
    assert float(mv) == mv.float
Ejemplo n.º 4
0
def test_memoryview_ne_false_with_unsupported_type(
        valid_bytes_128, unsupported_comparison_type):
    """
    Assert that :class:`~ulid.ulid.MemoryView` returns `True` on "not equal" comparisons
    against unsupported types.
    """
    assert ulid.MemoryView(valid_bytes_128) != unsupported_comparison_type()
Ejemplo n.º 5
0
def test_memoryview_supports_index(valid_bytes_128):
    """
    Assert that the `__index__` representation of a :class:`~ulid.ulid.MemoryView` is equal to the
    int value of the underlying :class:`~memoryview.`
    """
    mv = ulid.MemoryView(valid_bytes_128)
    assert mv.__index__() == mv.int
Ejemplo n.º 6
0
def test_memoryview_supports_copy(valid_bytes_128):
    """
    Assert that instances of :class:`~ulid.ulid.MemoryView` can be copied using
    :func:`~copy.copy`.
    """
    mv = ulid.MemoryView(valid_bytes_128)
    copied = copy.copy(mv)
    assert copied == mv
Ejemplo n.º 7
0
def test_memoryview_unorderble_with_unsupported_type(valid_bytes_128, unsupported_comparison_type):
    """
    Assert that :class:`~ulid.ulid.MemoryView` returns `False` on "less than" comparisons
    against unsupported types.
    """
    mv = ulid.MemoryView(valid_bytes_128)
    for op in (operator.lt, operator.gt, operator.le, operator.ge):
        with pytest.raises(TypeError):
            op(mv, unsupported_comparison_type())
Ejemplo n.º 8
0
def test_memoryview_supports_deepcopy(valid_bytes_128):
    """
    Assert that instances of :class:`~ulid.ulid.MemoryView` can be copied using
    :func:`~copy.deepcopy`.
    """
    mv = ulid.MemoryView(valid_bytes_128)
    data = dict(a=dict(b=dict(c=mv)))
    copied = copy.deepcopy(data)
    assert copied == data
Ejemplo n.º 9
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.º 10
0
def test_memoryview_supports_pickle(valid_bytes_128):
    """
    Assert that instances of :class:`~ulid.ulid.MemoryView` can be pickled and use the
    the str result of the underlying :class:`~memoryview.` as the serialized value.
    """
    mv = ulid.MemoryView(valid_bytes_128)
    serialized = pickle.dumps(mv)
    assert serialized is not None
    assert isinstance(serialized, bytes)
    deserialized = pickle.loads(serialized)
    assert deserialized == mv.str
Ejemplo n.º 11
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.º 12
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)