|
| 1 | +from enum import Enum, IntEnum, StrEnum |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from cattrs import BaseConverter, Converter |
| 6 | +from cattrs.preconf.msgspec import MsgspecJsonConverter |
| 7 | +from cattrs.preconf.orjson import OrjsonConverter |
| 8 | + |
| 9 | + |
| 10 | +class SimpleEnum(Enum): |
| 11 | + FIRST = "first" |
| 12 | + SECOND = "second" |
| 13 | + THIRD = "third" |
| 14 | + |
| 15 | + |
| 16 | +class SimpleIntEnum(IntEnum): |
| 17 | + ONE = 1 |
| 18 | + TWO = 2 |
| 19 | + THREE = 3 |
| 20 | + |
| 21 | + |
| 22 | +class SimpleStrEnum(StrEnum): |
| 23 | + ALPHA = "alpha" |
| 24 | + BETA = "beta" |
| 25 | + GAMMA = "gamma" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter, MsgspecJsonConverter, OrjsonConverter]) |
| 29 | +def test_unstructure_simple_enum(benchmark, converter_cls): |
| 30 | + """Benchmark unstructuring a simple enum.""" |
| 31 | + c = converter_cls() |
| 32 | + benchmark(c.unstructure, SimpleEnum.FIRST) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter, MsgspecJsonConverter, OrjsonConverter]) |
| 36 | +def test_structure_simple_enum(benchmark, converter_cls): |
| 37 | + """Benchmark structuring a simple enum.""" |
| 38 | + c = converter_cls() |
| 39 | + benchmark(c.structure, "first", SimpleEnum) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter, MsgspecJsonConverter, OrjsonConverter]) |
| 43 | +def test_unstructure_simple_int_enum(benchmark, converter_cls): |
| 44 | + """Benchmark unstructuring a simple IntEnum.""" |
| 45 | + c = converter_cls() |
| 46 | + benchmark(c.unstructure, SimpleIntEnum.ONE) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter, MsgspecJsonConverter, OrjsonConverter]) |
| 50 | +def test_structure_simple_int_enum(benchmark, converter_cls): |
| 51 | + """Benchmark structuring a simple IntEnum.""" |
| 52 | + c = converter_cls() |
| 53 | + benchmark(c.structure, 1, SimpleIntEnum) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter, MsgspecJsonConverter, OrjsonConverter]) |
| 57 | +def test_unstructure_simple_str_enum(benchmark, converter_cls): |
| 58 | + """Benchmark unstructuring a simple StrEnum.""" |
| 59 | + c = converter_cls() |
| 60 | + benchmark(c.unstructure, SimpleStrEnum.ALPHA) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize("converter_cls", [BaseConverter, Converter, MsgspecJsonConverter, OrjsonConverter]) |
| 64 | +def test_structure_simple_str_enum(benchmark, converter_cls): |
| 65 | + """Benchmark structuring a simple StrEnum.""" |
| 66 | + c = converter_cls() |
| 67 | + benchmark(c.structure, "alpha", SimpleStrEnum) |
| 68 | + |
0 commit comments