Skip to content

Commit

Permalink
add validation for API_KEY in .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
toravest committed May 22, 2025
1 parent 0588437 commit 477a10f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/my_package/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
14 changes: 10 additions & 4 deletions tests/unit/test_format_yeardata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 477a10f

Please sign in to comment.