Debian Codegraph @

test/units/utils/test_helpers.py

raw
1 # (c) 2015, Marius Gedminas <[email protected]>
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 import pytest
7
8 from datetime import datetime
9
10 from ansible.utils.helpers import pct_to_int, object_to_dict, deduplicate_list
11
12
13 pct_to_int_testdata = [
14 pytest.param(
15 1, 100, 1, 1, id="positive_percentage"
16 ),
17 pytest.param(
18 -1, 100, 1, -1, id="negative_percentage"
19 ),
20 pytest.param(
21 "1%", 10, 1, 1, id="string_percentage"
22 ),
23 pytest.param(
24 "1%", 10, 0, 0, id="string_percentage_with_zero_min_value"
25 ),
26 pytest.param(
27 "1", 100, 1, 1, id="string_percentage_without_sign"
28 ),
29 pytest.param(
30 "10%", 100, 1, 10, id="string_percentage_two_digit"
31 )
32 ]
33
34
35 @pytest.mark.parametrize("value,num_items,min_value,expected", pct_to_int_testdata)
36 def test_pct_to_int(value, num_items, min_value, expected):
37 assert pct_to_int(value, num_items, min_value) == expected
38
39
40 def test_object_to_dict():
41 test_dict = object_to_dict(datetime(2024, 7, 30))
42 assert test_dict['day'] == 30
43 assert test_dict['year'] == 2024
44 assert test_dict['month'] == 7
45
46 test_dict_without_day = object_to_dict(datetime(2024, 7, 30), exclude=['day'])
47 assert 'day' not in list(test_dict_without_day.keys())
48
49
50 def test_deduplicate_list():
51 assert deduplicate_list([1, 2, 2, 3]) == [1, 2, 3]
52 assert deduplicate_list([1, 2, 3]) == [1, 2, 3]