MCPcopy
hub / github.com/msgspec/msgspec / test_enum_missing

Method test_enum_missing

tests/unit/test_common.py:819–846  ·  view source on GitHub ↗
(self, proto)

Source from the content-addressed store, hash-verified

817 dec.decode(msgspec.msgpack.encode(key))
818
819 def test_enum_missing(self, proto):
820 class Ex(enum.Enum):
821 A = "a"
822 B = "b"
823
824 @classmethod
825 def _missing_(cls, val):
826 if val == "return-A":
827 return cls.A
828 elif val == "return-B":
829 return cls.B
830 elif val == "error":
831 raise ValueError("oh no!")
832 else:
833 return None
834
835 dec = proto.Decoder(Ex)
836
837 def roundtrip(msg):
838 return dec.decode(proto.encode(msg))
839
840 assert roundtrip("a") is Ex.A
841 assert roundtrip("return-A") is Ex.A
842 assert roundtrip("return-B") is Ex.B
843 with pytest.raises(ValidationError, match="Invalid enum value 'error'"):
844 roundtrip("error")
845 with pytest.raises(ValidationError, match="Invalid enum value 'other'"):
846 roundtrip("other")
847
848
849class TestLiterals:

Callers

nothing calls this directly

Calls 1

roundtripFunction · 0.85

Tested by

no test coverage detected