|
| 1 | +# Copyright 2020 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 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import pasta |
| 16 | +import pytest |
| 17 | + |
| 18 | +from sagemaker.cli.compatibility.v2.modifiers import training_input |
| 19 | +from tests.unit.sagemaker.cli.compatibility.v2.modifiers.ast_converter import ast_call, ast_import |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def constructors(): |
| 24 | + return ( |
| 25 | + "sagemaker.session.ShuffleConfig(seed)", |
| 26 | + "session.ShuffleConfig(seed)", |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def modified_constructors(constructors): |
| 32 | + return [c.replace("session", "inputs") for c in constructors] |
| 33 | + |
| 34 | + |
| 35 | +def test_constructor_node_should_be_modified(constructors): |
| 36 | + modifier = training_input.ShuffleConfigModuleRenamer() |
| 37 | + for constructor in constructors: |
| 38 | + node = ast_call(constructor) |
| 39 | + assert modifier.node_should_be_modified(node) |
| 40 | + |
| 41 | + |
| 42 | +def test_constructor_node_should_be_modified_random_call(): |
| 43 | + modifier = training_input.ShuffleConfigModuleRenamer() |
| 44 | + node = ast_call("FileSystemInput()") |
| 45 | + assert not modifier.node_should_be_modified(node) |
| 46 | + |
| 47 | + |
| 48 | +def test_constructor_modify_node(constructors, modified_constructors): |
| 49 | + modifier = training_input.ShuffleConfigModuleRenamer() |
| 50 | + |
| 51 | + for before, expected in zip(constructors, modified_constructors): |
| 52 | + node = ast_call(before) |
| 53 | + modifier.modify_node(node) |
| 54 | + assert expected == pasta.dump(node) |
| 55 | + |
| 56 | + |
| 57 | +def test_import_from_node_should_be_modified_training_input(): |
| 58 | + modifier = training_input.ShuffleConfigImportFromRenamer() |
| 59 | + node = ast_import("from sagemaker.session import ShuffleConfig") |
| 60 | + assert modifier.node_should_be_modified(node) |
| 61 | + |
| 62 | + |
| 63 | +def test_import_from_node_should_be_modified_random_import(): |
| 64 | + modifier = training_input.ShuffleConfigImportFromRenamer() |
| 65 | + node = ast_import("from sagemaker.session import Session") |
| 66 | + assert not modifier.node_should_be_modified(node) |
| 67 | + |
| 68 | + |
| 69 | +def test_import_from_modify_node(): |
| 70 | + modifier = training_input.ShuffleConfigImportFromRenamer() |
| 71 | + node = ast_import("from sagemaker.session import ShuffleConfig") |
| 72 | + |
| 73 | + modifier.modify_node(node) |
| 74 | + assert "from sagemaker.inputs import ShuffleConfig" == pasta.dump(node) |
0 commit comments