Skip to content

After calling delete_tasks, open a new session #2592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,17 +904,23 @@ def test_delete_task(self, db: _Database, temp_filename):
assert not db.delete_task(t2)

def test_delete_tasks(self, db: _Database, temp_filename):
"""Test the delete_tasks method.

We need a new session after calling delete_tasks.
"""
with db.session.begin():
t1 = db.add_url("https://1.com")
t2 = db.add_path(temp_filename, tags="x86")
t3 = db.add_url("https://3.com")
with db.session.begin():
assert db.delete_tasks(task_ids=[])
assert db.delete_tasks(task_ids=[t1, t2, t3 + 1])
with db.session.begin():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding a new session here ensures that the subsequent query operates on a fresh database snapshot, reflecting the changes made by delete_tasks. This is crucial for the test's correctness.

tasks = db.session.query(Task).all()
assert len(tasks) == 1
assert tasks[0].id == t3
assert db.delete_tasks(task_ids=[t1, t2])
with db.session.begin():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the previous session, this ensures that the final query accurately reflects the state of the database after the second delete_tasks call.

tasks = db.session.query(Task).all()
assert len(tasks) == 1
assert tasks[0].id == t3
Expand Down