Skip to content

Daily Test Coverage Improver - Fix Python test infrastructure and enable 88% coverage#247

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test/fix-python-test-imports-and-module-structure-be34cb51a4aa9cc0
Draft

Daily Test Coverage Improver - Fix Python test infrastructure and enable 88% coverage#247
github-actions[bot] wants to merge 1 commit intomainfrom
test/fix-python-test-imports-and-module-structure-be34cb51a4aa9cc0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

Goal and Rationale

Target: Python test infrastructure (Priority 2 from research phase)

This PR fixes the broken Python tests identified in Phase 1 research. The tests were failing due to:

  1. Missing __init__.py files preventing module imports
  2. Test files importing non-existent classes and functions
  3. Incorrect FastAPI test setup

This was a critical blocker preventing any Python test execution and coverage measurement.

Approach

Module Structure Setup

  1. Added __init__.py files to make Python packages importable:
    • server/__init__.py
    • backend/__init__.py
    • backend/api/__init__.py
    • tests/__init__.py
    • tests/unit/__init__.py
    • tests/integration/__init__.py

Test Fixes

  1. test_contact_handler.py: Complete rewrite to match actual implementation

    • Fixed imports to use ContactFormHandler (not ContactHandler)
    • Added comprehensive tests for all classes: ContactFormData, AuditLogger, DataRetentionManager
    • Added tests for utility functions: hash_ip_address, sanitize_user_agent, validate_csrf_token
    • Added tests for GDPR functions: handle_data_export_request, handle_data_deletion_request
    • Added tests for ContactFormHandler with rate limiting, CSRF validation, consent checks
  2. test_api_endpoints.py: Fixed FastAPI test setup

    • Created FastAPI app before using TestClient (was trying to test router directly)
    • Both GET and POST endpoint tests now pass
  3. test_validation.py: Removed

    • Was trying to import Python modules from src/utils/validation which are actually TypeScript

Impact Measurement

Before changes:

ERROR tests/test_contact_handler.py - ImportError: cannot import name 'ContactHandler'
ERROR tests/unit/test_validation.py - ModuleNotFoundError: No module named 'src.utils.validation'
!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!
```

**After changes**:
```
============================== 28 passed in 0.40s ==============================

Name                        Stmts   Miss  Cover
-----------------------------------------------
backend/__init__.py             0      0   100%
backend/api/__init__.py         0      0   100%
backend/api/users.py           11      1    91%
server/__init__.py              0      0   100%
server/contact_handler.py     127     15    88%
-----------------------------------------------
TOTAL                         138     16    88%

Test Coverage Results

Metric Before After Change
Python tests passing 0 28 +28
Python test collection errors 2 0 -2
Python code coverage 0% (unable to run) 88% +88%
Lines covered 0 122 +122

Coverage by Module

Module Statements Missed Coverage
backend/api/users.py 11 1 91%
server/contact_handler.py 127 15 88%
Total 138 16 88%

Trade-offs

Complexity: Added minimal boilerplate (__init__.py files) necessary for Python packaging
Maintainability: Tests now accurately reflect actual implementation, making them sustainable
Coverage gaps: 16 statements in server/contact_handler.py remain untested (mostly error paths and placeholder implementations)

Validation

Test Execution

All 28 tests pass successfully:

  • 26 tests for contact_handler.py covering validation, security, GDPR compliance
  • 2 tests for backend/api/users.py covering GET and POST endpoints

Test Categories

✅ ContactFormData validation (5 tests)
✅ Audit logging (1 test)
✅ Data retention policies (5 tests)
✅ Utility functions (6 tests)
✅ Contact form processing (6 tests)
✅ GDPR compliance (2 tests)
✅ API endpoints (2 tests)

Reproducibility

Setup

# Install test dependencies (if not already installed)
pip install pytest pytest-cov fastapi httpx

# Or use the Python from the coverage-steps action
/opt/hostedtoolcache/Python/3.11.14/x64/bin/pip install pytest pytest-cov

Run Tests

# Run all Python tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=server --cov=backend --cov-report=term --cov-report=json

# Expected output: 28 passed, 88% coverage
``````

### Measurement Results
``````
TOTAL                         138     16    88%
Coverage JSON written to file coverage-python.json
============================== 28 passed in 0.67s ==============================

Future Work

Additional coverage opportunities identified:

  1. Error paths in ContactFormHandler.process_submission (exception handling branches)
  2. Store contact data - currently placeholder implementation
  3. Additional security tests - SQL injection patterns, more XSS variants
  4. Backend API expansion - test error responses, validation edge cases
  5. Integration tests - test full contact form submission workflow

AI generated by Daily Test Coverage Improver

…t imports

- Add __init__.py to server/, backend/, backend/api/ to make them proper Python packages
- Add __init__.py to tests/, tests/unit/, tests/integration/ for test discovery
- Fix test_contact_handler.py to import correct classes and functions
- Rewrite test_contact_handler.py with comprehensive tests for actual implementation
- Fix test_api_endpoints.py to create FastAPI app before using TestClient
- Remove test_validation.py which tried to import non-existent Python modules

All 28 Python tests now pass with 88% coverage (138 statements, 16 missed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants