Django GraphQL Testing Images

Viewed 96

I'm currently performing pytest to test my GraphQL mutations. In this case, I input a test image file to determine if it's working. The following is a sample test in which the SimpleUploadedFile object throws a not JSON serializable error. What could be the cause and how should I fix this?

@pytest.mark.django_db
class TestUploadImage:
    query = """
        mutation uploadImage($input: Upload!) {
          uploadImage(file: $input) {
            __typename
            ... on ResponseErrors {
              errors {
                __typename
                code
                message
              }
            }
            ... on Output {
              url
            }
          }
        }
    """

    file = SimpleUploadedFile(
        name='test_image.jpg',
        content=b'',
        content_type='image/jpeg'
    )

    input_data = {"file": file}

    def test_non_authenticated_request(self, user, client, client_query):
        response = client_query(
            self.query,
            op_name="uploadMessageImage",
            input_data=self.input_data,
        )

        # Verify that there were no unhandled exceptions
        content = json.loads(response.content)
        assert "errors" not in content

        # Verify that AuthenticationError is in the response data
        assert AuthenticationError.__name__ in str(response.content)
0 Answers
Related