mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
Models like Archive/Video/Doc can be deleted, even if they are tagged. User is shown a warning and must confirm deletion of tagged files.
This commit is contained in:
parent
513ae52a6e
commit
09a967535b
23 changed files with 689 additions and 109 deletions
|
|
@ -77,14 +77,74 @@ async def test_docs_search_by_tag(async_client, test_session, doc_factory, tag_f
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc(async_client, test_session, test_directory, example_epub, refresh_files):
|
||||
"""Docs can be deleted."""
|
||||
"""Docs can be deleted, including their on-disk files and FileGroup row."""
|
||||
from wrolpi.files.models import FileGroup
|
||||
await refresh_files()
|
||||
|
||||
doc = test_session.query(Doc).first()
|
||||
assert doc
|
||||
file_group_id = doc.file_group_id
|
||||
paths = list(doc.file_group.my_paths())
|
||||
assert paths and all(p.is_file() for p in paths)
|
||||
|
||||
request, response = await async_client.delete(f'/api/docs/{file_group_id}')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
|
||||
# Doc, FileGroup, and on-disk files all removed.
|
||||
assert test_session.query(Doc).filter_by(file_group_id=file_group_id).one_or_none() is None
|
||||
assert test_session.query(FileGroup).filter_by(id=file_group_id).one_or_none() is None
|
||||
assert all(not p.exists() for p in paths)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc_tagged_requires_force(async_client, test_session, doc_factory, tag_factory,
|
||||
await_switches):
|
||||
"""DELETE /api/docs/<id> returns 409 for tagged docs until ?force=true is passed."""
|
||||
from wrolpi.files.models import FileGroup
|
||||
tag = await tag_factory()
|
||||
doc = doc_factory()
|
||||
doc.file_group.add_tag(test_session, tag.name)
|
||||
test_session.commit()
|
||||
file_group_id = doc.file_group_id
|
||||
paths = list(doc.file_group.my_paths())
|
||||
|
||||
request, response = await async_client.delete(f'/api/docs/{file_group_id}')
|
||||
assert response.status_code == HTTPStatus.CONFLICT
|
||||
assert response.json['code'] == 'FILE_GROUP_IS_TAGGED'
|
||||
assert len(response.json['file_groups']) == 1
|
||||
assert response.json['file_groups'][0]['id'] == file_group_id
|
||||
assert tag.name in response.json['file_groups'][0]['tags']
|
||||
# Nothing was deleted.
|
||||
assert all(p.is_file() for p in paths)
|
||||
assert test_session.query(Doc).count() == 1
|
||||
|
||||
# With force=true, the Doc is deleted.
|
||||
request, response = await async_client.delete(f'/api/docs/{file_group_id}?force=true')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert all(not p.exists() for p in paths)
|
||||
assert test_session.query(Doc).count() == 0
|
||||
assert test_session.query(FileGroup).filter_by(id=file_group_id).one_or_none() is None
|
||||
await await_switches()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_doc_atomic_when_some_tagged(async_client, test_session, doc_factory, tag_factory):
|
||||
"""When deleting multiple Docs and any are tagged, NONE are deleted until ?force=true."""
|
||||
untagged = doc_factory()
|
||||
tagged = doc_factory()
|
||||
tag = await tag_factory()
|
||||
tagged.file_group.add_tag(test_session, tag.name)
|
||||
test_session.commit()
|
||||
untagged_paths = list(untagged.file_group.my_paths())
|
||||
tagged_paths = list(tagged.file_group.my_paths())
|
||||
|
||||
ids = f'{untagged.file_group_id},{tagged.file_group_id}'
|
||||
request, response = await async_client.delete(f'/api/docs/{ids}')
|
||||
assert response.status_code == HTTPStatus.CONFLICT
|
||||
assert response.json['code'] == 'FILE_GROUP_IS_TAGGED'
|
||||
returned_ids = {fg['id'] for fg in response.json['file_groups']}
|
||||
assert returned_ids == {tagged.file_group_id}
|
||||
# Nothing was deleted.
|
||||
assert all(p.is_file() for p in untagged_paths)
|
||||
assert all(p.is_file() for p in tagged_paths)
|
||||
assert test_session.query(Doc).count() == 2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue