1-
21import io
32from unittest .mock import patch , Mock , MagicMock
43
54import pytest
6- import requests
75
86import databricks .sql .client as client
97
@@ -12,143 +10,104 @@ class TestStreamingPut:
1210 """Unit tests for streaming PUT functionality."""
1311
1412 @pytest .fixture
15- def mock_connection (self ):
16- return Mock ()
17-
18- @pytest .fixture
19- def mock_backend (self ):
20- return Mock ()
21-
22- @pytest .fixture
23- def cursor (self , mock_connection , mock_backend ):
24- return client .Cursor (
25- connection = mock_connection ,
26- backend = mock_backend
27- )
13+ def cursor (self ):
14+ return client .Cursor (connection = Mock (), backend = Mock ())
2815
2916 def _setup_mock_staging_put_stream_response (self , mock_backend ):
3017 """Helper method to set up mock staging PUT stream response."""
3118 mock_result_set = Mock ()
3219 mock_result_set .is_staging_operation = True
3320 mock_backend .execute_command .return_value = mock_result_set
34-
21+
3522 mock_row = Mock ()
3623 mock_row .operation = "PUT"
3724 mock_row .localFile = "__input_stream__"
3825 mock_row .presignedUrl = "https://example.com/upload"
3926 mock_row .headers = "{}"
4027 mock_result_set .fetchone .return_value = mock_row
41-
28+
4229 return mock_result_set
4330
44- def test_execute_with_valid_stream (self , cursor , mock_backend ):
31+ def test_execute_with_valid_stream (self , cursor ):
4532 """Test execute method with valid input stream."""
46-
33+
4734 # Mock the backend response
48- self ._setup_mock_staging_put_stream_response (mock_backend )
49-
35+ self ._setup_mock_staging_put_stream_response (cursor . backend )
36+
5037 # Test with valid stream
5138 test_stream = io .BytesIO (b"test data" )
52-
53- with patch .object (cursor , ' _handle_staging_put_stream' ) as mock_handler :
39+
40+ with patch .object (cursor , " _handle_staging_put_stream" ) as mock_handler :
5441 cursor .execute (
5542 "PUT '__input_stream__' INTO '/Volumes/test/cat/schema/vol/file.txt'" ,
56- input_stream = test_stream
43+ input_stream = test_stream ,
5744 )
58-
45+
5946 # Verify staging handler was called
6047 mock_handler .assert_called_once ()
6148
62- def test_execute_with_invalid_stream_types (self , cursor , mock_backend ):
63-
64- # Mock the backend response
65- self ._setup_mock_staging_put_stream_response (mock_backend )
66-
67- # Test with None input stream
68- with pytest .raises (client .ProgrammingError ) as excinfo :
69- cursor .execute (
70- "PUT '__input_stream__' INTO '/Volumes/test/cat/schema/vol/file.txt'" ,
71- input_stream = None
72- )
73- assert "No input stream provided for streaming operation" in str (excinfo .value )
74-
75- def test_execute_with_none_stream_for_staging_put (self , cursor , mock_backend ):
49+ def test_execute_with_none_stream_for_staging_put (self , cursor ):
7650 """Test execute method rejects None stream for streaming PUT operations."""
77-
51+
7852 # Mock staging operation response for None case
79- self ._setup_mock_staging_put_stream_response (mock_backend )
80-
53+ self ._setup_mock_staging_put_stream_response (cursor . backend )
54+
8155 # None with __input_stream__ raises ProgrammingError
8256 with pytest .raises (client .ProgrammingError ) as excinfo :
8357 cursor .execute (
8458 "PUT '__input_stream__' INTO '/Volumes/test/cat/schema/vol/file.txt'" ,
85- input_stream = None
59+ input_stream = None ,
8660 )
8761 error_msg = str (excinfo .value )
8862 assert "No input stream provided for streaming operation" in error_msg
8963
9064 def test_handle_staging_put_stream_success (self , cursor ):
9165 """Test successful streaming PUT operation."""
92-
66+
9367 presigned_url = "https://example.com/upload"
9468 headers = {"Content-Type" : "text/plain" }
95-
96- with patch .object (cursor .connection .http_client , 'request' ) as mock_http_request :
69+
70+ with patch .object (
71+ cursor .connection .http_client , "request"
72+ ) as mock_http_request :
9773 mock_response = MagicMock ()
9874 mock_response .status = 200
9975 mock_response .data = b"success"
10076 mock_http_request .return_value = mock_response
101-
77+
10278 test_stream = io .BytesIO (b"test data" )
10379 cursor ._handle_staging_put_stream (
104- presigned_url = presigned_url ,
105- stream = test_stream ,
106- headers = headers
80+ presigned_url = presigned_url , stream = test_stream , headers = headers
10781 )
108-
82+
10983 # Verify the HTTP client was called correctly
11084 mock_http_request .assert_called_once ()
11185 call_args = mock_http_request .call_args
11286 # Check positional arguments: (method, url, body=..., headers=...)
113- assert call_args [0 ][0 ].value == ' PUT' # First positional arg is method
87+ assert call_args [0 ][0 ].value == " PUT" # First positional arg is method
11488 assert call_args [0 ][1 ] == presigned_url # Second positional arg is url
11589 # Check keyword arguments
116- assert call_args [1 ][' body' ] == b"test data"
117- assert call_args [1 ][' headers' ] == headers
90+ assert call_args [1 ][" body" ] == b"test data"
91+ assert call_args [1 ][" headers" ] == headers
11892
11993 def test_handle_staging_put_stream_http_error (self , cursor ):
12094 """Test streaming PUT operation with HTTP error."""
121-
95+
12296 presigned_url = "https://example.com/upload"
123-
124- with patch .object (cursor .connection .http_client , 'request' ) as mock_http_request :
97+
98+ with patch .object (
99+ cursor .connection .http_client , "request"
100+ ) as mock_http_request :
125101 mock_response = MagicMock ()
126102 mock_response .status = 500
127103 mock_response .data = b"Internal Server Error"
128104 mock_http_request .return_value = mock_response
129-
105+
130106 test_stream = io .BytesIO (b"test data" )
131107 with pytest .raises (client .OperationalError ) as excinfo :
132108 cursor ._handle_staging_put_stream (
133- presigned_url = presigned_url ,
134- stream = test_stream
109+ presigned_url = presigned_url , stream = test_stream
135110 )
136-
111+
137112 # Check for the actual error message format
138113 assert "500" in str (excinfo .value )
139-
140- def test_handle_staging_put_stream_network_error (self , cursor ):
141- """Test streaming PUT operation with network error."""
142-
143- presigned_url = "https://example.com/upload"
144-
145- with patch .object (cursor .connection .http_client , 'request' ) as mock_http_request :
146- mock_http_request .side_effect = requests .exceptions .RequestException ("Network error" )
147- test_stream = io .BytesIO (b"test_data" )
148- with pytest .raises (requests .exceptions .RequestException ) as excinfo :
149- cursor ._handle_staging_put_stream (
150- presigned_url = presigned_url ,
151- stream = test_stream
152- )
153-
154- assert "Network error" in str (excinfo .value )
0 commit comments