Skip to content

Commit

Permalink
Merge pull request #7 from take-kun/release/0.2.1
Browse files Browse the repository at this point in the history
release: 0.2.1
  • Loading branch information
take-kun authored Jan 8, 2023
2 parents cf5055f + 859eb38 commit 6cb06fc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
5 changes: 4 additions & 1 deletion mercapi/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ def __getitem__(self, key):
raise IndexError(
f"{key} is not a valid member of {self.__class__.__name__}"
)
return getattr(self, key)
res = getattr(self, key)
if not issubclass(type(res), ResponseModel):
return res
return dict(res)

@classmethod
def from_dict(cls, d: dict) -> RM:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mercapi"
version = "0.2.0"
version = "0.2.1"
description = "Python API for querying and browsing mercari.jp"
authors = ["take-kun <[email protected]>"]
license = "MIT"
Expand Down
31 changes: 31 additions & 0 deletions tests/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,34 @@ def __init__(self, field_1: str, field_2: Optional[str]):
super().__init__()
self.field_1 = field_1
self.field_2 = field_2


class ModelTestBNested(ResponseModel):
_required_properties = [
ResponseProperty("field_a", "field_a", Extractors.get("field_a")),
]
_optional_properties = [
ResponseProperty("field_b", "field_b", Extractors.get("field_b")),
]

def __init__(self, field_a: str, field_b: Optional[str]):
super().__init__()
self.field_a = field_a
self.field_b = field_b


class ModelTestB(ResponseModel):
_required_properties = [
ResponseProperty("field_1", "field_1", Extractors.get("field_1")),
ResponseProperty(
"field_nested",
"field_nested",
Extractors.get_as_model("field_nested", ModelTestBNested),
),
]
_optional_properties = []

def __init__(self, field_1: str, field_nested: ModelTestBNested):
super().__init__()
self.field_1 = field_1
self.field_nested = field_nested
22 changes: 21 additions & 1 deletion tests/models/test_mapping_protocol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from tests.models import ModelTest
from tests.models import ModelTest, ModelTestB, ModelTestBNested


def test_retrieve_existing_required_field():
Expand All @@ -18,6 +18,12 @@ def test_retrieve_existing_optional_field():
assert obj["field_2"] == field_2


def test_retrieve_existing_empty_optional_field():
obj = ModelTest("foo", None)

assert obj["field_2"] is None


def test_retrieve_non_existent_field_throw_exception():
obj = ModelTest("foo", None)

Expand All @@ -43,3 +49,17 @@ def test_unpack_class_as_kwargs():
"field_1": obj.field_1,
"field_2": obj.field_2,
}


def test_convert_nested_class_to_dict():
obj_nested = ModelTestBNested("foo", "bar")
obj = ModelTestB("foo", obj_nested)
dict_obj = dict(obj)

assert dict_obj == {
"field_1": obj.field_1,
"field_nested": {
"field_a": obj_nested.field_a,
"field_b": obj_nested.field_b,
},
}

0 comments on commit 6cb06fc

Please sign in to comment.