The code below is my attempt to cache the response of a graphql query and a test to make sure the content is getting cached. I got this error: 'self' parameter lacking default value and it appears when running the test... Does anyone know what may be the issue? Is it a wrong implementation of mock? I feel like I can't think of any other way to write the test...
This is the code that appears on the view:
def fetch_cache_key(request):
""" Returns a hashed cache key. """
m = hashlib.md5()
m.update(request.body)
return m.hexdigest()
class GraphQLView(StaticApiKeyOrInternalMixin, GraphQLView):
""" GraphQL view for clients that use an API key. """
def super_call(self, request, *args, **kwargs):
""" Returns dispatch response """
response = super(GraphQLView, self).dispatch(self, request, *args, **kwargs)
return response
def dispatch(self, request, *args, **kwargs):
""" Fetches queried data from graphql and returns cached & hashed key """
cache_key = fetch_cache_key(request)
response_text = cache.get(cache_key)
if not response_text:
response = self.super_call(request, *args, **kwargs)
# cache key and value (all text)
cache.set(cache_key, response.text, 300)
else:
response = HttpResponse(content='response_text', content_type='application/json', status=200)
return response
This is the code that appears in the test:
class TestGraphqlView(SimpleTestCase):
nosegae_datastore_v3 = True
nosegae_memcache = True
nosegae_taskqueue = True
def setUp(self):
self.client = Client()
self.url = reverse('graphql_view')
self.headers = {
'HTTP_API_KEY': settings.VALID_API_KEYS[0],
}
@patch.object(GraphQLView, 'super_call', autospec=True)
def test_graphql_view(self, mock_super_call):
# Expects return value to be response = HttpResponse(content='response_text', content_type='application/json', status=200)
# mock_super_call.return_value.text = 'asdifua'
mock_super_call.return_value.text = HttpResponse(content='response_text', content_type='application/json', status=200)
response = self.client.post(
self.url,
data={
'portal_id': 'test_portal_id',
'language': 'en',
'page_size': 100,
},
**self.headers
)
# self.assertEqual(mock_super_call.called, True)
mock_super_call.assert_called_once_with()
Any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire