forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dataset.py
More file actions
277 lines (247 loc) · 11.9 KB
/
test_dataset.py
File metadata and controls
277 lines (247 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Integration tests for DataSet."""
import os
import time
import pytest
from sagemaker.ai_registry.dataset import DataSet
from sagemaker.ai_registry.dataset_utils import CustomizationTechnique
from sagemaker.ai_registry.air_constants import HubContentStatus
from unittest.mock import patch
@pytest.mark.serial
class TestDataSetIntegration:
"""Integration tests for DataSet operations."""
def test_create_dataset_from_local_file(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test creating dataset from local file."""
dataset = DataSet.create(
name=unique_name,
source=sample_jsonl_file,
customization_technique=CustomizationTechnique.SFT,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.arn is not None
assert dataset.version is not None
assert dataset.customization_technique == CustomizationTechnique.SFT
def test_create_dataset_from_s3_oss_sft(self, unique_name, test_bucket, cleanup_list):
"""Test creating SFT dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/OSS/oss_sft_train.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
customization_technique=CustomizationTechnique.SFT,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.customization_technique == CustomizationTechnique.SFT
def test_create_dataset_from_s3_oss_rlvr(self, unique_name, test_bucket, cleanup_list):
"""Test creating RLVR dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/OSS/oss_rlvr_train.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
customization_technique=CustomizationTechnique.RLVR,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.customization_technique == CustomizationTechnique.RLVR
def test_create_dataset_from_s3_oss_dpo(self, unique_name, test_bucket, cleanup_list):
"""Test creating RLVR dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/OSS/oss_dpo_train.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
customization_technique=CustomizationTechnique.DPO,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.customization_technique == CustomizationTechnique.DPO
def test_create_dataset_from_s3_nova_sft(self, unique_name, test_bucket, cleanup_list):
"""Test creating RLVR dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/Nova/nova_sft_train.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
customization_technique=CustomizationTechnique.SFT,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.customization_technique == CustomizationTechnique.SFT
def test_create_dataset_from_s3_nova_dpo(self, unique_name, test_bucket, cleanup_list):
"""Test creating RLVR dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/Nova/nova_dpo_train.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
customization_technique=CustomizationTechnique.DPO,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.customization_technique == CustomizationTechnique.DPO
def test_create_dataset_from_s3_nova_rft(self, unique_name, test_bucket, cleanup_list):
"""Test creating RLVR dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/Nova/nova_rft_train.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
customization_technique=CustomizationTechnique.RLVR,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
assert dataset.customization_technique == CustomizationTechnique.RLVR
def test_create_dataset_from_s3_nova_eval(self, unique_name, test_bucket, cleanup_list):
"""Test creating RLVR dataset from S3 URI."""
s3_uri = f"s3://{test_bucket}/test_datasets/Nova/nova_eval.jsonl"
dataset = DataSet.create(
name=unique_name,
source=s3_uri,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
def test_get_dataset(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test retrieving dataset by name."""
created = DataSet.create(name=unique_name, source=sample_jsonl_file, wait=False)
cleanup_list.append(created)
retrieved = DataSet.get(unique_name)
assert retrieved.name == created.name
assert retrieved.arn == created.arn
def test_get_all_datasets(self):
"""Test listing all datasets."""
datasets = list(DataSet.get_all(max_results=5))
assert isinstance(datasets, list)
def test_dataset_refresh(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test refreshing dataset status."""
dataset = DataSet.create(name=unique_name, source=sample_jsonl_file, wait=False)
cleanup_list.append(dataset)
dataset.refresh()
time.sleep(3)
assert dataset.status in [HubContentStatus.IMPORTING.value, HubContentStatus.AVAILABLE.value]
def test_dataset_get_versions(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test getting dataset versions."""
dataset = DataSet.create(name=unique_name, source=sample_jsonl_file, wait=False)
cleanup_list.append(dataset)
versions = dataset.get_versions()
assert len(versions) >= 1
assert all(isinstance(v, DataSet) for v in versions)
def test_dataset_delete(self, unique_name, sample_jsonl_file):
"""Test deleting dataset."""
dataset = DataSet.create(name=unique_name, source=sample_jsonl_file, wait=False)
result = dataset.delete()
assert result is True
def test_dataset_delete_by_name(self, unique_name, sample_jsonl_file):
"""Test deleting dataset by name."""
DataSet.create(name=unique_name, source=sample_jsonl_file, wait=False)
result = DataSet.delete_by_name(unique_name)
assert result is True
def test_dataset_wait(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test waiting for dataset to be available."""
dataset = DataSet.create(name=unique_name, source=sample_jsonl_file, wait=True)
time.sleep(3)
cleanup_list.append(dataset)
assert dataset.status == HubContentStatus.AVAILABLE.value
def test_create_dataset_version(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test creating new dataset version."""
dataset = DataSet.create(name=unique_name, source=sample_jsonl_file, wait=False)
result = dataset.create_version(sample_jsonl_file)
cleanup_list.append(dataset)
assert result is True
def test_dataset_validation_invalid_extension(self, unique_name):
"""Test dataset validation with invalid file extension."""
with pytest.raises(ValueError, match="Unsupported file extension"):
DataSet._validate_dataset_file("test.txt")
def test_create_dataset_with_invalid_format_s3(self, unique_name, test_bucket):
"""Test creating dataset from S3 with invalid format fails."""
# This would require an actual invalid file in S3, so we'll mock it
with patch('sagemaker.ai_registry.dataset.AIRHub.download_from_s3'), \
patch('sagemaker.ai_registry.dataset.DataSet._validate_dataset_format', side_effect=ValueError("Invalid format")):
with pytest.raises(ValueError, match="Invalid format"):
DataSet.create(
name=unique_name,
source=f"s3://{test_bucket}/invalid_file.jsonl",
wait=False
)
def test_create_dataset_with_invalid_format_local(self, unique_name):
"""Test creating dataset from local file with invalid format fails."""
import tempfile
with tempfile.NamedTemporaryFile(suffix='.jsonl', mode='w', delete=False) as f:
f.write("invalid content")
f.flush()
try:
with pytest.raises(ValueError, match="Unable to detect format"):
DataSet.create(
name=unique_name,
source=f.name,
wait=False
)
finally:
os.unlink(f.name)
def test_dataset_validation_large_file(self, unique_name):
"""Test dataset validation with oversized file."""
import tempfile
with tempfile.NamedTemporaryFile(suffix='.jsonl', delete=False) as f:
f.write(b'x' * (1024 * 1024 * 1024 + 1)) # > 1GB
f.flush()
with pytest.raises(ValueError, match="exceeds maximum allowed size"):
DataSet._validate_dataset_file(f.name)
os.unlink(f.name)
def test_dataset_with_description(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test creating dataset with description."""
description = "Test dataset description"
dataset = DataSet.create(
name=unique_name,
source=sample_jsonl_file,
description=description,
wait=False
)
cleanup_list.append(dataset)
assert dataset.description is not None
def test_dataset_with_tags(self, unique_name, sample_jsonl_file, cleanup_list):
"""Test creating dataset with custom tags."""
tags = [("env", "test"), ("team", "ml")]
dataset = DataSet.create(
name=unique_name,
source=sample_jsonl_file,
tags=tags,
wait=False
)
cleanup_list.append(dataset)
assert dataset.name == unique_name
def test_dataset_format_validation_success(self, unique_name, sample_jsonl_file):
"""Test dataset format validation succeeds for valid files."""
# Should not raise any exception for valid JSONL file
DataSet._validate_dataset_format(sample_jsonl_file)
def test_dataset_format_validation_failure_invalid_format(self, unique_name):
"""Test dataset format validation fails for invalid format."""
import tempfile
with tempfile.NamedTemporaryFile(suffix='.jsonl', mode='w', delete=False) as f:
f.write("invalid json content")
f.flush()
with pytest.raises(ValueError, match="Unable to detect format"):
DataSet._validate_dataset_format(f.name)
os.unlink(f.name)
def test_dataset_format_validation_failure_empty_file(self, unique_name):
"""Test dataset format validation fails for empty files."""
import tempfile
with tempfile.NamedTemporaryFile(suffix='.jsonl', delete=False) as f:
f.flush() # Create empty file
with pytest.raises(ValueError, match="Unable to detect format"):
DataSet._validate_dataset_format(f.name)
os.unlink(f.name)