|
| 1 | +# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 4 | +# with the License. A copy of the License is located at |
| 5 | +# |
| 6 | +# http://aws.amazon.com/apache2.0/ |
| 7 | +# |
| 8 | +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 9 | +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | +import pytest |
| 12 | + |
| 13 | +from pcluster.validators.utils import dig, is_boolean_string, is_valid_json, str_to_bool |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + "string_value, expected_result", |
| 18 | + [ |
| 19 | + ("true", True), |
| 20 | + ("True", True), |
| 21 | + ("TRUE", True), |
| 22 | + ("false", False), |
| 23 | + ("False", False), |
| 24 | + ("FALSE", False), |
| 25 | + ("yes", False), |
| 26 | + ("no", False), |
| 27 | + ("1", False), |
| 28 | + ("0", False), |
| 29 | + (None, False), |
| 30 | + ("", False), |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_str_to_bool(string_value, expected_result): |
| 34 | + assert str_to_bool(string_value) == expected_result |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "value, expected_result", |
| 39 | + [ |
| 40 | + ("true", True), |
| 41 | + ("True", True), |
| 42 | + ("TRUE", True), |
| 43 | + ("false", True), |
| 44 | + ("False", True), |
| 45 | + ("FALSE", True), |
| 46 | + (True, True), |
| 47 | + (False, True), |
| 48 | + (None, False), |
| 49 | + ("", False), |
| 50 | + ("yes", False), |
| 51 | + ("no", False), |
| 52 | + ("1", False), |
| 53 | + ("0", False), |
| 54 | + (1, False), |
| 55 | + (0, False), |
| 56 | + ], |
| 57 | +) |
| 58 | +def test_is_boolean_string(value, expected_result): |
| 59 | + assert is_boolean_string(value) == expected_result |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "json_string, expected_result", |
| 64 | + [ |
| 65 | + ('{"key": "value"}', True), |
| 66 | + ('{"nested": {"key": "value"}}', True), |
| 67 | + ("[]", True), |
| 68 | + ('[{"key": "value"}]', True), |
| 69 | + ("null", True), |
| 70 | + ("true", True), |
| 71 | + ("false", True), |
| 72 | + ('"string"', True), |
| 73 | + ("123", True), |
| 74 | + ("invalid json", False), |
| 75 | + ("{invalid: json}", False), |
| 76 | + ('{"unclosed": "json"', False), |
| 77 | + (None, False), |
| 78 | + ("", False), |
| 79 | + ], |
| 80 | +) |
| 81 | +def test_is_valid_json(json_string, expected_result): |
| 82 | + assert is_valid_json(json_string) == expected_result |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "dictionary, keys, expected_result", |
| 87 | + [ |
| 88 | + ({"a": {"b": {"c": "value"}}}, ("a", "b", "c"), "value"), |
| 89 | + ({"a": {"b": "value"}}, ("a", "b"), "value"), |
| 90 | + ({"a": "value"}, ("a",), "value"), |
| 91 | + ({"a": {"b": {"c": "value"}}}, ("a", "b"), {"c": "value"}), |
| 92 | + ({"a": {"b": {"c": "value"}}}, ("a", "nonexistent"), None), |
| 93 | + ({"a": {"b": {"c": "value"}}}, ("nonexistent",), None), |
| 94 | + ({"a": {"b": {"c": "value"}}}, ("a", "b", "c", "d"), None), |
| 95 | + ({}, ("a",), None), |
| 96 | + (None, ("a",), None), |
| 97 | + ({"a": None}, ("a", "b"), None), |
| 98 | + ({"a": "not_dict"}, ("a", "b"), None), |
| 99 | + ({"a": {"b": None}}, ("a", "b", "c"), None), |
| 100 | + ], |
| 101 | +) |
| 102 | +def test_dig(dictionary, keys, expected_result): |
| 103 | + assert dig(dictionary, *keys) == expected_result |
0 commit comments