77
88@pytest .fixture ()
99def client_setup ():
10+ """Create a default ClientBuilder instance for tests."""
1011 new_client = ClientBuilder (
1112 endpoint = "https://economia.awesomeapi.com.br/last/USD-BRL"
1213 )
1314 return new_client
1415
1516
16- @pytest .fixture ()
17- def response_setup ():
18- new_client = ClientBuilder (
19- endpoint = "https://economia.awesomeapi.com.br/last/USD-BRL"
20- )
21- return new_client .get_api_data ()
22-
23-
2417def test_constructor_raises ():
18+ """Ensure constructor validations raise errors for invalid inputs."""
2519 with pytest .raises (ValueError ):
2620 ClientBuilder (endpoint = "" )
2721
@@ -59,13 +53,14 @@ def test_constructor_raises():
5953
6054
6155def test_constructor_with_param (client_setup ): # pylint: disable=redefined-outer-name
56+ """Ensure constructor stores the provided endpoint value."""
6257 expected_result = "https://economia.awesomeapi.com.br/last/USD-BRL"
6358 new_client = client_setup
6459 assert new_client .endpoint == expected_result
6560
6661
6762def test_constructor_with_headers ():
68- """Test ClientBuilder with custom headers"""
63+ """Test ClientBuilder with custom headers. """
6964 custom_headers = {"Authorization" : "Bearer token123" , "Content-Type" : "application/json" }
7065 client = ClientBuilder (
7166 endpoint = "https://economia.awesomeapi.com.br/last/USD-BRL" ,
@@ -75,7 +70,7 @@ def test_constructor_with_headers():
7570
7671
7772def test_constructor_with_retry_strategy ():
78- """Test ClientBuilder with different retry strategies"""
73+ """Test ClientBuilder with different retry strategies. """
7974 client = ClientBuilder (
8075 endpoint = "https://economia.awesomeapi.com.br/last/USD-BRL" ,
8176 retry_strategy = RetryStrategies .LINEAR_RETRY_STRATEGY ,
@@ -87,20 +82,34 @@ def test_constructor_with_retry_strategy():
8782 assert client .delay == 2
8883
8984
85+ @responses .activate
9086def test_response_to_json (client_setup ): # pylint: disable=redefined-outer-name
87+ """Ensure API responses are converted to JSON objects."""
9188 new_client = client_setup
89+ expected_response = {"key" : "value" }
90+
91+ responses .add (
92+ responses .GET ,
93+ new_client .endpoint ,
94+ json = expected_response ,
95+ status = 200 ,
96+ )
97+
9298 response = new_client .get_api_data () # pylint: disable=protected-access
9399 assert isinstance (response , dict )
100+ assert response == expected_response
94101
95102
96- def test_to_dataframe (response_setup ): # pylint: disable=redefined-outer-name
97- df = ClientBuilder .api_to_dataframe (response_setup )
103+ def test_to_dataframe ():
104+ """Ensure responses can be converted into DataFrames."""
105+ sample_response = [{"currency" : "USD" , "bid" : "5.0" }]
106+ df = ClientBuilder .api_to_dataframe (sample_response )
98107 assert isinstance (df , pd .DataFrame )
99108
100109
101110@responses .activate
102111def test_get_api_data_with_mocked_response ():
103- """Test get_api_data with mocked API response"""
112+ """Test get_api_data with mocked API response. """
104113 endpoint = "https://api.test.com/data"
105114 expected_data = {"key" : "value" , "nested" : {"id" : 123 }}
106115
@@ -118,3 +127,77 @@ def test_get_api_data_with_mocked_response():
118127 assert response == expected_data
119128 assert len (responses .calls ) == 1
120129 assert responses .calls [0 ].request .url == endpoint
130+
131+
132+ def test_with_method_configures_request_method ():
133+ """Ensure with_method configures the HTTP method for requests."""
134+
135+ client = ClientBuilder (endpoint = "https://api.test.com" ).with_method ("post" )
136+ assert client ._method == "POST" # pylint: disable=protected-access
137+
138+
139+ def test_with_params_validates_input_type ():
140+ """Ensure with_params only accepts dictionaries."""
141+
142+ client = ClientBuilder (endpoint = "https://api.test.com" )
143+ assert client .with_params ({"page" : 1 }) is client
144+
145+ with pytest .raises (ValueError ):
146+ client .with_params ([("page" , 1 )])
147+
148+
149+ def test_with_payload_replaces_existing_payload ():
150+ """Ensure with_payload stores json, data and file payloads."""
151+
152+ client = ClientBuilder (endpoint = "https://api.test.com" )
153+ client .with_payload (json = {"name" : "Jane" }, data = None )
154+
155+ assert client ._json_payload == {"name" : "Jane" } # pylint: disable=protected-access
156+ assert client ._data_payload is None # pylint: disable=protected-access
157+
158+
159+ def test_with_session_validates_interface ():
160+ """Ensure with_session requires an object exposing request."""
161+
162+ client = ClientBuilder (endpoint = "https://api.test.com" )
163+
164+ class DummySession :
165+ """Expose a request method to mimic a real session."""
166+
167+ def request (self , ** kwargs ): # pragma: no cover - dummy implementation
168+ return kwargs
169+
170+ session = DummySession ()
171+ assert client .with_session (session ) is client
172+
173+ with pytest .raises (ValueError ):
174+ client .with_session (object ())
175+
176+
177+ @responses .activate
178+ def test_fluent_configuration_executes_request ():
179+ """Ensure fluent configuration works when executing requests."""
180+
181+ endpoint = "https://api.test.com/submit"
182+ payload = {"name" : "John" }
183+ expected_response = {"status" : "created" }
184+
185+ responses .add (
186+ responses .POST ,
187+ endpoint ,
188+ json = expected_response ,
189+ status = 201 ,
190+ )
191+
192+ client = (
193+ ClientBuilder (endpoint = endpoint )
194+ .with_method ("POST" )
195+ .with_payload (json = payload )
196+ .with_params ({"verbose" : "true" })
197+ )
198+
199+ response = client .get_api_data ()
200+
201+ assert response == expected_response
202+ assert responses .calls [0 ].request .method == "POST"
203+ assert "verbose=true" in responses .calls [0 ].request .url
0 commit comments