Skip to content

Commit 87eaf79

Browse files
committed
rename addToSet -> add_to_set
1 parent 11f9b58 commit 87eaf79

File tree

5 files changed

+39
-40
lines changed

5 files changed

+39
-40
lines changed

docs/index.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ $addToSet
9696

9797
MongoDB ``$addToSet`` update modifier is very useful. nanomongo implements it.
9898

99-
:meth:`~.document.BaseDocument.addToSet()` will do the `add-to-field-if-doesnt-exist`
99+
:meth:`~.document.BaseDocument.add_to_set()` will do the `add-to-field-if-doesnt-exist`
100100
on your document instance and record the change to be applied later when
101101
:meth:`~.document.BaseDocument.save()` is called.
102102

@@ -114,11 +114,11 @@ on your document instance and record the change to be applied later when
114114
doc = NewDoc.find_one({'_id': doc_id})
115115
# {'_id': ObjectId('...'), 'dict_field': {'foo': []}, 'list_field': [42]}
116116

117-
doc.addToSet('list_field', 1337)
118-
doc.addToSet('dict_field.foo', 'like a boss')
117+
doc.add_to_set('list_field', 1337)
118+
doc.add_to_set('dict_field.foo', 'like a boss')
119119
doc.save()
120120

121-
Both of the above ``addToSet`` are applied to the ``NewDoc`` instance like MongoDB does it eg.
121+
Both of the above ``add_to_set`` calls are applied to the ``NewDoc`` instance like MongoDB does it eg.
122122

123123
- create list field with new value if it doesn't exist
124124
- add new value to list field if it's missing (append)
@@ -133,7 +133,7 @@ When save is called, the following is called::
133133

134134
Undefined fields or field type mismatch raises :class:`~.errors.ValidationError`::
135135

136-
doc.addToSet('dict_field.foo', 'like a boss')
136+
doc.add_to_set('dict_field.foo', 'like a boss')
137137
ValidationError: Cannot apply $addToSet modifier to non-array: dict_field=<class 'dict'>
138138

139139
QuerySpec check
@@ -189,7 +189,7 @@ register the document class with a ``motor.MotorClient``.
189189
# and now some async motor queries (using @gen.engine)
190190
doc_id = yield motor.Op(MyDoc(foo=42).insert)
191191
doc = yield motor.Op(MyDoc.find_one, {'foo': 42})
192-
doc.addToSet('bar', 1337)
192+
doc.add_to_set('bar', 1337)
193193
yield motor.Op(doc.save)
194194

195195
**Note** however that pymongo vs motor behaviour is not necessarily identical.

examples/example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def follow(self, *categories):
4545
assert categories, 'categories expected'
4646
for category in categories:
4747
assert (category and isinstance(category, six.text_type)), 'category not str or emtpy'
48-
self.addToSet('following', category)
48+
self.add_to_set('following', category)
4949
self.save()
5050

5151
def get_entries(self, **kwargs):
@@ -100,7 +100,7 @@ def add_comment(self, text, author):
100100
assert (author and isinstance(author, User)), 'second argument not an instance of User'
101101
doc = {'text': text, 'author': author.name, 'created': datetime.utcnow()}
102102
# TODO: push is more appropriate in this situation, add when implemented
103-
self.addToSet('comments', doc)
103+
self.add_to_set('comments', doc)
104104
# we could have also done self.comments = self.comments + [doc]
105105
self.save()
106106
return text

nanomongo/document.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def save(self, **kwargs):
431431
self.reset_diff()
432432
return update_result
433433

