Skip to content

Commit

Permalink
Merge pull request #196 from IlyaSkriblovsky/find-args
Browse files Browse the repository at this point in the history
New signature for find() method and compatibility with old one
  • Loading branch information
psi29a committed Oct 17, 2016
2 parents a9226a2 + 630f4cb commit d813c66
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 65 deletions.
17 changes: 17 additions & 0 deletions docs/source/NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ Features
API Changes
^^^^^^^^^^^

- ``find()``, ``find_one()``, ``find_with_cursor()``, ``count()`` and ``distinct()`` signatures
changed to more closely match PyMongo's counterparts. New signatures are:

- ``find(filter=None, projection=None, skip=0, limit=0, sort=None, **kwargs)``
- ``find_with_cursor(filter=None, projection=None, skip=0, limit=0, sort=None, **kwargs)``
- ``find_one(filter=None, projection=None, **kwargs)``
- ``count(filter=None, **kwargs)``
- ``distinct(key, filter=None, **kwargs)``

Old signatures are now deprecated and will be supported in this and one subsequent releases.
After that only new signatures will be valid.
- ``cursor`` argument to ``find()`` is deprecated. Please use ``find_with_cursor()`` directly
if you need to iterate over results by batches. ``cursor`` will be supported in this and
one subsequent releases.
- ``as_class`` argument to ``find()``, ``find_with_cursor()`` and ``find_one()`` is deprecated.
Please use ``collection.with_options(codec_options=CodecOptions(document_class=...)).find()`
instead. It is lengthty, but it is more generic and this is how you do it with current PyMongo.
- ``Database.command()`` now takes ``codec_options`` argument.
- ``watchdog_interval`` and ``watchdog_timeout`` arguments of ``ConnectionPool`` renamed
to ``ping_interval`` and ``ping_timeout`` correspondingly along with internal change of
Expand Down
90 changes: 90 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,93 @@ def test_Fail(self):
else:
self.fail()
test_Fail.timeout = 10


class TestFindSignatureCompat(unittest.TestCase):
def test_convert(self):
self.assertEqual(
Collection._find_args_compat(spec={'x': 42}),
{"filter": {'x': 42}, "projection": None, "skip": 0, "limit": 0, "sort": None,
"cursor": False}
)
self.assertEqual(
Collection._find_args_compat(filter={'x': 42}),
{"filter": {'x': 42}, "projection": None, "skip": 0, "limit": 0, "sort": None}
)
self.assertEqual(
Collection._find_args_compat(filter=qf.sort(qf.ASCENDING('x'))),
{"filter": None, "projection": None, "skip": 0, "limit": 0,
"sort": qf.sort(qf.ASCENDING('x')), "cursor": False}
)
self.assertEqual(
Collection._find_args_compat(sort=qf.sort(qf.ASCENDING('x'))),
{"filter": None, "projection": None, "skip": 0, "limit": 0,
"sort": qf.sort(qf.ASCENDING('x'))}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}),
{"filter": {'x': 42}, "projection": None, "skip": 0, "limit": 0, "sort": None}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, unknown_arg=123),
{"filter": {'x': 42}, "projection": None, "skip": 0, "limit": 0, "sort": None,
"unknown_arg": 123}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, {'a': 1}),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 0, "limit": 0, "sort": None}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, projection={'a': 1}),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 0, "limit": 0, "sort": None}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, fields={'a': 1}),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 0, "limit": 0, "sort": None,
"cursor": False}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, 5),
{"filter": {'x': 42}, "projection": None, "skip": 5, "limit": 0, "sort": None,
"cursor": False}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, {'a': 1}, 5),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 0, "sort": None}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, {'a': 1}, 5, 6),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6, "sort": None}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, 5, 6, {'a': 1}),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6, "sort": None,
"cursor": False}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, {'a': 1}, 5, 6, qf.sort([('s', 1)])),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6,
"sort": qf.sort([('s', 1)])}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, 5, 6, {'a': 1}, qf.sort([('s', 1)])),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6,
"sort": qf.sort([('s', 1)]), "cursor": False}
)
self.assertEqual(
Collection._find_args_compat({'x': 42}, 5, 6, {'a': 1}, qf.sort([('s', 1)]), True),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6,
"sort": qf.sort([('s', 1)]), "cursor": True}
)
self.assertEqual(
Collection._find_args_compat(spec={'x': 42}, filter=qf.sort([('s', 1)]), limit=6,
fields={'a': 1}, skip=5),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6,
"sort": qf.sort([('s', 1)]), "cursor": False}
)
self.assertEqual(
Collection._find_args_compat(filter={'x': 42}, sort=qf.sort([('s', 1)]), limit=6,
projection={'a': 1}, skip=5),
{"filter": {'x': 42}, "projection": {'a': 1}, "skip": 5, "limit": 6,
"sort": qf.sort([('s', 1)])}
)
28 changes: 28 additions & 0 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,38 @@ def setUp(self):
yield super(TestCount, self).setUp()
yield self.coll.insert_many([{'x': 10}, {'x': 20}, {'x': 30}])

@defer.inlineCallbacks
def tearDown(self):
yield self.db.system.profile.drop()
yield super(TestCount, self).tearDown()

@defer.inlineCallbacks
def test_count(self):
self.assertEqual((yield self.coll.count()), 3)
self.assertEqual((yield self.coll.count({'x': 20})), 1)
self.assertEqual((yield self.coll.count({'x': {"$gt": 15}})), 2)

self.assertEqual((yield self.db.non_existing.count()), 0)

@defer.inlineCallbacks
def test_hint(self):
yield self.coll.create_index(qf.sort(qf.ASCENDING('x')))

yield self.db.command("profile", 2)
cnt = yield self.coll.count(hint=qf.hint(qf.ASCENDING('x')))
self.assertEqual(cnt, 3)
yield self.db.command("profile", 0)

cmds = yield self.db.system.profile.count({"command.hint": {"x": 1}})
self.assertEqual(cmds, 1)

self.assertRaises(TypeError, self.coll.count, hint={'x': 1})
self.assertRaises(TypeError, self.coll.count, hint=[('x', 1)])

@defer.inlineCallbacks
def test_skip_limit(self):
cnt = yield self.coll.count(limit = 2)
self.assertEqual(cnt, 2)

cnt = yield self.coll.count(skip = 1)
self.assertEqual(cnt, 2)

0 comments on commit d813c66

Please sign in to comment.