Skip to content

Commit 077fb20

Browse files
rdbShaneHarvey
authored andcommitted
Allow fields to be a set (mongodb#347)
Add test cases for set and tuple projection arguments.
1 parent 1a784e1 commit 077fb20

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

pymongo/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def _fields_list_to_dict(fields, option_name):
235235
if isinstance(fields, collections.Mapping):
236236
return fields
237237

238-
if isinstance(fields, collections.Sequence):
238+
if isinstance(fields, (collections.Sequence, collections.Set)):
239239
if not all(isinstance(field, string_type) for field in fields):
240240
raise TypeError("%s must be a list of key names, each an "
241241
"instance of %s" % (option_name,

test/test_collection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,16 @@ def test_find_one(self):
17431743

17441744
self.assertTrue("hello" in db.test.find_one(projection=["hello"]))
17451745
self.assertTrue("hello" not in db.test.find_one(projection=["foo"]))
1746+
1747+
self.assertTrue("hello" in db.test.find_one(projection=("hello",)))
1748+
self.assertTrue("hello" not in db.test.find_one(projection=("foo",)))
1749+
1750+
self.assertTrue("hello" in db.test.find_one(projection=set(["hello"])))
1751+
self.assertTrue("hello" not in db.test.find_one(projection=set(["foo"])))
1752+
1753+
self.assertTrue("hello" in db.test.find_one(projection=frozenset(["hello"])))
1754+
self.assertTrue("hello" not in db.test.find_one(projection=frozenset(["foo"])))
1755+
17461756
self.assertEqual(["_id"], list(db.test.find_one(projection=[])))
17471757

17481758
self.assertEqual(None, db.test.find_one({"hello": "foo"}))

0 commit comments

Comments
 (0)