Skip to content

Commit 75a0d9e

Browse files
nnethercottbrunoocasali
authored andcommitted
Update Document model and fix tests
1 parent c2bab14 commit 75a0d9e

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

meilisearch/models/document.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33

44
class Document:
5-
__doc: Dict
6-
75
def __init__(self, doc: Dict[str, Any]) -> None:
8-
self.__doc = doc
9-
for key in doc:
10-
setattr(self, key, doc[key])
6+
self.__dict__.update(**doc)
117

128
def __getattr__(self, attr: str) -> str:
13-
if attr in self.__doc.keys():
14-
return attr
15-
raise AttributeError(f"{self.__class__.__name__} object has no attribute {attr}")
9+
try:
10+
return self.__dict__[attr]
11+
except Exception as _:
12+
raise AttributeError(f"{self.__class__.__name__} object has no attribute {attr}")
1613

1714
def __iter__(self) -> Iterator:
1815
return iter(self.__dict__.items())

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ disable=[
114114
"too-few-public-methods",
115115
"line-too-long",
116116
"too-many-positional-arguments",
117+
"raise-missing-from",
117118
]
118119
enable=[
119120
"use-symbolic-message-instead",

tests/models/test_document.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
from meilisearch.models.document import Document
77

88

9+
def test_doc_init():
10+
d = {"field1": "test 1", "fiels2": "test 2"}
11+
document = Document(d)
12+
assert dict(document) == d
13+
14+
915
def test_getattr():
1016
document = Document({"field1": "test 1", "fiels2": "test 2"})
11-
assert document.__getattr__("field1") == "field1"
17+
assert document.__getattr__("field1") == "test 1"
1218

1319

1420
def test_getattr_not_found():
@@ -18,11 +24,5 @@ def test_getattr_not_found():
1824

1925

2026
def test_iter():
21-
# I wrote a test what what this does, but I have a feeling this isn't actually what it was
22-
# expected to do when written as it doesn't really act like I would expect an iterator to act.
2327
document = Document({"field1": "test 1", "fiels2": "test 2"})
24-
25-
assert next(document.__iter__()) == (
26-
"_Document__doc",
27-
{"field1": "test 1", "fiels2": "test 2"},
28-
)
28+
assert list(iter(document)) == [("field1", "test 1"), ("fiels2", "test 2")]

0 commit comments

Comments
 (0)