File: test_raw_bson.py

package info (click to toggle)
pymongo 4.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,692 kB
  • sloc: python: 107,407; ansic: 4,601; javascript: 137; makefile: 30; sh: 10
file content (219 lines) | stat: -rw-r--r-- 8,653 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Copyright 2015-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

import datetime
import sys
import uuid

sys.path[0:0] = [""]

from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest

from bson import Code, DBRef, decode, encode
from bson.binary import JAVA_LEGACY, Binary, UuidRepresentation
from bson.codec_options import CodecOptions
from bson.errors import InvalidBSON
from bson.raw_bson import DEFAULT_RAW_BSON_OPTIONS, RawBSONDocument
from bson.son import SON

_IS_SYNC = False


class TestRawBSONDocument(AsyncIntegrationTest):
    # {'_id': ObjectId('556df68b6e32ab21a95e0785'),
    #  'name': 'Sherlock',
    #  'addresses': [{'street': 'Baker Street'}]}
    bson_string = (
        b"Z\x00\x00\x00\x07_id\x00Um\xf6\x8bn2\xab!\xa9^\x07\x85\x02name\x00\t"
        b"\x00\x00\x00Sherlock\x00\x04addresses\x00&\x00\x00\x00\x030\x00\x1e"
        b"\x00\x00\x00\x02street\x00\r\x00\x00\x00Baker Street\x00\x00\x00\x00"
    )
    document = RawBSONDocument(bson_string)

    async def asyncTearDown(self):
        if async_client_context.connected:
            await self.client.pymongo_test.test_raw.drop()

    def test_decode(self):
        self.assertEqual("Sherlock", self.document["name"])
        first_address = self.document["addresses"][0]
        self.assertIsInstance(first_address, RawBSONDocument)
        self.assertEqual("Baker Street", first_address["street"])

    def test_raw(self):
        self.assertEqual(self.bson_string, self.document.raw)

    def test_empty_doc(self):
        doc = RawBSONDocument(encode({}))
        with self.assertRaises(KeyError):
            doc["does-not-exist"]

    def test_invalid_bson_sequence(self):
        bson_byte_sequence = encode({"a": 1}) + encode({})
        with self.assertRaisesRegex(InvalidBSON, "invalid object length"):
            RawBSONDocument(bson_byte_sequence)

    def test_invalid_bson_eoo(self):
        invalid_bson_eoo = encode({"a": 1})[:-1] + b"\x01"
        with self.assertRaisesRegex(InvalidBSON, "bad eoo"):
            RawBSONDocument(invalid_bson_eoo)

    @async_client_context.require_connection
    async def test_round_trip(self):
        db = self.client.get_database(
            "pymongo_test", codec_options=CodecOptions(document_class=RawBSONDocument)
        )
        await db.test_raw.insert_one(self.document)
        result = await db.test_raw.find_one(self.document["_id"])
        assert result is not None
        self.assertIsInstance(result, RawBSONDocument)
        self.assertEqual(dict(self.document.items()), dict(result.items()))

    @async_client_context.require_connection
    async def test_round_trip_raw_uuid(self):
        coll = self.client.get_database("pymongo_test").test_raw
        uid = uuid.uuid4()
        doc = {"_id": 1, "bin4": Binary(uid.bytes, 4), "bin3": Binary(uid.bytes, 3)}
        raw = RawBSONDocument(encode(doc))
        await coll.insert_one(raw)
        self.assertEqual(await coll.find_one(), doc)
        uuid_coll = coll.with_options(
            codec_options=coll.codec_options.with_options(
                uuid_representation=UuidRepresentation.STANDARD
            )
        )
        self.assertEqual(
            await uuid_coll.find_one(), {"_id": 1, "bin4": uid, "bin3": Binary(uid.bytes, 3)}
        )

        # Test that the raw bytes haven't changed.
        raw_coll = coll.with_options(codec_options=DEFAULT_RAW_BSON_OPTIONS)
        self.assertEqual(await raw_coll.find_one(), raw)

    def test_with_codec_options(self):
        # {'date': datetime.datetime(2015, 6, 3, 18, 40, 50, 826000),
        #  '_id': UUID('026fab8f-975f-4965-9fbf-85ad874c60ff')}
        # encoded with JAVA_LEGACY uuid representation.
        bson_string = (
            b"-\x00\x00\x00\x05_id\x00\x10\x00\x00\x00\x03eI_\x97\x8f\xabo\x02"
            b"\xff`L\x87\xad\x85\xbf\x9f\tdate\x00\x8a\xd6\xb9\xbaM"
            b"\x01\x00\x00\x00"
        )
        document = RawBSONDocument(
            bson_string,
            codec_options=CodecOptions(
                uuid_representation=JAVA_LEGACY, document_class=RawBSONDocument
            ),
        )

        self.assertEqual(uuid.UUID("026fab8f-975f-4965-9fbf-85ad874c60ff"), document["_id"])

    @async_client_context.require_connection
    async def test_round_trip_codec_options(self):
        doc = {
            "date": datetime.datetime(2015, 6, 3, 18, 40, 50, 826000),
            "_id": uuid.UUID("026fab8f-975f-4965-9fbf-85ad874c60ff"),
        }
        db = self.client.pymongo_test
        coll = db.get_collection(
            "test_raw", codec_options=CodecOptions(uuid_representation=JAVA_LEGACY)
        )
        await coll.insert_one(doc)
        raw_java_legacy = CodecOptions(
            uuid_representation=JAVA_LEGACY, document_class=RawBSONDocument
        )
        coll = db.get_collection("test_raw", codec_options=raw_java_legacy)
        self.assertEqual(
            RawBSONDocument(encode(doc, codec_options=raw_java_legacy)), await coll.find_one()
        )

    @async_client_context.require_connection
    async def test_raw_bson_document_embedded(self):
        doc = {"embedded": self.document}
        db = self.client.pymongo_test
        await db.test_raw.insert_one(doc)
        result = await db.test_raw.find_one()
        assert result is not None
        self.assertEqual(decode(self.document.raw), result["embedded"])

        # Make sure that CodecOptions are preserved.
        # {'embedded': [
        #   {'date': datetime.datetime(2015, 6, 3, 18, 40, 50, 826000),
        #    '_id': UUID('026fab8f-975f-4965-9fbf-85ad874c60ff')}
        # ]}
        # encoded with JAVA_LEGACY uuid representation.
        bson_string = (
            b"D\x00\x00\x00\x04embedded\x005\x00\x00\x00\x030\x00-\x00\x00\x00"
            b"\tdate\x00\x8a\xd6\xb9\xbaM\x01\x00\x00\x05_id\x00\x10\x00\x00"
            b"\x00\x03eI_\x97\x8f\xabo\x02\xff`L\x87\xad\x85\xbf\x9f\x00\x00"
            b"\x00"
        )
        rbd = RawBSONDocument(
            bson_string,
            codec_options=CodecOptions(
                uuid_representation=JAVA_LEGACY, document_class=RawBSONDocument
            ),
        )

        await db.test_raw.drop()
        await db.test_raw.insert_one(rbd)
        result = await db.get_collection(
            "test_raw", codec_options=CodecOptions(uuid_representation=JAVA_LEGACY)
        ).find_one()
        assert result is not None
        self.assertEqual(rbd["embedded"][0]["_id"], result["embedded"][0]["_id"])

    @async_client_context.require_connection
    async def test_write_response_raw_bson(self):
        coll = self.client.get_database(
            "pymongo_test", codec_options=CodecOptions(document_class=RawBSONDocument)
        ).test_raw

        # No Exceptions raised while handling write response.
        await coll.insert_one(self.document)
        await coll.delete_one(self.document)
        await coll.insert_many([self.document])
        await coll.delete_many(self.document)
        await coll.update_one(self.document, {"$set": {"a": "b"}}, upsert=True)
        await coll.update_many(self.document, {"$set": {"b": "c"}})

    def test_preserve_key_ordering(self):
        keyvaluepairs = [
            ("a", 1),
            ("b", 2),
            ("c", 3),
        ]
        rawdoc = RawBSONDocument(encode(SON(keyvaluepairs)))

        for rkey, elt in zip(rawdoc, keyvaluepairs):
            self.assertEqual(rkey, elt[0])

    def test_contains_code_with_scope(self):
        doc = RawBSONDocument(encode({"value": Code("x=1", scope={})}))

        self.assertEqual(decode(encode(doc)), {"value": Code("x=1", {})})
        self.assertEqual(doc["value"].scope, RawBSONDocument(encode({})))

    def test_contains_dbref(self):
        doc = RawBSONDocument(encode({"value": DBRef("test", "id")}))
        raw = {"$ref": "test", "$id": "id"}
        raw_encoded = encode(decode(encode(raw)))

        self.assertEqual(decode(encode(doc)), {"value": DBRef("test", "id")})
        self.assertEqual(doc["value"].raw, raw_encoded)


if __name__ == "__main__":
    unittest.main()