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:
parent
0710d10fac
commit
7872991e0b
42 changed files with 2376 additions and 2735 deletions
|
|
@ -29,7 +29,7 @@ class TestAddEntryModalDNSSupport:
|
|||
# Test that the compose method exists and can be called
|
||||
# We can't test the actual widget creation without mounting the modal
|
||||
# in a Textual app context, so we just verify the method exists
|
||||
assert hasattr(self.modal, 'compose')
|
||||
assert hasattr(self.modal, "compose")
|
||||
assert callable(self.modal.compose)
|
||||
|
||||
def test_validate_input_ip_entry_valid(self):
|
||||
|
|
@ -39,7 +39,7 @@ class TestAddEntryModalDNSSupport:
|
|||
ip_address="192.168.1.1",
|
||||
dns_name="",
|
||||
hostnames_str="example.com",
|
||||
is_dns_entry=False
|
||||
is_dns_entry=False,
|
||||
)
|
||||
assert result is True
|
||||
|
||||
|
|
@ -47,12 +47,9 @@ class TestAddEntryModalDNSSupport:
|
|||
"""Test validation for IP entry with missing IP address."""
|
||||
# Mock the error display method
|
||||
self.modal._show_error = Mock()
|
||||
|
||||
|
||||
result = self.modal._validate_input(
|
||||
ip_address="",
|
||||
dns_name="",
|
||||
hostnames_str="example.com",
|
||||
is_dns_entry=False
|
||||
ip_address="", dns_name="", hostnames_str="example.com", is_dns_entry=False
|
||||
)
|
||||
assert result is False
|
||||
self.modal._show_error.assert_called_with("ip-error", "IP address is required")
|
||||
|
|
@ -63,7 +60,7 @@ class TestAddEntryModalDNSSupport:
|
|||
ip_address="",
|
||||
dns_name="example.com",
|
||||
hostnames_str="www.example.com",
|
||||
is_dns_entry=True
|
||||
is_dns_entry=True,
|
||||
)
|
||||
assert result is True
|
||||
|
||||
|
|
@ -71,12 +68,9 @@ class TestAddEntryModalDNSSupport:
|
|||
"""Test validation for DNS entry with missing DNS name."""
|
||||
# Mock the error display method
|
||||
self.modal._show_error = Mock()
|
||||
|
||||
|
||||
result = self.modal._validate_input(
|
||||
ip_address="",
|
||||
dns_name="",
|
||||
hostnames_str="example.com",
|
||||
is_dns_entry=True
|
||||
ip_address="", dns_name="", hostnames_str="example.com", is_dns_entry=True
|
||||
)
|
||||
assert result is False
|
||||
self.modal._show_error.assert_called_with("dns-error", "DNS name is required")
|
||||
|
|
@ -85,55 +79,58 @@ class TestAddEntryModalDNSSupport:
|
|||
"""Test validation for DNS entry with invalid DNS name format."""
|
||||
# Mock the error display method
|
||||
self.modal._show_error = Mock()
|
||||
|
||||
|
||||
# Test various invalid DNS name formats
|
||||
invalid_dns_names = [
|
||||
"example .com", # Contains space
|
||||
".example.com", # Starts with dot
|
||||
"example.com.", # Ends with dot
|
||||
"example..com", # Double dots
|
||||
"ex@mple.com", # Invalid characters
|
||||
"ex@mple.com", # Invalid characters
|
||||
]
|
||||
|
||||
|
||||
for invalid_dns in invalid_dns_names:
|
||||
result = self.modal._validate_input(
|
||||
ip_address="",
|
||||
dns_name=invalid_dns,
|
||||
hostnames_str="example.com",
|
||||
is_dns_entry=True
|
||||
is_dns_entry=True,
|
||||
)
|
||||
assert result is False
|
||||
self.modal._show_error.assert_called_with("dns-error", "Invalid DNS name format")
|
||||
self.modal._show_error.assert_called_with(
|
||||
"dns-error", "Invalid DNS name format"
|
||||
)
|
||||
|
||||
def test_validate_input_missing_hostnames(self):
|
||||
"""Test validation for entries with missing hostnames."""
|
||||
# Mock the error display method
|
||||
self.modal._show_error = Mock()
|
||||
|
||||
|
||||
# Test IP entry without hostnames
|
||||
result = self.modal._validate_input(
|
||||
ip_address="192.168.1.1",
|
||||
dns_name="",
|
||||
hostnames_str="",
|
||||
is_dns_entry=False
|
||||
ip_address="192.168.1.1", dns_name="", hostnames_str="", is_dns_entry=False
|
||||
)
|
||||
assert result is False
|
||||
self.modal._show_error.assert_called_with("hostnames-error", "At least one hostname is required")
|
||||
self.modal._show_error.assert_called_with(
|
||||
"hostnames-error", "At least one hostname is required"
|
||||
)
|
||||
|
||||
def test_validate_input_invalid_hostnames(self):
|
||||
"""Test validation for entries with invalid hostnames."""
|
||||
# Mock the error display method
|
||||
self.modal._show_error = Mock()
|
||||
|
||||
|
||||
# Test with invalid hostname containing spaces
|
||||
result = self.modal._validate_input(
|
||||
ip_address="192.168.1.1",
|
||||
dns_name="",
|
||||
hostnames_str="invalid hostname",
|
||||
is_dns_entry=False
|
||||
is_dns_entry=False,
|
||||
)
|
||||
assert result is False
|
||||
self.modal._show_error.assert_called_with("hostnames-error", "Invalid hostname format: invalid hostname")
|
||||
self.modal._show_error.assert_called_with(
|
||||
"hostnames-error", "Invalid hostname format: invalid hostname"
|
||||
)
|
||||
|
||||
def test_clear_errors_includes_dns_error(self):
|
||||
"""Test that clear_errors method includes DNS error clearing."""
|
||||
|
|
@ -141,7 +138,7 @@ class TestAddEntryModalDNSSupport:
|
|||
mock_ip_error = Mock(spec=Static)
|
||||
mock_dns_error = Mock(spec=Static)
|
||||
mock_hostnames_error = Mock(spec=Static)
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type):
|
||||
if selector == "#ip-error":
|
||||
return mock_ip_error
|
||||
|
|
@ -150,12 +147,12 @@ class TestAddEntryModalDNSSupport:
|
|||
elif selector == "#hostnames-error":
|
||||
return mock_hostnames_error
|
||||
return Mock()
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Call clear_errors
|
||||
self.modal._clear_errors()
|
||||
|
||||
|
||||
# Verify all error widgets were cleared
|
||||
mock_ip_error.update.assert_called_with("")
|
||||
mock_dns_error.update.assert_called_with("")
|
||||
|
|
@ -166,10 +163,10 @@ class TestAddEntryModalDNSSupport:
|
|||
# Mock the query_one method to return a mock widget
|
||||
mock_error_widget = Mock(spec=Static)
|
||||
self.modal.query_one = Mock(return_value=mock_error_widget)
|
||||
|
||||
|
||||
# Test showing an error
|
||||
self.modal._show_error("dns-error", "Test error message")
|
||||
|
||||
|
||||
# Verify the error widget was updated
|
||||
self.modal.query_one.assert_called_with("#dns-error", Static)
|
||||
mock_error_widget.update.assert_called_with("Test error message")
|
||||
|
|
@ -178,7 +175,7 @@ class TestAddEntryModalDNSSupport:
|
|||
"""Test that show_error handles missing widgets gracefully."""
|
||||
# Mock query_one to raise an exception
|
||||
self.modal.query_one = Mock(side_effect=Exception("Widget not found"))
|
||||
|
||||
|
||||
# This should not raise an exception
|
||||
try:
|
||||
self.modal._show_error("dns-error", "Test error message")
|
||||
|
|
@ -199,7 +196,7 @@ class TestAddEntryModalRadioButtonLogic:
|
|||
mock_ip_section = Mock()
|
||||
mock_dns_section = Mock()
|
||||
mock_ip_input = Mock(spec=Input)
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type=None):
|
||||
if selector == "#ip-section":
|
||||
return mock_ip_section
|
||||
|
|
@ -208,25 +205,25 @@ class TestAddEntryModalRadioButtonLogic:
|
|||
elif selector == "#ip-address-input":
|
||||
return mock_ip_input
|
||||
return Mock()
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Create mock event
|
||||
mock_radio = Mock()
|
||||
mock_radio.id = "ip-entry-radio"
|
||||
mock_radio_set = Mock()
|
||||
mock_radio_set.id = "entry-type-radio"
|
||||
|
||||
|
||||
class MockEvent:
|
||||
def __init__(self):
|
||||
self.radio_set = mock_radio_set
|
||||
self.pressed = mock_radio
|
||||
|
||||
|
||||
event = MockEvent()
|
||||
|
||||
|
||||
# Call the event handler
|
||||
self.modal.on_radio_set_changed(event)
|
||||
|
||||
|
||||
# Verify IP section is shown and DNS section is hidden
|
||||
mock_ip_section.remove_class.assert_called_with("hidden")
|
||||
mock_dns_section.add_class.assert_called_with("hidden")
|
||||
|
|
@ -238,7 +235,7 @@ class TestAddEntryModalRadioButtonLogic:
|
|||
mock_ip_section = Mock()
|
||||
mock_dns_section = Mock()
|
||||
mock_dns_input = Mock(spec=Input)
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type=None):
|
||||
if selector == "#ip-section":
|
||||
return mock_ip_section
|
||||
|
|
@ -247,25 +244,25 @@ class TestAddEntryModalRadioButtonLogic:
|
|||
elif selector == "#dns-name-input":
|
||||
return mock_dns_input
|
||||
return Mock()
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Create mock event
|
||||
mock_radio = Mock()
|
||||
mock_radio.id = "dns-entry-radio"
|
||||
mock_radio_set = Mock()
|
||||
mock_radio_set.id = "entry-type-radio"
|
||||
|
||||
|
||||
class MockEvent:
|
||||
def __init__(self):
|
||||
self.radio_set = mock_radio_set
|
||||
self.pressed = mock_radio
|
||||
|
||||
|
||||
event = MockEvent()
|
||||
|
||||
|
||||
# Call the event handler
|
||||
self.modal.on_radio_set_changed(event)
|
||||
|
||||
|
||||
# Verify DNS section is shown and IP section is hidden
|
||||
mock_ip_section.add_class.assert_called_with("hidden")
|
||||
mock_dns_section.remove_class.assert_called_with("hidden")
|
||||
|
|
@ -285,26 +282,26 @@ class TestAddEntryModalSaveLogic:
|
|||
self.modal._validate_input = Mock(return_value=True)
|
||||
self.modal._clear_errors = Mock()
|
||||
self.modal.dismiss = Mock()
|
||||
|
||||
|
||||
# Mock form widgets
|
||||
mock_radio_set = Mock(spec=RadioSet)
|
||||
mock_radio_set.pressed_button = None # IP entry mode
|
||||
|
||||
|
||||
mock_ip_input = Mock(spec=Input)
|
||||
mock_ip_input.value = "192.168.1.1"
|
||||
|
||||
|
||||
mock_dns_input = Mock(spec=Input)
|
||||
mock_dns_input.value = ""
|
||||
|
||||
|
||||
mock_hostnames_input = Mock(spec=Input)
|
||||
mock_hostnames_input.value = "example.com, www.example.com"
|
||||
|
||||
|
||||
mock_comment_input = Mock(spec=Input)
|
||||
mock_comment_input.value = "Test comment"
|
||||
|
||||
|
||||
mock_active_checkbox = Mock(spec=Checkbox)
|
||||
mock_active_checkbox.value = True
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type):
|
||||
if selector == "#entry-type-radio":
|
||||
return mock_radio_set
|
||||
|
|
@ -319,17 +316,17 @@ class TestAddEntryModalSaveLogic:
|
|||
elif selector == "#active-checkbox":
|
||||
return mock_active_checkbox
|
||||
return Mock()
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Call action_save
|
||||
self.modal.action_save()
|
||||
|
||||
|
||||
# Verify validation was called
|
||||
self.modal._validate_input.assert_called_once_with(
|
||||
"192.168.1.1", "", "example.com, www.example.com", None
|
||||
)
|
||||
|
||||
|
||||
# Verify modal was dismissed with a HostEntry
|
||||
self.modal.dismiss.assert_called_once()
|
||||
created_entry = self.modal.dismiss.call_args[0][0]
|
||||
|
|
@ -345,28 +342,28 @@ class TestAddEntryModalSaveLogic:
|
|||
self.modal._validate_input = Mock(return_value=True)
|
||||
self.modal._clear_errors = Mock()
|
||||
self.modal.dismiss = Mock()
|
||||
|
||||
|
||||
# Mock form widgets
|
||||
mock_radio_button = Mock()
|
||||
mock_radio_button.id = "dns-entry-radio"
|
||||
mock_radio_set = Mock(spec=RadioSet)
|
||||
mock_radio_set.pressed_button = mock_radio_button
|
||||
|
||||
|
||||
mock_ip_input = Mock(spec=Input)
|
||||
mock_ip_input.value = ""
|
||||
|
||||
|
||||
mock_dns_input = Mock(spec=Input)
|
||||
mock_dns_input.value = "example.com"
|
||||
|
||||
|
||||
mock_hostnames_input = Mock(spec=Input)
|
||||
mock_hostnames_input.value = "www.example.com"
|
||||
|
||||
|
||||
mock_comment_input = Mock(spec=Input)
|
||||
mock_comment_input.value = ""
|
||||
|
||||
|
||||
mock_active_checkbox = Mock(spec=Checkbox)
|
||||
mock_active_checkbox.value = True
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type):
|
||||
if selector == "#entry-type-radio":
|
||||
return mock_radio_set
|
||||
|
|
@ -381,23 +378,23 @@ class TestAddEntryModalSaveLogic:
|
|||
elif selector == "#active-checkbox":
|
||||
return mock_active_checkbox
|
||||
return Mock()
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Call action_save
|
||||
self.modal.action_save()
|
||||
|
||||
|
||||
# Verify validation was called
|
||||
self.modal._validate_input.assert_called_once_with(
|
||||
"", "example.com", "www.example.com", True
|
||||
)
|
||||
|
||||
|
||||
# Verify modal was dismissed with a DNS HostEntry
|
||||
self.modal.dismiss.assert_called_once()
|
||||
created_entry = self.modal.dismiss.call_args[0][0]
|
||||
assert isinstance(created_entry, HostEntry)
|
||||
assert created_entry.ip_address == "0.0.0.0" # Placeholder IP for DNS entries
|
||||
assert hasattr(created_entry, 'dns_name')
|
||||
assert hasattr(created_entry, "dns_name")
|
||||
assert created_entry.dns_name == "example.com"
|
||||
assert created_entry.hostnames == ["www.example.com"]
|
||||
assert created_entry.comment is None
|
||||
|
|
@ -409,21 +406,21 @@ class TestAddEntryModalSaveLogic:
|
|||
self.modal._validate_input = Mock(return_value=False)
|
||||
self.modal._clear_errors = Mock()
|
||||
self.modal.dismiss = Mock()
|
||||
|
||||
|
||||
# Mock form widgets (minimal setup since validation fails)
|
||||
mock_radio_set = Mock(spec=RadioSet)
|
||||
mock_radio_set.pressed_button = None
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type):
|
||||
if selector == "#entry-type-radio":
|
||||
return mock_radio_set
|
||||
return Mock(spec=Input, value="")
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Call action_save
|
||||
self.modal.action_save()
|
||||
|
||||
|
||||
# Verify validation was called and modal was not dismissed
|
||||
self.modal._validate_input.assert_called_once()
|
||||
self.modal.dismiss.assert_not_called()
|
||||
|
|
@ -434,30 +431,33 @@ class TestAddEntryModalSaveLogic:
|
|||
self.modal._validate_input = Mock(return_value=True)
|
||||
self.modal._clear_errors = Mock()
|
||||
self.modal._show_error = Mock()
|
||||
|
||||
|
||||
# Mock form widgets
|
||||
mock_radio_set = Mock(spec=RadioSet)
|
||||
mock_radio_set.pressed_button = None
|
||||
|
||||
|
||||
mock_input = Mock(spec=Input)
|
||||
mock_input.value = "invalid"
|
||||
|
||||
|
||||
def mock_query_one(selector, widget_type):
|
||||
if selector == "#entry-type-radio":
|
||||
return mock_radio_set
|
||||
return mock_input
|
||||
|
||||
|
||||
self.modal.query_one = Mock(side_effect=mock_query_one)
|
||||
|
||||
|
||||
# Mock HostEntry to raise ValueError
|
||||
with pytest.MonkeyPatch.context() as m:
|
||||
|
||||
def mock_host_entry(*args, **kwargs):
|
||||
raise ValueError("Invalid IP address")
|
||||
|
||||
|
||||
m.setattr("src.hosts.tui.add_entry_modal.HostEntry", mock_host_entry)
|
||||
|
||||
|
||||
# Call action_save
|
||||
self.modal.action_save()
|
||||
|
||||
|
||||
# Verify error was shown
|
||||
self.modal._show_error.assert_called_once_with("hostnames-error", "Invalid IP address")
|
||||
self.modal._show_error.assert_called_once_with(
|
||||
"hostnames-error", "Invalid IP address"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue