Skip to content

Commit

Permalink
Add test for dict-to-enum coercing for choices_attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Mar 18, 2024
1 parent 1275178 commit 4ffaf8e
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions miio/tests/test_descriptorcollection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

import pytest

from miio import (
Expand Down Expand Up @@ -119,22 +121,39 @@ def test_handle_enum_constraints(dummy_device, mocker):
"status_attribute": "attr",
}

mocker.patch.object(dummy_device, "choices_attr", create=True)

# Check that error is raised if choices are missing
invalid = EnumDescriptor(id="missing", **data)
with pytest.raises(
ValueError, match="Neither choices nor choices_attribute was defined"
):
coll.add_descriptor(invalid)

# Check that binding works
# Check that enum binding works
mocker.patch.object(
dummy_device,
"choices_attr",
create=True,
return_value=Enum("test enum", {"foo": 1}),
)
choices_attribute = EnumDescriptor(
id="with_choices_attr", choices_attribute="choices_attr", **data
)
coll.add_descriptor(choices_attribute)
assert len(coll) == 1
assert coll["with_choices_attr"].choices is not None

assert issubclass(coll["with_choices_attr"].choices, Enum)

# Check that dict binding works
mocker.patch.object(
dummy_device, "choices_attr_dict", create=True, return_value={"test": "dict"}
)
choices_attribute_dict = EnumDescriptor(
id="with_choices_attr_dict", choices_attribute="choices_attr_dict", **data
)
coll.add_descriptor(choices_attribute_dict)
assert len(coll) == 2

assert issubclass(coll["with_choices_attr_dict"].choices, Enum)


def test_handle_range_constraints(dummy_device, mocker):
Expand Down

0 comments on commit 4ffaf8e

Please sign in to comment.