different GET params different MOCK, but still the first one's data returned
See original GitHub issueInside one test case I add two responses for GET with different url in terms of GET params.
what happens?
my test case inside in class overriden by django.test.TestCase looks like:
@responses.activate
def test_address_is_updated_correctly_when_spot_location_changed(self):
latitude_1, longitude_1 = 52.226297, 20.982749
url = REVERSE_GEOCODING_URL.format(
latitude=latitude_1,
longitude=longitude_1,
api_key=settings.GOOGLE_MAP_API_KEY
)
responses.add(
responses.GET,
url,
body=json.dumps(RESPONSE_GEOCODING_MOCK_2),
content_type="application/json"
)
s1 = SpotFactory(
name='test',
location=Point(longitude_1, latitude_1)
)
s1.save()
street_before = s1.address_street
self.assertEqual(street_before, 'Kolejowa')
latitude_2, longitude_2 = 52.27904, 20.980366
url2 = REVERSE_GEOCODING_URL.format(
latitude=latitude_2,
longitude=longitude_2,
api_key=settings.GOOGLE_MAP_API_KEY
)
responses.add(
responses.GET,
url2,
body=json.dumps(RESPONSE_GEOCODING_MOCK),
content_type="application/json"
)
# optionally IPDB here and done requests.get(url2) and got responses with RESPONSE_GEOCODING_MOCK_2
s1.location.coords = (longitude_2, latitude_2)
s1.save()
s1 = Spot.objects.get(pk=s1.pk)
self.assertEqual(s1.address_street, 'Mickiewicza')
firs assert is OK
the second one FAILS
because, the response is from the old one mock.
DEBUG: see the place optionally IPDB here and done requests.get(url2) and got responses with RESPONSE_GEOCODING_MOCK_2
When doing first time request I get (RESPONSE_GEOCODING_MOCK_2):
ipdb> import requests; requests.get(url2).json() {‘status’: ‘OK’, ‘results’: [{‘geometry’: {‘bounds’: {‘southwest’: {‘lat’: 52.2261726, ‘lng’: 20.9823615}, ‘northeast’: {‘lat’: 52.22673450000001, ‘lng’: 20.9833889}}, ‘viewport’: {‘southwest’: {‘lat’: 52.22510456970851, ‘lng’: 20.9815262197085}, ‘northeast’: {‘lat’: 52.22780253029151, ‘lng’: 20.98422418029151}}, ‘location’: {‘lat’: 52.2263065, ‘lng’: 20.9828941}, ‘location_type’: ‘ROOFTOP’}, ‘place_id’: ‘ChIJW6aw25vMHkcRxgIq9iMcxA8’, ‘types’: [‘establishment’, ‘point_of_interest’, ‘premise’], ‘formatted_address’: ‘Kolejowa 47, 01-210 Warszawa, Poland’, ‘address_components’: [{‘types’: [‘street_number’], ‘short_name’: ‘47’, ‘long_name’: ‘47’}, {‘types’: [‘route’], ‘short_name’: ‘Kolejowa’, ‘long_name’: ‘Kolejowa’}, {‘types’: [‘political’, ‘sublocality’, ‘sublocality_level_1’], ‘short_name’: ‘Wola’, ‘long_name’: ‘Wola’}, {‘types’: [‘locality’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_2’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_1’, ‘political’], ‘short_name’: ‘mazowieckie’, ‘long_name’: ‘mazowieckie’}, {‘types’: [‘country’, ‘political’], ‘short_name’: ‘PL’, ‘long_name’: ‘Poland’}, {‘types’: [‘postal_code’], ‘short_name’: ‘01-210’, ‘long_name’: ‘01-210’}]}]}
when doing it for second time I get (RESPONSE_GEOCODING_MOCK):
ipdb> import requests; requests.get(url2).json() {‘status’: ‘OK’, ‘results’: [{‘geometry’: {‘viewport’: {‘southwest’: {‘lat’: 52.2776733197085, ‘lng’: 20.9792990197085}, ‘northeast’: {‘lat’: 52.2803712802915, ‘lng’: 20.9819969802915}}, ‘location’: {‘lat’: 52.2790223, ‘lng’: 20.980648}, ‘location_type’: ‘ROOFTOP’}, ‘place_id’: ‘ChIJxXoMrOfLHkcRBAFsrjKGcMc’, ‘types’: [‘street_address’], ‘formatted_address’: ‘Mickiewicza 74, Warszawa, Poland’, ‘address_components’: [{‘types’: [‘street_number’], ‘short_name’: ‘74’, ‘long_name’: ‘74’}, {‘types’: [‘route’], ‘short_name’: ‘Mickiewicza’, ‘long_name’: ‘Mickiewicza’}, {‘types’: [‘political’, ‘sublocality’, ‘sublocality_level_1’], ‘short_name’: ‘Żoliborz’, ‘long_name’: ‘Żoliborz’}, {‘types’: [‘locality’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_2’, ‘political’], ‘short_name’: ‘Warszawa’, ‘long_name’: ‘Warszawa’}, {‘types’: [‘administrative_area_level_1’, ‘political’], ‘short_name’: ‘mazowieckie’, ‘long_name’: ‘mazowieckie’}, {‘types’: [‘country’, ‘political’], ‘short_name’: ‘PL’, ‘long_name’: ‘Poland’}]}]}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:6 (1 by maintainers)
Top GitHub Comments
Make sure you use
match_querystring=True
if you’re using GET params in your URL.Was there ever any movement on this? Seeing the same issue. Did @andilabs provide a PR with example?
EDIT:
So I spent a little more time investigating and this will only happen when your tests are defined within a class like:
In the above example the JSON from
test_1
is used for test_2 as well therefore the assertion fails.if I remove the class definition and just run the functions on their own with decorators still attached, then its fine.Nevermind this doesnt work either.