-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5779 Increase code coverage for compression_support.py #2770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
eb3a30d
5cec526
a9fb744
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,256 @@ | ||||||||||||
| # Copyright 2026-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. | ||||||||||||
|
|
||||||||||||
| """Unit tests for compression_support.py.""" | ||||||||||||
|
|
||||||||||||
| from __future__ import annotations | ||||||||||||
|
|
||||||||||||
| import sys | ||||||||||||
| from unittest.mock import patch | ||||||||||||
|
|
||||||||||||
| sys.path[0:0] = [""] | ||||||||||||
|
|
||||||||||||
| from test import unittest | ||||||||||||
|
|
||||||||||||
| from pymongo.compression_support import ( | ||||||||||||
| CompressionSettings, | ||||||||||||
| SnappyContext, | ||||||||||||
| ZlibContext, | ||||||||||||
| ZstdContext, | ||||||||||||
| _have_snappy, | ||||||||||||
| _have_zlib, | ||||||||||||
| _have_zstd, | ||||||||||||
| decompress, | ||||||||||||
| validate_compressors, | ||||||||||||
| validate_zlib_compression_level, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class TestHaveSnappy(unittest.TestCase): | ||||||||||||
| def test_returns_true_when_available(self): | ||||||||||||
| try: | ||||||||||||
| import snappy | ||||||||||||
| except ImportError: | ||||||||||||
| self.skipTest("python-snappy not installed") | ||||||||||||
| self.assertTrue(_have_snappy()) | ||||||||||||
|
|
||||||||||||
| def test_returns_false_on_import_error(self): | ||||||||||||
| with patch.dict(sys.modules, {"snappy": None}): | ||||||||||||
| self.assertFalse(_have_snappy()) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class TestHaveZlib(unittest.TestCase): | ||||||||||||
| def test_returns_true_when_available(self): | ||||||||||||
| self.assertTrue(_have_zlib()) | ||||||||||||
|
|
||||||||||||
| def test_returns_false_on_import_error(self): | ||||||||||||
| with patch.dict(sys.modules, {"zlib": None}): | ||||||||||||
| self.assertFalse(_have_zlib()) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class TestHaveZstd(unittest.TestCase): | ||||||||||||
| def test_returns_bool(self): | ||||||||||||
| result = _have_zstd() | ||||||||||||
| self.assertIsInstance(result, bool) | ||||||||||||
|
|
||||||||||||
| def test_returns_false_when_unavailable_pre_314(self): | ||||||||||||
| if sys.version_info >= (3, 14): | ||||||||||||
| self.skipTest("Python 3.14+ uses compression.zstd") | ||||||||||||
| with patch.dict(sys.modules, {"backports": None, "backports.zstd": None}): | ||||||||||||
| self.assertFalse(_have_zstd()) | ||||||||||||
|
|
||||||||||||
| def test_returns_false_when_unavailable_314_plus(self): | ||||||||||||
| if sys.version_info < (3, 14): | ||||||||||||
| self.skipTest("Only applies to Python 3.14+") | ||||||||||||
| with patch.dict(sys.modules, {"compression": None, "compression.zstd": None}): | ||||||||||||
| self.assertFalse(_have_zstd()) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class TestValidateCompressors(unittest.TestCase): | ||||||||||||
| def test_string_input_single(self): | ||||||||||||
| result = validate_compressors(None, "zlib") | ||||||||||||
| self.assertEqual(result, ["zlib"]) | ||||||||||||
|
|
||||||||||||
| def test_string_input_comma_separated(self): | ||||||||||||
| result = validate_compressors(None, "zlib,snappy") | ||||||||||||
| self.assertIn("zlib", result) | ||||||||||||
|
|
||||||||||||
| def test_iterable_input(self): | ||||||||||||
| result = validate_compressors(None, ["zlib"]) | ||||||||||||
| self.assertEqual(result, ["zlib"]) | ||||||||||||
|
|
||||||||||||
| def test_unsupported_compressor_warns_and_removes(self): | ||||||||||||
| with self.assertWarns(UserWarning) as ctx: | ||||||||||||
| result = validate_compressors(None, ["bogus"]) | ||||||||||||
| self.assertEqual(result, []) | ||||||||||||
| self.assertIn("Unsupported compressor: bogus", str(ctx.warning)) | ||||||||||||
|
|
||||||||||||
| def test_snappy_unavailable_warns_and_removes(self): | ||||||||||||
| with patch("pymongo.compression_support._have_snappy", return_value=False): | ||||||||||||
| with self.assertWarns(UserWarning) as ctx: | ||||||||||||
| result = validate_compressors(None, ["snappy"]) | ||||||||||||
| self.assertEqual(result, []) | ||||||||||||
| self.assertIn("python-snappy", str(ctx.warning)) | ||||||||||||
|
|
||||||||||||
| def test_zlib_unavailable_warns_and_removes(self): | ||||||||||||
| with patch("pymongo.compression_support._have_zlib", return_value=False): | ||||||||||||
| with self.assertWarns(UserWarning) as ctx: | ||||||||||||
| result = validate_compressors(None, ["zlib"]) | ||||||||||||
| self.assertEqual(result, []) | ||||||||||||
| self.assertIn("zlib", str(ctx.warning)) | ||||||||||||
|
|
||||||||||||
| def test_zstd_unavailable_warns_and_removes_pre_314(self): | ||||||||||||
| if sys.version_info >= (3, 14): | ||||||||||||
| self.skipTest("Python 3.14+ uses different warning message") | ||||||||||||
| with patch("pymongo.compression_support._have_zstd", return_value=False): | ||||||||||||
| with self.assertWarns(UserWarning) as ctx: | ||||||||||||
| result = validate_compressors(None, ["zstd"]) | ||||||||||||
| self.assertEqual(result, []) | ||||||||||||
| self.assertIn("backports.zstd", str(ctx.warning)) | ||||||||||||
|
|
||||||||||||
| def test_zstd_unavailable_warns_and_removes_314_plus(self): | ||||||||||||
| if sys.version_info < (3, 14): | ||||||||||||
| self.skipTest("Only applies to Python 3.14+") | ||||||||||||
| with patch("pymongo.compression_support._have_zstd", return_value=False): | ||||||||||||
| with self.assertWarns(UserWarning) as ctx: | ||||||||||||
| result = validate_compressors(None, ["zstd"]) | ||||||||||||
| self.assertEqual(result, []) | ||||||||||||
| self.assertIn("compression.zstd", str(ctx.warning)) | ||||||||||||
|
|
||||||||||||
| def test_valid_zlib_always_included(self): | ||||||||||||
| result = validate_compressors(None, ["zlib"]) | ||||||||||||
| self.assertEqual(result, ["zlib"]) | ||||||||||||
|
|
||||||||||||
| def test_multiple_valid_compressors(self): | ||||||||||||
| result = validate_compressors(None, ["zlib"]) | ||||||||||||
| self.assertIn("zlib", result) | ||||||||||||
|
||||||||||||
| result = validate_compressors(None, ["zlib"]) | |
| self.assertIn("zlib", result) | |
| with patch("pymongo.compression_support._have_snappy", return_value=True): | |
| result = validate_compressors(None, ["zlib", "snappy"]) | |
| self.assertEqual(result, ["zlib", "snappy"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validate_compressors(None, "zlib,snappy")will emit aUserWarningand dropsnappywhenpython-snappyisn’t installed (which is the default test extras). This makes the test output/environment-dependent. Consider patching_have_snappyto returnTruefor this test (or assert the warning explicitly) so the test is deterministic and doesn’t rely on global warning filters.