-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor test_register_face function to use TestClient and improve fi…
…le handling
- Loading branch information
Showing
1 changed file
with
21 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']}") |