Commit c82676b6 authored by Yury's avatar Yury

document: add to_json convert method

parent cfe23d5f
Pipeline #340 failed with stages
in 2 minutes and 33 seconds
......@@ -270,10 +270,10 @@ class Document(metaclass=MetaDocument):
@classmethod
@check_setup
async def many(cls, **kwargs) -> list:
async def many(cls, **kwargs) -> DocumentArray:
"""Finds multiple documents based on kwargs."""
result_list = []
result_list = DocumentArray()
cursor = cls.collection.find(kwargs)
async for doc in cursor:
result_list.append(cls(**doc))
......@@ -344,3 +344,10 @@ class Document(metaclass=MetaDocument):
return result
return func_wrapper
def to_json(self):
_id = self._id
result = {"_id": str(self._document_.__data__.pop("_id"))}
result.update(self._document_.__data__)
self._document_["_id"] = _id
return result
......@@ -352,3 +352,18 @@ async def test_create_indexes(client):
}
indexes = await Video.collection.index_information()
assert indexes.get("user_1_value_1") == user_1_value_1
@pytest.mark.asyncio
async def test_to_json(client):
Video = mock_Document("Video", client)
video = await Video.create(name="movie", awards=3, bytes=b"123")
assert video.to_json() == {
"_id": str(video._id),
"name": "movie",
"awards": 3,
"bytes": b"123",
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment