Skip to content

Commit

Permalink
Add delete_file endpoint and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Devasy23 committed Jan 10, 2024
1 parent dc07154 commit ef7195d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 deletions.
7 changes: 6 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ async def read_random_file():

path = f"{IMAGEDIR}{files[random_index]}"

return FileResponse(path)
return FileResponse(path)

@app.delete("/delete/{filename}")
async def delete_file(filename: str):
os.remove(f"{IMAGEDIR}/{filename}")
return {"message": "Face deleted successfully"}
Binary file removed test-faces/c8866490-5fbe-45fe-8ac0-38652ec779bd.jpg
Binary file not shown.
70 changes: 23 additions & 47 deletions testing/test_register_face.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
# import pytest
# from starlette.testclient import TestClient
# from ..main import app
# import os

# IMAGEDIR = "/test-faces"

# def test_register_face():
# client = TestClient(app)
# with open('test_image.jpg', 'rb') as test_file:
# response = client.post(
# "/upload/",
# files={"file": ("test_image.jpg", test_file, "image/jpeg")},
# )

# assert response.status_code == 200
# assert 'filename' in response.json()

# # Cleanup: remove the uploaded file after test
# os.remove(f"{IMAGEDIR}{response.json()['filename']}")

import pytest
import requests
import base64

# Open the image file in binary mode, read it, and encode it to base64
# with open('test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg', 'rb') as image_file:
# encoded_string = base64.b64encode(image_file.read()).decode('utf-8')

# # Define the URL where you want to send the POST request
# url = "http://localhost:8000/upload/"

# # Define the headers for the POST request
# headers = {'Content-Type': 'application/json'}

# # Define the body of the POST request
# data = {"file": encoded_string}
def test_register_face():
with open('test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg', 'rb') as image_file:
url = "http://localhost:8000/upload/"
response = requests.post(url, files={'file': image_file})

# # Send the POST request
# response = requests.post(url, headers=headers, json=data)

# # Print the response
# print(response.json())

import requests
assert response.status_code == 200
filename = response.json()['filename']
url = f"http://localhost:8000/delete/{filename}"
response = requests.delete(url)
assert response.status_code == 200
assert response.json() == {'message': 'Face deleted successfully'}




# assert response.json() == {'message': 'Face registered successfully'}
# Open the image file in binary mode
with open('test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg', 'rb') as image_file:
# Define the URL where you want to send the POST request
url = "http://localhost:8000/upload/"
# with open('test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg', 'rb') as image_file:
# # Define the URL where you want to send the POST request
# url = "http://localhost:8000/upload/"

# Send the POST request with the file
response = requests.post(url, files={'file': image_file})
# # Send the POST request with the file
# response = requests.post(url, files={'file': image_file})

# Print the response
print(response.json())
# # Print the response
# assert response.status_code == 200
# # print(response.json())

0 comments on commit ef7195d

Please sign in to comment.