|
| 1 | +import pytest |
| 2 | +import copy |
| 3 | +from azure.ai.evaluation._evaluate._evaluate_aoai import _combine_item_schemas |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def default_data_source_config(): |
| 8 | + return { |
| 9 | + "type": "custom", |
| 10 | + "item_schema": { |
| 11 | + "type": "object", |
| 12 | + "properties": { |
| 13 | + "id": {"type": "string"}, |
| 14 | + "text": {"type": "string"}, |
| 15 | + }, |
| 16 | + "required": ["id", "text"], |
| 17 | + }, |
| 18 | + "include_sample_schema": False, |
| 19 | + } |
| 20 | + |
| 21 | + |
| 22 | +class TestCombineItemSchemas: |
| 23 | + """Unit tests for _combine_item_schemas""" |
| 24 | + |
| 25 | + def test_combine_item_schemas_success(self, default_data_source_config): |
| 26 | + data_source_config = copy.deepcopy(default_data_source_config) |
| 27 | + kwargs = { |
| 28 | + "item_schema": { |
| 29 | + "properties": { |
| 30 | + "metadata": {"type": "object"}, |
| 31 | + "timestamp": {"type": "string", "format": "date-time"}, |
| 32 | + }, |
| 33 | + "required": ["metadata"], |
| 34 | + } |
| 35 | + } |
| 36 | + _combine_item_schemas(data_source_config, kwargs) |
| 37 | + |
| 38 | + expected_properties = { |
| 39 | + "id": {"type": "string"}, |
| 40 | + "text": {"type": "string"}, |
| 41 | + "metadata": {"type": "object"}, |
| 42 | + "timestamp": {"type": "string", "format": "date-time"}, |
| 43 | + } |
| 44 | + expected_required = ["id", "text", "metadata"] |
| 45 | + assert data_source_config["item_schema"]["properties"] == expected_properties |
| 46 | + assert data_source_config["item_schema"]["required"] == expected_required |
| 47 | + |
| 48 | + def test_combine_item_schemas_without_item_schema(self, default_data_source_config): |
| 49 | + data_source_config = copy.deepcopy(default_data_source_config) |
| 50 | + |
| 51 | + expected_properties = { |
| 52 | + "id": {"type": "string"}, |
| 53 | + "text": {"type": "string"}, |
| 54 | + } |
| 55 | + expected_required = ["id", "text"] |
| 56 | + |
| 57 | + # No "item_schema" in kwargs |
| 58 | + kwargs = {} |
| 59 | + _combine_item_schemas(data_source_config, kwargs) |
| 60 | + assert data_source_config["item_schema"]["properties"] == expected_properties |
| 61 | + assert data_source_config["item_schema"]["required"] == expected_required |
| 62 | + |
| 63 | + # "item_schema" without "properties" in kwargs |
| 64 | + kwargs = {"item_schema": {}} |
| 65 | + _combine_item_schemas(data_source_config, kwargs) |
| 66 | + assert data_source_config["item_schema"]["properties"] == expected_properties |
| 67 | + assert data_source_config["item_schema"]["required"] == expected_required |
| 68 | + |
| 69 | + def test_combine_item_schemas_with_empty_external_properties(self, default_data_source_config): |
| 70 | + data_source_config = copy.deepcopy(default_data_source_config) |
| 71 | + kwargs = { |
| 72 | + "item_schema": { |
| 73 | + "properties": {}, |
| 74 | + "required": [], |
| 75 | + } |
| 76 | + } |
| 77 | + _combine_item_schemas(data_source_config, kwargs) |
| 78 | + |
| 79 | + expected_properties = { |
| 80 | + "id": {"type": "string"}, |
| 81 | + "text": {"type": "string"}, |
| 82 | + } |
| 83 | + expected_required = ["id", "text"] |
| 84 | + |
| 85 | + assert data_source_config["item_schema"]["properties"] == expected_properties |
| 86 | + assert data_source_config["item_schema"]["required"] == expected_required |
| 87 | + |
| 88 | + def test_combine_item_schemas_with_external_properties_without_required(self, default_data_source_config): |
| 89 | + data_source_config = copy.deepcopy(default_data_source_config) |
| 90 | + kwargs = { |
| 91 | + "item_schema": { |
| 92 | + "properties": { |
| 93 | + "metadata": {"type": "object"}, |
| 94 | + "timestamp": {"type": "string", "format": "date-time"}, |
| 95 | + }, |
| 96 | + } |
| 97 | + } |
| 98 | + _combine_item_schemas(data_source_config, kwargs) |
| 99 | + |
| 100 | + expected_properties = { |
| 101 | + "id": {"type": "string"}, |
| 102 | + "text": {"type": "string"}, |
| 103 | + "metadata": {"type": "object"}, |
| 104 | + "timestamp": {"type": "string", "format": "date-time"}, |
| 105 | + } |
| 106 | + expected_required = ["id", "text"] |
| 107 | + |
| 108 | + assert data_source_config["item_schema"]["properties"] == expected_properties |
| 109 | + assert data_source_config["item_schema"]["required"] == expected_required |
0 commit comments