-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |