general_posts/python-weather-api/test/test_api.py

79 lines
2.2 KiB
Python

from test.base import BaseTestCase
class TestAPI(BaseTestCase):
def test_unauthorized_error(self):
url = "/api/current/boston"
response = self.client.get(url)
self.assertEqual(response.status_code, 401)
def test_authorized_request(self):
url = "/api/current/boston?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_current_weather_success(self):
url = "/api/current/boston?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
data = response.get_json().get("data")
entry = data[0]
self.assertEqual(entry.get("city"), "boston")
def test_current_weather_not_found(self):
url = "/api/current/doesntexistcity?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_forecast_weather_success(self):
url = "/api/forecast/boston?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
data = response.get_json().get("data")
entry = data[0]
self.assertEqual(entry.get("city"), "boston")
def test_forecast_weather_not_found(self):
url = "/api/forecast/doesntexistcity?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_historical_weather_success(self):
url = "/api/historical/boston?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
url = "/api/historical/boston/2023-01-01?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_historical_weather_not_found(self):
url = "/api/historical/doesntexistcity?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_historical_weather_bad_date_format(self):
url = "/api/historical/boston/01-01-2023?api_key=TEST_KEY"
response = self.client.get(url)
self.assertEqual(response.status_code, 400)