|
| 1 | +# Copyright 2015-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import datetime |
| 17 | +import sys |
| 18 | +import uuid |
| 19 | + |
| 20 | +sys.path[0:0] = [""] |
| 21 | + |
| 22 | +from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest |
| 23 | + |
| 24 | +from bson import Code, DBRef, decode, encode |
| 25 | +from bson.binary import JAVA_LEGACY, Binary, UuidRepresentation |
| 26 | +from bson.codec_options import CodecOptions |
| 27 | +from bson.errors import InvalidBSON |
| 28 | +from bson.raw_bson import DEFAULT_RAW_BSON_OPTIONS, RawBSONDocument |
| 29 | +from bson.son import SON |
| 30 | + |
| 31 | +_IS_SYNC = False |
| 32 | + |
| 33 | + |
| 34 | +class TestRawBSONDocument(AsyncIntegrationTest): |
| 35 | + # {'_id': ObjectId('556df68b6e32ab21a95e0785'), |
| 36 | + # 'name': 'Sherlock', |
| 37 | + # 'addresses': [{'street': 'Baker Street'}]} |
| 38 | + bson_string = ( |
| 39 | + b"Z\x00\x00\x00\x07_id\x00Um\xf6\x8bn2\xab!\xa9^\x07\x85\x02name\x00\t" |
| 40 | + b"\x00\x00\x00Sherlock\x00\x04addresses\x00&\x00\x00\x00\x030\x00\x1e" |
| 41 | + b"\x00\x00\x00\x02street\x00\r\x00\x00\x00Baker Street\x00\x00\x00\x00" |
| 42 | + ) |
| 43 | + document = RawBSONDocument(bson_string) |
| 44 | + |
| 45 | + async def asyncTearDown(self): |
| 46 | + if async_client_context.connected: |
| 47 | + await self.client.pymongo_test.test_raw.drop() |
| 48 | + |
| 49 | + def test_decode(self): |
| 50 | + self.assertEqual("Sherlock", self.document["name"]) |
| 51 | + first_address = self.document["addresses"][0] |
| 52 | + self.assertIsInstance(first_address, RawBSONDocument) |
| 53 | + self.assertEqual("Baker Street", first_address["street"]) |
| 54 | + |
| 55 | + def test_raw(self): |
| 56 | + self.assertEqual(self.bson_string, self.document.raw) |
| 57 | + |
| 58 | + def test_empty_doc(self): |
| 59 | + doc = RawBSONDocument(encode({})) |
| 60 | + with self.assertRaises(KeyError): |
| 61 | + doc["does-not-exist"] |
| 62 | + |
| 63 | + def test_invalid_bson_sequence(self): |
| 64 | + bson_byte_sequence = encode({"a": 1}) + encode({}) |
| 65 | + with self.assertRaisesRegex(InvalidBSON, "invalid object length"): |
| 66 | + RawBSONDocument(bson_byte_sequence) |
| 67 | + |
| 68 | + def test_invalid_bson_eoo(self): |
| 69 | + invalid_bson_eoo = encode({"a": 1})[:-1] + b"\x01" |
| 70 | + with self.assertRaisesRegex(InvalidBSON, "bad eoo"): |
| 71 | + RawBSONDocument(invalid_bson_eoo) |
| 72 | + |
| 73 | + @async_client_context.require_connection |
| 74 | + async def test_round_trip(self): |
| 75 | + db = self.client.get_database( |
| 76 | + "pymongo_test", codec_options=CodecOptions(document_class=RawBSONDocument) |
| 77 | + ) |
| 78 | + await db.test_raw.insert_one(self.document) |
| 79 | + result = await db.test_raw.find_one(self.document["_id"]) |
| 80 | + assert result is not None |
| 81 | + self.assertIsInstance(result, RawBSONDocument) |
| 82 | + self.assertEqual(dict(self.document.items()), dict(result.items())) |
| 83 | + |
| 84 | + @async_client_context.require_connection |
| 85 | + async def test_round_trip_raw_uuid(self): |
| 86 | + coll = self.client.get_database("pymongo_test").test_raw |
| 87 | + uid = uuid.uuid4() |
| 88 | + doc = {"_id": 1, "bin4": Binary(uid.bytes, 4), "bin3": Binary(uid.bytes, 3)} |
| 89 | + raw = RawBSONDocument(encode(doc)) |
| 90 | + await coll.insert_one(raw) |
| 91 | + self.assertEqual(await coll.find_one(), doc) |
| 92 | + uuid_coll = coll.with_options( |
| 93 | + codec_options=coll.codec_options.with_options( |
| 94 | + uuid_representation=UuidRepresentation.STANDARD |
| 95 | + ) |
| 96 | + ) |
| 97 | + self.assertEqual( |
| 98 | + await uuid_coll.find_one(), {"_id": 1, "bin4": uid, "bin3": Binary(uid.bytes, 3)} |
| 99 | + ) |
| 100 | + |
| 101 | + # Test that the raw bytes haven't changed. |
| 102 | + raw_coll = coll.with_options(codec_options=DEFAULT_RAW_BSON_OPTIONS) |
| 103 | + self.assertEqual(await raw_coll.find_one(), raw) |
| 104 | + |
| 105 | + def test_with_codec_options(self): |
| 106 | + # {'date': datetime.datetime(2015, 6, 3, 18, 40, 50, 826000), |
| 107 | + # '_id': UUID('026fab8f-975f-4965-9fbf-85ad874c60ff')} |
| 108 | + # encoded with JAVA_LEGACY uuid representation. |
| 109 | + bson_string = ( |
| 110 | + b"-\x00\x00\x00\x05_id\x00\x10\x00\x00\x00\x03eI_\x97\x8f\xabo\x02" |
| 111 | + b"\xff`L\x87\xad\x85\xbf\x9f\tdate\x00\x8a\xd6\xb9\xbaM" |
| 112 | + b"\x01\x00\x00\x00" |
| 113 | + ) |
| 114 | + document = RawBSONDocument( |
| 115 | + bson_string, |
| 116 | + codec_options=CodecOptions( |
| 117 | + uuid_representation=JAVA_LEGACY, document_class=RawBSONDocument |
| 118 | + ), |
| 119 | + ) |
| 120 | + |
| 121 | + self.assertEqual(uuid.UUID("026fab8f-975f-4965-9fbf-85ad874c60ff"), document["_id"]) |
| 122 | + |
| 123 | + @async_client_context.require_connection |
| 124 | + async def test_round_trip_codec_options(self): |
| 125 | + doc = { |
| 126 | + "date": datetime.datetime(2015, 6, 3, 18, 40, 50, 826000), |
| 127 | + "_id": uuid.UUID("026fab8f-975f-4965-9fbf-85ad874c60ff"), |
| 128 | + } |
| 129 | + db = self.client.pymongo_test |
| 130 | + coll = db.get_collection( |
| 131 | + "test_raw", codec_options=CodecOptions(uuid_representation=JAVA_LEGACY) |
| 132 | + ) |
| 133 | + await coll.insert_one(doc) |
| 134 | + raw_java_legacy = CodecOptions( |
| 135 | + uuid_representation=JAVA_LEGACY, document_class=RawBSONDocument |
| 136 | + ) |
| 137 | + coll = db.get_collection("test_raw", codec_options=raw_java_legacy) |
| 138 | + self.assertEqual( |
| 139 | + RawBSONDocument(encode(doc, codec_options=raw_java_legacy)), await coll.find_one() |
| 140 | + ) |
| 141 | + |
| 142 | + @async_client_context.require_connection |
| 143 | + async def test_raw_bson_document_embedded(self): |
| 144 | + doc = {"embedded": self.document} |
| 145 | + db = self.client.pymongo_test |
| 146 | + await db.test_raw.insert_one(doc) |
| 147 | + result = await db.test_raw.find_one() |
| 148 | + assert result is not None |
| 149 | + self.assertEqual(decode(self.document.raw), result["embedded"]) |
| 150 | + |
| 151 | + # Make sure that CodecOptions are preserved. |
| 152 | + # {'embedded': [ |
| 153 | + # {'date': datetime.datetime(2015, 6, 3, 18, 40, 50, 826000), |
| 154 | + # '_id': UUID('026fab8f-975f-4965-9fbf-85ad874c60ff')} |
| 155 | + # ]} |
| 156 | + # encoded with JAVA_LEGACY uuid representation. |
| 157 | + bson_string = ( |
| 158 | + b"D\x00\x00\x00\x04embedded\x005\x00\x00\x00\x030\x00-\x00\x00\x00" |
| 159 | + b"\tdate\x00\x8a\xd6\xb9\xbaM\x01\x00\x00\x05_id\x00\x10\x00\x00" |
| 160 | + b"\x00\x03eI_\x97\x8f\xabo\x02\xff`L\x87\xad\x85\xbf\x9f\x00\x00" |
| 161 | + b"\x00" |
| 162 | + ) |
| 163 | + rbd = RawBSONDocument( |
| 164 | + bson_string, |
| 165 | + codec_options=CodecOptions( |
| 166 | + uuid_representation=JAVA_LEGACY, document_class=RawBSONDocument |
| 167 | + ), |
| 168 | + ) |
| 169 | + |
| 170 | + await db.test_raw.drop() |
| 171 | + await db.test_raw.insert_one(rbd) |
| 172 | + result = await db.get_collection( |
| 173 | + "test_raw", codec_options=CodecOptions(uuid_representation=JAVA_LEGACY) |
| 174 | + ).find_one() |
| 175 | + assert result is not None |
| 176 | + self.assertEqual(rbd["embedded"][0]["_id"], result["embedded"][0]["_id"]) |
| 177 | + |
| 178 | + @async_client_context.require_connection |
| 179 | + async def test_write_response_raw_bson(self): |
| 180 | + coll = self.client.get_database( |
| 181 | + "pymongo_test", codec_options=CodecOptions(document_class=RawBSONDocument) |
| 182 | + ).test_raw |
| 183 | + |
| 184 | + # No Exceptions raised while handling write response. |
| 185 | + await coll.insert_one(self.document) |
| 186 | + await coll.delete_one(self.document) |
| 187 | + await coll.insert_many([self.document]) |
| 188 | + await coll.delete_many(self.document) |
| 189 | + await coll.update_one(self.document, {"$set": {"a": "b"}}, upsert=True) |
| 190 | + await coll.update_many(self.document, {"$set": {"b": "c"}}) |
| 191 | + |
| 192 | + def test_preserve_key_ordering(self): |
| 193 | + keyvaluepairs = [ |
| 194 | + ("a", 1), |
| 195 | + ("b", 2), |
| 196 | + ("c", 3), |
| 197 | + ] |
| 198 | + rawdoc = RawBSONDocument(encode(SON(keyvaluepairs))) |
| 199 | + |
| 200 | + for rkey, elt in zip(rawdoc, keyvaluepairs): |
| 201 | + self.assertEqual(rkey, elt[0]) |
| 202 | + |
| 203 | + def test_contains_code_with_scope(self): |
| 204 | + doc = RawBSONDocument(encode({"value": Code("x=1", scope={})})) |
| 205 | + |
| 206 | + self.assertEqual(decode(encode(doc)), {"value": Code("x=1", {})}) |
| 207 | + self.assertEqual(doc["value"].scope, RawBSONDocument(encode({}))) |
| 208 | + |
| 209 | + def test_contains_dbref(self): |
| 210 | + doc = RawBSONDocument(encode({"value": DBRef("test", "id")})) |
| 211 | + raw = {"$ref": "test", "$id": "id"} |
| 212 | + raw_encoded = encode(decode(encode(raw))) |
| 213 | + |
| 214 | + self.assertEqual(decode(encode(doc)), {"value": DBRef("test", "id")}) |
| 215 | + self.assertEqual(doc["value"].raw, raw_encoded) |
| 216 | + |
| 217 | + |
| 218 | +if __name__ == "__main__": |
| 219 | + unittest.main() |
0 commit comments