Repository: Chia-Network/chia-blockchain
File: chia/_tests/core/util/test_streamable.py (Python)

Code before:
    class TestClassTuple(Streamable):
        a: tuple[uint8, str]

    # Does not have the required elements
    with pytest.raises(AssertionError):
        TestClassTuple.from_bytes(bytes([0, 0, 100, 24]))


def test_ambiguous_deserialization_str() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassStr(Streamable):
        a: str

    # Does not have the required str size
    with pytest.raises(AssertionError):
        TestClassStr.from_bytes(bytes([0, 0, 100, 24, 52]))


def test_ambiguous_deserialization_str_enum() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassStr(Streamable):
        a: StrEnum

    # Does not have the required str size
    with pytest.raises(AssertionError):
        TestClassStr.from_bytes(bytes([0, 0, 100, 24, 52]))


def test_ambiguous_deserialization_bytes() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassBytes(Streamable):
        a: bytes

    # Does not have the required str size
    with pytest.raises(AssertionError):
        TestClassBytes.from_bytes(bytes([0, 0, 100, 24, 52]))

    with pytest.raises(AssertionError):
        TestClassBytes.from_bytes(bytes([0, 0, 0, 1]))

    TestClassBytes.from_bytes(bytes([0, 0, 0, 1, 52]))
    TestClassBytes.from_bytes(bytes([0, 0, 0, 2, 52, 21]))


def test_ambiguous_deserialization_bool() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassBool(Streamable):

Review comment (suggestion):
same comment as above and my misunderstanding, but, i'm not sure i understand.  is the `StrEnum` implicitly configured for only three character strings because that's what the two values are?  or is this more generally saying the bytes don't match any of the values?  or...

Code after:
    class TestClassTuple(Streamable):
        a: tuple[uint8, str]

    # Does not have the required elements
    with pytest.raises(AssertionError):
        TestClassTuple.from_bytes(bytes([0, 0, 100, 24]))


def test_ambiguous_deserialization_str() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassStr(Streamable):
        a: str

    # Does not have the required str size
    with pytest.raises(AssertionError):
        TestClassStr.from_bytes(bytes([0, 0, 100, 24, 52]))


def test_ambiguous_deserialization_str_enum() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassStr(Streamable):
        a: StringEnum

    # passed bytes are incorrect size for serialization proxy
    with pytest.raises(AssertionError):
        TestClassStr.from_bytes(bytes([0, 0, 100, 24, 52]))


def test_deserialization_to_invalid_enum() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassStr(Streamable):
        a: StringEnum

    # encodes the string "baz" which is not a valid value for StringEnum
    with pytest.raises(ValueError, match=re.escape("'baz' is not a valid StringEnum")):
        TestClassStr.from_bytes(bytes([0, 0, 0, 3, 98, 97, 122]))


def test_ambiguous_deserialization_bytes() -> None:
    @streamable
    @dataclass(frozen=True)
    class TestClassBytes(Streamable):
        a: bytes

    # Does not have the required str size
    with pytest.raises(AssertionError):
        TestClassBytes.from_bytes(bytes([0, 0, 100, 24, 52]))