Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code, add tests, and update dependencies #6

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions API/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

from bson import ObjectId
from deepface import DeepFace
from fastapi import APIRouter, Form, HTTPException, Response
from fastapi import APIRouter, HTTPException, Response
from matplotlib import pyplot as plt
from PIL import Image
from pydantic import BaseModel
from pymongo import MongoClient

from API.database import Database
from API.utils import init_logging_config
Expand Down Expand Up @@ -122,7 +121,7 @@ async def get_employees():
Name=employee.get("Name", "N/A"),
gender=employee.get("gender", "N/A"),
Department=employee.get("Department", "N/A"),
Images=employee.get("Images", "N/A"),
Images=employee.get("Images", []),
)
for employee in employees_mongo
]
Expand Down Expand Up @@ -227,7 +226,7 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee):
try:
update_result = client.update_one(
collection,
filter={"_id": ObjectId(user_id["_id"])},
{"_id": ObjectId(user_id["_id"])},
update={"$set": Employee_data},
)
logging.info(f"Update result {update_result}")
Expand Down
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@
- Split `test_face_lifecycle` function in [`test_face_cycle.py`](testing/test_face_cycle.py) into multiple smaller test cases that execute in a particularly specified order. Changes made by @Devasy23.

### Fixed
- Resolved issues in the test cases of [`test_face_cycle.py`](testing/test_face_cycle.py) to ensure they pass with the updated code structure. Fixes made by @Devasy23.
- Resolved issues in the test cases of [`test_face_cycle.py`](testing/test_face_cycle.py) to ensure they pass with the updated code structure. Fixes made by @Devasy23.

### Removed
- Removed deprecated code from various modules to improve performance and maintainability. Changes made by @Devasy23.

## [0.1.3] - 2024-03-10 - 00:51

### Added
- Created a new file [`test_face_endpoints.py`](testing/test_face_endpoints.py) to separately test each endpoint. Changes made by @Devasy23
- Split the test cases in [`test_face_cycle.py`](testing/test_face_cycle.py) into smaller tests for each endpoint.
- Added new dependencies to the [`requirements.txt`](requirements.txt) file to support the latest features and improvements.

### Fixed
- Resolved various bugs and issues identified during the testing process.

### Removed
- Removed deprecated code and unused dependencies from the project.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ urllib3==2.1.0
uvicorn==0.25.0
Werkzeug==3.0.1
wrapt==1.14.1
zipp==3.17.0
zipp==3.17.0
pytest-ordering
164 changes: 164 additions & 0 deletions testing/test_face_endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import base64
import logging
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

from API.route import router
from API.utils import init_logging_config

init_logging_config()

client = TestClient(router)


@pytest.mark.run(order=1)
def test_register_face1():
mock_doc = {
"_id": "65e6284d01f95cd96ea334a7",
"EmployeeCode": "1",
"Name": "Devansh",
"gender": "Male",
"Department": "IT",
"Images": ["encoded_string1", "encoded_string2"],
}

mock_find = MagicMock(return_value=[mock_doc, mock_doc])
mock_insert_one = MagicMock(return_value=MagicMock(inserted_id="1"))
mock_find_one = MagicMock(return_value=mock_doc)
mock_update_one = MagicMock(return_value=MagicMock(modified_count=1))
mock_find_one_and_delete = MagicMock(return_value=mock_doc)

with patch("API.database.Database.find", mock_find), patch(
"API.database.Database.insert_one", mock_insert_one
), patch("API.database.Database.find_one", mock_find_one), patch(
"API.database.Database.update_one", mock_update_one
), patch(
"API.database.Database.find_one_and_delete", mock_find_one_and_delete
):

with open("./test-faces/devansh.jpg", "rb") as image_file:
encoded_string1 = base64.b64encode(image_file.read()).decode("utf-8")

response1 = client.post(
"/create_new_faceEntry",
json={
"EmployeeCode": "1",
"Name": "Devansh",
"gender": "Male",
"Department": "IT",
"Images": [encoded_string1, encoded_string1],
},
)
assert response1.status_code == 200
assert response1.json() == {"message": "Face entry created successfully"}


@pytest.mark.run(order=2)
def test_register_face2():
mock_doc = {
"_id": "65e6284d01f95cd96ea334a7",
"EmployeeCode": "1",
"Name": "Devansh",
"gender": "Male",
"Department": "IT",
"Images": ["encoded_string1", "encoded_string2"],
}

mock_find = MagicMock(return_value=[mock_doc, mock_doc])
mock_insert_one = MagicMock(return_value=MagicMock(inserted_id="1"))
mock_find_one = MagicMock(return_value=mock_doc)
mock_update_one = MagicMock(return_value=MagicMock(modified_count=1))
mock_find_one_and_delete = MagicMock(return_value=mock_doc)

with patch("API.database.Database.find", mock_find), patch(
"API.database.Database.insert_one", mock_insert_one
), patch("API.database.Database.find_one", mock_find_one), patch(
"API.database.Database.update_one", mock_update_one
), patch(
"API.database.Database.find_one_and_delete", mock_find_one_and_delete
):

with open("./test-faces/devansh.jpg", "rb") as image_file:
encoded_string2 = base64.b64encode(image_file.read()).decode("utf-8")

response2 = client.post(
"/create_new_faceEntry",
json={
"EmployeeCode": "2",
"Name": "test",
"gender": "Female",
"Department": "IT",
"Images": [encoded_string2, encoded_string2],
},
)
assert response2.status_code == 200
assert response2.json() == {"message": "Face entry created successfully"}


@pytest.mark.run(order=3)
def test_get_all_faces_after_registration():
mock_doc = {
"_id": "65e6284d01f95cd96ea334a7",
"EmployeeCode": "1",
"Name": "Devansh",
"gender": "Male",
"Department": "IT",
"Images": ["encoded_string1", "encoded_string2"],
}

mock_find = MagicMock(return_value=[mock_doc, mock_doc])

with patch("API.database.Database.find", mock_find):
response = client.get("/Data/")
assert response.status_code == 200
logging.debug(response.json())
assert len(response.json()) == 2


@pytest.mark.run(order=4)
def test_update_face():
mock_doc = {
"_id": "65e6284d01f95cd96ea334a7",
"EmployeeCode": "1",
"Name": "Devansh",
"gender": "Male",
"Department": "IT",
"Images": ["encoded_string1", "encoded_string2"],
}

mock_find = MagicMock(return_value=[mock_doc, mock_doc])
mock_insert_one = MagicMock(return_value=MagicMock(inserted_id="1"))
mock_find_one = MagicMock(return_value=mock_doc)
mock_update_one = MagicMock(return_value=MagicMock(modified_count=1))
mock_find_one_and_delete = MagicMock(return_value=mock_doc)

with patch("API.database.Database.find", mock_find), patch(
"API.database.Database.insert_one", mock_insert_one
), patch("API.database.Database.find_one", mock_find_one), patch(
"API.database.Database.update_one", mock_update_one
), patch(
"API.database.Database.find_one_and_delete", mock_find_one_and_delete
):
with open("./test-faces/devansh.jpg", "rb") as image_file:
encoded_string2 = base64.b64encode(image_file.read()).decode("utf-8")

response = client.put(
"/update/1",
json={
"Name": "Test",
"gender": "Male",
"Department": "IT_Test",
"Images": [encoded_string2, encoded_string2],
},
)
assert response.status_code == 200
assert response.json() == "Updated Successfully"


@pytest.mark.run(order=5)
def test_delete_face():
response = client.delete("/delete/1")
assert response.status_code == 200
assert response.json() == {"Message": "Successfully Deleted"}
Loading