Skip to content

Commit d95ad13

Browse files
authored
Merge pull request #2291 from compdemocracy/te-delphi-tests-2
add more tests
2 parents 780f129 + 769c4e6 commit d95ad13

File tree

9 files changed

+809
-299
lines changed

9 files changed

+809
-299
lines changed

.github/workflows/python-ci.yml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ jobs:
6666
echo "Copying test files into container..."
6767
docker compose -f docker-compose.test.yml cp delphi/tests delphi:/app/tests
6868
docker compose -f docker-compose.test.yml cp delphi/real_data delphi:/app/real_data
69+
echo "Copying coverage script into container..."
70+
docker compose -f docker-compose.test.yml cp delphi/generate_coverage_md.py delphi:/app/generate_coverage_md.py
71+
echo "Copying script to be tested into container..."
72+
docker compose -f docker-compose.test.yml cp delphi/polismath/run_math_pipeline.py delphi:/app/run_math_pipeline.py
6973
70-
echo "Running tests..."
74+
echo "Running tests and generating coverage report..."
7175
docker compose \
7276
-f docker-compose.test.yml \
7377
--env-file .env \
@@ -76,15 +80,44 @@ jobs:
7680
-e AWS_REGION=us-east-1 \
7781
-e AWS_ACCESS_KEY_ID=dummy \
7882
-e AWS_SECRET_ACCESS_KEY=dummy \
83+
-e POSTGRES_HOST=postgres \
84+
-e POSTGRES_PASSWORD=PdwPNS2mDN73Vfbc \
85+
-e POSTGRES_DB=polis-test \
7986
delphi \
8087
bash -c " \
88+
set -e; \
8189
echo '--- Setting up DynamoDB Tables ---'; \
8290
python create_dynamodb_tables.py --region us-east-1; \
8391
echo '--- Running Pytest ---'; \
84-
pytest /app/tests \
92+
export PYTHONPATH=\$PYTHONPATH:/app; \
93+
pytest --cov=polismath --cov=run_math_pipeline --cov-report=xml:/app/coverage.xml /app/tests --ignore=/app/tests/test_pakistan_conversation.py
94+
echo '--- Generating Coverage Comment Text ---'; \
95+
python /app/generate_coverage_md.py > /app/coverage-comment.md \
8596
"
8697
87-
- name: 7. Show service logs on failure
98+
- name: 7. Copy coverage report from container
99+
if: success()
100+
run: |
101+
echo "Copying coverage-comment.md from delphi container..."
102+
docker compose -f docker-compose.test.yml cp delphi:/app/coverage-comment.md .
103+
104+
- name: 8. Post Coverage Comment
105+
if: success() && github.event_name == 'pull_request'
106+
uses: actions/github-script@v6
107+
with:
108+
github-token: ${{ secrets.GITHUB_TOKEN }}
109+
script: |
110+
const fs = require('fs');
111+
const commentBody = fs.readFileSync('coverage-comment.md', 'utf8');
112+
113+
await github.rest.issues.createComment({
114+
issue_number: context.issue.number,
115+
owner: context.repo.owner,
116+
repo: context.repo.repo,
117+
body: "## Coverage Report\n\n" + commentBody
118+
});
119+
120+
- name: 9. Show service logs on failure
88121
if: failure()
89122
run: |
90123
echo "=== Delphi service logs ==="
@@ -94,7 +127,7 @@ jobs:
94127
echo "=== DynamoDB service logs ==="
95128
docker compose -f docker-compose.test.yml logs dynamodb || true
96129
97-
- name: 8. Clean up services
130+
- name: 10. Clean up services
98131
if: always()
99132
run: |
100133
echo "Cleaning up services..."

delphi/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ clean: ## Clean build artifacts
6060
rm -rf build/
6161
rm -rf dist/
6262
rm -rf htmlcov/
63-
rm -f coverage.xml
64-
rm -f .coverage
6563
rm -f bandit-report.json
6664

