Add a Behave feature with four scenarios that capture the session leak
bug in AutomationProfileRepository. The upsert() and delete() methods
commit when auto_commit is True but never call session.close() in a
finally block, leaking database sessions. SessionRepository correctly
uses finally: if self._auto_commit: db_session.close() in every method
— AutomationProfileRepository does not.
The test uses a _TrackingSession subclass of sqlalchemy.orm.Session that
records whether close() was called, providing direct assertion of the bug.
Four scenarios cover: (1) upsert success path, (2) delete success path,
(3) upsert error path, (4) delete error path. All scenarios are tagged
@tdd_expected_fail @tdd_bug @tdd_bug_987 so CI passes while the bug
remains unfixed.
Fixes from review:
- M1: Added 4th scenario for delete() error path
- M2: Registered engine.dispose() in _cleanup_handlers for all scenarios
- M3: Registered session.close() in _cleanup_handlers; explicitly close
setup session in step_given_persisted_profile
- M4: Replaced type: ignore annotations with proper subclass pattern
(_TrackingSession uses **kwargs: Any; _FailingFlushTrackingSession
overrides flush() instead of monkey-patching)
- M5: Moved OperationalError import from function body to module level
Robot integration tests are N/A — this is purely a unit-level session
lifecycle bug within a single repository class.
ISSUES CLOSED: #1092