Refactor DNS resolution tests to use AsyncMock and improve timeout handling

This commit is contained in:
Philip Henning 2025-08-18 18:32:33 +02:00
parent d7ca9cc87f
commit 8d99cfe53c
3 changed files with 50 additions and 27 deletions

View file

@ -833,11 +833,20 @@ class TestHostsManagerApp:
app.query_one = mock_query_one
app.edit_handler.handle_entry_type_change = Mock()
# Mock the set_timer method to avoid event loop issues in tests
with patch.object(app, 'set_timer') as mock_set_timer:
app.edit_handler.populate_edit_form_with_type_detection()
# Verify timer was set with the correct callback
mock_set_timer.assert_called_once_with(0.1, app.edit_handler._delayed_radio_setup)
# Manually call the delayed setup to test the actual logic
app.edit_handler._delayed_radio_setup()
app.edit_handler.populate_edit_form_with_type_detection()
# Should set DNS radio button as pressed and populate DNS field
assert mock_radio_set.pressed_button == mock_dns_radio
# Verify that the DNS radio was set to True (which should be the pressed button)
assert mock_dns_radio.value
assert not mock_ip_radio.value
assert mock_dns_input.value == "example.com"
app.edit_handler.handle_entry_type_change.assert_called_with("dns")
@ -937,7 +946,15 @@ class TestHostsManagerApp:
app.manager.save_hosts_file = Mock(return_value=(True, "Success"))
app.table_handler.populate_entries_table = Mock()
app.details_handler.update_entry_details = Mock()
app.run_worker = Mock()
# Create a mock that properly handles and closes coroutines
def consume_coro(coro, **kwargs):
# If it's a coroutine, close it to prevent warnings
if hasattr(coro, 'close'):
coro.close()
return None
app.run_worker = Mock(side_effect=consume_coro)
# Test action_refresh_dns in edit mode - should proceed
app.action_refresh_dns()