PYTHON-2518 SON class should be compatible with Python 3's OrderedDict API (#730)

This commit is contained in:
Julius Park 2021-09-16 15:21:40 -07:00 committed by GitHub
parent 23fe13fcba
commit fbd5599deb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -62,9 +62,6 @@ class SON(dict):
self.__keys.remove(key)
dict.__delitem__(self, key)
def keys(self):
return list(self.__keys)
def copy(self):
other = SON()
other.update(self)

View File

@ -23,7 +23,7 @@ sys.path[0:0] = [""]
from bson.son import SON
from test import unittest
from collections import OrderedDict
class TestSON(unittest.TestCase):
def test_ordered_dict(self):
@ -189,5 +189,21 @@ class TestSON(unittest.TestCase):
test_son.popitem()
self.assertEqual(2, len(test_son))
def test_keys(self):
# Test to make sure that set operations do not throw an error
d = SON().keys()
for i in [OrderedDict, dict]:
try:
d - i().keys()
except TypeError:
self.fail("SON().keys() is not returning an object compatible "
"with %s objects" % (str(i)))
# Test to verify correctness
d = SON({"k": "v"}).keys()
for i in [OrderedDict, dict]:
self.assertEqual(d | i({"k1": 0}).keys(), {"k", "k1"})
for i in [OrderedDict, dict]:
self.assertEqual(d - i({"k": 0}).keys(), set())
if __name__ == "__main__":
unittest.main()