Skip to content

Commit f5b2f6a

Browse files
committed
Adding new test cases
1 parent b424f46 commit f5b2f6a

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

search/tests.py

+53-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
from django.urls import reverse
1313
from search.helpers.photo import UnplashCityPhotoHelper
1414
from urllib.request import urlopen
15-
from unittest.mock import patch
15+
from unittest.mock import MagicMock, patch
1616
from info.models import FavCityEntry
1717
from search.helpers.photo import UnplashCityPhotoHelper
1818
from urllib.request import urlopen
1919
from info.models import ItineraryItem
20-
from django.utils import timezone # Added import for timezone
20+
from django.utils import timezone
21+
22+
from search.utils.search import AmadeusCitySearch, GeoDB # Added import for timezone
2123

2224
image_formats = ("image/png", "image/jpeg", "image/gif")
2325

@@ -654,3 +656,52 @@ def test_profile_page_and_fav_city(self):
654656
self.assertContains(response, 'Paris')
655657

656658
# # should add more tests relating to the profile page
659+
class TestGeoDB(TestCase):
660+
def setUp(self):
661+
# Provide required arguments for the URL class
662+
self.url = URL(protocol="https", host="test-geodb.com", port=None)
663+
self.geo_db = GeoDB(url=self.url)
664+
665+
def tearDown(self):
666+
self.geo_db = None
667+
668+
def test_get_city_suggestions_success(self):
669+
with patch("requests.request") as mock_request:
670+
mock_response = MagicMock()
671+
mock_response.json.return_value = {"data": [{"city": "Test City", "country": "Test Country"}]}
672+
mock_request.return_value = mock_response
673+
674+
city_suggestions = self.geo_db.get_city_suggestions(city="Test")
675+
self.assertIn("data", city_suggestions)
676+
self.assertEqual(city_suggestions["data"][0]["city"], "Test City")
677+
678+
def test_get_city_suggestions_failure(self):
679+
with patch("requests.request") as mock_request:
680+
mock_response = MagicMock()
681+
mock_response.json.return_value = {"error": "Invalid Request"}
682+
mock_request.return_value = mock_response
683+
684+
city_suggestions = self.geo_db.get_city_suggestions(city="Invalid")
685+
self.assertIn("error", city_suggestions)
686+
self.assertEqual(city_suggestions["error"], "Invalid Request")
687+
688+
689+
class TestAmadeusCitySearch(TestCase):
690+
def setUp(self):
691+
# Provide required arguments for the URL class
692+
self.url = URL(protocol="https", host="test-amadeus.com", port=None)
693+
self.city_search = AmadeusCitySearch(url=self.url)
694+
695+
def tearDown(self):
696+
self.city_search = None
697+
698+
def test_get_city_suggestions_success(self):
699+
with patch("requests.request") as mock_request:
700+
mock_response = MagicMock()
701+
mock_response.json.return_value = {"data": [{"name": "New York"}]}
702+
mock_request.return_value = mock_response
703+
704+
self.city_search._access_token = "test_access_token"
705+
city_suggestions = self.city_search.get_city_suggestions(city="New York")
706+
self.assertIn("data", city_suggestions)
707+
self.assertEqual(city_suggestions["data"][0]["name"], "New York")

0 commit comments

Comments
 (0)