Skip to content

Commit

Permalink
test - simulate user input
Browse files Browse the repository at this point in the history
  • Loading branch information
hannhegg committed Apr 19, 2025
1 parent 035d281 commit 75fbf2d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/unit/test_input.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 75fbf2d

Please sign in to comment.