434-
def addToSet(self, field, value):
434+
def add_to_set(self, field, value):
435435
"""
436436
Explicitly defined ``$addToSet`` functionality. This sets/updates the field value accordingly
437437
and records the change to be saved with :meth:`~save()`.
@@ -440,15 +440,14 @@ def addToSet(self, field, value):
440440
# MongoDB style dot notation can be used to add to lists
441441
# in embedded documents
442442
doc = Doc(foo=[], bar={})
443-
doc.addToSet('foo', new_value)
443+
doc.add_to_set('foo', new_value)
444444
445445
Contrary to how ``$set`` ing the same value has no effect under __setitem__ (see
446-
``.util.RecordingDict.__setitem__()``), when the new value is equal to the current;
447-
``addToSet()`` explicitly records the change so it will be sent to the
448-
database when :meth:`~save()` is called.
446+
``.util.RecordingDict.__setitem__()``) when the new value is equal to the current, this
447+
explicitly records the change so it will be sent to the database when :meth:`~save()` is called.
449448
"""
450-
# TODO: rename to add_to_set
451-
# TODO: doc.addToSet('bar.sub_field', new_value) doesn't actually work
449+
450+
# TODO: doc.add_to_set('bar.sub_field', new_value) doesn't actually work
452451
def top_level_add(self, field, value):
453452
"""add the value to field. appending if the list exists and
454453
does not contain the value; create new list otherwise.

nanomongo/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def get_sub_diff(self):
151151
def check_can_update(self, modifier, field_name):
152152
"""Check if given `modifier` `field_name` combination can be
153153
added. MongoDB does not allow field duplication with update
154-
modifiers. This is to be used with methods :meth:`~.document.BaseDocument.addToSet()` ...
154+
modifiers. This is to be used with methods :meth:`~.document.BaseDocument.add_to_set()` ...
155155
"""
156156
for mod, updates in self.__nanodiff__.items():
157157
if mod == modifier:

test/test_document.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ class Doc(BaseDocument):
294294
self.assertRaises(ValidationError, d.save)
295295

296296
@unittest.skipUnless(PYMONGO_CLIENT, 'pymongo not installed or connection refused')
297-
def test_partial_update_addToSet(self):
298-
"""Pymongo: addToSet functionality with `save`"""
297+
def test_partial_update_add_to_set(self):
298+
"""Pymongo: $addToSet functionality with `save`"""
299299

300300
class Doc(BaseDocument):
301301
dot_notation = True
@@ -304,28 +304,28 @@ class Doc(BaseDocument):
304304
moo = Field(six.text_type)
305305
Doc.register(client=PYMONGO_CLIENT, db=TEST_DBNAME)
306306

307-
self.assertRaises(ValidationError, Doc().addToSet, *('$fail', 42))
308-
self.assertRaises(ValidationError, Doc().addToSet, *('bar.$1', 42))
307+
self.assertRaises(ValidationError, Doc().add_to_set, *('$fail', 42))
308+
self.assertRaises(ValidationError, Doc().add_to_set, *('bar.$1', 42))
309309
# use dict.__setitem__ to bypass RecordingDict cast at self.__setitem__
310310
d = Doc()
311311
dict.__setitem__(d, 'bar', {})
312312
self.assertEqual(dict, type(d.bar)) # not RecordingDict
313-
self.assertRaises(ValidationError, d.addToSet, *('bar.1', 42))
313+
self.assertRaises(ValidationError, d.add_to_set, *('bar.1', 42))
314314
d = Doc(foo=['foo_1', 'foo_2'], bar={'1': 'bar_1', '2': []}, moo=six.u('moo val'))
315315
d.insert()
316-
self.assertRaises(ValidationError, d.addToSet, *('moo', 42))
317-
self.assertRaises(ValidationError, d.addToSet, *('moo.not_dict', 42))
318-
self.assertRaises(ValidationError, d.addToSet, *('undefined.field', 42))
319-
self.assertRaises(UnsupportedOperation, d.addToSet, *('bar.a.b', 42))
320-
d.addToSet('foo', 'foo_1')
316+
self.assertRaises(ValidationError, d.add_to_set, *('moo', 42))
317+
self.assertRaises(ValidationError, d.add_to_set, *('moo.not_dict', 42))
318+
self.assertRaises(ValidationError, d.add_to_set, *('undefined.field', 42))
319+
self.assertRaises(UnsupportedOperation, d.add_to_set, *('bar.a.b', 42))
320+
d.add_to_set('foo', 'foo_1')
321321
d.moo = six.u('new moo')
322-
d.addToSet('foo', 'foo_3')
323-
d.addToSet('foo', 'foo_1')
324-
d.addToSet('bar.2', 'L33t')
325-
d.addToSet('bar.3', 'new_1')
326-
d.addToSet('bar.3', 'new_1')
327-
d.addToSet('bar.3', 'new_2')
328-
self.assertRaises(ValidationError, d.addToSet, *('bar.1', 1))
322+
d.add_to_set('foo', 'foo_3')
323+
d.add_to_set('foo', 'foo_1')
324+
d.add_to_set('bar.2', 'L33t')
325+
d.add_to_set('bar.3', 'new_1')
326+
d.add_to_set('bar.3', 'new_1')
327+
d.add_to_set('bar.3', 'new_2')
328+
self.assertRaises(ValidationError, d.add_to_set, *('bar.1', 1))
329329
topdiff = {'$set': {'moo': 'new moo'}, '$unset': {},
330330
'$addToSet': {'foo': {'$each': ['foo_1', 'foo_3']}}}
331331
subdiff = {'$set': {}, '$unset': {},
@@ -337,20 +337,20 @@ class Doc(BaseDocument):
337337
d.save()
338338
d_db = Doc.find_one()
339339
self.assertTrue(d_copy == d == d_db)
340-
# check against field duplication at addToSet
340+
# check against field duplication at $addToSet
341341
d = Doc()
342342
d.foo = [42] # set -- top-level $addToSet will clash
343343
self.assertEqual([42], d.__nanodiff__['$set']['foo'])
344-
self.assertRaises(ValidationError, d.addToSet, *('foo', 42))
344+
self.assertRaises(ValidationError, d.add_to_set, *('foo', 42))
345345
del d.foo # unset -- top-level $addToSet will clash
346-
self.assertRaises(ValidationError, d.addToSet, *('foo', 42))
346+
self.assertRaises(ValidationError, d.add_to_set, *('foo', 42))
347347
d = Doc(bar={})
348348
d.bar['1'] = [42] # deep-level set -- dotted $addToSet will clash
349-
self.assertRaises(ValidationError, d.addToSet, *('bar.1', 42))
349+
self.assertRaises(ValidationError, d.add_to_set, *('bar.1', 42))
350350
d = Doc()
351351
d.bar = {'1': [42]} # dict set on top-level -- dotted $addToSet will clash
352352
self.assertEqual({'bar': {'1': [42]}}, d.__nanodiff__['$set'])
353-
self.assertRaises(ValidationError, d.addToSet, *('bar.1', 42))
353+
self.assertRaises(ValidationError, d.add_to_set, *('bar.1', 42))
354354

355355
class Doc2(BaseDocument):
356356
dot_notation = True
@@ -359,8 +359,8 @@ class Doc2(BaseDocument):
359359
Doc2.register(client=PYMONGO_CLIENT, db=TEST_DBNAME)
360360
dd = Doc2()
361361
dd.insert()
362-
# addToSet on unset field
363-
dd.addToSet('optional.sub', 42)
362+
# $addToSet on unset field
363+
dd.add_to_set('optional.sub', 42)
364364
self.assertEqual([42], dd.optional['sub'])
365365
self.assertEqual({'sub': {'$each': [42]}}, dd.optional.__nanodiff__['$addToSet'])
366366
dd.save()

0 commit comments

Comments
 (0)