Skip to content

Commit

Permalink
Add "getitem" and "contains" to Request.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdavis committed Mar 18, 2015
1 parent 19f3ee2 commit e6ef7e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ PyMongo sends a ``writeConcern`` argument if you specify ``w=1``:
>>> client = MongoClient(server.uri)
>>> collection = client.db.coll
>>> future = go(collection.insert_one, {'_id': 5})
>>> assert 'writeConcern' not in server.receives().doc
>>> assert 'writeConcern' not in server.receives()
>>> client.close()

Test Cursor Behavior
Expand Down
32 changes: 31 additions & 1 deletion mockupdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,27 @@ def get(self, block=True, timeout=None):


class Request(object):
"""Base class for `Command`, `OpInsert`, and so on."""
"""Base class for `Command`, `OpInsert`, and so on.
Some useful asserts you can do in tests:
>>> {'_id': 0} in OpInsert({'_id': 0})
True
>>> {'_id': 1} in OpInsert({'_id': 0})
False
>>> {'_id': 1} in OpInsert([{'_id': 0}, {'_id': 1}])
True
>>> {'_id': 1} == OpInsert([{'_id': 0}, {'_id': 1}])[1]
True
>>> 'field' in Command(field=1)
True
>>> 'field' in Command()
False
>>> 'field' in Command('ismaster')
False
>>> Command(ismaster=False)['ismaster'] is False
True
"""
opcode = None
is_command = None

Expand Down Expand Up @@ -423,6 +443,16 @@ def _replies(self, *args, **kwargs):
reply_bytes = reply_msg.reply_bytes(self)
self._client.sendall(reply_bytes)

def __contains__(self, item):
if item in self.docs:
return True
if len(self.docs) == 1 and isinstance(item, (string_type, text_type)):
return item in self.doc
return False

def __getitem__(self, item):
return self.doc[item] if len(self.docs) == 1 else self.docs[item]

def __str__(self):
return docs_repr(*self.docs)

Expand Down

0 comments on commit e6ef7e2

Please sign in to comment.