|
| 1 | +""" |
| 2 | +Unit tests for ErrorFormatter class. |
| 3 | +
|
| 4 | +Tests error and warning formatting utilities for CLI display. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +from io import StringIO |
| 9 | + |
| 10 | +from rich.console import Console |
| 11 | + |
| 12 | +from mci.utils.error_formatter import ( |
| 13 | + ErrorFormatter, |
| 14 | + ValidationError, |
| 15 | + ValidationWarning, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def formatter(): |
| 21 | + """Create an ErrorFormatter with StringIO console for testing.""" |
| 22 | + console = Console(file=StringIO(), force_terminal=True) |
| 23 | + return ErrorFormatter(console) |
| 24 | + |
| 25 | + |
| 26 | +def test_format_validation_errors_with_single_error(formatter): |
| 27 | + """Test formatting a single validation error.""" |
| 28 | + errors = [ValidationError(message="Missing required field: name")] |
| 29 | + formatter.format_validation_errors(errors) |
| 30 | + |
| 31 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 32 | + assert "❌ Validation Errors" in output |
| 33 | + assert "Missing required field: name" in output |
| 34 | + assert "Schema Validation Failed" in output |
| 35 | + |
| 36 | + |
| 37 | +def test_format_validation_errors_with_multiple_errors(formatter): |
| 38 | + """Test formatting multiple validation errors.""" |
| 39 | + errors = [ |
| 40 | + ValidationError(message="Missing required field: name"), |
| 41 | + ValidationError(message="Invalid type for field: age", location="tools[0]"), |
| 42 | + ] |
| 43 | + formatter.format_validation_errors(errors) |
| 44 | + |
| 45 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 46 | + assert "❌ Validation Errors" in output |
| 47 | + assert "Missing required field: name" in output |
| 48 | + assert "Invalid type for field: age" in output |
| 49 | + assert "[tools[0]]" in output |
| 50 | + |
| 51 | + |
| 52 | +def test_format_validation_errors_with_empty_list(formatter): |
| 53 | + """Test formatting with empty error list (should not output anything).""" |
| 54 | + formatter.format_validation_errors([]) |
| 55 | + |
| 56 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 57 | + assert output == "" |
| 58 | + |
| 59 | + |
| 60 | +def test_format_validation_warnings_with_single_warning(formatter): |
| 61 | + """Test formatting a single validation warning.""" |
| 62 | + warnings = [ |
| 63 | + ValidationWarning( |
| 64 | + message="Toolset file not found: weather.mci.json", |
| 65 | + suggestion="Create the file or update your schema", |
| 66 | + ) |
| 67 | + ] |
| 68 | + formatter.format_validation_warnings(warnings) |
| 69 | + |
| 70 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 71 | + assert "⚠️ Validation Warnings" in output |
| 72 | + assert "Toolset file not found" in output |
| 73 | + assert "💡 Create the file or update your schema" in output |
| 74 | + |
| 75 | + |
| 76 | +def test_format_validation_warnings_with_no_suggestion(formatter): |
| 77 | + """Test formatting a warning without suggestion.""" |
| 78 | + warnings = [ValidationWarning(message="Potential issue detected")] |
| 79 | + formatter.format_validation_warnings(warnings) |
| 80 | + |
| 81 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 82 | + assert "⚠️ Validation Warnings" in output |
| 83 | + assert "Potential issue detected" in output |
| 84 | + |
| 85 | + |
| 86 | +def test_format_validation_warnings_with_empty_list(formatter): |
| 87 | + """Test formatting with empty warning list (should not output anything).""" |
| 88 | + formatter.format_validation_warnings([]) |
| 89 | + |
| 90 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 91 | + assert output == "" |
| 92 | + |
| 93 | + |
| 94 | +def test_format_validation_success(formatter): |
| 95 | + """Test formatting validation success message.""" |
| 96 | + formatter.format_validation_success("mci.json") |
| 97 | + |
| 98 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 99 | + assert "✅ Schema is valid!" in output |
| 100 | + assert "File: mci.json" in output |
| 101 | + assert "Validation Successful" in output |
| 102 | + |
| 103 | + |
| 104 | +def test_format_mci_error(formatter): |
| 105 | + """Test formatting an MCI error message.""" |
| 106 | + formatter.format_mci_error("Failed to load schema: Invalid JSON") |
| 107 | + |
| 108 | + output = formatter.console.file.getvalue() # type: ignore[attr-defined] |
| 109 | + assert "❌ MCI Error" in output |
| 110 | + assert "Failed to load schema: Invalid JSON" in output |
| 111 | + |
| 112 | + |
| 113 | +def test_error_formatter_default_console(): |
| 114 | + """Test ErrorFormatter with default console.""" |
| 115 | + formatter = ErrorFormatter() |
| 116 | + assert formatter.console is not None |
| 117 | + |
| 118 | + # Should not raise any errors |
| 119 | + formatter.format_validation_success("test.json") |
| 120 | + |
| 121 | + |
| 122 | +def test_validation_error_with_location(): |
| 123 | + """Test ValidationError with location.""" |
| 124 | + error = ValidationError(message="Test error", location="tools[0].name") |
| 125 | + assert error.message == "Test error" |
| 126 | + assert error.location == "tools[0].name" |
| 127 | + |
| 128 | + |
| 129 | +def test_validation_error_without_location(): |
| 130 | + """Test ValidationError without location.""" |
| 131 | + error = ValidationError(message="Test error") |
| 132 | + assert error.message == "Test error" |
| 133 | + assert error.location is None |
| 134 | + |
| 135 | + |
| 136 | +def test_validation_warning_with_suggestion(): |
| 137 | + """Test ValidationWarning with suggestion.""" |
| 138 | + warning = ValidationWarning(message="Test warning", suggestion="Fix it") |
| 139 | + assert warning.message == "Test warning" |
| 140 | + assert warning.suggestion == "Fix it" |
| 141 | + |
| 142 | + |
| 143 | +def test_validation_warning_without_suggestion(): |
| 144 | + """Test ValidationWarning without suggestion.""" |
| 145 | + warning = ValidationWarning(message="Test warning") |
| 146 | + assert warning.message == "Test warning" |
| 147 | + assert warning.suggestion is None |
0 commit comments