Refactor test cases for improved readability and consistency

- Updated test_dns.py to enhance mock function definitions and improve spacing for better readability.
- Modified test_filters.py to streamline assertions and ensure consistent formatting across test cases.
- Cleaned up test_import_export.py by organizing imports and ensuring consistent formatting in CSV and JSON tests.
- Improved test_main.py by refining mock setups and ensuring consistent error handling in assertions.
This commit is contained in:
Philip Henning 2026-09-03 12:47:11 +02:00
parent 0710d10fac
commit 7872991e0b
42 changed files with 2376 additions and 2735 deletions

View file

@ -115,9 +115,10 @@ class TestResolveHostname:
@pytest.mark.asyncio
async def test_timeout_resolution(self):
"""Test hostname resolution timeout."""
async def mock_wait_for(*args, **kwargs):
raise asyncio.TimeoutError()
with patch("asyncio.wait_for", side_effect=mock_wait_for) as mock_wait_for:
resolution = await resolve_hostname("slow.example", timeout=1.0)
@ -142,9 +143,10 @@ class TestResolveHostname:
@pytest.mark.asyncio
async def test_empty_result_resolution(self):
"""Test hostname resolution with empty result."""
async def mock_wait_for(*args, **kwargs):
return []
with patch("asyncio.get_event_loop") as mock_loop:
mock_event_loop = AsyncMock()
mock_loop.return_value = mock_event_loop
@ -167,7 +169,9 @@ class TestResolveHostnamesBatch:
"""Test successful batch hostname resolution."""
hostnames = ["example.com", "test.example"]
with patch("src.hosts.core.dns.resolve_hostname", new_callable=AsyncMock) as mock_resolve:
with patch(
"src.hosts.core.dns.resolve_hostname", new_callable=AsyncMock
) as mock_resolve:
# Mock successful resolutions
mock_resolve.side_effect = [
DNSResolution(
@ -214,9 +218,8 @@ class TestResolveHostnamesBatch:
resolved_at=datetime.now(),
error_message="Name not found",
)
with patch("src.hosts.core.dns.resolve_hostname", mock_resolve_hostname):
with patch("src.hosts.core.dns.resolve_hostname", mock_resolve_hostname):
resolutions = await resolve_hostnames_batch(hostnames)
assert len(resolutions) == 2
@ -278,16 +281,20 @@ class TestDNSService:
"""Test async resolution when service is enabled."""
service = DNSService(enabled=True)
with patch("src.hosts.core.dns.resolve_hostname", new_callable=AsyncMock) as mock_resolve:
with patch(
"src.hosts.core.dns.resolve_hostname", new_callable=AsyncMock
) as mock_resolve:
mock_resolution = DNSResolution(
hostname="example.com",
resolved_ip="192.0.2.1",
status=DNSResolutionStatus.RESOLVED,
resolved_at=datetime.now(),
)
# Use proper async setup
async def mock_side_effect(hostname, timeout=5.0):
return mock_resolution
mock_resolve.side_effect = mock_side_effect
resolution = await service.resolve_entry_async("example.com")
@ -312,16 +319,20 @@ class TestDNSService:
"""Test manual entry refresh."""
service = DNSService(enabled=True)
with patch("src.hosts.core.dns.resolve_hostname", new_callable=AsyncMock) as mock_resolve:
with patch(
"src.hosts.core.dns.resolve_hostname", new_callable=AsyncMock
) as mock_resolve:
mock_resolution = DNSResolution(
hostname="example.com",
resolved_ip="192.0.2.1",
status=DNSResolutionStatus.RESOLVED,
resolved_at=datetime.now(),
)
# Use proper async setup
async def mock_side_effect(hostname, timeout=5.0):
return mock_resolution
mock_resolve.side_effect = mock_side_effect
result = await service.refresh_entry("example.com")