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

@ -604,7 +604,7 @@ class TestHostsManagerApp:
mock_radio_set.id = "edit-entry-type-radio"
mock_pressed_radio = Mock()
mock_pressed_radio.id = "edit-ip-entry-radio"
event = Mock()
event.radio_set = mock_radio_set
event.pressed = mock_pressed_radio
@ -631,7 +631,7 @@ class TestHostsManagerApp:
mock_radio_set.id = "edit-entry-type-radio"
mock_pressed_radio = Mock()
mock_pressed_radio.id = "edit-dns-entry-radio"
event = Mock()
event.radio_set = mock_radio_set
event.pressed = mock_pressed_radio
@ -812,12 +812,12 @@ class TestHostsManagerApp:
mock_radio_set = Mock()
mock_ip_radio = Mock()
mock_dns_radio = Mock()
# Use a simple object to track value assignment
class MockDNSInput:
def __init__(self):
self.value = ""
mock_dns_input = MockDNSInput()
def mock_query_one(selector, widget_type=None):
@ -833,14 +833,16 @@ 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:
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)
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()
@ -946,21 +948,24 @@ 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()
# 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'):
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()
# Should not show error message about read-only mode
error_calls = [call for call in app.update_status.call_args_list
if "read-only mode" in str(call)]
error_calls = [
call
for call in app.update_status.call_args_list
if "read-only mode" in str(call)
]
assert len(error_calls) == 0
# Should start DNS resolution
app.run_worker.assert_called()
@ -972,8 +977,11 @@ class TestHostsManagerApp:
# Test action_update_single_dns in edit mode - should proceed
app.action_update_single_dns()
# Should not show error message about read-only mode
error_calls = [call for call in app.update_status.call_args_list
if "read-only mode" in str(call)]
error_calls = [
call
for call in app.update_status.call_args_list
if "read-only mode" in str(call)
]
assert len(error_calls) == 0
# Should start DNS resolution
app.run_worker.assert_called()