|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# # Copyright: (c) 2022, XLAB Steampunk <steampunk@xlab.si> |
| 3 | +# |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +__metaclass__ = type |
| 9 | + |
| 10 | +import sys |
| 11 | +import pytest |
| 12 | +import datetime |
| 13 | + |
| 14 | +from ansible_collections.scale_computing.hypercore.roles.check_local_time.files import ( |
| 15 | + check_local_time, |
| 16 | +) |
| 17 | + |
| 18 | +# from ansible_collections.scale_computing.hypercore.plugins.module_utils import ( |
| 19 | +# check_local_time, |
| 20 | +# ) |
| 21 | + |
| 22 | +from ansible_collections.scale_computing.hypercore.plugins.module_utils.utils import ( |
| 23 | + MIN_PYTHON_VERSION, |
| 24 | +) |
| 25 | + |
| 26 | +pytestmark = pytest.mark.skipif( |
| 27 | + sys.version_info < MIN_PYTHON_VERSION, |
| 28 | + reason=f"requires python{MIN_PYTHON_VERSION[0]}.{MIN_PYTHON_VERSION[1]} or higher", |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +class TestCheckLocalTime: |
| 33 | + def test_get_local_time(self): |
| 34 | + local_time = check_local_time.get_local_time("Europe/Ljubljana") |
| 35 | + assert type(local_time) == datetime.datetime |
| 36 | + |
| 37 | + def test_get_time_interval(self): |
| 38 | + start_time, end_time = check_local_time.get_time_interval("22:31-4:45") |
| 39 | + start_time_str = datetime.datetime.strftime(start_time, "%H:%M") |
| 40 | + end_time_str = datetime.datetime.strftime(end_time, "%H:%M") |
| 41 | + |
| 42 | + assert start_time_str == "22:31" |
| 43 | + assert end_time_str == "04:45" |
| 44 | + |
| 45 | + @pytest.mark.parametrize( |
| 46 | + "time_interval, expected_result", |
| 47 | + [ |
| 48 | + ("22:30-1:30", "False"), |
| 49 | + ("6:30-08:30", "False"), |
| 50 | + ("12:30-13:00", "True"), |
| 51 | + ("1:00-12:30", "False"), |
| 52 | + ("1:00-12:31", "True"), |
| 53 | + ("22:00-12:30", "False"), |
| 54 | + ("22:00-12:31", "True"), |
| 55 | + ], |
| 56 | + ) |
| 57 | + def test_is_local_time_in_time_interval( |
| 58 | + self, time_interval, expected_result, capfd |
| 59 | + ): |
| 60 | + local_time = datetime.datetime.now() |
| 61 | + local_time_constant = local_time.replace(hour=12, minute=30) |
| 62 | + |
| 63 | + start_time, end_time = check_local_time.get_time_interval(time_interval) |
| 64 | + check_local_time.is_local_time_in_time_interval( |
| 65 | + local_time_constant, start_time, end_time |
| 66 | + ) |
| 67 | + result, err = capfd.readouterr() |
| 68 | + |
| 69 | + assert result.strip() == expected_result # strip removes "\n" |
| 70 | + |
| 71 | + @pytest.mark.parametrize( |
| 72 | + "time_interval, expected_result", |
| 73 | + [ |
| 74 | + ("22:30-1:30", "False"), |
| 75 | + ("6:30-08:30", "False"), |
| 76 | + ("12:30-13:00", "True"), |
| 77 | + ("1:00-12:30", "False"), |
| 78 | + ("1:00-12:31", "True"), |
| 79 | + ("22:00-12:30", "False"), |
| 80 | + ("22:00-12:31", "True"), |
| 81 | + ], |
| 82 | + ) |
| 83 | + def test_main(self, mocker, time_interval, expected_result, capfd): |
| 84 | + local_time = datetime.datetime.now() |
| 85 | + local_time_constant = local_time.replace(hour=12, minute=30) |
| 86 | + mocker.patch( |
| 87 | + "ansible_collections.scale_computing.hypercore.roles.check_local_time.files.check_local_time.get_local_time" |
| 88 | + ).return_value = local_time_constant |
| 89 | + |
| 90 | + check_local_time.main("Europe/Ljubljana", time_interval) |
| 91 | + result, err = capfd.readouterr() |
| 92 | + |
| 93 | + assert result.strip() == expected_result |
0 commit comments