6765
# Build

delphi/generate_coverage_md.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
3+
import xml.etree.ElementTree as ET
4+
import sys
5+
6+
def main():
7+
"""
8+
Parses a coverage.xml file and prints a Markdown table to stdout.
9+
"""
10+
try:
11+
tree = ET.parse('/app/coverage.xml')
12+
root = tree.getroot()
13+
14+
lines = [
15+
"| File | Stmts | Miss | Cover |",
16+
"|------|-------|------|-------|"
17+
]
18+
19+
total_statements = 0
20+
total_missed = 0
21+
22+
packages = root.find('packages')
23+
if packages is None:
24+
print("Could not find 'packages' tag in coverage.xml", file=sys.stderr)
25+
sys.exit(1)
26+
27+
all_files = []
28+
for package in packages.findall('package'):
29+
classes = package.find('classes')
30+
if classes is None:
31+
continue
32+
33+
for cls in classes.findall('class'):
34+
filename = cls.get('filename') # This is the path
35+
line_rate = float(cls.get('line-rate', '0'))
36+
37+
lines_node = cls.find('lines')
38+
if lines_node is None:
39+
continue
40+
41+
lines_valid = 0
42+
lines_covered = 0
43+
44+
for line in lines_node.findall('line'):
45+
lines_valid += 1
46+
if int(line.get('hits', '0')) > 0:
47+
lines_covered += 1
48+
49+
if lines_valid == 0:
50+
continue # Skip files with no executable statements
51+
52+
lines_missed = lines_valid - lines_covered
53+
total_statements += lines_valid
54+
total_missed += lines_missed
55+
56+
coverage_percent = line_rate * 100
57+
all_files.append((filename, lines_valid, lines_missed, coverage_percent))
58+
59+
# Sort files alphabetically
60+
all_files.sort(key=lambda x: x[0])
61+
62+
for f in all_files:
63+
lines.append(f"| {f[0]} | {f[1]} | {f[2]} | {f[3]:.0f}% |")
64+
65+
if total_statements > 0:
66+
total_coverage = ((total_statements - total_missed) / total_statements) * 100
67+
lines.append(f"| **Total** | **{total_statements}** | **{total_missed}** | **{total_coverage:.0f}%** |")
68+
else:
69+
lines.append("| **Total** | **0** | **0** | **N/A** |")
70+
71+
# Print to stdout
72+
print('\n'.join(lines))
73+
74+
except Exception as e:
75+
print(f"Error parsing coverage.xml: {e}", file=sys.stderr)
76+
print(f"Error generating coverage report: {e}")
77+
sys.exit(1)
78+
79+
if __name__ == "__main__":
80+
main()

delphi/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ filterwarnings = [
121121
# Ignore python_multipart deprecation warning from ddtrace (third-party)
122122
"ignore:Please use `import python_multipart`:PendingDeprecationWarning:ddtrace.internal.module",
123123
]
124+
125+
[tool.coverage.run]
126+
relative_files = true

delphi/real_data/r4tykwac8thvzv35jrn53/python_output/conversation_result.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"last_updated": 1762912986405,
2+
"last_updated": 1763157841353,
33
"participant_count": 536,
44
"comment_count": 314,
55
"vote_stats": {
@@ -19562,8 +19562,8 @@
1956219562
}
1956319563
},
1956419564
"zid": "biodiversity",
19565-
"lastVoteTimestamp": 1762912986405,
19566-
"lastModTimestamp": 1762912986405,
19565+
"lastVoteTimestamp": 1763157841353,
19566+
"lastModTimestamp": 1763157841353,
1956719567
"tids": [
1956819568
0,
1956919569
1,
@@ -26469,5 +26469,5 @@
2646926469
"disagree": [],
2647026470
"comment-stats": {}
2647126471
},
26472-
"math_tick": 27987
26472+
"math_tick": 32844
2647326473
}

0 commit comments

Comments
 (0)