Skip to content

Commit

Permalink
Export UnicodeProtobufEnumAttribute and make prefix optional (#53)
Browse files Browse the repository at this point in the history
* Add `UnicodeProtobufEnumAttribute` back to module `__init__.py`
* Make enum prefix optional (default to empty string) so db stored
enum.Name as-is by default

Test:
* unit test
* `pip install -e ./pynamodb-attributes` from another local repo to test
import and save
  • Loading branch information
Roytangrb authored May 17, 2024
1 parent 1a74dea commit af4b50f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pynamodb_attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .unicode_datetime import UnicodeDatetimeAttribute
from .unicode_delimited_tuple import UnicodeDelimitedTupleAttribute
from .unicode_enum import UnicodeEnumAttribute
from .unicode_protobuf_enum import UnicodeProtobufEnumAttribute
from .uuid import UUIDAttribute

__all__ = [
Expand All @@ -22,6 +23,7 @@
"IntegerEnumAttribute",
"UnicodeDelimitedTupleAttribute",
"UnicodeEnumAttribute",
"UnicodeProtobufEnumAttribute",
"TimedeltaAttribute",
"TimedeltaMsAttribute",
"TimedeltaUsAttribute",
Expand Down
2 changes: 1 addition & 1 deletion pynamodb_attributes/unicode_protobuf_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
enum_type: Type[_TProtobufEnum],
*,
unknown_value: Optional[_TProtobufEnum] = _fail,
prefix: str,
prefix: str = "",
lower: bool = True,
**kwargs: Any,
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ universal = 1
[metadata]
license_file = LICENSE
name = pynamodb-attributes
version = 0.5.0
version = 0.5.1
description = Common attributes for PynamoDB
long_description = file:README.md
long_description_content_type = text/markdown
Expand Down
12 changes: 11 additions & 1 deletion tests/unicode_protobuf_enum_attribute_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pynamodb.models import Model
from typing_extensions import assert_type

from pynamodb_attributes.unicode_protobuf_enum import UnicodeProtobufEnumAttribute
from pynamodb_attributes import UnicodeProtobufEnumAttribute
from tests.connection import _connection
from tests.meta import dynamodb_table_meta

Expand Down Expand Up @@ -85,6 +85,12 @@ class MyModel(Model):
prefix="SHAKE_FLAVOR_",
null=True,
)
value_with_prefix = UnicodeProtobufEnumAttribute(
diner_pb2.ShakeFlavor,
unknown_value=diner_pb2.SHAKE_FLAVOR_UNKNOWN,
null=True,
lower=False,
)
map_attr = MyMapAttr(null=True)


Expand Down Expand Up @@ -140,6 +146,7 @@ def test_serialization_unknown_value_success(uuid_key):
"value": {"S": "vanilla"},
"value_upper": {"S": "VANILLA"},
"value_with_unknown": {"S": "vanilla"},
"value_with_prefix": {"S": "SHAKE_FLAVOR_VANILLA"},
},
),
(
Expand All @@ -148,6 +155,7 @@ def test_serialization_unknown_value_success(uuid_key):
"value": {"S": "chocolate"},
"value_upper": {"S": "CHOCOLATE"},
"value_with_unknown": {"S": "chocolate"},
"value_with_prefix": {"S": "SHAKE_FLAVOR_CHOCOLATE"},
},
),
],
Expand All @@ -162,6 +170,7 @@ def test_serialization(
model.value = value
model.value_upper = value
model.value_with_unknown = value
model.value_with_prefix = value
model.save()

# verify underlying storage
Expand All @@ -173,6 +182,7 @@ def test_serialization(
assert model.value == value
assert model.value_upper == value
assert model.value_with_unknown == value
assert model.value_with_prefix == value


def test_map_attribute( # exercises the __deepcopy__ method
Expand Down

0 comments on commit af4b50f

Please sign in to comment.