2424from test import unittest
2525
2626from bson import CodecOptions
27+ from pymongo .compression_support import ZlibContext
2728from pymongo .errors import DocumentTooLarge
2829from pymongo .message import (
2930 MAX_INT32 ,
5657# ---------------------------------------------------------------------------
5758
5859_OPTS = CodecOptions ()
59-
60-
61- class _MockCtx :
62- """Minimal compression context for testing _compress."""
63-
64- compressor_id = 2 # zlib id
65-
66- def compress (self , data : bytes ) -> bytes :
67- import zlib
68-
69- return zlib .compress (data )
60+ _ZLIB_CTX = ZlibContext (- 1 ) # default zlib compression level
7061
7162
7263class _MockConn :
@@ -242,14 +233,14 @@ def test_write_error_with_err_info(self):
242233
243234class TestCompress (unittest .TestCase ):
244235 def test_returns_request_id_and_bytes (self ):
245- ctx = _MockCtx ()
236+ ctx = _ZLIB_CTX
246237 data = b"hello world"
247238 request_id , msg = _compress (2013 , data , ctx )
248239 self .assertIsInstance (request_id , int )
249240 self .assertIsInstance (msg , bytes )
250241
251242 def test_compressed_message_has_op_compressed_header (self ):
252- ctx = _MockCtx ()
243+ ctx = _ZLIB_CTX
253244 data = b"hello world"
254245 _request_id , msg = _compress (2013 , data , ctx )
255246 # Header: 4 bytes msglen, 4 bytes requestId, 4 bytes responseTo, 4 bytes opCode
@@ -272,7 +263,7 @@ def test_basic_command_no_docs(self):
272263
273264 def test_command_with_docs (self ):
274265 cmd = {"insert" : "col" }
275- docs = [{"_id" : 1 , "x" : 2 }, {"_id" : 3 , "x" : 4 }]
266+ docs : list = [{"_id" : 1 , "x" : 2 }, {"_id" : 3 , "x" : 4 }]
276267 data , total_size , max_doc_size = _op_msg_no_header (0 , cmd , "documents" , docs , _OPTS )
277268 self .assertIsInstance (data , bytes )
278269 self .assertGreater (max_doc_size , 0 )
@@ -331,7 +322,7 @@ def test_none_read_preference_skipped(self):
331322 self .assertNotIn ("$readPreference" , cmd )
332323
333324 def test_with_compression_context (self ):
334- ctx = _MockCtx ()
325+ ctx = _ZLIB_CTX
335326 cmd : dict = {"ping" : 1 }
336327 rid , msg , total_size , max_doc_size = _op_msg (0 , cmd , "testdb" , None , _OPTS , ctx )
337328 self .assertIsInstance (rid , int )
@@ -378,7 +369,7 @@ def test_op_code(self):
378369
379370class TestQueryCompressed (unittest .TestCase ):
380371 def test_returns_compressed (self ):
381- ctx = _MockCtx ()
372+ ctx = _ZLIB_CTX
382373 rid , msg , max_bson_size = _query_compressed (0 , "db.col" , 0 , 0 , {}, None , _OPTS , ctx )
383374 self .assertIsInstance (rid , int )
384375 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
@@ -392,7 +383,7 @@ def test_uncompressed_path(self):
392383 self .assertEqual (op_code , 2004 )
393384
394385 def test_compressed_path (self ):
395- ctx = _MockCtx ()
386+ ctx = _ZLIB_CTX
396387 rid , msg , max_bson_size = _query (0 , "db.col" , 0 , 0 , {}, None , _OPTS , ctx )
397388 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
398389 self .assertEqual (op_code , 2012 )
@@ -423,7 +414,7 @@ def test_op_code(self):
423414
424415class TestGetMoreCompressed (unittest .TestCase ):
425416 def test_returns_compressed (self ):
426- ctx = _MockCtx ()
417+ ctx = _ZLIB_CTX
427418 rid , msg = _get_more_compressed ("db.col" , 0 , 0 , ctx )
428419 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
429420 self .assertEqual (op_code , 2012 )
@@ -436,7 +427,7 @@ def test_uncompressed_path(self):
436427 self .assertEqual (op_code , 2005 )
437428
438429 def test_compressed_path (self ):
439- ctx = _MockCtx ()
430+ ctx = _ZLIB_CTX
440431 rid , msg = _get_more ("db.col" , 0 , 0 , ctx )
441432 op_code = struct .unpack ("<i" , msg [12 :16 ])[0 ]
442433 self .assertEqual (op_code , 2012 )
@@ -555,30 +546,30 @@ def test_dollar_query_with_read_preference_removed(self):
555546class TestGenGetMoreCommand (unittest .TestCase ):
556547 def test_basic (self ):
557548 conn = _MockConn ()
558- cmd = _gen_get_more_command (12345 , "col" , None , None , None , conn )
549+ cmd = _gen_get_more_command (12345 , "col" , None , None , None , conn ) # type: ignore[arg-type]
559550 self .assertEqual (cmd ["getMore" ], 12345 )
560551 self .assertEqual (cmd ["collection" ], "col" )
561552
562553 def test_with_batch_size (self ):
563554 conn = _MockConn ()
564- cmd = _gen_get_more_command (1 , "col" , 100 , None , None , conn )
555+ cmd = _gen_get_more_command (1 , "col" , 100 , None , None , conn ) # type: ignore[arg-type]
565556 self .assertEqual (cmd ["batchSize" ], 100 )
566557
567558 def test_with_max_await_time_ms (self ):
568559 conn = _MockConn ()
569- cmd = _gen_get_more_command (1 , "col" , None , 500 , None , conn )
560+ cmd = _gen_get_more_command (1 , "col" , None , 500 , None , conn ) # type: ignore[arg-type]
570561 self .assertEqual (cmd ["maxTimeMS" ], 500 )
571562
572563 def test_comment_added_on_high_wire_version (self ):
573564 conn = _MockConn ()
574565 conn .max_wire_version = 9
575- cmd = _gen_get_more_command (1 , "col" , None , None , "my comment" , conn )
566+ cmd = _gen_get_more_command (1 , "col" , None , None , "my comment" , conn ) # type: ignore[arg-type]
576567 self .assertEqual (cmd ["comment" ], "my comment" )
577568
578569 def test_comment_not_added_on_low_wire_version (self ):
579570 conn = _MockConn ()
580571 conn .max_wire_version = 8
581- cmd = _gen_get_more_command (1 , "col" , None , None , "my comment" , conn )
572+ cmd = _gen_get_more_command (1 , "col" , None , None , "my comment" , conn ) # type: ignore[arg-type]
582573 self .assertNotIn ("comment" , cmd )
583574
584575
0 commit comments