Skip to content
This repository has been archived by the owner on Aug 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #10 from ncrmro/dev
Browse files Browse the repository at this point in the history
Adds solid graphql enpoint testing and coverage support
  • Loading branch information
ncrmro authored Dec 12, 2016
2 parents 3f672b8 + 805fbb5 commit c33fd38
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
5 changes: 4 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
machine:
python:
version: 3.5.2
general:
artifacts:
- ".coverage"
dependencies:
post:
- pip install -r ./deps/testing.txt
test:
override:
- ./src/manage.py test ./src
- coverage run --source='.' ./src/manage.py test ./src
1 change: 1 addition & 0 deletions src/ango/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

DEBUG = True

MIDDLEWARE.remove('django.middleware.csrf.CsrfViewMiddleware')

DATABASES = {
'default': {
Expand Down
71 changes: 71 additions & 0 deletions src/todos/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Todos App
You may use graphiql or curl to try out some of the queries.

## Queries
The following is an example query to get the first ten todos and
return the id and text of each one.
```
query {
allTodos(first: 10){
edges{
node{
id,
text
}
}
}
}
```
The response
```
{
"data": {
"allTodos": {
"edges": [
{
"node": {
"id": "VG9kb05vZGU6MQ==",
"text": "adfasdfdsa"
}
}
]
}
}
}
```

### Queries with Variables
```
query getSomeTodos($todos_count: Int!) {
allTodos(first: $todos_count) {
edges {
node {
id
text
}
}
}
}
```

### Using Curl
The actual post request is a json string to be careful of empty space or new lines.
Running the example query again would look like this
```
curl -XPOST -H 'Content-Type:application/graphql' \
-d '{ allTodos(first: 10){edges{node{id,text}}}}' \
http://localhost:8000/graphql
```
And return the following data.
```
{"data":{"allTodos":{"edges":[{"node":{"id":"VG9kb05vZGU6MQ==","text":"adsfasdfasdfsd"}},{"node":{"id":"VG9kb05vZGU6Mg==","text":"adsfadsfa"}}]}}}
```

### Using CocoaRestClient GUI /JSON Body
This is a valid json body you can paste into the raw input of the coca rest client.
`{"query": "query {allTodos(first: 10){edges{node{id,text}}}}"}`

## Mutations


39 changes: 35 additions & 4 deletions src/todos/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import json

from django.contrib.auth.models import User
from django.test import TestCase, RequestFactory
from django.test import TestCase, Client

from .models import TodoModel


Expand All @@ -14,7 +17,35 @@ def setUp(self):
password='top_secret'
)

TodoModel.objects.create(user=self.user, text="roar")
TodoModel.objects.create(user=self.user, text="My todo")

def test_all_todos_graphql_endpoint(self):
"""Test all_todos graphql query"""

# Sanity Checks
todo = TodoModel.objects.get(text="My todo")
self.assertEqual(todo.text, 'My todo')

# Setup query and response
query = {
"query": "query {allTodos(first: 10){edges{node{id,text}}}}"
}
expected_response = {
"data": {
"allTodos": {
"edges": [
{
"node": {
"id": "VG9kb05vZGU6MQ==",
"text": "My todo"
}}
]}
}
}
# Make the post request
c = Client()
response = c.post('/graphql', query)

def test_animals_can_speak(self):
todo = TodoModel.objects.get(text="roar")
# Decode byte code response.content and load the json
graphql_response = json.loads(response.content.decode('ascii'))
self.assertEqual(graphql_response, expected_response)

0 comments on commit c33fd38

Please sign in to comment.