Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a framework independent top level testing module. #71

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
50 changes: 50 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [2.7.18, 3.4.10, 3.5.10, 3.6.15, 3.7.12, 3.8.12, 3.9.10, 3.10.2, '3.11.0-alpha.4']

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/[email protected]
with:
python-version: ${{ matrix.python-version }}

- uses: actions/cache@v2
id: pip-cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-${{ matrix.platform }}-pip-${{ matrix.python-version }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-${{ matrix.platform }}-pip-${{ matrix.python-version }}-

- name: Install dependencies
run: |
pip install tox

- name: Run test
run: |
tox
env:
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
PLATFORM: ${{ matrix.platform }}

- name: "Upload coverage to Codecov"
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
23 changes: 0 additions & 23 deletions .travis.yml

This file was deleted.

91 changes: 43 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ It currently supports Python 2.7 and 3.4+.
## Installation:

```shell script

pip install graphene-file-upload

```

## Usage
Expand All @@ -24,8 +22,10 @@ To add an upload type to your mutation, import and use `Upload`.
Upload is a scalar type.

```python
import graphene
from graphene_file_upload.scalars import Upload


class UploadMutation(graphene.Mutation):
class Arguments:
file = Upload(required=True)
Expand All @@ -44,10 +44,12 @@ To use, import the view, then add to your list of urls (replace previous
GraphQL view).

```python
from django.urls import path
from graphene_file_upload.django import FileUploadGraphQLView


urlpatterns = [
url(r'^graphql', FileUploadGraphQLView.as_view(graphiql=True)),
path('graphql', FileUploadGraphQLView.as_view(graphiql=True)),
]
```

Expand All @@ -60,8 +62,12 @@ writing this README, you must install `flask-graphql` with
Simply import the modified view and create a new url rule on your app:

```python
from flask import Flask
from graphene_file_upload.flask import FileUploadGraphQLView


app = Flask(__name__)
app.debug = True
app.add_url_rule(
'/graphql',
view_func=FileUploadGraphQLView.as_view(
Expand All @@ -76,15 +82,15 @@ app.add_url_rule(

https://flask.palletsprojects.com/en/1.1.x/testing/#the-testing-skeleton

```py
```python
# Create a fixture using the file_graphql_query helper and `client` fixture.
import os
import json
import tempfile

from flaskr import flaskr
import pytest
from graphene_file_upload.flask.testing import file_graphql_query
from graphene_file_upload.testing import file_graphql_query


@pytest.fixture
Expand All @@ -101,31 +107,28 @@ def client():
os.unlink(flaskr.app.config['DATABASE'])


@pytest.fixture
def client_query(client):
def func(*args, **kwargs):
return file_graphql_query(*args, **kwargs, client=client)

return func

# Test your query using the client_query fixture
def test_some_query(client_query):
test_file = SimpleUploadedFile(name='test.txt', content=file_text.encode('utf-8'))
def test_some_query(client):
with tempfile.NamedTemporaryFile() as test_file:
test_file.write(b"test")
test_file.seek(0)

response = client_query(
'''
mutation testMutation($file: Upload!) {
myUpload(fileIn: $file) {
ok
query = '''
mutation testMutation($file: Upload!) {
myUpload(fileIn: $file) {
ok
}
}
}
''',
op_name='testMutation'
files={'file': test_file},
)

content = json.loads(response.content)
assert 'errors' not in content
'''

response = file_graphql_query(
query,
op_name='testMutation',
files={'file': test_file},
client=client,
)

content = json.loads(response.content)
assert 'errors' not in content
```

### Django
Expand All @@ -137,33 +140,26 @@ Writing test using [django's test client](https://docs.djangoproject.com/en/3.1/
To use pytest define a simple fixture using the query helper below

```py
# Create a fixture using the file_graphql_query helper and `client` fixture from `pytest-django`.

import json
import pytest
from graphene_file_upload.django.testing import file_graphql_query

@pytest.fixture
def client_query(client):
def func(*args, **kwargs):
return file_graphql_query(*args, **kwargs, client=client)
from django.core.files.uploadedfile import SimpleUploadedFile
from graphene_file_upload.testing import file_graphql_query

return func

# Test your query using the client_query fixture
def test_some_query(client_query):
test_file = SimpleUploadedFile(name='test.txt', content=file_text.encode('utf-8'))
def test_some_query(client):
test_file = SimpleUploadedFile(name='test.txt', content=b"test")

response = client_query(
response = file_graphql_query(
'''
mutation testMutation($file: Upload!) {
myUpload(fileIn: $file) {
ok
}
}
''',
op_name='testMutation'
op_name='testMutation',
files={'file': test_file},
client=client,
)

content = json.loads(response.content)
Expand All @@ -172,16 +168,15 @@ def test_some_query(client_query):

#### Using unittest

Your endpoint is set through the `GRAPHQL_URL` attribute on `GraphQLFileUploadTestCase`. The default endpoint is `GRAPHQL_URL = “/graphql/”`.
Your endpoint is set through the `GRAPHQL_URL` attribute on `GraphQLFileUploadTestCase`. The default endpoint is `GRAPHQL_URL = “/graphql”`.

```py
import json

from graphene_file_upload.django.testing import GraphQLFileUploadTestCase
from django.core.files.uploadedfile import SimpleUploadedFile
from graphene_file_upload.django.testing import GraphQLFileUploadSimpleTestCase

class MutationTestCase(GraphQLFileUploadTestCase):
class MutationTestCase(GraphQLFileUploadSimpleTestCase):
def test_some_mutation(self):
test_file = SimpleUploadedFile(name='test.txt', content=file_text.encode('utf-8'))
test_file = SimpleUploadedFile(name='test.txt', content=b"test")

response = self.file_query(
'''
Expand Down
Loading