|
2 | 2 | # Licensed under the MIT license. |
3 | 3 |
|
4 | 4 | from pathlib import Path |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 |
|
8 | 9 | from pyrit.datasets.seed_datasets.local.local_dataset_loader import _LocalDatasetLoader |
9 | | -from pyrit.models import SeedDataset |
| 10 | +from pyrit.models import SeedDataset, SeedPrompt |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class TestLocalDatasetLoader: |
@@ -49,6 +50,59 @@ async def test_fetch_dataset(self, tmp_path, valid_yaml_content): |
49 | 50 | assert len(dataset.prompts) == 1 |
50 | 51 | assert dataset.prompts[0].value == "test prompt" |
51 | 52 |
|
| 53 | + async def test_fetch_dataset_offloads_file_read(self, tmp_path: Path) -> None: |
| 54 | + """Dataset file loading runs outside the event loop thread.""" |
| 55 | + file_path = tmp_path / "test.yaml" |
| 56 | + loader = _LocalDatasetLoader.__new__(_LocalDatasetLoader) |
| 57 | + loader.file_path = file_path |
| 58 | + loader._dataset_name = "test_dataset" |
| 59 | + expected = SeedDataset( |
| 60 | + dataset_name="test_dataset", |
| 61 | + seeds=[SeedPrompt(value="test prompt", data_type="text")], |
| 62 | + ) |
| 63 | + to_thread_mock = AsyncMock(return_value=expected) |
| 64 | + |
| 65 | + with ( |
| 66 | + patch.object(SeedDataset, "from_yaml_file") as load_mock, |
| 67 | + patch( |
| 68 | + "pyrit.datasets.seed_datasets.local.local_dataset_loader.asyncio.to_thread", |
| 69 | + new=to_thread_mock, |
| 70 | + ), |
| 71 | + ): |
| 72 | + dataset = await loader.fetch_dataset_async() |
| 73 | + |
| 74 | + assert dataset is expected |
| 75 | + to_thread_mock.assert_awaited_once_with(load_mock, file_path) |
| 76 | + load_mock.assert_not_called() |
| 77 | + |
| 78 | + async def test_parse_metadata_offloads_file_read(self, tmp_path: Path) -> None: |
| 79 | + """Metadata YAML parsing runs outside the event loop thread.""" |
| 80 | + file_path = tmp_path / "test.yaml" |
| 81 | + loader = _LocalDatasetLoader.__new__(_LocalDatasetLoader) |
| 82 | + loader.file_path = file_path |
| 83 | + loader._dataset_name = "test_dataset" |
| 84 | + read_yaml_mock = MagicMock( |
| 85 | + return_value={ |
| 86 | + "dataset_name": "test_dataset", |
| 87 | + "harm_categories": ["violence"], |
| 88 | + } |
| 89 | + ) |
| 90 | + to_thread_mock = AsyncMock(return_value=read_yaml_mock.return_value) |
| 91 | + |
| 92 | + with ( |
| 93 | + patch.object(loader, "_read_yaml", new=read_yaml_mock), |
| 94 | + patch( |
| 95 | + "pyrit.datasets.seed_datasets.local.local_dataset_loader.asyncio.to_thread", |
| 96 | + new=to_thread_mock, |
| 97 | + ), |
| 98 | + ): |
| 99 | + metadata = await loader._parse_metadata_async() |
| 100 | + |
| 101 | + assert metadata is not None |
| 102 | + assert metadata.harm_categories == {"violence"} |
| 103 | + to_thread_mock.assert_awaited_once_with(read_yaml_mock) |
| 104 | + read_yaml_mock.assert_not_called() |
| 105 | + |
52 | 106 | async def test_fetch_dataset_file_not_found(self): |
53 | 107 | loader = _LocalDatasetLoader(file_path=Path("non_existent.yaml")) |
54 | 108 | with pytest.raises(Exception): |
|
0 commit comments