Skip to content

Commit

Permalink
Merge e6ad588 into 05c89c1
Browse files Browse the repository at this point in the history
  • Loading branch information
phalt committed May 2, 2019
2 parents 05c89c1 + e6ad588 commit 9cdc08b
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 62 deletions.
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
language: python
sudo: false
sudo: required
dist: xenial
python:
- 2.7
- 3.4
- 3.5
- 3.6
- 3.7
install:
- |
if [ "$TEST_TYPE" = build ]; then
pip install -e .[test]
pip install psycopg2 # Required for Django postgres fields testing
pip install psycopg2==2.8.2 # Required for Django postgres fields testing
pip install django==$DJANGO_VERSION
python setup.py develop
elif [ "$TEST_TYPE" = lint ]; then
pip install flake8
pip install flake8==3.7.7
fi
script:
- |
Expand Down Expand Up @@ -45,10 +47,16 @@ matrix:
env: TEST_TYPE=build DJANGO_VERSION=2.1
- python: '3.6'
env: TEST_TYPE=build DJANGO_VERSION=2.1
- python: '3.6'
env: TEST_TYPE=build DJANGO_VERSION=2.2
- python: '3.7'
env: TEST_TYPE=build DJANGO_VERSION=2.2
- python: '2.7'
env: TEST_TYPE=lint
- python: '3.6'
env: TEST_TYPE=lint
- python: '3.7'
env: TEST_TYPE=lint
deploy:
provider: pypi
user: syrusakbary
Expand Down
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ After developing, the full test suite can be evaluated by running:
make tests
```

## Opening Pull Requests

Please fork the project and open a pull request against the master branch.

This will trigger a series of test and lint checks.

We advise that you format and run lint locally before doing this to save time:

```sh
make format
make lint
```

## Documentation

The [documentation](http://docs.graphene-python.org/projects/django/en/latest/) is generated using the excellent [Sphinx](http://www.sphinx-doc.org/) and a custom theme.
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
dev-setup:
pip install -e ".[test]"
pip install -e ".[dev]"

tests:
py.test graphene_django --cov=graphene_django -vv
py.test graphene_django --cov=graphene_django -vv

format:
black graphene_django

lint:
flake8 graphene_django
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ To learn more check out the following [examples](examples/):

## Contributing

See [CONTRIBUTING.md](contributing.md)
See [CONTRIBUTING.md](CONTRIBUTING.md)
8 changes: 6 additions & 2 deletions graphene_django/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class MissingType(object):
try:
# Postgres fields are only available in Django with psycopg2 installed
# and we cannot have psycopg2 on PyPy
from django.contrib.postgres.fields import (ArrayField, HStoreField,
JSONField, RangeField)
from django.contrib.postgres.fields import (
ArrayField,
HStoreField,
JSONField,
RangeField,
)
except ImportError:
ArrayField, HStoreField, JSONField, RangeField = (MissingType,) * 4
29 changes: 8 additions & 21 deletions graphene_django/debug/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

class DjangoDebugSQL(ObjectType):
class Meta:
description = (
"Represents a single database query made to a Django managed DB."
)
description = "Represents a single database query made to a Django managed DB."

vendor = String(
required=True,
Expand All @@ -14,37 +12,26 @@ class Meta:
),
)
alias = String(
required=True,
description="The Django database alias (e.g. 'default').",
required=True, description="The Django database alias (e.g. 'default')."
)
sql = String(description="The actual SQL sent to this database.")
duration = Float(
required=True,
description="Duration of this database query in seconds.",
required=True, description="Duration of this database query in seconds."
)
raw_sql = String(
required=True,
description="The raw SQL of this query, without params.",
required=True, description="The raw SQL of this query, without params."
)
params = String(
required=True,
description="JSON encoded database query parameters.",
)
start_time = Float(
required=True,
description="Start time of this database query.",
)
stop_time = Float(
required=True,
description="Stop time of this database query.",
required=True, description="JSON encoded database query parameters."
)
start_time = Float(required=True, description="Start time of this database query.")
stop_time = Float(required=True, description="Stop time of this database query.")
is_slow = Boolean(
required=True,
description="Whether this database query took more than 10 seconds.",
)
is_select = Boolean(
required=True,
description="Whether this database query was a SELECT.",
required=True, description="Whether this database query was a SELECT."
)

# Postgres
Expand Down
5 changes: 1 addition & 4 deletions graphene_django/debug/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ class DjangoDebug(ObjectType):
class Meta:
description = "Debugging information for the current query."

sql = List(
DjangoDebugSQL,
description="Executed SQL queries for this API query.",
)
sql = List(DjangoDebugSQL, description="Executed SQL queries for this API query.")
4 changes: 1 addition & 3 deletions graphene_django/filter/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class GrapheneFilterSetMixin(BaseFilterSet):

FILTER_DEFAULTS = dict(
itertools.chain(
FILTER_FOR_DBFIELD_DEFAULTS.items(),
GRAPHENE_FILTER_SET_OVERRIDES.items()
FILTER_FOR_DBFIELD_DEFAULTS.items(), GRAPHENE_FILTER_SET_OVERRIDES.items()
)
)

Expand All @@ -59,7 +58,6 @@ class GrapheneFilterSetMixin(BaseFilterSet):
from django.utils.text import capfirst

class GrapheneFilterSetMixinPython2(GrapheneFilterSetMixin):

@classmethod
def filter_for_reverse_field(cls, f, name):
"""Handles retrieving filters for reverse relationships
Expand Down
9 changes: 5 additions & 4 deletions graphene_django/forms/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyForm(forms.Form):
class PetForm(forms.ModelForm):
class Meta:
model = Pet
fields = '__all__'
fields = "__all__"


def test_needs_form_class():
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_exclude_fields_input_meta_fields(self):
class PetMutation(DjangoModelFormMutation):
class Meta:
form_class = PetForm
exclude_fields = ['id']
exclude_fields = ["id"]

self.assertEqual(PetMutation._meta.model, Pet)
self.assertEqual(PetMutation._meta.return_field_name, "pet")
Expand Down Expand Up @@ -102,7 +102,9 @@ class Meta:

pet = Pet.objects.create(name="Axel", age=10)

result = PetMutation.mutate_and_get_payload(None, None, id=pet.pk, name="Mia", age=10)
result = PetMutation.mutate_and_get_payload(
None, None, id=pet.pk, name="Mia", age=10
)

self.assertEqual(Pet.objects.count(), 1)
pet.refresh_from_db()
Expand Down Expand Up @@ -132,7 +134,6 @@ class Meta:
# A pet was not created
self.assertEqual(Pet.objects.count(), 0)


fields_w_error = [e.field for e in result.errors]
self.assertEqual(len(result.errors), 2)
self.assertIn("name", fields_w_error)
Expand Down
2 changes: 1 addition & 1 deletion graphene_django/management/commands/graphql_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def handle(self, *args, **options):

indent = options.get("indent")
schema_dict = {"data": schema.introspect()}
if out == '-':
if out == "-":
self.stdout.write(json.dumps(schema_dict, indent=indent, sort_keys=True))
else:
self.save_file(out, schema_dict, indent)
Expand Down
21 changes: 12 additions & 9 deletions graphene_django/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GraphQLTestCase(TestCase):
"""

# URL to graphql endpoint
GRAPHQL_URL = '/graphql/'
GRAPHQL_URL = "/graphql/"
# Here you need to set your graphql schema for the tests
GRAPHQL_SCHEMA = None

Expand All @@ -20,7 +20,9 @@ def setUpClass(cls):
super(GraphQLTestCase, cls).setUpClass()

if not cls.GRAPHQL_SCHEMA:
raise AttributeError('Variable GRAPHQL_SCHEMA not defined in GraphQLTestCase.')
raise AttributeError(
"Variable GRAPHQL_SCHEMA not defined in GraphQLTestCase."
)

cls._client = Client(cls.GRAPHQL_SCHEMA)

Expand All @@ -37,14 +39,15 @@ def query(self, query, op_name=None, input_data=None):
Returns:
Response object from client
"""
body = {'query': query}
body = {"query": query}
if op_name:
body['operation_name'] = op_name
body["operation_name"] = op_name
if input_data:
body['variables'] = {'input': input_data}
body["variables"] = {"input": input_data}

resp = self._client.post(self.GRAPHQL_URL, json.dumps(body),
content_type='application/json')
resp = self._client.post(
self.GRAPHQL_URL, json.dumps(body), content_type="application/json"
)
return resp

def assertResponseNoErrors(self, resp):
Expand All @@ -55,12 +58,12 @@ def assertResponseNoErrors(self, resp):
"""
content = json.loads(resp.content)
self.assertEqual(resp.status_code, 200)
self.assertNotIn('errors', list(content.keys()))
self.assertNotIn("errors", list(content.keys()))

def assertResponseHasErrors(self, resp):
"""
Assert that the call was failing. Take care: Even with errors, GraphQL returns status 200!
:resp HttpResponse: Response
"""
content = json.loads(resp.content)
self.assertIn('errors', list(content.keys()))
self.assertIn("errors", list(content.keys()))
14 changes: 9 additions & 5 deletions graphene_django/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ def test_generate_file_on_call_graphql_schema(savefile_mock, settings):
assert "Successfully dumped GraphQL schema to schema.json" in out.getvalue()


@patch('json.dump')
@patch("json.dump")
def test_files_are_canonical(dump_mock):
open_mock = mock_open()
with patch('graphene_django.management.commands.graphql_schema.open', open_mock):
management.call_command('graphql_schema', schema='')
with patch("graphene_django.management.commands.graphql_schema.open", open_mock):
management.call_command("graphql_schema", schema="")

open_mock.assert_called_once()

dump_mock.assert_called_once()
assert dump_mock.call_args[1]["sort_keys"], "json.mock() should be used to sort the output"
assert dump_mock.call_args[1]["indent"] > 0, "output should be pretty-printed by default"
assert dump_mock.call_args[1][
"sort_keys"
], "json.mock() should be used to sort the output"
assert (
dump_mock.call_args[1]["indent"] > 0
), "output should be pretty-printed by default"
6 changes: 2 additions & 4 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ class A(DjangoObjectType):
class Meta:
model = Article

graphene_field = convert_django_field(Reporter.articles.rel,
A._meta.registry)
graphene_field = convert_django_field(Reporter.articles.rel, A._meta.registry)
assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field)
Expand All @@ -255,8 +254,7 @@ class A(DjangoObjectType):
class Meta:
model = FilmDetails

graphene_field = convert_django_field(Film.details.related,
A._meta.registry)
graphene_field = convert_django_field(Film.details.related, A._meta.registry)
assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field)
Expand Down
3 changes: 1 addition & 2 deletions graphene_django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ def dispatch(self, request, *args, **kwargs):

if show_graphiql:
return self.render_graphiql(
request,
graphiql_version=self.graphiql_version,
request, graphiql_version=self.graphiql_version
)

if self.batch:
Expand Down
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"pytest-django>=3.3.2",
] + rest_framework_require


dev_requires = [
"black==19.3b0",
"flake8==3.7.7",
] + tests_require

setup(
name="graphene-django",
version=version,
Expand Down Expand Up @@ -58,7 +64,7 @@
setup_requires=["pytest-runner"],
tests_require=tests_require,
rest_framework_require=rest_framework_require,
extras_require={"test": tests_require, "rest_framework": rest_framework_require},
extras_require={"test": tests_require, "rest_framework": rest_framework_require, "dev": dev_requires},
include_package_data=True,
zip_safe=False,
platforms="any",
Expand Down

0 comments on commit 9cdc08b

Please sign in to comment.