|
13 | 13 | from __future__ import absolute_import |
14 | 14 |
|
15 | 15 | import inspect |
16 | | -from mock import Mock |
| 16 | +from mock import Mock, patch |
17 | 17 | import os |
18 | | -from sagemaker.fw_utils import create_image_uri, framework_name_from_image, framework_version_from_tag |
| 18 | +from sagemaker.fw_utils import create_image_uri, framework_name_from_image, framework_version_from_tag, \ |
| 19 | + model_code_key_prefix |
19 | 20 | from sagemaker.fw_utils import tar_and_upload_dir, parse_s3_url, UploadedCode, validate_source_dir |
20 | 21 | import pytest |
21 | 22 |
|
| 23 | +from sagemaker.utils import name_from_image |
22 | 24 |
|
23 | 25 | DATA_DIR = 'data_dir' |
24 | 26 | BUCKET_NAME = 'mybucket' |
25 | 27 | ROLE = 'Sagemaker' |
26 | 28 | REGION = 'us-west-2' |
27 | 29 | SCRIPT_PATH = 'script.py' |
| 30 | +TIMESTAMP = '2017-10-10-14-14-15' |
28 | 31 |
|
29 | 32 |
|
30 | 33 | @pytest.fixture() |
@@ -189,3 +192,37 @@ def test_parse_s3_url_fail(): |
189 | 192 | with pytest.raises(ValueError) as error: |
190 | 193 | parse_s3_url('t3://code_location') |
191 | 194 | assert 'Expecting \'s3\' scheme' in str(error) |
| 195 | + |
| 196 | + |
| 197 | +def test_model_code_key_prefix_with_all_values_present(): |
| 198 | + key_prefix = model_code_key_prefix('prefix', 'model_name', 'image_name') |
| 199 | + assert key_prefix == 'prefix/model_name' |
| 200 | + |
| 201 | + |
| 202 | +def test_model_code_key_prefix_with_no_prefix_and_all_other_values_present(): |
| 203 | + key_prefix = model_code_key_prefix(None, 'model_name', 'image_name') |
| 204 | + assert key_prefix == 'model_name' |
| 205 | + |
| 206 | + |
| 207 | +@patch('time.strftime', return_value=TIMESTAMP) |
| 208 | +def test_model_code_key_prefix_with_only_image_present(time): |
| 209 | + key_prefix = model_code_key_prefix(None, None, 'image_name') |
| 210 | + assert key_prefix == name_from_image('image_name') |
| 211 | + |
| 212 | + |
| 213 | +@patch('time.strftime', return_value=TIMESTAMP) |
| 214 | +def test_model_code_key_prefix_and_image_present(time): |
| 215 | + key_prefix = model_code_key_prefix('prefix', None, 'image_name') |
| 216 | + assert key_prefix == 'prefix/' + name_from_image('image_name') |
| 217 | + |
| 218 | + |
| 219 | +def test_model_code_key_prefix_with_prefix_present_and_others_none_fail(): |
| 220 | + with pytest.raises(TypeError) as error: |
| 221 | + model_code_key_prefix('prefix', None, None) |
| 222 | + assert 'expected string' in str(error) |
| 223 | + |
| 224 | + |
| 225 | +def test_model_code_key_prefix_with_all_none_fail(): |
| 226 | + with pytest.raises(TypeError) as error: |
| 227 | + model_code_key_prefix(None, None, None) |
| 228 | + assert 'expected string' in str(error) |
0 commit comments