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

@ -179,13 +179,13 @@ class TestUndoRedoHistory:
"""Test that executing a new command clears the redo stack."""
history = UndoRedoHistory()
hosts_file = HostsFile()
# Execute and undo a command
command1 = Mock(spec=Command)
command1.execute.return_value = OperationResult(True, "Command 1")
command1.undo.return_value = OperationResult(True, "Undo 1")
command1.get_description.return_value = "Command 1"
history.execute_command(command1, hosts_file)
history.undo(hosts_file)
assert history.can_redo()
@ -194,7 +194,7 @@ class TestUndoRedoHistory:
command2 = Mock(spec=Command)
command2.execute.return_value = OperationResult(True, "Command 2")
command2.get_description.return_value = "Command 2"
history.execute_command(command2, hosts_file)
assert not history.can_redo() # Redo stack should be cleared
@ -205,7 +205,9 @@ class TestToggleEntryCommand:
def test_toggle_active_to_inactive(self):
"""Test toggling an active entry to inactive."""
hosts_file = HostsFile()
entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"], is_active=True)
entry = HostEntry(
ip_address="192.168.1.1", hostnames=["test.local"], is_active=True
)
hosts_file.entries.append(entry)
command = ToggleEntryCommand(0)
@ -218,7 +220,9 @@ class TestToggleEntryCommand:
def test_toggle_inactive_to_active(self):
"""Test toggling an inactive entry to active."""
hosts_file = HostsFile()
entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"], is_active=False)
entry = HostEntry(
ip_address="192.168.1.1", hostnames=["test.local"], is_active=False
)
hosts_file.entries.append(entry)
command = ToggleEntryCommand(0)
@ -231,7 +235,9 @@ class TestToggleEntryCommand:
def test_toggle_undo(self):
"""Test undoing a toggle operation."""
hosts_file = HostsFile()
entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"], is_active=True)
entry = HostEntry(
ip_address="192.168.1.1", hostnames=["test.local"], is_active=True
)
hosts_file.entries.append(entry)
command = ToggleEntryCommand(0)
@ -454,10 +460,17 @@ class TestUpdateEntryCommand:
def test_update_entry(self):
"""Test updating an entry."""
hosts_file = HostsFile()
old_entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"], comment="old comment", is_active=True)
old_entry = HostEntry(
ip_address="192.168.1.1",
hostnames=["test.local"],
comment="old comment",
is_active=True,
)
hosts_file.entries.append(old_entry)
command = UpdateEntryCommand(0, "192.168.1.2", ["updated.local"], "new comment", False)
command = UpdateEntryCommand(
0, "192.168.1.2", ["updated.local"], "new comment", False
)
result = command.execute(hosts_file)
assert result.success is True
@ -470,10 +483,17 @@ class TestUpdateEntryCommand:
def test_update_entry_undo(self):
"""Test undoing an update operation."""
hosts_file = HostsFile()
old_entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"], comment="old comment", is_active=True)
old_entry = HostEntry(
ip_address="192.168.1.1",
hostnames=["test.local"],
comment="old comment",
is_active=True,
)
hosts_file.entries.append(old_entry)
command = UpdateEntryCommand(0, "192.168.1.2", ["updated.local"], "new comment", False)
command = UpdateEntryCommand(
0, "192.168.1.2", ["updated.local"], "new comment", False
)
command.execute(hosts_file)
result = command.undo(hosts_file)
@ -507,7 +527,7 @@ class TestHostsManagerIntegration:
def test_manager_undo_redo_properties(self):
"""Test undo/redo availability properties."""
manager = HostsManager()
# Initially no undo/redo available
assert not manager.can_undo()
assert not manager.can_redo()
@ -519,7 +539,7 @@ class TestHostsManagerIntegration:
manager = HostsManager()
hosts_file = HostsFile()
result = manager.undo_last_operation(hosts_file)
assert result.success is False
assert "Not in edit mode" in result.message
@ -528,46 +548,48 @@ class TestHostsManagerIntegration:
manager = HostsManager()
hosts_file = HostsFile()
result = manager.redo_last_operation(hosts_file)
assert result.success is False
assert "Not in edit mode" in result.message
@patch('src.hosts.core.manager.HostsManager.enter_edit_mode')
@patch("src.hosts.core.manager.HostsManager.enter_edit_mode")
def test_manager_execute_toggle_command(self, mock_enter_edit):
"""Test executing a toggle command through the manager."""
mock_enter_edit.return_value = (True, "Edit mode enabled")
manager = HostsManager()
manager.edit_mode = True # Simulate edit mode
hosts_file = HostsFile()
entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"], is_active=True)
entry = HostEntry(
ip_address="192.168.1.1", hostnames=["test.local"], is_active=True
)
hosts_file.entries.append(entry)
result = manager.execute_toggle_command(hosts_file, 0)
assert result.success is True
assert not hosts_file.entries[0].is_active
@patch('src.hosts.core.manager.HostsManager.enter_edit_mode')
@patch("src.hosts.core.manager.HostsManager.enter_edit_mode")
def test_manager_execute_move_command(self, mock_enter_edit):
"""Test executing a move command through the manager."""
mock_enter_edit.return_value = (True, "Edit mode enabled")
manager = HostsManager()
manager.edit_mode = True # Simulate edit mode
hosts_file = HostsFile()
entry1 = HostEntry(ip_address="192.168.1.1", hostnames=["test1.local"])
entry2 = HostEntry(ip_address="192.168.1.2", hostnames=["test2.local"])
hosts_file.entries.extend([entry1, entry2])
result = manager.execute_move_command(hosts_file, 1, "up")
assert result.success is True
assert hosts_file.entries[0] == entry2
@patch('src.hosts.core.manager.HostsManager.enter_edit_mode')
@patch("src.hosts.core.manager.HostsManager.enter_edit_mode")
def test_manager_command_not_in_edit_mode(self, mock_enter_edit):
"""Test executing commands when not in edit mode."""
manager = HostsManager()
@ -575,7 +597,7 @@ class TestHostsManagerIntegration:
entry = HostEntry(ip_address="192.168.1.1", hostnames=["test.local"])
result = manager.execute_add_command(hosts_file, entry)
assert result.success is False
assert "Not in edit mode" in result.message