11"""
2- Basic tests for the Bright Data SDK client.
2+ Comprehensive tests for the Bright Data SDK client.
33
4- These are placeholder tests to demonstrate the testing structure.
5- Full implementation would require mock responses and environment setup.
4+ This test suite covers:
5+ - Client initialization with API tokens (from parameter and environment)
6+ - API token validation and error handling for missing tokens
7+ - Zone configuration (default and custom zone names)
8+ - URL validation in scrape method (scheme requirement)
9+ - Search query validation (empty query handling)
10+ - Search engine validation (unsupported engine handling)
11+
12+ All tests are designed to run without requiring real API tokens by:
13+ - Using sufficiently long test tokens to pass validation
14+ - Mocking zone management to avoid network calls
15+ - Testing validation logic and error messages
616"""
717
818import pytest
919import os
10- from unittest .mock import patch , MagicMock
20+ from unittest .mock import patch
1121
1222from brightdata import bdclient
13- from brightdata .exceptions import ValidationError , AuthenticationError
23+ from brightdata .exceptions import ValidationError
1424
1525
1626class TestBdClient :
1727 """Test cases for the main bdclient class"""
1828
19- def test_client_init_with_token (self ):
29+ @patch ('brightdata.utils.zone_manager.ZoneManager.ensure_required_zones' )
30+ def test_client_init_with_token (self , mock_zones ):
2031 """Test client initialization with API token"""
2132 with patch .dict (os .environ , {}, clear = True ):
22- client = bdclient (api_token = "test_token " , auto_create_zones = False )
23- assert client .api_token == "test_token "
33+ client = bdclient (api_token = "valid_test_token_12345678 " , auto_create_zones = False )
34+ assert client .api_token == "valid_test_token_12345678 "
2435
25- def test_client_init_from_env (self ):
36+ @patch ('brightdata.utils.zone_manager.ZoneManager.ensure_required_zones' )
37+ def test_client_init_from_env (self , mock_zones ):
2638 """Test client initialization from environment variable"""
27- with patch .dict (os .environ , {"BRIGHTDATA_API_TOKEN" : "env_token " }):
39+ with patch .dict (os .environ , {"BRIGHTDATA_API_TOKEN" : "valid_env_token_12345678 " }):
2840 client = bdclient (auto_create_zones = False )
29- assert client .api_token == "env_token "
41+ assert client .api_token == "valid_env_token_12345678 "
3042
3143 def test_client_init_no_token_raises_error (self ):
3244 """Test that missing API token raises ValidationError"""
3345 with patch .dict (os .environ , {}, clear = True ):
34- # Mock dotenv to prevent loading .env file
3546 with patch ('dotenv.load_dotenv' ):
3647 with pytest .raises (ValidationError , match = "API token is required" ):
3748 bdclient ()
3849
39- def test_client_zone_defaults (self ):
50+ @patch ('brightdata.utils.zone_manager.ZoneManager.ensure_required_zones' )
51+ def test_client_zone_defaults (self , mock_zones ):
4052 """Test default zone configurations"""
4153 with patch .dict (os .environ , {}, clear = True ):
42- client = bdclient (api_token = "test_token " , auto_create_zones = False )
54+ client = bdclient (api_token = "valid_test_token_12345678 " , auto_create_zones = False )
4355 assert client .web_unlocker_zone == "sdk_unlocker"
4456 assert client .serp_zone == "sdk_serp"
4557
46- def test_client_custom_zones (self ):
58+ @patch ('brightdata.utils.zone_manager.ZoneManager.ensure_required_zones' )
59+ def test_client_custom_zones (self , mock_zones ):
4760 """Test custom zone configuration"""
4861 with patch .dict (os .environ , {}, clear = True ):
4962 client = bdclient (
50- api_token = "test_token " ,
63+ api_token = "valid_test_token_12345678 " ,
5164 web_unlocker_zone = "custom_unlocker" ,
5265 serp_zone = "custom_serp" ,
5366 auto_create_zones = False
@@ -60,25 +73,26 @@ class TestClientMethods:
6073 """Test cases for client methods with mocked responses"""
6174
6275 @pytest .fixture
63- def client (self ):
64- """Create a test client with mocked session"""
76+ @patch ('brightdata.utils.zone_manager.ZoneManager.ensure_required_zones' )
77+ def client (self , mock_zones ):
78+ """Create a test client with mocked validation"""
6579 with patch .dict (os .environ , {}, clear = True ):
66- client = bdclient (api_token = "test_token " , auto_create_zones = False )
80+ client = bdclient (api_token = "valid_test_token_12345678 " , auto_create_zones = False )
6781 return client
6882
6983 def test_scrape_single_url_validation (self , client ):
7084 """Test URL validation in scrape method"""
71- with pytest .raises (ValidationError , match = "Invalid URL format " ):
85+ with pytest .raises (ValidationError , match = "URL must include a scheme " ):
7286 client .scrape ("not_a_url" )
7387
7488 def test_search_empty_query_validation (self , client ):
7589 """Test query validation in search method"""
76- with pytest .raises (ValidationError , match = "Query must be a non- empty string " ):
90+ with pytest .raises (ValidationError , match = "cannot be empty" ):
7791 client .search ("" )
7892
7993 def test_search_unsupported_engine (self , client ):
8094 """Test unsupported search engine validation"""
81- with pytest .raises (ValidationError , match = "Unsupported search engine" ):
95+ with pytest .raises (ValidationError , match = "Invalid search engine" ):
8296 client .search ("test query" , search_engine = "invalid_engine" )
8397
8498
0 commit comments