Skip to content

Commit

Permalink
Increase watchdog coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-f-cruz committed Nov 3, 2024
1 parent 89ee955 commit d53c143
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
151 changes: 150 additions & 1 deletion tests/test_data_transfer_service_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
from pathlib import Path
from unittest.mock import MagicMock, patch

from aind_data_schema_models.modalities import Modality
from aind_data_schema_models.platforms import Platform
from aind_watchdog_service.models.manifest_config import BucketType

from aind_behavior_experiment_launcher.data_mappers.aind_data_schema import AindDataSchemaSessionDataMapper
from aind_behavior_experiment_launcher.data_transfer.watchdog_service import WatchConfig, WatchdogDataTransferService
from aind_behavior_experiment_launcher.data_transfer.watchdog_service import (
ManifestConfig,
WatchConfig,
WatchdogDataTransferService,
)


class TestWatchdogDataTransferService(unittest.TestCase):
Expand Down Expand Up @@ -113,6 +118,150 @@ def test_validate_fail(self, mock_is_running):
with self.assertRaises(FileNotFoundError):
self.service.validate()

def test_missing_env_variables(self):
del os.environ["WATCHDOG_EXE"]
del os.environ["WATCHDOG_CONFIG"]
with self.assertRaises(ValueError):
WatchdogDataTransferService(
self.source,
destination=self.destination,
aind_session_data_mapper=self.aind_data_mapper,
schedule_time=self.schedule_time,
project_name=self.project_name,
platform=self.platform,
capsule_id=self.capsule_id,
script=self.script,
s3_bucket=self.s3_bucket,
mount=self.mount,
force_cloud_sync=self.force_cloud_sync,
transfer_endpoint=self.transfer_endpoint,
validate=self.validate,
)

@patch("aind_behavior_experiment_launcher.data_transfer.watchdog_service.Path.exists", return_value=True)
def test_find_ads_schemas(self, mock_exists):
# TODO this test should be updated once the corresponding tested method is also updated
source = "mock_source_path"
expected_files = [
Path(source) / "subject.json",
Path(source) / "data_description.json",
Path(source) / "procedures.json",
Path(source) / "session.json",
Path(source) / "rig.json",
Path(source) / "processing.json",
Path(source) / "acquisition.json",
Path(source) / "instrument.json",
Path(source) / "quality_control.json",
]

result = WatchdogDataTransferService._find_ads_schemas(source)
self.assertEqual(result, expected_files)

@patch("aind_behavior_experiment_launcher.data_transfer.watchdog_service.Path.mkdir")
@patch("aind_behavior_experiment_launcher.data_transfer.watchdog_service.WatchdogDataTransferService._write_yaml")
def test_dump_manifest_config(self, mock_write_yaml, mock_mkdir):
self.service._manifest_config = ManifestConfig(
name="test_manifest",
modalities={Modality.BEHAVIOR: ["path/to/modality"]},
subject_id=1,
acquisition_datetime="2023-01-01T00:00:00",
schemas=["path/to/schema"],
destination="path/to/destination",
mount="mount_path",
processor_full_name="processor_name",
project_name="test_project",
schedule_time=self.schedule_time,
platform=Platform.BEHAVIOR,
capsule_id="capsule_id",
s3_bucket=BucketType.PRIVATE,
script={"script_key": ["script_value"]},
force_cloud_sync=True,
transfer_endpoint="http://aind-data-transfer-service/api/v1/submit_jobs",
)
self.service._watch_config = WatchConfig(
flag_dir="flag_dir",
manifest_complete="manifest_complete",
)

path = Path("flag_dir/manifest_test_manifest.yaml")
result = self.service.dump_manifest_config()

self.assertIsInstance(result, Path)
self.assertIsInstance(path, Path)
self.assertEqual(result.resolve(), path.resolve())

mock_write_yaml.assert_called_once()
mock_mkdir.assert_called_once_with(parents=True, exist_ok=True)

@patch("aind_behavior_experiment_launcher.data_transfer.watchdog_service.Path.mkdir")
@patch("aind_behavior_experiment_launcher.data_transfer.watchdog_service.WatchdogDataTransferService._write_yaml")
def test_dump_manifest_config_custom_path(self, mock_write_yaml, mock_mkdir):
self.service._manifest_config = ManifestConfig(
name="test_manifest",
modalities={Modality.BEHAVIOR: ["path/to/modality"]},
subject_id=1,
acquisition_datetime="2023-01-01T00:00:00",
schemas=["path/to/schema"],
destination="path/to/destination",
mount="mount_path",
processor_full_name="processor_name",
project_name="test_project",
schedule_time=self.schedule_time,
platform=Platform.BEHAVIOR,
capsule_id="capsule_id",
s3_bucket=BucketType.PRIVATE,
script={"script_key": ["script_value"]},
force_cloud_sync=True,
transfer_endpoint="http://aind-data-transfer-service/api/v1/submit_jobs",
)
self.service._watch_config = WatchConfig(
flag_dir="flag_dir",
manifest_complete="manifest_complete",
)

custom_path = Path("custom_path/manifest_test_manifest.yaml")
result = self.service.dump_manifest_config(path=custom_path)

self.assertIsInstance(result, Path)
self.assertIsInstance(custom_path, Path)
self.assertEqual(result.resolve(), custom_path.resolve())
mock_write_yaml.assert_called_once()
mock_mkdir.assert_called_once_with(parents=True, exist_ok=True)

def test_dump_manifest_config_no_manifest_config(self):
self.service._manifest_config = None
self.service._watch_config = WatchConfig(
flag_dir="flag_dir",
manifest_complete="manifest_complete",
)

with self.assertRaises(ValueError):
self.service.dump_manifest_config()

def test_dump_manifest_config_no_watch_config(self):
self.service._manifest_config = ManifestConfig(
name="test_manifest",
modalities={Modality.BEHAVIOR_VIDEOS: ["path/to/modality"]},
subject_id=1,
acquisition_datetime="2023-01-01T00:00:00",
schemas=["path/to/schema"],
destination="path/to/destination",
mount="mount_path",
processor_full_name="processor_name",
project_name="test_project",
schedule_time=self.schedule_time,
platform=Platform.BEHAVIOR,
capsule_id="capsule_id",
s3_bucket=BucketType.PRIVATE,
script={"script_key": ["script_value"]},
force_cloud_sync=True,
transfer_endpoint="http://aind-data-transfer-service/api/v1/submit_jobs",
)
self.service._watch_config = None

with self.assertRaises(ValueError):
self.service.dump_manifest_config()


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pragma: no cover
"""testing examples"""

import glob
import unittest

Expand Down

0 comments on commit d53c143

Please sign in to comment.