2424
2525from test import unittest
2626
27- from bson import CodecOptions
27+ from bson import CodecOptions , encode
2828from pymongo .compression_support import ZlibContext
2929from pymongo .errors import DocumentTooLarge , OperationFailure
3030from pymongo .message import (
@@ -98,6 +98,11 @@ def test_client_bulk_exception_includes_code(self):
9898
9999
100100class TestConvertWriteResult (unittest .TestCase ):
101+ """Tests for _convert_write_result.
102+
103+ In the update command spec, `q` is the query/filter and `u` is the update document.
104+ """
105+
101106 def test_insert_basic (self ):
102107 cmd = {"documents" : [{"_id" : 1 }, {"_id" : 2 }]}
103108 result = _convert_write_result ("insert" , cmd , {"n" : 0 })
@@ -116,12 +121,12 @@ def test_update_with_upserted_id(self):
116121 self .assertIn ("upserted" , result )
117122 self .assertEqual (result ["upserted" ][0 ]["_id" ], 42 )
118123
119- def test_update_implicit_upsert_from_updatedExisting_false (self ):
120- cmd = { "updates" : [{ "q" : { "_id" : 99 }, "u" : { "$set" : { "x" : 1 }}}]}
121- result = _convert_write_result (
122- "update" , cmd , { "n " : 1 , "updatedExisting " : False , "upserted " : None }
123- )
124- self .assertIn ( "upserted" , result )
124+ def test_update_upsert_id_precedence (self ):
125+ # When _id is in both the update document and the query spec,
126+ # the update document's _id wins.
127+ cmd = { "updates" : [{ "q" : { "_id " : 99 } , "u " : { "_id " : 42 }}] }
128+ result = _convert_write_result ( "update" , cmd , { "n" : 1 , "updatedExisting" : False } )
129+ self .assertEqual ( result [ "upserted" ][ 0 ][ "_id" ], 42 )
125130
126131 def test_update_upsert_no_upserted_id_from_query (self ):
127132 cmd = {"updates" : [{"q" : {"_id" : 77 }, "u" : {"$set" : {"x" : 1 }}}]}
@@ -174,10 +179,11 @@ def test_max_doc_size_zero_without_docs(self):
174179 _ , _ , _ , max_doc_size = _op_msg (0 , {"ping" : 1 }, "testdb" , None , _OPTS )
175180 self .assertEqual (max_doc_size , 0 )
176181
177- def test_max_doc_size_nonzero_with_docs (self ):
178- cmd : dict = {"insert" : "col" , "documents" : [{"_id" : 1 , "x" : 2 }, {"_id" : 3 , "x" : 4 }]}
182+ def test_max_doc_size_matches_largest_encoded_doc (self ):
183+ docs = [{"_id" : 1 , "x" : 2 }, {"_id" : 3 , "x" : 4 }]
184+ cmd : dict = {"insert" : "col" , "documents" : docs }
179185 _ , _ , _ , max_doc_size = _op_msg (0 , cmd , "testdb" , None , _OPTS )
180- self .assertGreater (max_doc_size , 0 )
186+ self .assertEqual (max_doc_size , max ( len ( encode ( d )) for d in docs ) )
181187
182188 def test_read_preference_added_for_non_primary (self ):
183189 cmd : dict = {"find" : "col" }
@@ -203,10 +209,6 @@ def test_command_with_documents_field_is_restored(self):
203209
204210
205211class TestQuery (unittest .TestCase ):
206- def test_basic (self ):
207- _ , max_bson_size = _query_impl (0 , "db.col" , 0 , 0 , {"x" : 1 }, None , _OPTS )
208- self .assertGreater (max_bson_size , 0 )
209-
210212 def test_uncompressed_op_code (self ):
211213 _rid , msg , _mbs = _query_uncompressed (0 , "db.col" , 0 , 0 , {}, None , _OPTS )
212214 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
@@ -252,7 +254,7 @@ def test_basic(self):
252254
253255 def test_with_projection (self ):
254256 cmd = _gen_find_command ("col" , {}, {"x" : 1 }, 0 , 0 , None , None , ReadConcern ())
255- self .assertIn ( "projection" , cmd )
257+ self .assertEqual ( cmd [ "projection" ], { "x" : 1 } )
256258
257259 def test_with_skip (self ):
258260 cmd = _gen_find_command ("col" , {}, None , 5 , 0 , None , None , ReadConcern ())
@@ -279,7 +281,7 @@ def test_batch_size_not_adjusted_when_different(self):
279281
280282 def test_read_concern_level_included (self ):
281283 cmd = _gen_find_command ("col" , {}, None , 0 , 0 , None , None , ReadConcern ("majority" ))
282- self .assertIn ( "readConcern" , cmd )
284+ self .assertEqual ( cmd [ "readConcern" ], { "level" : "majority" } )
283285
284286 def test_query_with_dollar_query_modifier (self ):
285287 spec = {"$query" : {"x" : 1 }, "$orderby" : {"x" : 1 }}
0 commit comments