|
| 1 | +# Copyright 2011-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 | + |
| 15 | +"""Test the pymongo common module.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import sys |
| 19 | +import uuid |
| 20 | + |
| 21 | +sys.path[0:0] = [""] |
| 22 | + |
| 23 | +from test.asynchronous import AsyncIntegrationTest, async_client_context, connected, unittest |
| 24 | + |
| 25 | +from bson.binary import PYTHON_LEGACY, STANDARD, Binary, UuidRepresentation |
| 26 | +from bson.codec_options import CodecOptions |
| 27 | +from bson.objectid import ObjectId |
| 28 | +from pymongo.errors import OperationFailure |
| 29 | +from pymongo.write_concern import WriteConcern |
| 30 | + |
| 31 | +_IS_SYNC = False |
| 32 | + |
| 33 | + |
| 34 | +class TestCommon(AsyncIntegrationTest): |
| 35 | + async def test_uuid_representation(self): |
| 36 | + coll = self.db.uuid |
| 37 | + await coll.drop() |
| 38 | + |
| 39 | + # Test property |
| 40 | + self.assertEqual(UuidRepresentation.UNSPECIFIED, coll.codec_options.uuid_representation) |
| 41 | + |
| 42 | + # Test basic query |
| 43 | + uu = uuid.uuid4() |
| 44 | + # Insert as binary subtype 3 |
| 45 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) |
| 46 | + legacy_opts = coll.codec_options |
| 47 | + await coll.insert_one({"uu": uu}) |
| 48 | + self.assertEqual(uu, (await coll.find_one({"uu": uu}))["uu"]) # type: ignore |
| 49 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=STANDARD)) |
| 50 | + self.assertEqual(STANDARD, coll.codec_options.uuid_representation) |
| 51 | + self.assertEqual(None, await coll.find_one({"uu": uu})) |
| 52 | + uul = Binary.from_uuid(uu, PYTHON_LEGACY) |
| 53 | + self.assertEqual(uul, (await coll.find_one({"uu": uul}))["uu"]) # type: ignore |
| 54 | + |
| 55 | + # Test count_documents |
| 56 | + self.assertEqual(0, await coll.count_documents({"uu": uu})) |
| 57 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) |
| 58 | + self.assertEqual(1, await coll.count_documents({"uu": uu})) |
| 59 | + |
| 60 | + # Test delete |
| 61 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=STANDARD)) |
| 62 | + await coll.delete_one({"uu": uu}) |
| 63 | + self.assertEqual(1, await coll.count_documents({})) |
| 64 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) |
| 65 | + await coll.delete_one({"uu": uu}) |
| 66 | + self.assertEqual(0, await coll.count_documents({})) |
| 67 | + |
| 68 | + # Test update_one |
| 69 | + await coll.insert_one({"_id": uu, "i": 1}) |
| 70 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=STANDARD)) |
| 71 | + await coll.update_one({"_id": uu}, {"$set": {"i": 2}}) |
| 72 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) |
| 73 | + self.assertEqual(1, (await coll.find_one({"_id": uu}))["i"]) # type: ignore |
| 74 | + await coll.update_one({"_id": uu}, {"$set": {"i": 2}}) |
| 75 | + self.assertEqual(2, (await coll.find_one({"_id": uu}))["i"]) # type: ignore |
| 76 | + |
| 77 | + # Test Cursor.distinct |
| 78 | + self.assertEqual([2], await coll.find({"_id": uu}).distinct("i")) |
| 79 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=STANDARD)) |
| 80 | + self.assertEqual([], await coll.find({"_id": uu}).distinct("i")) |
| 81 | + |
| 82 | + # Test findAndModify |
| 83 | + self.assertEqual(None, await coll.find_one_and_update({"_id": uu}, {"$set": {"i": 5}})) |
| 84 | + coll = self.db.get_collection("uuid", CodecOptions(uuid_representation=PYTHON_LEGACY)) |
| 85 | + self.assertEqual(2, (await coll.find_one_and_update({"_id": uu}, {"$set": {"i": 5}}))["i"]) |
| 86 | + self.assertEqual(5, (await coll.find_one({"_id": uu}))["i"]) # type: ignore |
| 87 | + |
| 88 | + # Test command |
| 89 | + self.assertEqual( |
| 90 | + 5, |
| 91 | + ( |
| 92 | + await self.db.command( |
| 93 | + "findAndModify", |
| 94 | + "uuid", |
| 95 | + update={"$set": {"i": 6}}, |
| 96 | + query={"_id": uu}, |
| 97 | + codec_options=legacy_opts, |
| 98 | + ) |
| 99 | + )["value"]["i"], |
| 100 | + ) |
| 101 | + self.assertEqual( |
| 102 | + 6, |
| 103 | + ( |
| 104 | + await self.db.command( |
| 105 | + "findAndModify", |
| 106 | + "uuid", |
| 107 | + update={"$set": {"i": 7}}, |
| 108 | + query={"_id": Binary.from_uuid(uu, PYTHON_LEGACY)}, |
| 109 | + ) |
| 110 | + )["value"]["i"], |
| 111 | + ) |
| 112 | + |
| 113 | + async def test_write_concern(self): |
| 114 | + c = await self.async_rs_or_single_client(connect=False) |
| 115 | + self.assertEqual(WriteConcern(), c.write_concern) |
| 116 | + |
| 117 | + c = await self.async_rs_or_single_client(connect=False, w=2, wTimeoutMS=1000) |
| 118 | + wc = WriteConcern(w=2, wtimeout=1000) |
| 119 | + self.assertEqual(wc, c.write_concern) |
| 120 | + |
| 121 | + # Can we override back to the server default? |
| 122 | + db = c.get_database("pymongo_test", write_concern=WriteConcern()) |
| 123 | + self.assertEqual(db.write_concern, WriteConcern()) |
| 124 | + |
| 125 | + db = c.pymongo_test |
| 126 | + self.assertEqual(wc, db.write_concern) |
| 127 | + coll = db.test |
| 128 | + self.assertEqual(wc, coll.write_concern) |
| 129 | + |
| 130 | + cwc = WriteConcern(j=True) |
| 131 | + coll = db.get_collection("test", write_concern=cwc) |
| 132 | + self.assertEqual(cwc, coll.write_concern) |
| 133 | + self.assertEqual(wc, db.write_concern) |
| 134 | + |
| 135 | + async def test_mongo_client(self): |
| 136 | + pair = await async_client_context.pair |
| 137 | + m = await self.async_rs_or_single_client(w=0) |
| 138 | + coll = m.pymongo_test.write_concern_test |
| 139 | + await coll.drop() |
| 140 | + doc = {"_id": ObjectId()} |
| 141 | + await coll.insert_one(doc) |
| 142 | + self.assertTrue(await coll.insert_one(doc)) |
| 143 | + coll = coll.with_options(write_concern=WriteConcern(w=1)) |
| 144 | + with self.assertRaises(OperationFailure): |
| 145 | + await coll.insert_one(doc) |
| 146 | + |
| 147 | + m = await self.async_rs_or_single_client() |
| 148 | + coll = m.pymongo_test.write_concern_test |
| 149 | + new_coll = coll.with_options(write_concern=WriteConcern(w=0)) |
| 150 | + self.assertTrue(await new_coll.insert_one(doc)) |
| 151 | + with self.assertRaises(OperationFailure): |
| 152 | + await coll.insert_one(doc) |
| 153 | + |
| 154 | + m = await self.async_rs_or_single_client( |
| 155 | + f"mongodb://{pair}/", replicaSet=async_client_context.replica_set_name |
| 156 | + ) |
| 157 | + |
| 158 | + coll = m.pymongo_test.write_concern_test |
| 159 | + with self.assertRaises(OperationFailure): |
| 160 | + await coll.insert_one(doc) |
| 161 | + m = await self.async_rs_or_single_client( |
| 162 | + f"mongodb://{pair}/?w=0", replicaSet=async_client_context.replica_set_name |
| 163 | + ) |
| 164 | + |
| 165 | + coll = m.pymongo_test.write_concern_test |
| 166 | + await coll.insert_one(doc) |
| 167 | + |
| 168 | + # Equality tests |
| 169 | + direct = await connected(await self.async_single_client(w=0)) |
| 170 | + direct2 = await connected( |
| 171 | + await self.async_single_client(f"mongodb://{pair}/?w=0", **self.credentials) |
| 172 | + ) |
| 173 | + self.assertEqual(direct, direct2) |
| 174 | + self.assertFalse(direct != direct2) |
| 175 | + |
| 176 | + async def test_validate_boolean(self): |
| 177 | + await self.db.test.update_one({}, {"$set": {"total": 1}}, upsert=True) |
| 178 | + with self.assertRaisesRegex( |
| 179 | + TypeError, "upsert must be True or False, was: upsert={'upsert': True}" |
| 180 | + ): |
| 181 | + await self.db.test.update_one({}, {"$set": {"total": 1}}, {"upsert": True}) # type: ignore |
| 182 | + |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + unittest.main() |
0 commit comments