1- import sys
2- import pytest
3- import types
4- import warnings
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+
515from unittest import mock
6- import pyarrow as pa
16+
17+ import pytest
718
819# Module paths used for mocking
920MODULE_PATH = "db_dtypes"
1021HELPER_MODULE_PATH = f"{ MODULE_PATH } ._versions_helpers"
1122MOCK_EXTRACT_VERSION = f"{ HELPER_MODULE_PATH } .extract_runtime_version"
12- MOCK_WARN = "warnings.warn" # Target the standard warnings module
23+ MOCK_WARN = "warnings.warn" # Target the standard warnings module
24+
1325
1426@pytest .mark .parametrize (
1527 "mock_version_tuple, version_str" ,
1830 ((3 , 7 , 0 ), "3.7.0" ),
1931 ((3 , 8 , 5 ), "3.8.5" ),
2032 ((3 , 8 , 12 ), "3.8.12" ),
21- ]
33+ ],
2234)
2335def test_check_python_version_warns_on_unsupported (mock_version_tuple , version_str ):
2436 """
2537 Test that _check_python_version issues a FutureWarning for Python 3.7/3.8.
2638 """
27- # Import the function under test directly
39+
2840 from db_dtypes import _check_python_version
2941
3042 # Mock the helper function it calls and the warnings.warn function
31- with mock .patch (MOCK_EXTRACT_VERSION , return_value = mock_version_tuple ), \
32- mock . patch ( MOCK_WARN ) as mock_warn_call :
33-
34- _check_python_version () # Call the 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
3547
3648 # Assert that warnings.warn was called exactly once
3749 mock_warn_call .assert_called_once ()
3850
3951 # Check the arguments passed to warnings.warn
4052 args , kwargs = mock_warn_call .call_args
41- assert len (args ) >= 1 # Should have at least the message
53+ assert len (args ) >= 1 # Should have at least the message
4254 warning_message = args [0 ]
43- warning_category = args [1 ] if len (args ) > 1 else kwargs .get (' category' )
55+ warning_category = args [1 ] if len (args ) > 1 else kwargs .get (" category" )
4456
4557 # Verify message content and category
4658 assert "longer supports Python 3.7 and Python 3.8" in warning_message
47- assert f"Your Python version is { version_str } " in warning_message
48- assert "https://cloud.google.com/python/docs/supported-python-versions" in warning_message
4959 assert warning_category == FutureWarning
50- # Optionally check stacklevel if important
51- assert kwargs .get ('stacklevel' ) == 2
5260
5361
5462@pytest .mark .parametrize (
@@ -58,22 +66,22 @@ def test_check_python_version_warns_on_unsupported(mock_version_tuple, version_s
5866 (3 , 10 , 0 ),
5967 (3 , 11 , 2 ),
6068 (3 , 12 , 0 ),
61- (4 , 0 , 0 ), # Future version
62- (3 , 6 , 0 ), # Older unsupported, but not 3.7/3.8
63- ]
69+ (4 , 0 , 0 ), # Future version
70+ (3 , 6 , 0 ), # Older unsupported, but not 3.7/3.8
71+ ],
6472)
6573def test_check_python_version_does_not_warn_on_supported (mock_version_tuple ):
6674 """
6775 Test that _check_python_version does NOT issue a warning for other versions.
6876 """
69- # Import the function under test directly
77+
7078 from db_dtypes import _check_python_version
7179
7280 # Mock the helper function it calls and the warnings.warn function
73- with mock .patch (MOCK_EXTRACT_VERSION , return_value = mock_version_tuple ), \
74- mock . patch ( MOCK_WARN ) as mock_warn_call :
75-
76- _check_python_version () # Call the function
81+ with mock .patch (MOCK_EXTRACT_VERSION , return_value = mock_version_tuple ), mock . patch (
82+ MOCK_WARN
83+ ) as mock_warn_call :
84+ _check_python_version ()
7785
7886 # Assert that warnings.warn was NOT called
7987 mock_warn_call .assert_not_called ()
@@ -83,7 +91,7 @@ def test_determine_all_includes_json_when_available():
8391 """
8492 Test that _determine_all includes JSON types when both are truthy.
8593 """
86- # Import the function directly for testing
94+
8795 from db_dtypes import _determine_all
8896
8997 # Simulate available types (can be any truthy object)
@@ -107,19 +115,20 @@ def test_determine_all_includes_json_when_available():
107115 assert "JSONArray" in result
108116 assert "JSONArrowType" in result
109117
118+
110119@pytest .mark .parametrize (
111120 "mock_array, mock_dtype" ,
112121 [
113- (None , object ()), # JSONArray is None
114- (object (), None ), # JSONDtype is None
115- (None , None ), # Both are None
116- ]
122+ (None , object ()), # JSONArray is None
123+ (object (), None ), # JSONDtype is None
124+ (None , None ), # Both are None
125+ ],
117126)
118127def test_determine_all_excludes_json_when_unavailable (mock_array , mock_dtype ):
119128 """
120129 Test that _determine_all excludes JSON types if either is falsy.
121130 """
122- # Import the function directly for testing
131+
123132 from db_dtypes import _determine_all
124133
125134 result = _determine_all (mock_array , mock_dtype )
@@ -134,4 +143,4 @@ def test_determine_all_excludes_json_when_unavailable(mock_array, mock_dtype):
134143 assert set (result ) == set (expected_all )
135144 assert "JSONDtype" not in result
136145 assert "JSONArray" not in result
137- assert "JSONArrowType" not in result
146+ assert "JSONArrowType" not in result
0 commit comments