From 75fbf2d02f27113aaecc8096f71cfe7e1b5ed02d Mon Sep 17 00:00:00 2001 From: Hanne Heggdal Date: Sat, 19 Apr 2025 11:22:57 +0200 Subject: [PATCH] test - simulate user input --- tests/unit/test_input.py | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/unit/test_input.py diff --git a/tests/unit/test_input.py b/tests/unit/test_input.py new file mode 100644 index 0000000..2f76b5c --- /dev/null +++ b/tests/unit/test_input.py @@ -0,0 +1,51 @@ +import unittest +from unittest.mock import patch +import datetime +import sys +import os +from src.my_package import date_to_unix # This is the module we are testing + +# This will make the absolute path from the root of the project, and will therefore work every time +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../src"))) + + +#the goal for this test is to simulate user input and check if the returned UNIX timestamps are correct, for this we use mock input + +class TestDateToUnix(unittest.TestCase): + + @patch('builtins.input', side_effect=["2024, 4, 13, 12, 30", "2024, 4, 14, 15, 45"]) + def test_get_unix_timestamp(self, mock_input): + _ = mock_input + + # Expected UNIX timestamps for the hardcoded inputs + expected_start = int(datetime.datetime(2024, 4, 13, 12, 30).timestamp()) + expected_end = int(datetime.datetime(2024, 4, 14, 15, 45).timestamp()) + + # Call the function which uses input() — the @patch above mocks input to return predefined values + unix_start, unix_end = date_to_unix.get_unix_timestamp() + + # Check that the returned timestamps match the expected values + self.assertEqual(unix_start, expected_start) + self.assertEqual(unix_end, expected_end) + + # test the function to ensure it correctly convert timestamp back to datetime + def test_from_unix_timestamp(self): + + # Create known datetime objects + start_dt = datetime.datetime(2024, 4, 13, 12, 30) + end_dt = datetime.datetime(2024, 4, 14, 15, 45) + + # Convert these to UNIX timestamps + unix_start = int(start_dt.timestamp()) + unix_end = int(end_dt.timestamp()) + + # Use the function to convert them back to datetime + start_from_unix, end_from_unix = date_to_unix.from_unix_timestamp(unix_start, unix_end) + + # Assert that the conversion back to datetime matches the original values + self.assertEqual(start_from_unix, start_dt) + self.assertEqual(end_from_unix, end_dt) + +# Run all tests in this file when the script is executed directly +if __name__ == '__main__': + unittest.main()