From 250bf4e8d2e9feb4d42403233b567d63a72c8760 Mon Sep 17 00:00:00 2001 From: Inhyuk Andy Cho Date: Wed, 19 Apr 2023 09:09:40 +0900 Subject: [PATCH] test: test path exists Signed-off-by: Inhyuk Andy Cho --- tests/unit/core/data/manager/test_dataset_manager.py | 7 +++++++ tests/unit/core/data/test_helpers.py | 11 ++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/unit/core/data/manager/test_dataset_manager.py b/tests/unit/core/data/manager/test_dataset_manager.py index c9e9805485a..cd308a36615 100644 --- a/tests/unit/core/data/manager/test_dataset_manager.py +++ b/tests/unit/core/data/manager/test_dataset_manager.py @@ -4,6 +4,7 @@ # import shutil from typing import List +from tempfile import TemporaryDirectory import datumaro as dm import pytest @@ -76,6 +77,12 @@ def test_get_image_path(self, task, subset): ) assert random_data is None + with TemporaryDirectory() as temp_dir: + random_data = DatasetManager.get_image_path( + generate_datumaro_dataset_item(item_id="0", subset=subset, task=task, temp_dir=temp_dir) + ) + assert random_data is not None + @e2e_pytest_unit @pytest.mark.parametrize("task", AVAILABLE_TASKS) @pytest.mark.parametrize("subset", AVAILABLE_SUBSETS) diff --git a/tests/unit/core/data/test_helpers.py b/tests/unit/core/data/test_helpers.py index 624d3ee7a9f..23b3019c8e5 100644 --- a/tests/unit/core/data/test_helpers.py +++ b/tests/unit/core/data/test_helpers.py @@ -2,8 +2,10 @@ # Copyright (C) 2023 Intel Corporation # SPDX-License-Identifier: Apache-2.0 # -from typing import List +from typing import List, Optional +import os +import cv2 import datumaro as dm import numpy as np @@ -86,6 +88,7 @@ def generate_datumaro_dataset_item( task: str, image_shape: np.array = np.array((5, 5, 3)), mask_shape: np.array = np.array((5, 5)), + temp_dir: Optional[str] = None, ) -> dm.DatasetItem: """Generate Datumaro DatasetItem. @@ -95,6 +98,7 @@ def generate_datumaro_dataset_item( task (str): task type, e.g. "classification" image_shape (np.array): the shape of image. image_shape (np.array): the shape of mask. + temp_dir (str): directory to save image data Returns: dm.DatasetItem: Datumaro DatasetItem @@ -105,6 +109,11 @@ def generate_datumaro_dataset_item( "segmentation": dm.Mask(np.zeros(mask_shape)), } + if temp_dir: + path = os.path.join(temp_dir, "image.png") + cv2.imwrite(path, np.ones(image_shape)) + return dm.DatasetItem(id=item_id, subset=subset, image=path, annotations=[ann_task_dict[task]]) + return dm.DatasetItem(id=item_id, subset=subset, image=np.ones(image_shape), annotations=[ann_task_dict[task]])