Skip to content

Commit 3b29458

Browse files
committed
PYTHON-1821 Preserve field ordering when iterating over RawBSONDocument
instances
1 parent 2f2fe9d commit 3b29458

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Changes in Version 3.8.0.dev0
101101
- :class:`~bson.raw_bson.RawBSONDocument` now validates that the ``bson_bytes``
102102
passed in represent a single bson document. Earlier versions would mistakenly
103103
accept multiple bson documents.
104+
- Iterating over a :class:`~bson.raw_bson.RawBSONDocument` now maintains the
105+
same field order of the underlying raw BSON document.
104106

105107
Issues Resolved
106108
...............

test/test_raw_bson.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from bson.codec_options import CodecOptions
2121
from bson.errors import InvalidBSON
2222
from bson.raw_bson import RawBSONDocument
23+
from bson.son import SON
2324
from test import client_context, unittest
2425

2526

@@ -157,3 +158,10 @@ def test_write_response_raw_bson(self):
157158
coll.delete_many(self.document)
158159
coll.update_one(self.document, {'$set': {'a': 'b'}}, upsert=True)
159160
coll.update_many(self.document, {'$set': {'b': 'c'}})
161+
162+
def test_preserve_key_ordering(self):
163+
keyvaluepairs = [('a', 1), ('b', 2), ('c', 3),]
164+
rawdoc = RawBSONDocument(BSON.encode(SON(keyvaluepairs)))
165+
166+
for rkey, elt in zip(rawdoc, keyvaluepairs):
167+
self.assertEqual(rkey, elt[0])

0 commit comments

Comments
 (0)