Skip to content

Commit

Permalink
Use Matcher({'a': absent'}) to prohibit a field.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdavis committed Mar 23, 2015
1 parent 10065c8 commit cc1fd9d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions mockupdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def reraise(exctype, value, trace=None):
'Request', 'Command', 'OpQuery', 'OpGetMore', 'OpKillCursors', 'OpInsert',
'OpUpdate', 'OpDelete', 'OpReply',

'Matcher',
'Matcher', 'absent',
]


Expand Down Expand Up @@ -753,6 +753,9 @@ def __repr__(self):
return rep + ')'


absent = object()


class Matcher(object):
"""Matches a subset of `.Request` objects.
Expand Down Expand Up @@ -790,6 +793,13 @@ def matches(self, *args, **kwargs):
>>> Matcher({'a': 1}).matches({'a': 1, 'b': 1})
True
Prohibit a field:
>>> Matcher({'a': absent}).matches({'a': 1})
False
>>> Matcher({'a': absent}).matches({'b': 1})
True
Order matters if you use an OrderedDict:
>>> doc0 = OrderedDict([('a', 1), ('b', 1)])
Expand Down Expand Up @@ -896,7 +906,7 @@ def matches(self, *args, **kwargs):
for i, doc in enumerate(self._prototype.docs):
other_doc = request.docs[i]
for key, value in doc.items():
if other_doc.get(key) != value:
if other_doc.get(key, absent) != value:
return False
if isinstance(doc, (OrderedDict, bson.SON)):
if not isinstance(other_doc, (OrderedDict, bson.SON)):
Expand Down

0 comments on commit cc1fd9d

Please sign in to comment.