Skip to content

Fix Document model: bad test, attribute bug, and field cleanup #1113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions meilisearch/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@


class Document:
__doc: Dict

def __init__(self, doc: Dict[str, Any]) -> None:
self.__doc = doc
for key in doc:
setattr(self, key, doc[key])
self.__dict__.update(**doc)

def __getattr__(self, attr: str) -> str:
if attr in self.__doc.keys():
return attr
def __getattr__(self, attr: str) -> Any:
if attr in self.__dict__:
return self.__dict__[attr]
raise AttributeError(f"{self.__class__.__name__} object has no attribute {attr}")

def __iter__(self) -> Iterator:
Expand Down
22 changes: 11 additions & 11 deletions tests/models/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
from meilisearch.models.document import Document


def test_doc_init():
d = {"field1": "test 1", "field2": "test 2"}
document = Document(d)
assert dict(document) == d


def test_getattr():
document = Document({"field1": "test 1", "fiels2": "test 2"})
assert document.__getattr__("field1") == "field1"
document = Document({"field1": "test 1", "field2": "test 2"})
assert document.__getattr__("field1") == "test 1"


def test_getattr_not_found():
document = Document({"field1": "test 1", "fiels2": "test 2"})
document = Document({"field1": "test 1", "field2": "test 2"})
with pytest.raises(AttributeError):
document.__getattr__("bad")


def test_iter():
# I wrote a test what what this does, but I have a feeling this isn't actually what it was
# expected to do when written as it doesn't really act like I would expect an iterator to act.
document = Document({"field1": "test 1", "fiels2": "test 2"})

assert next(document.__iter__()) == (
"_Document__doc",
{"field1": "test 1", "fiels2": "test 2"},
)
document = Document({"field1": "test 1", "field2": "test 2"})
assert list(iter(document)) == [("field1", "test 1"), ("field2", "test 2")]
Loading