|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT license. |
3 | 3 |
|
| 4 | +from pathlib import Path |
| 5 | + |
4 | 6 | import pytest |
5 | 7 |
|
6 | 8 | from pyrit.common.path import SCORER_EVALS_PATH |
|
13 | 15 | ) |
14 | 16 |
|
15 | 17 |
|
| 18 | +@pytest.mark.parametrize("value", ["00123", "1e3", "1.2300", "True"]) |
| 19 | +@pytest.mark.parametrize("encoding", ["utf-8", "latin-1"]) |
| 20 | +@pytest.mark.parametrize("metrics_type", [MetricsType.OBJECTIVE, MetricsType.HARM]) |
| 21 | +def test_from_csv_preserves_numeric_and_boolean_text( |
| 22 | + tmp_path: Path, value: str, encoding: str, metrics_type: MetricsType |
| 23 | +) -> None: |
| 24 | + column = "objective" if metrics_type == MetricsType.OBJECTIVE else "harm_category" |
| 25 | + label = "1" if metrics_type == MetricsType.OBJECTIVE else "0.5" |
| 26 | + csv_path = tmp_path / "text.csv" |
| 27 | + csv_path.write_text( |
| 28 | + f"assistant_response,{column},human_score,notes\n{value},00456,{label},café\n", |
| 29 | + encoding=encoding, |
| 30 | + ) |
| 31 | + |
| 32 | + dataset = HumanLabeledDataset.from_csv(csv_path=csv_path, metrics_type=metrics_type, version="1.0") |
| 33 | + entry = dataset.entries[0] |
| 34 | + piece = entry.conversation[0].message_pieces[0] |
| 35 | + |
| 36 | + assert piece.original_value == value |
| 37 | + assert piece.converted_value == value |
| 38 | + if isinstance(entry, ObjectiveHumanLabeledEntry): |
| 39 | + assert entry.objective == "00456" |
| 40 | + assert entry.human_scores == [True] |
| 41 | + else: |
| 42 | + assert isinstance(entry, HarmHumanLabeledEntry) |
| 43 | + assert entry.harm_category == "00456" |
| 44 | + assert entry.human_scores == [0.5] |
| 45 | + |
| 46 | + |
16 | 47 | @pytest.fixture |
17 | 48 | def sample_messages(): |
18 | 49 | return [ |
|
0 commit comments