Skip to content

Commit

Permalink
Merge b82fb0b into 31ece6d
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Apr 26, 2020
2 parents 31ece6d + b82fb0b commit 7e65722
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions tests/frameworks/test_txmongo.py
Expand Up @@ -180,7 +180,7 @@ def test_find_with_cursor(self, classroom_model):
Student.collection.drop()
for i in range(10):
yield Student(name='student-%s' % i).commit()
batch1, cursor1 = yield Student.find(limit=5, skip=6, cursor=True)
batch1, cursor1 = yield Student.find_with_cursor(limit=5, skip=6)
assert len(batch1) == 4
batch2, cursor2 = yield cursor1
assert len(batch2) == 0
Expand All @@ -191,7 +191,7 @@ def test_find_with_cursor(self, classroom_model):
names.append(elem.name)
# Filter + projection
assert sorted(names) == ['student-%s' % i for i in range(6, 10)]
batch1, cursor1 = yield Student.find({'name': 'student-0'}, ['name'], cursor=True)
batch1, cursor1 = yield Student.find_with_cursor({'name': 'student-0'}, ['name'])
assert len(batch1) == 1
assert batch1[0].name == 'student-0'

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -10,7 +10,7 @@ deps =
motor1: motor>=1.0,<2.0
motor2: motor>=2.0,<3.0
pymongo: mongomock>=3.5.0
txmongo: txmongo>=16.0.1
txmongo: txmongo>=19.2.O
txmongo: pytest-twisted>=1.12
commands =
py.test --cov=umongo tests {posargs}
Expand Down
51 changes: 29 additions & 22 deletions umongo/frameworks/txmongo.py
Expand Up @@ -152,47 +152,54 @@ def io_validate(self, validate_all=False):

@classmethod
@inlineCallbacks
def find_one(cls, spec=None, *args, **kwargs):
def find_one(cls, filter=None, *args, **kwargs):
"""
Find a single document in database.
"""
# In txmongo, `spec` is for filtering and `filter` is for sorting
spec = cook_find_filter(cls, spec)
ret = yield cls.collection.find_one(spec, *args, **kwargs)
filter = cook_find_filter(cls, filter)
ret = yield cls.collection.find_one(filter, *args, **kwargs)
if ret is not None:
ret = cls.build_from_mongo(ret, use_cls=True)
return ret

@classmethod
@inlineCallbacks
def find(cls, spec=None, *args, **kwargs):
def find(cls, filter=None, *args, **kwargs):
"""
Find a list document in database.
Returns a cursor that provide Documents.
Returns a list of Documents.
"""
# In txmongo, `spec` is for filtering and `filter` is for sorting
spec = cook_find_filter(cls, spec)
raw_cursor_or_list = yield cls.collection.find(spec, *args, **kwargs)
if isinstance(raw_cursor_or_list, tuple):

def wrap_raw_results(result):
cursor = result[1]
if cursor is not None:
cursor.addCallback(wrap_raw_results)
return ([cls.build_from_mongo(e, use_cls=True) for e in result[0]], cursor)

return wrap_raw_results(raw_cursor_or_list)
filter = cook_find_filter(cls, filter)
raw_cursor_or_list = yield cls.collection.find(filter, *args, **kwargs)
return [cls.build_from_mongo(e, use_cls=True) for e in raw_cursor_or_list]

@classmethod
def count(cls, spec=None, **kwargs):
@inlineCallbacks
def find_with_cursor(cls, filter=None, *args, **kwargs):
"""
Find a list document in database.
Returns a cursor that provides Documents.
"""
filter = cook_find_filter(cls, filter)
raw_cursor_or_list = yield cls.collection.find_with_cursor(filter, *args, **kwargs)

def wrap_raw_results(result):
cursor = result[1]
if cursor is not None:
cursor.addCallback(wrap_raw_results)
return ([cls.build_from_mongo(e, use_cls=True) for e in result[0]], cursor)

return wrap_raw_results(raw_cursor_or_list)

@classmethod
def count(cls, filter=None, **kwargs):
"""
Get the number of documents in this collection.
"""
# In txmongo, `spec` is for filtering and `filter` is for sorting
spec = cook_find_filter(cls, spec)
return cls.collection.count(spec=spec, **kwargs)
filter = cook_find_filter(cls, filter)
return cls.collection.count(filter=filter, **kwargs)

@classmethod
@inlineCallbacks
Expand Down

0 comments on commit 7e65722

Please sign in to comment.