def test_resources_are_cached(self, mock_request): query = Query(self.client, Tour) self.assertEqual(self.cache.count(), 0) query.get(21346) self.assertEqual(self.cache.count(), 1) self.assertEqual(len(mock_request.mock_calls), 1) query.get(21346) self.assertEqual(len(mock_request.mock_calls), 1)
def test_resources_are_cached(self, mock_request): query = Query(self.client, Tour) self.assertEqual(self.cache.count(), 0) query.get(21346) self.assertEqual(self.cache.count(), 1) self.assertEqual(len(mock_request.mock_calls), 1) query.get(21346) self.assertEqual(len(mock_request.mock_calls), 1)
def test_get_instance_by_id_with_non_404_error(self, mock_request): response = Response() response.status_code = 401 http_error = HTTPError(response=response) mock_request.side_effect = http_error query = Query(self.client, Tour) with self.assertRaises(HTTPError) as cm: query.get(1234) self.assertEqual(cm.exception.response.status_code, 401)
def test_get_instance_by_id_with_non_404_error(self, mock_request): response = Response() response.status_code = 401 http_error = HTTPError(response=response) mock_request.side_effect = http_error query = Query(self.client, Tour) with self.assertRaises(HTTPError) as cm: query.get(1234) self.assertEqual(cm.exception.response.status_code, 401)
def test_get_instance_with_gone_id(self, mock_request): response = Response() response.status_code = 410 http_error = HTTPError(response=response) mock_request.side_effect = http_error # default behaviour is to return None... query = Query(self.client, Tour) self.assertIsNone(query.get(1234)) # ... but if this status code is not in the list of ones to explicitly # turn into Nones, then it will get raised: with self.assertRaises(HTTPError) as context: query.get(1234, httperrors_mapped_to_none=None) self.assertEqual(context.exception.response.status_code, response.status_code)
def test_get_instance_with_gone_id(self, mock_request): response = Response() response.status_code = 410 http_error = HTTPError(response=response) mock_request.side_effect = http_error query = Query(self.client, Tour) t = query.get(1234) self.assertIsNone(t)
def test_get_instance_with_gone_id(self, mock_request): response = Response() response.status_code = 410 http_error = HTTPError(response=response) mock_request.side_effect = http_error query = Query(self.client, Tour) t = query.get(1234) self.assertIsNone(t)
def test_get_instance_by_id_with_non_404_error(self, mock_request): response = Response() response.status_code = 401 http_error = HTTPError(response=response) mock_request.side_effect = http_error # default behaviour is to raise... query = Query(self.client, Tour) with self.assertRaises(HTTPError) as context: query.get(1234) self.assertEqual(context.exception.response.status_code, response.status_code) # ... but if we don't want that exception raised, then we can include # that status code in our `httperrors_mapped_to_none` and verify that a # `None` is returned: self.assertIsNone( query.get(1234, httperrors_mapped_to_none=[response.status_code]))
def test_cached_get_does_not_set(self): """ Regression test https://github.com/gadventures/gapipy/issues/65 We discovered that when getting a resource, even if it is a hit in our local cache we would set the data back into our cache every time. When using a cache with a TTL on keys (e.g. Redis) this has the effect of resetting the TTL each time that that key is retrieved. This is not the expected behaviour wrt cache key TTLs. """ query = Query(self.client, Tour) # act like we already have the data in our cache mock_cache_get = MagicMock(return_value=PPP_TOUR_DATA) self.cache.get = mock_cache_get mock_cache_set = MagicMock() self.cache.set = mock_cache_set query.get(21346) self.assertEqual(len(mock_cache_get.mock_calls), 1) self.assertEqual(len(mock_cache_set.mock_calls), 0)
def test_cached_get_does_not_set(self): """ Regression test https://github.com/gadventures/gapipy/issues/65 We discovered that when getting a resource, even if it is a hit in our local cache we would set the data back into our cache every time. When using a cache with a TTL on keys (e.g. Redis) this has the effect of resetting the TTL each time that that key is retrieved. This is not the expected behaviour wrt cache key TTLs. """ query = Query(self.client, Tour) # act like we already have the data in our cache mock_cache_get = MagicMock(return_value=PPP_TOUR_DATA) self.cache.get = mock_cache_get mock_cache_set = MagicMock() self.cache.set = mock_cache_set query.get(21346) self.assertEqual(len(mock_cache_get.mock_calls), 1) self.assertEqual(len(mock_cache_set.mock_calls), 0)
def test_get_instance_by_id(self, mock_request): query = Query(self.client, Tour) t = query.get(1234) self.assertIsInstance(t, Tour)
def test_get_instance_by_id(self, mock_request): query = Query(self.client, Tour) t = query.get(1234) self.assertIsInstance(t, Tour)