Skip to content

Commit

Permalink
for form data api tests, download files to local filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil committed Nov 10, 2024
1 parent 60067e2 commit 0b38a3c
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
# built-in dependencies
import os
import base64
import unittest

# 3rd party dependencies
import gdown

# project dependencies
from deepface.api.src.app import create_app
from deepface.commons.logger import Logger

logger = Logger()

IMG1_SOURCE = (
"https://raw.githubusercontent.com/serengil/deepface/refs/heads/master/tests/dataset/img1.jpg"
)
IMG2_SOURCE = (
"https://raw.githubusercontent.com/serengil/deepface/refs/heads/master/tests/dataset/img2.jpg"
)


class TestVerifyEndpoint(unittest.TestCase):
def setUp(self):
download_test_images(IMG1_SOURCE)
download_test_images(IMG2_SOURCE)
app = create_app()
app.config["DEBUG"] = True
app.config["TESTING"] = True
Expand Down Expand Up @@ -240,7 +253,7 @@ def test_invalid_analyze(self):
assert response.status_code == 400

def test_analyze_for_multipart_form_data(self):
with open("dataset/img1.jpg", "rb") as img_file:
with open("/tmp/img1.jpg", "rb") as img_file:
response = self.app.post(
"/analyze",
content_type="multipart/form-data",
Expand All @@ -258,8 +271,8 @@ def test_analyze_for_multipart_form_data(self):
logger.info("✅ analyze api for multipart form data test is done")

def test_verify_for_multipart_form_data(self):
with open("dataset/img1.jpg", "rb") as img1_file:
with open("dataset/img2.jpg", "rb") as img2_file:
with open("/tmp/img1.jpg", "rb") as img1_file:
with open("/tmp/img2.jpg", "rb") as img2_file:
response = self.app.post(
"/verify",
content_type="multipart/form-data",
Expand All @@ -284,7 +297,7 @@ def test_verify_for_multipart_form_data(self):
logger.info("✅ verify api for multipart form data test is done")

def test_represent_for_multipart_form_data(self):
with open("dataset/img1.jpg", "rb") as img_file:
with open("/tmp/img1.jpg", "rb") as img_file:
response = self.app.post(
"/represent",
content_type="multipart/form-data",
Expand All @@ -304,7 +317,7 @@ def test_represent_for_multipart_form_data_and_filepath(self):
"/represent",
content_type="multipart/form-data",
data={
"img": "dataset/img1.jpg",
"img": "/tmp/img1.jpg",
"model_name": "Facenet",
"detector_backend": "mtcnn",
},
Expand All @@ -313,3 +326,12 @@ def test_represent_for_multipart_form_data_and_filepath(self):
result = response.json
assert isinstance(result, dict)
logger.info("✅ represent api for multipart form data and file path test is done")


def download_test_images(url: str):
file_name = url.split("/")[-1]
target_file = f"/tmp/{file_name}"
if os.path.exists(target_file) is True:
return

gdown.download(url, target_file, quiet=False)

0 comments on commit 0b38a3c

Please sign in to comment.