2828from pymongo .compression_support import ZlibContext
2929from pymongo .errors import DocumentTooLarge , OperationFailure
3030from pymongo .message import (
31- MAX_INT32 ,
32- MIN_INT32 ,
3331 _compress ,
3432 _convert_client_bulk_exception ,
3533 _convert_exception ,
3634 _convert_write_result ,
3735 _gen_find_command ,
3836 _gen_get_more_command ,
39- _get_more ,
4037 _get_more_compressed ,
4138 _get_more_uncompressed ,
4239 _maybe_add_read_preference ,
4340 _op_msg ,
44- _query ,
4541 _query_compressed ,
4642 _query_impl ,
4743 _query_uncompressed ,
4844 _raise_document_too_large ,
49- _randint ,
5045)
5146from pymongo .read_concern import ReadConcern
52- from pymongo .read_preferences import ReadPreference , Secondary , SecondaryPreferred
47+ from pymongo .read_preferences import ReadPreference , SecondaryPreferred
5348
5449_OPTS = CodecOptions ()
5550_ZLIB_CTX = ZlibContext (- 1 )
5651
5752
58- class TestRandint (unittest .TestCase ):
59- def test_in_range (self ):
60- for _ in range (50 ):
61- r = _randint ()
62- self .assertGreaterEqual (r , MIN_INT32 )
63- self .assertLessEqual (r , MAX_INT32 )
64-
65-
6653class TestMaybeAddReadPreference (unittest .TestCase ):
6754 def test_primary_no_read_preference_added (self ):
6855 spec : dict = {"find" : "col" }
@@ -77,12 +64,6 @@ def test_secondary_adds_read_preference(self):
7764 self .assertEqual (result ["$readPreference" ]["mode" ], "secondary" )
7865 self .assertIn ("$query" , result )
7966
80- def test_nearest_adds_read_preference (self ):
81- spec : dict = {"find" : "col" }
82- result = _maybe_add_read_preference (spec , ReadPreference .NEAREST )
83- self .assertIn ("$readPreference" , result )
84- self .assertEqual (result ["$readPreference" ]["mode" ], "nearest" )
85-
8667 def test_secondary_preferred_no_tags_does_not_add (self ):
8768 spec : dict = {"find" : "col" }
8869 result = _maybe_add_read_preference (spec , ReadPreference .SECONDARY_PREFERRED )
@@ -108,11 +89,6 @@ def test_basic_exception(self):
10889 self .assertEqual (doc ["errmsg" ], "bad value" )
10990 self .assertEqual (doc ["errtype" ], "ValueError" )
11091
111- def test_runtime_error (self ):
112- exc = RuntimeError ("oops" )
113- doc = _convert_exception (exc )
114- self .assertEqual (doc ["errtype" ], "RuntimeError" )
115-
11692 def test_client_bulk_exception_includes_code (self ):
11793 exc = OperationFailure ("failed" , code = 11000 )
11894 doc = _convert_client_bulk_exception (exc )
@@ -174,6 +150,7 @@ def test_write_concern_timeout(self):
174150 self .assertEqual (result ["writeConcernError" ]["code" ], 64 )
175151
176152 def test_write_error_with_err_info (self ):
153+ # Covers the `if "errInfo" in result:` branch, which test_write_error does not enter.
177154 cmd = {"documents" : [{"_id" : 1 }]}
178155 gle = {"n" : 0 , "err" : "err" , "code" : 123 , "errInfo" : {"detail" : "x" }}
179156 result = _convert_write_result ("insert" , cmd , gle )
@@ -188,11 +165,6 @@ def test_compressed_message_has_op_compressed_header(self):
188165
189166
190167class TestOpMsg (unittest .TestCase ):
191- def test_basic_uncompressed (self ):
192- cmd : dict = {"ping" : 1 }
193- _op_msg (0 , cmd , "testdb" , None , _OPTS )
194- self .assertEqual (cmd ["$db" ], "testdb" )
195-
196168 def test_uncompressed_op_code (self ):
197169 _ , msg , _ , _ = _op_msg (0 , {"ping" : 1 }, "testdb" , None , _OPTS )
198170 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
@@ -212,21 +184,11 @@ def test_read_preference_added_for_non_primary(self):
212184 _op_msg (0 , cmd , "testdb" , ReadPreference .SECONDARY , _OPTS )
213185 self .assertIn ("$readPreference" , cmd )
214186
215- def test_read_preference_not_added_for_primary (self ):
216- cmd : dict = {"find" : "col" }
217- _op_msg (0 , cmd , "testdb" , ReadPreference .PRIMARY , _OPTS )
218- self .assertNotIn ("$readPreference" , cmd )
219-
220187 def test_read_preference_skipped_if_already_present (self ):
221188 cmd : dict = {"find" : "col" , "$readPreference" : {"mode" : "nearest" }}
222189 _op_msg (0 , cmd , "testdb" , ReadPreference .SECONDARY , _OPTS )
223190 self .assertEqual (cmd ["$readPreference" ]["mode" ], "nearest" )
224191
225- def test_none_read_preference_skipped (self ):
226- cmd : dict = {"find" : "col" }
227- _op_msg (0 , cmd , "testdb" , None , _OPTS )
228- self .assertNotIn ("$readPreference" , cmd )
229-
230192 def test_with_compression_context (self ):
231193 _ , msg , _ , _ = _op_msg (0 , {"ping" : 1 }, "testdb" , None , _OPTS , _ZLIB_CTX )
232194 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
@@ -255,16 +217,6 @@ def test_compressed_op_code(self):
255217 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
256218 self .assertEqual (op_code , 2012 ) # OP_COMPRESSED
257219
258- def test_uncompressed_path (self ):
259- _rid , msg , _mbs = _query (0 , "db.col" , 0 , 0 , {}, None , _OPTS )
260- op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
261- self .assertEqual (op_code , 2004 )
262-
263- def test_compressed_path (self ):
264- _rid , msg , _mbs = _query (0 , "db.col" , 0 , 0 , {}, None , _OPTS , _ZLIB_CTX )
265- op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
266- self .assertEqual (op_code , 2012 )
267-
268220
269221class TestGetMore (unittest .TestCase ):
270222 def test_uncompressed_op_code (self ):
@@ -277,16 +229,6 @@ def test_compressed_op_code(self):
277229 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
278230 self .assertEqual (op_code , 2012 ) # OP_COMPRESSED
279231
280- def test_uncompressed_path (self ):
281- _rid , msg = _get_more ("db.col" , 0 , 0 )
282- op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
283- self .assertEqual (op_code , 2005 )
284-
285- def test_compressed_path (self ):
286- _rid , msg = _get_more ("db.col" , 0 , 0 , _ZLIB_CTX )
287- op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
288- self .assertEqual (op_code , 2012 )
289-
290232
291233class TestRaiseDocumentTooLarge (unittest .TestCase ):
292234 def test_insert_includes_sizes (self ):
@@ -301,11 +243,6 @@ def test_update_generic_message(self):
301243 _raise_document_too_large ("update" , 2_000_000 , 1_000_000 )
302244 self .assertIn ("update" , str (ctx .exception ))
303245
304- def test_delete_generic_message (self ):
305- with self .assertRaises (DocumentTooLarge ) as ctx :
306- _raise_document_too_large ("delete" , 2_000_000 , 1_000_000 )
307- self .assertIn ("delete" , str (ctx .exception ))
308-
309246
310247class TestGenFindCommand (unittest .TestCase ):
311248 def test_basic (self ):
@@ -336,6 +273,7 @@ def test_batch_size_adjusted_when_equal_to_limit(self):
336273 self .assertEqual (cmd ["batchSize" ], 11 )
337274
338275 def test_batch_size_not_adjusted_when_different (self ):
276+ # Covers the False branch of `if limit == batch_size:` — distinct from the True branch above.
339277 cmd = _gen_find_command ("col" , {}, None , 0 , 10 , 5 , None , ReadConcern ())
340278 self .assertEqual (cmd ["batchSize" ], 5 )
341279
@@ -372,6 +310,7 @@ def test_dollar_query_with_explain_removed(self):
372310 self .assertNotIn ("$explain" , cmd )
373311
374312 def test_dollar_query_with_read_preference_removed (self ):
313+ # Covers the separate `if "$readPreference" in cmd:` branch — not entered by test_dollar_query_with_explain_removed.
375314 spec = {"$query" : {"x" : 1 }, "$readPreference" : {"mode" : "secondary" }}
376315 cmd = _gen_find_command ("col" , spec , None , 0 , 0 , None , None , ReadConcern ())
377316 self .assertNotIn ("$readPreference" , cmd )
0 commit comments