Skip to content

Commit

Permalink
Implement Cursor.to_list() for more consistency with AsyncCursor
Browse files Browse the repository at this point in the history
  • Loading branch information
ramnes committed Dec 1, 2022
1 parent e2be300 commit eba0dc3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mongo_thingy/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def __getitem__(self, index):
document = self.delegate.__getitem__(index)
return self.bind(document)

def to_list(self, length):
self.limit(length)
return list(self)

def delete(self):
ids = self.distinct("_id")
return self.thingy_cls.collection.delete_many({"_id": {"$in": ids}})
Expand Down
15 changes: 15 additions & 0 deletions tests/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ class Foo(thingy_cls):
assert result.bar == "qux"


def test_cursor_to_list(thingy_cls, collection):
class Foo(thingy_cls):
_collection = collection

collection.insert_many([{"bar": "baz"}, {"bar": "qux"}])
cursor = Cursor(collection.find(), thingy_cls=Foo)

results = cursor.to_list(length=10)
assert isinstance(results[0], Foo)
assert results[0].bar == "baz"

assert isinstance(results[1], Foo)
assert results[1].bar == "qux"


async def test_async_cursor_to_list(thingy_cls, collection):
class Foo(thingy_cls):
_collection = collection
Expand Down

0 comments on commit eba0dc3

Please sign in to comment.