|
| 1 | +# Copyright 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 | +"""Unit tests for pipeline parameters, focusing on hashability.""" |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from sagemaker.core.workflow.parameters import ( |
| 20 | + Parameter, |
| 21 | + ParameterBoolean, |
| 22 | + ParameterFloat, |
| 23 | + ParameterInteger, |
| 24 | + ParameterString, |
| 25 | + ParameterTypeEnum, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def all_parameters(): |
| 31 | + """Return one instance of each concrete parameter type.""" |
| 32 | + return [ |
| 33 | + ParameterString(name="MyString", default_value="foo"), |
| 34 | + ParameterInteger(name="MyInteger", default_value=1), |
| 35 | + ParameterFloat(name="MyFloat", default_value=1.0), |
| 36 | + ParameterBoolean(name="MyBoolean", default_value=True), |
| 37 | + ] |
| 38 | + |
| 39 | + |
| 40 | +def test_all_parameters_are_hashable(all_parameters): |
| 41 | + """Every concrete parameter type must be hashable.""" |
| 42 | + for param in all_parameters: |
| 43 | + assert isinstance(hash(param), int) |
| 44 | + |
| 45 | + |
| 46 | +def test_base_parameter_is_hashable(): |
| 47 | + """The base Parameter must be hashable.""" |
| 48 | + param = Parameter(name="Base", parameter_type=ParameterTypeEnum.STRING) |
| 49 | + assert isinstance(hash(param), int) |
| 50 | + |
| 51 | + |
| 52 | +def test_parameters_usable_as_set_members(all_parameters): |
| 53 | + """Parameters must be usable as set members without raising.""" |
| 54 | + param_set = set(all_parameters) |
| 55 | + assert len(param_set) == len(all_parameters) |
| 56 | + for param in all_parameters: |
| 57 | + assert param in param_set |
| 58 | + |
| 59 | + |
| 60 | +def test_parameters_usable_as_dict_keys(all_parameters): |
| 61 | + """Parameters must be usable as dict keys without raising.""" |
| 62 | + mapping = {param: param.name for param in all_parameters} |
| 63 | + for param in all_parameters: |
| 64 | + assert mapping[param] == param.name |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + "factory, kwargs", |
| 69 | + [ |
| 70 | + (ParameterString, {"name": "P", "default_value": "v"}), |
| 71 | + (ParameterInteger, {"name": "P", "default_value": 3}), |
| 72 | + (ParameterFloat, {"name": "P", "default_value": 3.0}), |
| 73 | + (ParameterBoolean, {"name": "P", "default_value": False}), |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_equal_parameters_hash_equal(factory, kwargs): |
| 77 | + """Equal parameters must hash to the same value (eq/hash invariant).""" |
| 78 | + first = factory(**kwargs) |
| 79 | + second = factory(**kwargs) |
| 80 | + assert first == second |
| 81 | + assert hash(first) == hash(second) |
| 82 | + |
| 83 | + |
| 84 | +def test_different_type_parameters_are_distinct(): |
| 85 | + """Parameters of the same name but different type are unequal and distinct. |
| 86 | +
|
| 87 | + Note hash *inequality* is not part of the hashing contract (collisions are |
| 88 | + legal), so we assert on equality/distinct-membership rather than hashes. |
| 89 | + """ |
| 90 | + integer_param = ParameterInteger(name="Shared") |
| 91 | + float_param = ParameterFloat(name="Shared") |
| 92 | + assert integer_param != float_param |
| 93 | + assert len({integer_param, float_param}) == 2 |
| 94 | + |
| 95 | + |
| 96 | +def test_parameter_string_dedupes_equal_instances(): |
| 97 | + """ParameterString remains hashable and dedupes equal instances in a set.""" |
| 98 | + first = ParameterString(name="Dup", default_value="x") |
| 99 | + second = ParameterString(name="Dup", default_value="x") |
| 100 | + assert len({first, second}) == 1 |
| 101 | + |
| 102 | + |
| 103 | +def test_parameter_string_hash_invariant_ignores_enum_values(): |
| 104 | + """Equal ParameterStrings that differ only in enum_values must hash equal. |
| 105 | +
|
| 106 | + ``enum_values`` is not part of attrs equality, so two such instances are |
| 107 | + equal; the old ``hash(tuple(self.to_request()))`` implementation broke the |
| 108 | + ``a == b`` implies ``hash(a) == hash(b)`` invariant here. |
| 109 | + """ |
| 110 | + with_enum = ParameterString(name="P", enum_values=["a", "b"]) |
| 111 | + without_enum = ParameterString(name="P") |
| 112 | + assert with_enum == without_enum |
| 113 | + assert hash(with_enum) == hash(without_enum) |
0 commit comments