Skip to content

Commit

Permalink
Refactor test_register_face function to use TestClient and improve fi…
Browse files Browse the repository at this point in the history
…le handling
  • Loading branch information
Devasy23 committed Jan 11, 2024
1 parent 1b3c93f commit 7a3938f
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions testing/test_register_face.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import pytest
import requests


from fastapi.testclient import TestClient
from main import app
import os

client = TestClient(app)
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})

# Open a test image file in binary mode
IMAGEDIR = "test-faces/"
with open("test-faces/07c64ef1-b32e-4396-97ea-0894249d58ee.jpg", "rb") as image_file:
# Create a tuple with the file's name and its content
file_tuple = ("test_image.jpg", image_file.read())
# Create a dictionary with the file's data
data = {"file": file_tuple}
# Send a POST request to the register_face endpoint with the test image
response = client.post("/upload/", files=data)
# Assert that the response status code is 200 (success)
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/"

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

# # Print the response
# assert response.status_code == 200
# # print(response.json())
# Assert that the response json contains the filename
assert "filename" in response.json()
# Assert that the file was saved correctly
assert os.path.exists(f"{IMAGEDIR}{response.json()['filename']}")
# Clean up the created file
os.remove(f"{IMAGEDIR}{response.json()['filename']}")

0 comments on commit 7a3938f

Please sign in to comment.