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
+
5
15
from unittest import mock
6
- import pyarrow as pa
16
+
17
+ import pytest
7
18
8
19
# Module paths used for mocking
9
20
MODULE_PATH = "db_dtypes"
10
21
HELPER_MODULE_PATH = f"{ MODULE_PATH } ._versions_helpers"
11
22
MOCK_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
+
13
25
14
26
@pytest .mark .parametrize (
15
27
"mock_version_tuple, version_str" ,
18
30
((3 , 7 , 0 ), "3.7.0" ),
19
31
((3 , 8 , 5 ), "3.8.5" ),
20
32
((3 , 8 , 12 ), "3.8.12" ),
21
- ]
33
+ ],
22
34
)
23
35
def test_check_python_version_warns_on_unsupported (mock_version_tuple , version_str ):
24
36
"""
25
37
Test that _check_python_version issues a FutureWarning for Python 3.7/3.8.
26
38
"""
27
- # Import the function under test directly
39
+
28
40
from db_dtypes import _check_python_version
29
41
30
42
# 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
35
47
36
48
# Assert that warnings.warn was called exactly once
37
49
mock_warn_call .assert_called_once ()
38
50
39
51
# Check the arguments passed to warnings.warn
40
52
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
42
54
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" )
44
56
45
57
# Verify message content and category
46
58
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
49
59
assert warning_category == FutureWarning
50
- # Optionally check stacklevel if important
51
- assert kwargs .get ('stacklevel' ) == 2
52
60
53
61
54
62
@pytest .mark .parametrize (
@@ -58,22 +66,22 @@ def test_check_python_version_warns_on_unsupported(mock_version_tuple, version_s
58
66
(3 , 10 , 0 ),
59
67
(3 , 11 , 2 ),
60
68
(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
+ ],
64
72
)
65
73
def test_check_python_version_does_not_warn_on_supported (mock_version_tuple ):
66
74
"""
67
75
Test that _check_python_version does NOT issue a warning for other versions.
68
76
"""
69
- # Import the function under test directly
77
+
70
78
from db_dtypes import _check_python_version
71
79
72
80
# 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 ()
77
85
78
86
# Assert that warnings.warn was NOT called
79
87
mock_warn_call .assert_not_called ()
@@ -83,7 +91,7 @@ def test_determine_all_includes_json_when_available():
83
91
"""
84
92
Test that _determine_all includes JSON types when both are truthy.
85
93
"""
86
- # Import the function directly for testing
94
+
87
95
from db_dtypes import _determine_all
88
96
89
97
# Simulate available types (can be any truthy object)
@@ -107,19 +115,20 @@ def test_determine_all_includes_json_when_available():
107
115
assert "JSONArray" in result
108
116
assert "JSONArrowType" in result
109
117
118
+
110
119
@pytest .mark .parametrize (
111
120
"mock_array, mock_dtype" ,
112
121
[
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
+ ],
117
126
)
118
127
def test_determine_all_excludes_json_when_unavailable (mock_array , mock_dtype ):
119
128
"""
120
129
Test that _determine_all excludes JSON types if either is falsy.
121
130
"""
122
- # Import the function directly for testing
131
+
123
132
from db_dtypes import _determine_all
124
133
125
134
result = _determine_all (mock_array , mock_dtype )
@@ -134,4 +143,4 @@ def test_determine_all_excludes_json_when_unavailable(mock_array, mock_dtype):
134
143
assert set (result ) == set (expected_all )
135
144
assert "JSONDtype" not in result
136
145
assert "JSONArray" not in result
137
- assert "JSONArrowType" not in result
146
+ assert "JSONArrowType" not in result
0 commit comments