enum_parity
frequenz.client.common.test.enum_parity ¤
Shared parity and conversion test base class for protobuf-backed enums.
Every Python enum that mirrors a protobuf enum follows the same convention:
- The protobuf enum value name is a fixed prefix (e.g.
EVENT_) followed by the Python enum member name. - The protobuf enum value number matches the Python enum value.
- There is a versioned
<enum>_from_protofunction returning the Python enum member for known values and the rawintfor unknown values. - There is a versioned
<enum>_to_protofunction returning the numeric protobuf value.
EnumParityTest is
a parametrized pytest base class covering all those invariants. New enum
wrappers add a one-line subclass that pins the protobuf-specific attributes
instead of copy-pasting the same scaffold.
Classes¤
frequenz.client.common.test.enum_parity.EnumParityTest ¤
Base test class checking protobuf/Python enum parity.
Subclasses must set python_enum, proto_enum, name_prefix,
from_proto and to_proto. The subclass name must start with Test so
pytest picks it up.
For each subclass, the inherited tests verify that:
- every protobuf enum name maps to a Python enum member with the same value (tolerating new protobuf values not yet mirrored in Python);
- every Python enum member maps to a protobuf enum name and value;
from_protoreturns the matching member for known values and the rawintfor unknown values;to_protoreturns the numeric protobuf value.
Subclasses are free to add further test_* methods.
Example
from frequenz.api.common.v1alpha8.streaming import event_pb2
from frequenz.client.common.streaming import Event
from frequenz.client.common.streaming.proto.v1alpha8 import (
event_from_proto,
event_to_proto,
)
from frequenz.client.common.test.enum_parity import EnumParityTest
class TestEventParity(EnumParityTest):
python_enum = Event
proto_enum = event_pb2.Event
name_prefix = "EVENT_"
from_proto = staticmethod(event_from_proto)
to_proto = staticmethod(event_to_proto)
| ATTRIBUTE | DESCRIPTION |
|---|---|
python_enum |
The Python enum subclass mirroring the protobuf enum. |
proto_enum |
The generated protobuf enum wrapper (e.g.
TYPE:
|
name_prefix |
Prefix used for protobuf enum value names (e.g.
TYPE:
|
from_proto |
Versioned converter from a protobuf enum value to the
Python enum. Returns the Python enum member for known values, or
the raw |
to_proto |
Versioned converter from a Python enum member to a protobuf
enum value. Bind with |
Source code in src/frequenz/client/common/test/enum_parity.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
Functions¤
pytest_generate_tests ¤
Parametrize pb_name and member from the configured enums.
| PARAMETER | DESCRIPTION |
|---|---|
metafunc
|
The
TYPE:
|
Source code in src/frequenz/client/common/test/enum_parity.py
test_enum_matches_proto_enum_name ¤
test_enum_matches_proto_enum_name(member: Enum) -> None
Test that all Python enum members have a matching protobuf name.
| PARAMETER | DESCRIPTION |
|---|---|
member
|
The Python enum member to check.
TYPE:
|
Source code in src/frequenz/client/common/test/enum_parity.py
test_enum_matches_proto_enum_value ¤
test_enum_matches_proto_enum_value(member: Enum) -> None
Test that all Python enum members have a matching protobuf value.
| PARAMETER | DESCRIPTION |
|---|---|
member
|
The Python enum member to check.
TYPE:
|
Source code in src/frequenz/client/common/test/enum_parity.py
test_from_proto ¤
test_from_proto(pb_name: str) -> None
Test conversion from protobuf returns a matching member or int.
| PARAMETER | DESCRIPTION |
|---|---|
pb_name
|
The protobuf enum value name to convert.
TYPE:
|
Source code in src/frequenz/client/common/test/enum_parity.py
test_from_proto_unknown ¤
Test conversion from protobuf for unknown values returns the int.
Source code in src/frequenz/client/common/test/enum_parity.py
test_proto_enum_matches_enum_name ¤
test_proto_enum_matches_enum_name(pb_name: str) -> None
Test that all known protobuf enum names match a Python member.
| PARAMETER | DESCRIPTION |
|---|---|
pb_name
|
The protobuf enum value name to check.
TYPE:
|
Source code in src/frequenz/client/common/test/enum_parity.py
test_proto_enum_matches_enum_value ¤
test_proto_enum_matches_enum_value(pb_name: str) -> None
Test that all known protobuf enum values match a Python member.
| PARAMETER | DESCRIPTION |
|---|---|
pb_name
|
The protobuf enum value name to check.
TYPE:
|