MCPcopy Create free account
hub / github.com/mongodb/mongo-python-driver / test_encoding_3

Method test_encoding_3

test/test_dbref.py:209–232  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

207 self.assertEqual(getattr(dbref, extra), doc[extra])
208
209 def test_encoding_3(self):
210 for doc in [
211 # 3, Encoding DBRefs re-orders any out of order fields during
212 # decoding:
213 {"$id": 1, "$ref": "coll0"},
214 {"$db": "db0", "$ref": "coll0", "$id": 1},
215 {"foo": 1, "$id": 1, "$ref": "coll0"},
216 {"foo": 1, "$ref": "coll0", "$id": 1, "$db": "db0"},
217 {"foo": 1, "$ref": "coll0", "$id": 1, "$db": "db0", "bar": 1},
218 ]:
219 with self.subTest(doc=doc):
220 # Decode the test input to a DBRef via a BSON roundtrip.
221 encoded_doc = encode({"dbref": doc})
222 decoded = decode(encoded_doc)
223 dbref = decoded["dbref"]
224 self.assertIsInstance(dbref, DBRef)
225 # Encode the DBRef.
226 encoded_dbref = encode(decoded)
227 # BSON does not match because DBRef fields are reordered.
228 self.assertNotEqual(encoded_dbref, encoded_doc)
229 self.assertEqual(decode(encoded_dbref), decode(encoded_doc))
230 # Ensure extra fields are present.
231 for extra in set(doc.keys()) - {"$ref", "$id", "$db"}:
232 self.assertEqual(getattr(dbref, extra), doc[extra])
233
234
235if __name__ == "__main__":

Callers

nothing calls this directly

Calls 3

encodeFunction · 0.90
decodeFunction · 0.90
keysMethod · 0.80

Tested by

no test coverage detected