Skip to content

Add unit test infrastructure with WebSocket mocking #92

@ajshedivy

Description

@ajshedivy

Summary

All 107 existing tests are integration tests that require a live IBM i server connection. There are zero unit tests and zero mocks. This means:

  • Tests cannot run in standard CI/CD without server access
  • Tests cannot run offline during local development
  • Error recovery paths and edge cases are untestable
  • The test suite is slow (network I/O for every test)
  • 7 pool tests are commented out due to job status tracking issues that could be debugged with mocks

Current State

  • 107 active tests, all integration (require VITE_SERVER, VITE_DB_USER, VITE_DB_PASS)
  • No conftest.py — no shared fixtures
  • No mocking of WebSocket connections, server responses, or authentication
  • Empty test subdirectories (tests/unit/, tests/integration/, etc.) — a planned reorg that was never completed
  • 7 commented-out tests in pooling_test.py and pep249_test.py due to job status tracking issues
  • pytest-cov installed but not used — no coverage metrics collected
  • Estimated coverage: ~55-65% overall, ~35% for error handling paths

Proposed Changes

1. Create shared test fixtures (tests/conftest.py)

  • Mock WebSocket connection fixture that returns configurable JSON responses
  • Mock DaemonServer credentials fixture
  • Factory functions for QueryResult, ConnectionResult, and other server response types
  • Fixtures that configure the mock to simulate error conditions

2. Create unit tests for core components

Priority modules to test in isolation:

Module What to test
Query / PoolQuery State machine transitions (NOT_YET_RUN -> RUN_MORE_DATA_AVAIL -> RUN_DONE -> ERROR)
Pool Scaling logic (add/remove jobs), job selection algorithm, pool exhaustion
DaemonServer Config parsing from dicts, INI files, kwargs; validation of required fields
core/exceptions.py Error classification mapping (RuntimeError -> PEP 249 exceptions)
core/cursor.py Cursor lifecycle, fetchone/fetchall behavior, closed cursor detection
core/connection.py Connection state management, cursor creation
base_job.py _parse_connection_input() for all input types
ssl.py Certificate retrieval and SSL context creation

3. Fix commented-out tests

Investigate and fix the 7 commented-out tests:

  • test_pop_job_with_pool_ignore — JobStatus tracking
  • test_pool_with_no_space_no_ready_job... — JobStatus tracking
  • test_pool_with_space_but_no_ready_job... — JobStatus tracking
  • test_freeist_job_is_returned — JobStatus tracking
  • test_pep249_execute_many — Duplicate of working test
  • test_prepare_statement_mult_params_seq_tuple — Tuple parameter support
  • test_prepare_statement_mult_params_seq_tuple_opts — Tuple parameter support

4. Add coverage reporting

  • Configure .coveragerc or pyproject.toml coverage settings
  • Add --cov=mapepire_python --cov-report=term-missing to default pytest invocation
  • Set a coverage floor (e.g., 70%) as a CI gate

Files to Create/Modify

  • New: tests/conftest.py — Shared fixtures and mock WebSocket
  • New: tests/unit/test_query_state.py — Query state machine tests
  • New: tests/unit/test_pool_scaling.py — Pool logic tests
  • New: tests/unit/test_config_parsing.py — DaemonServer/config tests
  • New: tests/unit/test_error_classification.py — Exception mapping tests
  • New: tests/unit/test_cursor.py — Cursor lifecycle tests
  • Modify: tests/pooling_test.py — Fix commented-out tests
  • Modify: tests/pep249_test.py — Fix commented-out tests
  • Modify: pyproject.toml — Add coverage configuration

Acceptance Criteria

  • conftest.py provides reusable mock WebSocket fixtures
  • At least 15 new unit tests covering the modules listed above
  • Unit tests run without any server connection (pytest tests/unit/)
  • All 7 commented-out tests are either fixed or replaced with equivalent coverage
  • Coverage reporting is configured and runs with pytest --cov
  • Unit tests pass in CI without IBM i server credentials

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions