diff --git a/tests/unit/test_format.py b/tests/unit/test_format.py new file mode 100644 index 0000000..82be88f --- /dev/null +++ b/tests/unit/test_format.py @@ -0,0 +1,63 @@ +import unittest +import os + +# Function to check if the .env file is created correctly +def check_env_file_creation(): + env_filepath = os.path.join(os.path.dirname(__file__), "../../.env") + + # Check if the .env file exists + if not os.path.exists(env_filepath): + return False, f".env file does not exist at {env_filepath}" + + # Check if the file contains the expected keys + with open(env_filepath, 'r') as env_file: + file_content = env_file.read() + + if 'API_EMAIL' not in file_content or 'API_KEY' not in file_content: + return False, "The .env file is missing 'API_EMAIL' or 'API_KEY'." + + return True, ".env file is correctly created with API credentials." + +# Function to check if the API_EMAIL and API_KEY values are formatted correctly +def check_api_credentials(): + env_filepath = os.path.join(os.path.dirname(__file__), "../../.env") + + with open(env_filepath, 'r') as env_file: + lines = env_file.readlines() + + api_email = None + api_key = None + + for line in lines: + if 'API_EMAIL' in line: + api_email = line.strip().split('=')[1].strip().replace('"', '') + elif 'API_KEY' in line: + api_key = line.strip().split('=')[1].strip().replace('"', '') + + if not api_email or not api_key: + return False, "API_EMAIL or API_KEY is missing or formatted incorrectly." + + # Validate the email format (basic validation for '@' symbol) + if '@' not in api_email: + return False, "API_EMAIL format is incorrect." + + # Validate API_KEY length (assuming it's a standard length for OpenWeatherMap keys) + if len(api_key) < 32: # OpenWeatherMap keys are typically longer + return False, "API_KEY format is incorrect." + + return True, "API credentials are valid and properly formatted." + +class TestDataFormatConsistency(unittest.TestCase): + + # Test if the .env file is created correctly + def test_env_file_creation(self): + valid, message = check_env_file_creation() + self.assertTrue(valid, message) + + # Test if the API credentials are correct + def test_api_credentials(self): + valid, message = check_api_credentials() + self.assertTrue(valid, message) + +if __name__ == '__main__': + unittest.main()