fix: make deletion of single file commit to DB, create tests for file deletion (#8953)
* Added commit to db session * Changed order * Deleted flush * Added test to files
This commit is contained in:
parent
eb69cddc1c
commit
1362aa1172
2 changed files with 50 additions and 3 deletions
|
|
@ -275,7 +275,6 @@ async def delete_files_batch(
|
||||||
await session.delete(file)
|
await session.delete(file)
|
||||||
|
|
||||||
# Delete all files from the database
|
# Delete all files from the database
|
||||||
await session.flush() # Ensures delete is staged
|
|
||||||
await session.commit() # Commit deletion
|
await session.commit() # Commit deletion
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -474,7 +473,7 @@ async def delete_file(
|
||||||
|
|
||||||
# Delete from the database
|
# Delete from the database
|
||||||
await session.delete(file_to_delete)
|
await session.delete(file_to_delete)
|
||||||
await session.flush() # Ensures delete is staged
|
await session.commit()
|
||||||
|
|
||||||
except HTTPException:
|
except HTTPException:
|
||||||
# Re-raise HTTPException to avoid being caught by the generic exception handler
|
# Re-raise HTTPException to avoid being caught by the generic exception handler
|
||||||
|
|
@ -506,7 +505,6 @@ async def delete_all_files(
|
||||||
await session.delete(file)
|
await session.delete(file)
|
||||||
|
|
||||||
# Delete all files from the database
|
# Delete all files from the database
|
||||||
await session.flush() # Ensures delete is staged
|
|
||||||
await session.commit() # Commit deletion
|
await session.commit() # Commit deletion
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
|
|
@ -207,3 +207,52 @@ async def test_edit_file(files_client, files_created_api_key):
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
file = response.json()
|
file = response.json()
|
||||||
assert file["name"] == "potato.txt"
|
assert file["name"] == "potato.txt"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_upload_list_delete_and_validate_files(files_client, files_created_api_key):
|
||||||
|
headers = {"x-api-key": files_created_api_key.api_key}
|
||||||
|
|
||||||
|
# Upload two files
|
||||||
|
response1 = await files_client.post(
|
||||||
|
"api/v2/files",
|
||||||
|
files={"file": ("file1.txt", b"content1")},
|
||||||
|
headers=headers,
|
||||||
|
)
|
||||||
|
assert response1.status_code == 201
|
||||||
|
file1 = response1.json()
|
||||||
|
|
||||||
|
response2 = await files_client.post(
|
||||||
|
"api/v2/files",
|
||||||
|
files={"file": ("file2.txt", b"content2")},
|
||||||
|
headers=headers,
|
||||||
|
)
|
||||||
|
assert response2.status_code == 201
|
||||||
|
file2 = response2.json()
|
||||||
|
|
||||||
|
# List files and validate both are present
|
||||||
|
response = await files_client.get("api/v2/files", headers=headers)
|
||||||
|
assert response.status_code == 200
|
||||||
|
files = response.json()
|
||||||
|
file_names = [f["name"] for f in files]
|
||||||
|
file_ids = [f["id"] for f in files]
|
||||||
|
assert file1["name"] in file_names
|
||||||
|
assert file2["name"] in file_names
|
||||||
|
assert file1["id"] in file_ids
|
||||||
|
assert file2["id"] in file_ids
|
||||||
|
assert len(files) == 2
|
||||||
|
|
||||||
|
# Delete one file
|
||||||
|
response = await files_client.delete(f"api/v2/files/{file1['id']}", headers=headers)
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
# List files again and validate only the other remains
|
||||||
|
response = await files_client.get("api/v2/files", headers=headers)
|
||||||
|
assert response.status_code == 200
|
||||||
|
files = response.json()
|
||||||
|
file_names = [f["name"] for f in files]
|
||||||
|
file_ids = [f["id"] for f in files]
|
||||||
|
assert file1["name"] not in file_names
|
||||||
|
assert file1["id"] not in file_ids
|
||||||
|
assert file2["name"] in file_names
|
||||||
|
assert file2["id"] in file_ids
|
||||||
|
assert len(files) == 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue