Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/ambiguous location 729 #760

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
116 changes: 0 additions & 116 deletions projects/API Based Weather Report/API Based Weather Report.py

This file was deleted.

41 changes: 41 additions & 0 deletions projects/API Based Weather Report/EndToEnd_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from unittest.mock import patch
from io import StringIO
from main import main

class TestMainFunction(unittest.TestCase):

@patch('builtins.input', side_effect=["API_KEY", "London", "England"])
@patch('main.fetch_weather', return_value=(200, {
"main": {"temp": 280, "humidity": 80},
"weather": [{"description": "Cloudy"}],
"wind": {"speed": 4},
"sys": {"country": "GB"}
}))
@patch('main.write_to_file')
def test_main_success(self, mock_write_to_file, mock_fetch_weather, mock_input):
with patch('sys.stdout', new=StringIO()) as fake_out:
main()
output = fake_out.getvalue().strip()
self.assertIn("Current temperature is:", output)
self.assertIn("Current weather desc :", output)
self.assertIn("Current Humidity :", output)
self.assertIn("Current wind speed :", output)
self.assertIn("Country Code :", output)
mock_write_to_file.assert_called_once_with("London", {
"main": {"temp": 280, "humidity": 80},
"weather": [{"description": "Cloudy"}],
"wind": {"speed": 4},
"sys": {"country": "GB"}
})

@patch('builtins.input', side_effect=["API_KEY", "London","England"])
@patch('main.fetch_weather', return_value=(500, None))
def test_main_server_error(self, mock_fetch_weather, mock_input):
with patch('sys.stdout', new=StringIO()) as fake_out:
main()
output = fake_out.getvalue().strip()
self.assertIn("Failed to fetch weather data. Server error: Status Code 500", output)

if __name__ == '__main__':
unittest.main()
29 changes: 24 additions & 5 deletions projects/API Based Weather Report/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Weather Information App
# _Weather Information App_

Welcome to the Weather Information App! This application allows users to fetch current weather information for a specific city using the OpenWeatherMap API. The fetched data is then displayed on the console and saved to a text file for future reference.

Expand All @@ -13,7 +13,14 @@ Welcome to the Weather Information App! This application allows users to fetch c
- Python 3.x installed on your system.
- OpenWeatherMap API key. You can obtain it by signing up at [OpenWeatherMap](https://home.openweathermap.org/users/sign_up).

## Usage
## Tech
- Python 3.x
- Docstring documentation
- Json data
- Python Unit Tests
- Git Hub

## Installation

1. Obtain your OpenWeatherMap API key by signing up at [OpenWeatherMap](https://home.openweathermap.org/users/sign_up).

Expand All @@ -22,6 +29,9 @@ Welcome to the Weather Information App! This application allows users to fetch c
```bash
python weather_app.py
```
## Sequence Diagram
**Note:** The code has been broken into modules. Which constantly interacts with each other to provide various functionality.
![Weather API](weatherapi.png)

## Usage

Expand All @@ -34,15 +44,24 @@ Welcome to the Weather Information App! This application allows users to fetch c
```

3. Follow the on-screen prompts to enter your API key and the city name for which you want to fetch weather information.

#### _user journey1_ : user provides country name along with citi location
**Note:** When entering the city name, ensure to provide the following:
- Exact City Name: Spell the city name correctly to ensure accurate results (e.g., "New York", "London").
- Country (Optional): User is asked to provide optional Country name (e.g., "USA","Australia").
- Special Characters: Input special characters or diacritics correctly if applicable (e.g., "Paris", "München").
- Alternative Names: Use alternative or local names if known (e.g., "Mumbai" for "Bombay").
- City Name with Spaces: Input the city name with spaces as it appears (e.g., "Los Angeles", "San Francisco").
- City District or Area (Optional): Specify a district or area within larger cities for more localized weather data (e.g., "Manhattan, New York", "Shinjuku, Tokyo").
-
#### _user journey2_ : when user provides only citi location
**Note:** When entering the city name, ensure to provide the following:
- Exact City Name: Spell the city name correctly to ensure accurate results (e.g., "New York", "London").
- City Name and Country Code (Optional): Use the format "City, Country Code" to specify the country if needed (e.g., "London, UK", "Springfield, US").
- Country (Optional): Can press enter to optout the value.
- Special Characters: Input special characters or diacritics correctly if applicable (e.g., "Paris", "München").
- Alternative Names: Use alternative or local names if known (e.g., "Mumbai" for "Bombay").
- City Name with Spaces: Input the city name with spaces as it appears (e.g., "Los Angeles", "San Francisco").
- City District or Area (Optional): Specify a district or area within larger cities for more localized weather data (e.g., "Manhattan, New York", "Shinjuku, Tokyo").

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
15 changes: 15 additions & 0 deletions projects/API Based Weather Report/TestCountryCode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest
from getCountryCode import extract_key_value_from_file
class TestExtractKeyValueFromFile(unittest.TestCase):
def test_existing_key(self):
# Assuming 'countryCode.json' contains valid data
result = extract_key_value_from_file("usa")
self.assertEqual(result, "US")

def test_nonexistent_key(self):
# Assuming 'countryCode.json' contains valid data
result = extract_key_value_from_file("XYZ")
self.assertIsNone(result)

if __name__ == "__main__":
unittest.main()
93 changes: 93 additions & 0 deletions projects/API Based Weather Report/TestFetchWeather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import unittest
from weather_api import fetch_weather

class TestFetchWeather(unittest.TestCase):

# Test case for successful weather data retrieval
def test_successful_fetch(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = "Salem"
status_code, weather_data = fetch_weather(api_key, location, "USA")
self.assertEqual(status_code, 200)
#self.assertIsNotNone(weather_data)

# Test case for invalid API key
def test_invalid_api_key(self):
api_key = "INVALID_API_KEY"
location = "London"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertEqual(status_code, 401)

# Test case for invalid location
def test_invalid_location(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = "InvalidCityName"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertEqual(status_code, 404)

# Test case for missing API key
def test_missing_api_key(self):
api_key = ""
location = "London"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertNotEqual(status_code, 200)

# Test case for missing location
def test_missing_location(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = ""
status_code, weather_data = fetch_weather(api_key, location,"")
self.assertNotEqual(status_code, 200)

# Test case for invalid API key and location
def test_invalid_api_key_and_location(self):
api_key = "INVALID_API_KEY"
location = "InvalidCityName"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertNotEqual(status_code, 200)

# Test case for empty API key and location
def test_empty_api_key_and_location(self):
api_key = ""
location = ""
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertNotEqual(status_code, 200)

# Test case for invalid API key format
def test_invalid_api_key_format(self):
api_key = "INVALID_API_KEY_FORMAT"
location = "London"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertNotEqual(status_code, 200)

# Test case for invalid location format
def test_invalid_location_format(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = "InvalidCityName123"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertNotEqual(status_code, 200)

# Test case for valid location with special characters
def test_valid_location_with_special_characters(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = "New%20York"
status_code, weather_data = fetch_weather(api_key, location,"USA")
self.assertEqual(status_code, 200)

# Test case for valid location with invalid country
def test_valid_location_with_invalid_country(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = "New%20York"
status_code, weather_data = fetch_weather(api_key, location, "India")
self.assertEqual(status_code, 404)

# Test case for valid location with invalid country

def test_invalid_location_with_valid_country(self):
api_key = "e082b74fb59e29a4838166304b19c678"
location = "Patna"
status_code, weather_data = fetch_weather(api_key, location, "USA")
self.assertEqual(status_code, 404)

if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions projects/API Based Weather Report/countryCode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"india": "IN",
"england": "UK",
"usa": "US",
"australia": "AU"
}
Loading
Loading