test_enum.py


Overview

`test_enum.py` is a test module designed to validate the serialization behavior of various `enum` types when encoded using the `orjson` JSON library in Python. The file explores different types of Python enumerations (`enum.Enum` and its subclasses) including string-based, integer-based, floating-point, flags, and custom classes, ensuring that `orjson` correctly serializes these enums to JSON. It also tests custom serialization via a default function and verifies certain type restrictions and serialization options.

This file primarily serves as a **unit test suite** to guarantee compatibility and expected behavior for enum serialization in the context of `orjson`, a performant JSON library.


Modules and Libraries


Classes and Functions

Enum Classes

The file defines multiple enum classes to test different enum types:

StrEnum(str, enum.Enum)

IntEnum(int, enum.Enum)

IntEnumEnum(enum.IntEnum)

IntFlagEnum(enum.IntFlag)

FlagEnum(enum.Flag)

AutoEnum(enum.auto)

FloatEnum(float, enum.Enum)

UnspecifiedEnum(enum.Enum)


Custom Classes

Custom


Functions

default(obj)


Test Class: TestEnum

Contains multiple test methods validating serialization and behavior of enums.

Each test method uses `pytest` for assertions and exception handling.


Methods


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

import orjson
from test_enum import UnspecifiedEnum, default

# Serialize an enum member with a string value
json_bytes = orjson.dumps(UnspecifiedEnum.A)  # b'"a"'

# Serialize an enum member with a custom class using default handler
json_bytes_custom = orjson.dumps(UnspecifiedEnum.E, default=default)  # b'"c"'

# Serialize a dictionary with enum keys using non-str keys option
json_bytes_dict = orjson.dumps({UnspecifiedEnum.A: 1}, option=orjson.OPT_NON_STR_KEYS)  # b'{"a":1}'

Mermaid Class Diagram

classDiagram
    class StrEnum {
        +AAA: str = "aaa"
    }
    class IntEnum {
        +ONE: int = 1
    }
    class IntEnumEnum {
        +ONE: int = 1
    }
    class IntFlagEnum {
        +ONE: int = 1
    }
    class FlagEnum {
        +ONE: int = 1
    }
    class AutoEnum {
        +A: str = "a"
    }
    class FloatEnum {
        +ONE: float = 1.1
    }
    class Custom {
        -val: any
        +__init__(val)
    }
    class UnspecifiedEnum {
        +A: str = "a"
        +B: int = 1
        +C: float = FloatEnum.ONE
        +D: dict = {"d": IntEnum.ONE}
        +E: Custom = Custom("c")
        +F: datetime = datetime.datetime(1970,1,1)
    }
    class TestEnum {
        +test_cannot_subclass()
        +test_arbitrary_enum()
        +test_custom_enum()
        +test_enum_options()
        +test_int_enum()
        +test_intenum_enum()
        +test_intflag_enum()
        +test_flag_enum()
        +test_auto_enum()
        +test_float_enum()
        +test_str_enum()
        +test_bool_enum()
        +test_non_str_keys_enum()
    }

    StrEnum <|-- UnspecifiedEnum
    IntEnum <|-- UnspecifiedEnum
    FloatEnum <|-- UnspecifiedEnum
    Custom <|-- UnspecifiedEnum

Summary

`test_enum.py` is a comprehensive test suite validating how different types of Python enums are serialized into JSON using the `orjson` library. It covers primitive value enums, flags, mixed-type enums, and custom serialization cases, while also verifying Python enum subclassing rules and serialization options. This file ensures that enums behave predictably and correctly in the context of JSON data interchange within the broader application ecosystem.