diff --git a/src/my_package/data.py b/src/my_package/data.py index 23889d1..55fda66 100644 --- a/src/my_package/data.py +++ b/src/my_package/data.py @@ -4,12 +4,18 @@ from dotenv import load_dotenv import json import pandas as pd +import sys load_dotenv() -# Gets the key, from my env file +# Gets the key, from the .env file API_KEY = os.getenv("API_KEY") +# Check if API_KET exsist in the .env file +if API_KEY is None: + print("Error! Missing the environment variable: `API_KEY`. Check `resources/README.md` for setup configurations.") + raise EnvironmentError("Missing API_KEY") # Raise error + # Gets the current data from the API - openweathermap.org def fetch_current_data(city_name): diff --git a/tests/unit/test_format_yeardata.py b/tests/unit/test_format_yeardata.py index ee73a61..7c5363b 100644 --- a/tests/unit/test_format_yeardata.py +++ b/tests/unit/test_format_yeardata.py @@ -72,11 +72,17 @@ def test_fetch_data_failure(self, mock_get): def test_api_key_in_env(self): # Check if the API_KEY is loaded from the environment - api_key = os.getenv("API_KEY") + API_KEY = os.getenv("API_KEY") + + + # Check if API_KET exsist in the .env file + if API_KEY is None: + print("Error! Missing the environment variable: `API_KEY`. Check `resources/README.md` for setup configurations.") + raise EnvironmentError("Missing API_KEY") # Raise error - self.assertIsNotNone(api_key, "API_KEY is not set in the environment.") - self.assertIsInstance(api_key, str) - self.assertGreater(len(api_key), 0, "API_KEY should not be an empty string.") + self.assertIsNotNone(API_KEY, "API_KEY is not set in the environment.") + self.assertIsInstance(API_KEY, str) + self.assertGreater(len(API_KEY), 0, "API_KEY should not be an empty string.") if __name__ == '__main__': unittest.main()