Allow fields to be a set (#347)

Add test cases for set and tuple projection arguments.
This commit is contained in:
rdb 2018-02-12 20:33:36 +01:00 committed by Shane Harvey
parent 1a784e1afb
commit 077fb2041a
2 changed files with 11 additions and 1 deletions

View File

@ -235,7 +235,7 @@ def _fields_list_to_dict(fields, option_name):
if isinstance(fields, collections.Mapping):
return fields
if isinstance(fields, collections.Sequence):
if isinstance(fields, (collections.Sequence, collections.Set)):
if not all(isinstance(field, string_type) for field in fields):
raise TypeError("%s must be a list of key names, each an "
"instance of %s" % (option_name,

View File

@ -1743,6 +1743,16 @@ class TestCollection(IntegrationTest):
self.assertTrue("hello" in db.test.find_one(projection=["hello"]))
self.assertTrue("hello" not in db.test.find_one(projection=["foo"]))
self.assertTrue("hello" in db.test.find_one(projection=("hello",)))
self.assertTrue("hello" not in db.test.find_one(projection=("foo",)))
self.assertTrue("hello" in db.test.find_one(projection=set(["hello"])))
self.assertTrue("hello" not in db.test.find_one(projection=set(["foo"])))
self.assertTrue("hello" in db.test.find_one(projection=frozenset(["hello"])))
self.assertTrue("hello" not in db.test.find_one(projection=frozenset(["foo"])))
self.assertEqual(["_id"], list(db.test.find_one(projection=[])))
self.assertEqual(None, db.test.find_one({"hello": "foo"}))