diff --git a/env.py b/env.py index b160dc2..0e412e4 100644 --- a/env.py +++ b/env.py @@ -70,6 +70,28 @@ def validate_date_format(env_var_name: str) -> str: return date_to_validate +def validate_date_range(start_date: str, end_date: str) -> None: + """Validate that start_date is before end_date. + + Does nothing if either date is not set. + + Args: + start_date: The start date string in YYYY-MM-DD format. + end_date: The end date string in YYYY-MM-DD format. + + Raises: + ValueError: If end_date is before or equal to start_date. + """ + if not start_date or not end_date: + return + + # YYYY-MM-DD format allows direct string comparison + if end_date <= start_date: + raise ValueError( + f"END_DATE ('{end_date}') must be after START_DATE ('{start_date}')" + ) + + def get_env_vars( test: bool = False, ) -> tuple[ @@ -142,6 +164,7 @@ def get_env_vars( start_date = validate_date_format("START_DATE") end_date = validate_date_format("END_DATE") + validate_date_range(start_date, end_date) sponsor_info = get_bool_env_var("SPONSOR_INFO", False) link_to_profile = get_bool_env_var("LINK_TO_PROFILE", False) diff --git a/test_env.py b/test_env.py index 638e6e5..6059760 100644 --- a/test_env.py +++ b/test_env.py @@ -222,6 +222,65 @@ def test_get_env_vars_auth_with_github_app_installation_missing_inputs(self): "GH_APP_ID set and GH_APP_INSTALLATION_ID or GH_APP_PRIVATE_KEY variable not set", ) + @patch.dict( + os.environ, + { + "ORGANIZATION": "org", + "REPOSITORY": "repo", + "GH_TOKEN": "token", + "START_DATE": "2025-01-01", + "END_DATE": "2024-01-01", + }, + clear=True, + ) + def test_get_env_vars_end_date_before_start_date(self): + """Test that an error is raised when END_DATE is before START_DATE""" + with self.assertRaises(ValueError) as cm: + env.get_env_vars() + the_exception = cm.exception + self.assertEqual( + str(the_exception), + "END_DATE ('2024-01-01') must be after START_DATE ('2025-01-01')", + ) + + @patch.dict( + os.environ, + { + "ORGANIZATION": "org", + "REPOSITORY": "repo", + "GH_TOKEN": "token", + "START_DATE": "2024-01-01", + "END_DATE": "2024-01-01", + }, + clear=True, + ) + def test_get_env_vars_equal_start_and_end_date(self): + """Test that an error is raised when START_DATE equals END_DATE""" + with self.assertRaises(ValueError) as cm: + env.get_env_vars() + the_exception = cm.exception + self.assertEqual( + str(the_exception), + "END_DATE ('2024-01-01') must be after START_DATE ('2024-01-01')", + ) + + @patch.dict( + os.environ, + { + "ORGANIZATION": "org", + "REPOSITORY": "repo", + "GH_TOKEN": "token", + "START_DATE": "2024-01-01", + "END_DATE": "2025-01-01", + }, + clear=True, + ) + def test_get_env_vars_valid_date_range(self): + """Test that valid date range (START_DATE before END_DATE) is accepted""" + result = env.get_env_vars() + self.assertEqual(result[8], "2024-01-01") # start_date + self.assertEqual(result[9], "2025-01-01") # end_date + if __name__ == "__main__": unittest.main()