Repository navigation
refactor(api): use caller session for dataset-deleted cleanup - #45841
ojasarora77 wants to merge 2 commits into
Conversation
Pass the delete transaction into clean_when_dataset_deleted instead of opening the global db.session for doc-form lookup.
Pyrefly Type Coverage
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #45841 +/- ##
==========================================
+ Coverage 89.72% 90.45% +0.73%
==========================================
Files 6562 6569 +7
Lines 409450 409745 +295
Branches 82067 82077 +10
==========================================
+ Hits 367393 370650 +3257
+ Misses 35602 32709 -2893
+ Partials 6455 6386 -69
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
asukaminato0721
left a comment
There was a problem hiding this comment.
2 inline suggestions at 1cbea62f62, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.
dify-codereview
| with patch("events.event_handlers.clean_when_dataset_deleted.clean_dataset_task.delay") as delay: | ||
| handle(dataset, session=sqlite_session) | ||
|
|
||
| delay.assert_called_once_with( |
There was a problem hiding this comment.
Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.
dify-codereview · tests-behavior-not-calls Reply here if this is wrong.
| assert result is True | ||
| assert db_session_with_containers.get(Dataset, dataset.id) is None | ||
| dataset_deleted_signal.send.assert_called_once_with(dataset) | ||
| dataset_deleted_signal.send.assert_called_once_with(dataset, session=db_session_with_containers) |
There was a problem hiding this comment.
Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.
dify-codereview · tests-behavior-not-calls Reply here if this is wrong.
asukaminato0721
left a comment
There was a problem hiding this comment.
3 inline suggestions at 258e62d009, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong. 2 review threads are still unresolved.
dify-codereview
| assert result is True | ||
| assert db_session_with_containers.get(Dataset, dataset.id) is None | ||
| mock_dataset_was_deleted.send.assert_called_once_with(dataset) | ||
| mock_dataset_was_deleted.send.assert_called_once_with(dataset, session=db_session_with_containers) |
There was a problem hiding this comment.
Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.
dify-codereview · tests-behavior-not-calls Reply here if this is wrong.
| assert result is True | ||
| assert db_session_with_containers.get(Dataset, dataset.id) is None | ||
| send_deleted_signal.assert_called_once_with(dataset) | ||
| send_deleted_signal.assert_called_once_with(dataset, session=db_session_with_containers) |
There was a problem hiding this comment.
Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.
dify-codereview · tests-behavior-not-calls Reply here if this is wrong.
| def test_enqueues_cleanup_with_provided_session(sqlite_session: Session) -> None: | ||
| dataset = _dataset(indexing_technique=IndexTechniqueType.ECONOMY, chunk_structure="text_model") | ||
|
|
||
| with patch("events.event_handlers.clean_when_dataset_deleted.clean_dataset_task.delay") as delay: |
There was a problem hiding this comment.
Rather than patching this in the test, change the production code to take the dependency as a parameter (pass the session / account / service in), so the test hands in a real one and needs no patch.
dify-codereview · tests-real-dependencies (inject-dependency) Learned from an earlier review. Reply here if this is wrong.
asukaminato0721
left a comment
There was a problem hiding this comment.
CI is failing: API Tests, Run API Tests / API Integration Tests (3.13). Please fix it. Checked 258e62d009.
dify-codereview
Summary
clean_when_dataset_deletedoff the globaldb.session.sessionand pass it fromDatasetService.delete_dataset(already session-scoped).Test plan
uv run pytest tests/unit_tests/events/event_handlers/test_clean_when_dataset_deleted.py -quv run pyrefly checkon the changed handler + unit test