|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +import pytest |
| 14 | +import unittest.mock |
| 15 | +import datetime |
| 16 | + |
| 17 | +from smexperiments import trial, api_types |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def sagemaker_boto_client(): |
| 22 | + return unittest.mock.Mock() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def datetime_obj(): |
| 27 | + return datetime.datetime(2017, 6, 16, 15, 55, 0) |
| 28 | + |
| 29 | +def test_list_trial_components(sagemaker_boto_client, datetime_obj): |
| 30 | + sagemaker_boto_client.list_trial_components.return_value = { |
| 31 | + "TrialComponentSummaries": [ |
| 32 | + { |
| 33 | + "TrialComponentName": "trial-component-1", |
| 34 | + "CreationTime": datetime_obj, |
| 35 | + "LastModifiedTime": datetime_obj, |
| 36 | + }, |
| 37 | + { |
| 38 | + "TrialComponentName": "trial-component-2", |
| 39 | + "CreationTime": datetime_obj, |
| 40 | + "LastModifiedTime": datetime_obj, |
| 41 | + }, |
| 42 | + ] |
| 43 | + } |
| 44 | + expected = [ |
| 45 | + api_types.TrialComponentSummary( |
| 46 | + trial_component_name="trial-component-1", |
| 47 | + creation_time=datetime_obj, |
| 48 | + last_modified_time=datetime_obj, |
| 49 | + ), |
| 50 | + api_types.TrialComponentSummary( |
| 51 | + trial_component_name="trial-component-2", |
| 52 | + creation_time=datetime_obj, |
| 53 | + last_modified_time=datetime_obj, |
| 54 | + ), |
| 55 | + ] |
| 56 | + |
| 57 | + trial_obj = trial.Trial(sagemaker_boto_client=sagemaker_boto_client) |
| 58 | + |
| 59 | + assert expected == list(trial_obj.list_trial_components()) |
| 60 | + |
| 61 | + |
| 62 | +def test_list_trial_components_empty(sagemaker_boto_client): |
| 63 | + sagemaker_boto_client.list_trial_components.return_value = {"TrialComponentSummaries": []} |
| 64 | + trial_obj = trial.Trial(sagemaker_boto_client=sagemaker_boto_client) |
| 65 | + assert list(trial_obj.list_trial_components()) == [] |
| 66 | + |
| 67 | + |
| 68 | +def test_list_trial_components_single(sagemaker_boto_client, datetime_obj): |
| 69 | + trial_obj = trial.Trial(sagemaker_boto_client=sagemaker_boto_client) |
| 70 | + sagemaker_boto_client.list_trial_components.return_value = { |
| 71 | + "TrialComponentSummaries": [ |
| 72 | + { |
| 73 | + "TrialComponentName": "trial-component-foo", |
| 74 | + "CreationTime": datetime_obj, |
| 75 | + "LastModifiedTime": datetime_obj |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + assert list(trial_obj.list_trial_components()) == [ |
| 81 | + api_types.TrialComponentSummary( |
| 82 | + trial_component_name="trial-component-foo", |
| 83 | + creation_time=datetime_obj, |
| 84 | + last_modified_time=datetime_obj |
| 85 | + ) |
| 86 | + ] |
| 87 | + |
| 88 | + |
| 89 | +def test_list_trial_components_two_values(sagemaker_boto_client, datetime_obj): |
| 90 | + trial_obj = trial.Trial(sagemaker_boto_client=sagemaker_boto_client) |
| 91 | + sagemaker_boto_client.list_trial_components.return_value = { |
| 92 | + "TrialComponentSummaries": [ |
| 93 | + {"TrialComponentName": "trial-component-foo-1", "CreationTime": datetime_obj, "LastModifiedTime": datetime_obj}, |
| 94 | + {"TrialComponentName": "trial-component-foo-2", "CreationTime": datetime_obj, "LastModifiedTime": datetime_obj}, |
| 95 | + ] |
| 96 | + } |
| 97 | + |
| 98 | + assert list(trial_obj.list_trial_components()) == [ |
| 99 | + api_types.TrialComponentSummary( |
| 100 | + trial_component_name="trial-component-foo-1", |
| 101 | + creation_time=datetime_obj, |
| 102 | + last_modified_time=datetime_obj |
| 103 | + ), |
| 104 | + api_types.TrialComponentSummary( |
| 105 | + trial_component_name="trial-component-foo-2", |
| 106 | + creation_time=datetime_obj, |
| 107 | + last_modified_time=datetime_obj |
| 108 | + ), |
| 109 | + ] |
| 110 | + |
| 111 | + |
| 112 | +def test_next_token(sagemaker_boto_client, datetime_obj): |
| 113 | + trial_obj = trial.Trial(sagemaker_boto_client) |
| 114 | + sagemaker_boto_client.list_trial_components.side_effect = [ |
| 115 | + { |
| 116 | + "TrialComponentSummaries": [ |
| 117 | + { |
| 118 | + "TrialComponentName": "trial-component-foo-1", |
| 119 | + "CreationTime": datetime_obj, |
| 120 | + "LastModifiedTime": datetime_obj, |
| 121 | + }, |
| 122 | + { |
| 123 | + "TrialComponentName": "trial-component-foo-2", |
| 124 | + "CreationTime": datetime_obj, |
| 125 | + "LastModifiedTime": datetime_obj, |
| 126 | + }, |
| 127 | + ], |
| 128 | + "NextToken": "foo", |
| 129 | + }, |
| 130 | + { |
| 131 | + "TrialComponentSummaries": [ |
| 132 | + { |
| 133 | + "TrialComponentName": "trial-component-foo-3", |
| 134 | + "CreationTime": datetime_obj, |
| 135 | + "LastModifiedTime": datetime_obj, |
| 136 | + } |
| 137 | + ] |
| 138 | + }, |
| 139 | + ] |
| 140 | + |
| 141 | + assert list(trial_obj.list_trial_components()) == [ |
| 142 | + api_types.TrialComponentSummary( |
| 143 | + trial_component_name="trial-component-foo-1", creation_time=datetime_obj, last_modified_time=datetime_obj |
| 144 | + ), |
| 145 | + api_types.TrialComponentSummary( |
| 146 | + trial_component_name="trial-component-foo-2", creation_time=datetime_obj, last_modified_time=datetime_obj |
| 147 | + ), |
| 148 | + api_types.TrialComponentSummary( |
| 149 | + trial_component_name="trial-component-foo-3", creation_time=datetime_obj, last_modified_time=datetime_obj |
| 150 | + ), |
| 151 | + ] |
| 152 | + |
| 153 | + sagemaker_boto_client.list_trial_components.assert_any_call(**{}) |
| 154 | + sagemaker_boto_client.list_trial_components.assert_any_call(NextToken="foo") |
| 155 | + |
| 156 | + |
| 157 | +def test_list_trial_components_call_args(sagemaker_boto_client): |
| 158 | + created_before = datetime.datetime(1999, 10, 12, 0, 0, 0) |
| 159 | + created_after = datetime.datetime(1990, 10, 12, 0, 0, 0) |
| 160 | + trial_name = 'foo-trial' |
| 161 | + next_token = 'thetoken' |
| 162 | + max_results = 99 |
| 163 | + |
| 164 | + trial_obj = trial.Trial(sagemaker_boto_client=sagemaker_boto_client) |
| 165 | + trial_obj.trial_name=trial_name |
| 166 | + |
| 167 | + sagemaker_boto_client.list_trial_components.return_value = {} |
| 168 | + assert [] == list( |
| 169 | + trial_obj.list_trial_components( |
| 170 | + created_after=created_after, |
| 171 | + created_before=created_before, |
| 172 | + next_token=next_token, |
| 173 | + max_results=max_results) |
| 174 | + ) |
| 175 | + sagemaker_boto_client.list_trial_components.assert_called_with( |
| 176 | + CreatedBefore=created_before, |
| 177 | + CreatedAfter=created_after, |
| 178 | + TrialName=trial_name, |
| 179 | + NextToken=next_token, |
| 180 | + MaxResults=max_results, |
| 181 | + ) |
0 commit comments