Skip to content

TASK-17: Add backend test suite with pytest - #24

Open
z4ab wants to merge 8 commits into
devfrom
task-17-backend-tests
Open

TASK-17: Add backend test suite with pytest#24
z4ab wants to merge 8 commits into
devfrom
task-17-backend-tests

Conversation

@z4ab

@z4ab z4ab commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Closes #20

What

Adds a comprehensive pytest test suite for the backend: unit tests for utility functions and integration tests for all API endpoints.

Changes

File Description
backend/pytest.ini Pytest config with asyncio_mode = auto
backend/tests/conftest.py Session-scoped DB pool, per-test table truncation, sample seed data (6 projects across 5 cities), ASGI httpx client
backend/tests/test_queries.py 18 unit tests for haversine distance, bounding box, zoom-to-grid
backend/tests/test_api.py 17 integration tests covering health, list/filter/search projects, single project, submit, stats
backend/requirements.txt Added pytest, pytest-asyncio, asgi-lifespan
db/setup_test_db.sh Script to create and initialize the constructnear_test database
db/Makefile Added test-db target

How to Run

# 1. Create test database (PostgreSQL must be running)
make -C db test-db

# 2. Run tests
pytest backend/tests -v

Or with custom DB URL:

DATABASE_URL_TEST=postgres://postgres:postgres@localhost:5432/constructnear_test pytest backend/tests -v

Test Coverage

Unit tests (18):

  • Haversine: same point, NYC↔LA, NYC↔London, commutativity, antipodal, small distance
  • Bounding box: NYC 5km, zero radius, large radius, ordering
  • Zoom-to-grid: all tiers (8→18+), None, monotonic property

Integration tests (17):

  • Health check
  • List all, filter by type, filter by status
  • Spatial search (within radius, exclude far-away)
  • Pagination (limit/offset, disjoint pages)
  • Clustering mode
  • Empty results, invalid params (422)
  • Get by ID, get nonexistent (404)
  • Submit valid/minimal/invalid (422)
  • Stats structure, counts, grouping

Verification

  • ruff check . — passes
  • ruff format --check . — passes

- pytest config with asyncio_mode=auto
- conftest with session-scoped DB pool, per-test table truncation, seed data fixture, and ASGI httpx client
- test_queries.py: unit tests for haversine, bounding box, zoom-to-grid
- test_api.py: integration tests for all API endpoints (health, projects, submit, stats)
- db/setup_test_db.sh script to create test database
- db/Makefile with test-db target
- Test dependencies added to requirements.txt
@z4ab
z4ab force-pushed the task-17-backend-tests branch from 90c4a4c to 0e7a857 Compare June 17, 2026 02:12
z4ab added 7 commits June 16, 2026 22:16
- Add repo root to sys.path in conftest.py so backend module resolves
- Run ruff format on pipeline/status_inference.py
- Move MapView helper functions outside component to fix lint rules
- Replace 'as any' casts with proper types in MapView
_SCHEMA_PATH was computing path relative to conftest.py but
only going 2 dirs up (to backend/) instead of 3 (to repo root).
Now uses _REPO_ROOT which is already correct.
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.

1 participant