7
7
from typing import Any
8
8
9
9
import pytest
10
+ import yaml
10
11
from dirty_equals import HasRepr , IsNumber
11
12
from inline_snapshot import snapshot
12
13
from pydantic import BaseModel , TypeAdapter
@@ -819,10 +820,29 @@ async def test_serialization_to_yaml(example_dataset: Dataset[TaskInput, TaskOut
819
820
# Test loading back
820
821
loaded_dataset = Dataset [TaskInput , TaskOutput , TaskMetadata ].from_file (yaml_path )
821
822
assert len (loaded_dataset .cases ) == 2
823
+ assert loaded_dataset .name == 'example'
822
824
assert loaded_dataset .cases [0 ].name == 'case1'
823
825
assert loaded_dataset .cases [0 ].inputs .query == 'What is 2+2?'
824
826
825
827
828
+ async def test_deserializing_without_name (
829
+ example_dataset : Dataset [TaskInput , TaskOutput , TaskMetadata ], tmp_path : Path
830
+ ):
831
+ """Test serializing a dataset to YAML."""
832
+ # Save the dataset
833
+ yaml_path = tmp_path / 'test_cases.yaml'
834
+ example_dataset .to_file (yaml_path )
835
+
836
+ # Rewrite the file _without_ a name to test deserializing a name-less file
837
+ obj = yaml .safe_load (yaml_path .read_text ())
838
+ obj .pop ('name' , None )
839
+ yaml_path .write_text (yaml .dump (obj ))
840
+
841
+ # Test loading results in the name coming from the filename stem
842
+ loaded_dataset = Dataset [TaskInput , TaskOutput , TaskMetadata ].from_file (yaml_path )
843
+ assert loaded_dataset .name == 'test_cases'
844
+
845
+
826
846
async def test_serialization_to_json (example_dataset : Dataset [TaskInput , TaskOutput , TaskMetadata ], tmp_path : Path ):
827
847
"""Test serializing a dataset to JSON."""
828
848
json_path = tmp_path / 'test_cases.json'
@@ -854,6 +874,7 @@ def test_serialization_errors(tmp_path: Path):
854
874
async def test_from_text ():
855
875
"""Test creating a dataset from text."""
856
876
dataset_dict = {
877
+ 'name' : 'my dataset' ,
857
878
'cases' : [
858
879
{
859
880
'name' : '1' ,
@@ -873,6 +894,7 @@ async def test_from_text():
873
894
}
874
895
875
896
loaded_dataset = Dataset [TaskInput , TaskOutput , TaskMetadata ].from_text (json .dumps (dataset_dict ))
897
+ assert loaded_dataset .name == 'my dataset'
876
898
assert loaded_dataset .cases == snapshot (
877
899
[
878
900
Case (
0 commit comments