|
| 1 | +"""Tests for pitfalls module.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import tempfile |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from sw_metadata_bot.pitfalls import ( |
| 10 | + create_issue_body, |
| 11 | + format_report, |
| 12 | + get_pitfalls_list, |
| 13 | + get_repository_url, |
| 14 | + get_warnings_list, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def sample_data(): |
| 20 | + """Provide sample pitfalls data for testing.""" |
| 21 | + return { |
| 22 | + "assessedSoftware": {"url": "https://github.com/example/repo"}, |
| 23 | + "checks": [ |
| 24 | + { |
| 25 | + "checkId": "P001", |
| 26 | + "process": "Pitfall process description", |
| 27 | + "evidence": "Evidence of pitfall", |
| 28 | + "suggestion": "How to fix this", |
| 29 | + }, |
| 30 | + { |
| 31 | + "checkId": "P002", |
| 32 | + "process": "Another pitfall", |
| 33 | + "evidence": "More evidence", |
| 34 | + }, |
| 35 | + { |
| 36 | + "checkId": "W001", |
| 37 | + "evidence": "Warning evidence", |
| 38 | + "suggestion": "Warning suggestion", |
| 39 | + }, |
| 40 | + { |
| 41 | + "checkId": "W002", |
| 42 | + "evidence": "Another warning", |
| 43 | + }, |
| 44 | + ], |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def temp_jsonld_file(sample_data): |
| 50 | + """Create a temporary JSON-LD file for testing.""" |
| 51 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".jsonld", delete=False) as f: |
| 52 | + json.dump(sample_data, f) |
| 53 | + temp_path = Path(f.name) |
| 54 | + yield temp_path |
| 55 | + temp_path.unlink() |
| 56 | + |
| 57 | + |
| 58 | +def test_get_repository_url(sample_data): |
| 59 | + """Test extracting repository URL from data.""" |
| 60 | + url = get_repository_url(sample_data) |
| 61 | + assert url == "https://github.com/example/repo" |
| 62 | + |
| 63 | + |
| 64 | +def test_get_repository_url_empty(): |
| 65 | + """Test extracting URL from empty data.""" |
| 66 | + url = get_repository_url({}) |
| 67 | + assert url == "" |
| 68 | + |
| 69 | + |
| 70 | +def test_get_pitfalls_list(sample_data): |
| 71 | + """Test filtering pitfalls from checks.""" |
| 72 | + pitfalls = get_pitfalls_list(sample_data) |
| 73 | + assert len(pitfalls) == 2 |
| 74 | + assert pitfalls[0]["checkId"] == "P001" |
| 75 | + assert pitfalls[1]["checkId"] == "P002" |
| 76 | + |
| 77 | + |
| 78 | +def test_get_warnings_list(sample_data): |
| 79 | + """Test filtering warnings from checks.""" |
| 80 | + warnings = get_warnings_list(sample_data) |
| 81 | + assert len(warnings) == 2 |
| 82 | + assert warnings[0]["checkId"] == "W001" |
| 83 | + assert warnings[1]["checkId"] == "W002" |
| 84 | + |
| 85 | + |
| 86 | +def test_get_pitfalls_list_empty(): |
| 87 | + """Test pitfalls extraction from empty data.""" |
| 88 | + pitfalls = get_pitfalls_list({}) |
| 89 | + assert pitfalls == [] |
| 90 | + |
| 91 | + |
| 92 | +def test_format_report(sample_data): |
| 93 | + """Test report formatting.""" |
| 94 | + report = format_report("https://github.com/example/repo", sample_data) |
| 95 | + |
| 96 | + assert "# Metadata Quality Report" in report |
| 97 | + assert "https://github.com/example/repo" in report |
| 98 | + assert "Pitfalls" in report |
| 99 | + assert "Warnings" in report |
| 100 | + assert "P001" in report |
| 101 | + assert "P002" in report |
| 102 | + assert "W001" in report |
| 103 | + assert "W002" in report |
| 104 | + |
| 105 | + |
| 106 | +def test_format_report_with_suggestions(sample_data): |
| 107 | + """Test that suggestions are included in report.""" |
| 108 | + report = format_report("https://github.com/example/repo", sample_data) |
| 109 | + |
| 110 | + assert "How to fix this" in report |
| 111 | + assert "Warning suggestion" in report |
| 112 | + |
| 113 | + |
| 114 | +def test_format_report_no_pitfalls(): |
| 115 | + """Test report with no pitfalls.""" |
| 116 | + data = { |
| 117 | + "assessedSoftware": {"url": "https://github.com/test/repo"}, |
| 118 | + "checks": [{"checkId": "W001", "evidence": "A warning"}], |
| 119 | + } |
| 120 | + report = format_report("https://github.com/test/repo", data) |
| 121 | + |
| 122 | + assert "🔴 Pitfalls (0)" not in report |
| 123 | + assert "⚠️ Warnings (1)" in report |
| 124 | + |
| 125 | + |
| 126 | +def test_create_issue_body(sample_data): |
| 127 | + """Test issue body creation.""" |
| 128 | + report = format_report("https://github.com/example/repo", sample_data) |
| 129 | + issue_body = create_issue_body(report) |
| 130 | + |
| 131 | + assert "CodeMetaSoft" in issue_body |
| 132 | + assert "sw-metadata-bot" in issue_body |
| 133 | + assert report in issue_body |
| 134 | + assert "unsubscribe" in issue_body |
0 commit comments