Skip to content

Commit 7be9d1d

Browse files
committed
dev: add unit tests
1 parent e3f0734 commit 7be9d1d

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ docs = [
5050
metacheck = { git = "https://github.com/SoftwareUnderstanding/RsMetaCheck"}
5151

5252

53+
[tool.pytest.ini_options]
54+
testpaths = ["tests"]
55+
python_files = ["test_*.py"]
56+
python_classes = ["Test*"]
57+
python_functions = ["test_*"]
58+
addopts = "-v --tb=short"
59+
5360
[tool.bandit]
5461
exclude_dirs = ["tests",".venv", ".ruff-cache"]
5562
tests = ["B201", "B301"]

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Tests for conftest module - shared pytest fixtures."""
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def tmp_config_file(tmp_path):
8+
"""Create a temporary config file for testing."""
9+
config_file = tmp_path / "config.json"
10+
config_file.write_text('{"test": "value"}')
11+
return config_file

tests/test_init.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Tests for __init__ module."""
2+
3+
from sw_metadata_bot import __version__
4+
5+
6+
def test_version_is_set():
7+
"""Test that __version__ is set."""
8+
assert __version__ is not None
9+
assert isinstance(__version__, str)
10+
11+
12+
def test_version_format():
13+
"""Test that version follows expected format."""
14+
# Version could be 'unknown' during development
15+
# or a proper version string like 0.1.0
16+
assert __version__ in ("unknown",) or "." in __version__

tests/test_pitfalls.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)