|
| 1 | +# Copyright 2025 Google LLC |
| 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 | +from unittest import mock |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +# Module paths used for mocking |
| 20 | +MODULE_PATH = "db_dtypes" |
| 21 | +HELPER_MODULE_PATH = f"{MODULE_PATH}._versions_helpers" |
| 22 | +MOCK_EXTRACT_VERSION = f"{HELPER_MODULE_PATH}.extract_runtime_version" |
| 23 | +MOCK_WARN = "warnings.warn" # Target the standard warnings module |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "mock_version_tuple, version_str", |
| 28 | + [ |
| 29 | + ((3, 7, 10), "3.7.10"), |
| 30 | + ((3, 7, 0), "3.7.0"), |
| 31 | + ((3, 8, 5), "3.8.5"), |
| 32 | + ((3, 8, 12), "3.8.12"), |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_check_python_version_warns_on_unsupported(mock_version_tuple, version_str): |
| 36 | + """ |
| 37 | + Test that _check_python_version issues a FutureWarning for Python 3.7/3.8. |
| 38 | + """ |
| 39 | + |
| 40 | + from db_dtypes import _check_python_version |
| 41 | + |
| 42 | + # Mock the helper function it calls and the warnings.warn function |
| 43 | + with mock.patch(MOCK_EXTRACT_VERSION, return_value=mock_version_tuple), mock.patch( |
| 44 | + MOCK_WARN |
| 45 | + ) as mock_warn_call: |
| 46 | + _check_python_version() # Call the function |
| 47 | + |
| 48 | + # Assert that warnings.warn was called exactly once |
| 49 | + mock_warn_call.assert_called_once() |
| 50 | + |
| 51 | + # Check the arguments passed to warnings.warn |
| 52 | + args, kwargs = mock_warn_call.call_args |
| 53 | + assert len(args) >= 1 # Should have at least the message |
| 54 | + warning_message = args[0] |
| 55 | + warning_category = args[1] if len(args) > 1 else kwargs.get("category") |
| 56 | + |
| 57 | + # Verify message content and category |
| 58 | + assert "longer supports Python 3.7 and Python 3.8" in warning_message |
| 59 | + assert warning_category == FutureWarning |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "mock_version_tuple", |
| 64 | + [ |
| 65 | + (3, 9, 1), |
| 66 | + (3, 10, 0), |
| 67 | + (3, 11, 2), |
| 68 | + (3, 12, 0), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_check_python_version_does_not_warn_on_supported(mock_version_tuple): |
| 72 | + """ |
| 73 | + Test that _check_python_version does NOT issue a warning for other versions. |
| 74 | + """ |
| 75 | + |
| 76 | + from db_dtypes import _check_python_version |
| 77 | + |
| 78 | + # Mock the helper function it calls and the warnings.warn function |
| 79 | + with mock.patch(MOCK_EXTRACT_VERSION, return_value=mock_version_tuple), mock.patch( |
| 80 | + MOCK_WARN |
| 81 | + ) as mock_warn_call: |
| 82 | + _check_python_version() |
| 83 | + |
| 84 | + # Assert that warnings.warn was NOT called |
| 85 | + mock_warn_call.assert_not_called() |
0 commit comments