Skip to content

Commit acd91c7

Browse files
committed
add more tests for coverage
1 parent 2f2c3f4 commit acd91c7

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

proxyproviders/models/proxy.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def format(
102102
f"Invalid format type: '{format_type}'. Valid options are: {[f.value for f in ProxyFormat]}"
103103
)
104104
elif not isinstance(format_type, ProxyFormat):
105-
raise ValueError(
105+
raise ValueError( # pyright: ignore[reportUnreachable] this is actually reachable
106106
f"Invalid format type: {type(format_type).__name__}. Expected ProxyFormat enum or string."
107107
)
108108

@@ -115,10 +115,7 @@ def format(
115115
ProxyFormat.PLAYWRIGHT: lambda: self._format_playwright(**kwargs),
116116
}
117117

118-
handler = format_handlers.get(format_type)
119-
if handler is None:
120-
raise ValueError(f"Unsupported format: {format_type}")
121-
118+
handler = format_handlers[format_type]
122119
return handler()
123120

124121
def _format_requests(self, kwargs):

tests/test_proxy_model.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,62 @@ def test_format_unsupported_format(self, sample_proxy):
263263
with pytest.raises(ValueError, match="Invalid format type"):
264264
sample_proxy.format("unsupported")
265265

266+
def test_format_invalid_type_not_string_or_enum(self, sample_proxy):
267+
"""Test format with invalid type (not string or ProxyFormat enum)."""
268+
with pytest.raises(
269+
ValueError,
270+
match="Invalid format type: int. Expected ProxyFormat enum or string.",
271+
):
272+
sample_proxy.format(123) # int instead of string/enum
273+
274+
def test_format_invalid_type_none(self, sample_proxy):
275+
"""Test format with None type."""
276+
with pytest.raises(
277+
ValueError,
278+
match="Invalid format type: NoneType. Expected ProxyFormat enum or string.",
279+
):
280+
sample_proxy.format(None)
281+
282+
def test_format_invalid_type_list(self, sample_proxy):
283+
"""Test format with list type."""
284+
with pytest.raises(
285+
ValueError,
286+
match="Invalid format type: list. Expected ProxyFormat enum or string.",
287+
):
288+
sample_proxy.format(["requests"])
289+
290+
def test_format_invalid_type_dict(self, sample_proxy):
291+
"""Test format with dict type."""
292+
with pytest.raises(
293+
ValueError,
294+
match="Invalid format type: dict. Expected ProxyFormat enum or string.",
295+
):
296+
sample_proxy.format({"format": "requests"})
297+
298+
def test_format_handlers_complete_coverage(self, sample_proxy):
299+
"""Test that all ProxyFormat enum values have corresponding handlers."""
300+
# This test ensures that if someone adds a new ProxyFormat enum value,
301+
# they must also add a corresponding handler in the format_handlers dictionary
302+
303+
# Get all ProxyFormat enum values
304+
all_proxy_formats = set(ProxyFormat)
305+
306+
# Test that each format can be processed without KeyError
307+
for format_type in all_proxy_formats:
308+
try:
309+
result = sample_proxy.format(format_type)
310+
assert (
311+
result is not None
312+
), f"Format {format_type.value} should return a result"
313+
except KeyError as e:
314+
pytest.fail(f"Missing handler for ProxyFormat.{format_type.name}: {e}")
315+
except Exception as e:
316+
# Other exceptions are fine (like validation errors), but KeyError means missing handler
317+
if "KeyError" in str(type(e)):
318+
pytest.fail(
319+
f"Missing handler for ProxyFormat.{format_type.name}: {e}"
320+
)
321+
266322
def test_format_proxy_with_limited_protocols(self):
267323
"""Test format with proxy that only supports specific protocols."""
268324
proxy = Proxy(

0 commit comments

Comments
 (0)