From 91445ffa6517dcd0627604c42d2315776ccc4eee Mon Sep 17 00:00:00 2001 From: Vitali Yanushchyk Date: Fri, 24 May 2024 04:18:31 -0400 Subject: [PATCH] chg ! tests --- src/hope_dedup_engine/apps/core/storage.py | 20 ++++++++++---------- tests/faces/test_celery_tasks.py | 13 ++++++++----- tests/faces/test_duplication_detector.py | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/hope_dedup_engine/apps/core/storage.py b/src/hope_dedup_engine/apps/core/storage.py index de9d2bc6..438efa24 100644 --- a/src/hope_dedup_engine/apps/core/storage.py +++ b/src/hope_dedup_engine/apps/core/storage.py @@ -30,16 +30,16 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.azure_container = settings.AZURE_CONTAINER_HOPE - # def delete(self, name): - # raise RuntimeError("This storage cannot delete files") + def delete(self, name): + raise RuntimeError("This storage cannot delete files") - # def open(self, name, mode="rb"): - # if "w" in mode: - # raise RuntimeError("This storage cannot open files in write mode") - # return super().open(name, mode="rb") + def open(self, name, mode="rb"): + if "w" in mode: + raise RuntimeError("This storage cannot open files in write mode") + return super().open(name, mode="rb") - # def save(self, name, content, max_length=None): - # raise RuntimeError("This storage cannot save files") + def save(self, name, content, max_length=None): + raise RuntimeError("This storage cannot save files") - # def listdir(self, path=""): - # return [] + def listdir(self, path=""): + return [] diff --git a/tests/faces/test_celery_tasks.py b/tests/faces/test_celery_tasks.py index d448293a..dc986b64 100644 --- a/tests/faces/test_celery_tasks.py +++ b/tests/faces/test_celery_tasks.py @@ -1,5 +1,6 @@ from unittest.mock import patch +from celery import states from faces_const import FILENAME, FILENAMES from hope_dedup_engine.apps.faces.celery_tasks import deduplicate @@ -20,12 +21,12 @@ def test_deduplicate_task_already_running(mock_redis_client, mock_duplication_de def test_deduplicate_task_success(dd, mock_redis_client, mock_duplication_detector, celery_app, celery_worker): mock_set, mock_delete = mock_redis_client mock_find = mock_duplication_detector - mock_set.return_value = True # Lock is acquired mock_find.return_value = set(FILENAMES[:2]) # Assuming the first two are duplicates based on mock data with patch("hope_dedup_engine.apps.faces.celery_tasks.DuplicationDetector", return_value=dd): task_result = deduplicate.apply(args=[FILENAME]).get() + assert task_result == set(FILENAMES[:2]) # Assuming the first two are duplicates based on mock data mock_set.assert_called_once_with(f"Deduplicate_{FILENAME}", "true", nx=True, ex=3600) mock_delete.assert_called_once_with(f"Deduplicate_{FILENAME}") # Lock is released @@ -41,9 +42,11 @@ def test_deduplicate_task_exception_handling( with patch("hope_dedup_engine.apps.faces.celery_tasks.DuplicationDetector", return_value=dd): task = deduplicate.apply(args=[FILENAME]) - assert task.result is None # Task is not executed - mock_duplication_detector.assert_called_once() # DeduplicationDetector is called + assert task.state == states.FAILURE + assert isinstance(task.result, Exception) + assert str(task.result) == "Simulated task failure" + assert task.traceback is not None - # Check that the Redis lock was acquired and then released + mock_find.assert_called_once() mock_set.assert_called_once_with(f"Deduplicate_{FILENAME}", "true", nx=True, ex=3600) - mock_delete.assert_called_once_with(f"Deduplicate_{FILENAME}") + mock_delete.assert_called_once_with(f"Deduplicate_{FILENAME}") # Lock is released diff --git a/tests/faces/test_duplication_detector.py b/tests/faces/test_duplication_detector.py index 48c49d29..d63d6c7a 100644 --- a/tests/faces/test_duplication_detector.py +++ b/tests/faces/test_duplication_detector.py @@ -118,7 +118,6 @@ def test_load_encodings_all_exception_handling(dd): try: dd._load_encodings_all() except Exception: - print(f"\n{dd.logger.exception.assert_called_once()=}") ... dd.logger.reset_mock() @@ -149,6 +148,7 @@ def test_encode_face_invalid_region(dd, image_bytes_io): # Check that the error was logged with the correct message mock_error_logger.assert_called_once_with(f"Invalid face region {(0, 0, 10)}") + dd.logger.reset_mock() def test_encode_face_exception_handling(dd):