Skip to content

Commit

Permalink
Merge pull request #26 from hfaran/functest
Browse files Browse the repository at this point in the history
100% Coverage
  • Loading branch information
hfaran committed Feb 17, 2014
2 parents 02ea004 + 19a2185 commit 69dd5e6
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 60 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ install:
- "pip install pytest-cov"
- "pip install coverage"
- "pip install coveralls"
- "pip install mock"
script:
coverage run --source=tornado_json setup.py test
after_success:
Expand Down
68 changes: 66 additions & 2 deletions demos/helloworld/API_Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Shouts hello to the world (asynchronously)!



# `/api/greeting/(?P<name>[a-zA-Z0-9_]+)/?$`
# `/api/greeting/(?P<fname>[a-zA-Z0-9_]+)/(?P<lname>[a-zA-Z0-9_]+)/?$`

Content-Type: application/json

Expand All @@ -50,7 +50,7 @@ null

### Output Example
```json
"Greetings, Greg!"
"Greetings, Named Person!"
```


Expand Down Expand Up @@ -85,3 +85,67 @@ null

Shouts hello to the world!





# `/api/postit`

Content-Type: application/json

## POST
### Input Schema
```json
{
"type": "object",
"properties": {
"body": {
"type": "string"
},
"index": {
"type": "number"
},
"title": {
"type": "string"
}
}
}
```

### Input Example
```json
{
"body": "Equally important message",
"index": 0,
"title": "Very Important Post-It Note"
}
```

### Output Schema
```json
{
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
}
```

### Output Example
```json
{
"message": "Very Important Post-It Note was posted."
}
```



POST the required parameters to post a Post-It note

* `title`: Title of the note
* `body`: Body of the note
* `index`: An easy index with which to find the note


117 changes: 85 additions & 32 deletions demos/helloworld/helloworld/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

class HelloWorldHandler(APIHandler):

apid = {
"get": {
"input_schema": None,
"output_schema": {
"type": "string",
},
"output_example": "Hello world!",
"input_example": None,
"doc": "Shouts hello to the world!",
apid = {}
apid["get"] = {
"input_schema": None,
"output_schema": {
"type": "string",
},
"output_example": "Hello world!",
"input_example": None,
"doc": "Shouts hello to the world!",
}

# Decorate any HTTP methods with the `io_schema` decorator
Expand All @@ -30,16 +29,15 @@ def get(self):

class AsyncHelloWorld(APIHandler):

apid = {
"get": {
"input_schema": None,
"output_schema": {
"type": "string",
},
"output_example": "Hello (asynchronous) world!",
"input_example": None,
"doc": "Shouts hello to the world (asynchronously)!",
apid = {}
apid["get"] = {
"input_schema": None,
"output_schema": {
"type": "string",
},
"output_example": "Hello (asynchronous) world!",
"input_example": None,
"doc": "Shouts hello to the world (asynchronously)!",
}

def hello(self, callback=None):
Expand All @@ -58,31 +56,86 @@ def get(self):
# In Python 3.3, using `raise Return(value)` is no longer
# necessary and can be replaced with simply `return value`.
# For details, see:
# http://www.tornadoweb.org/en/branch3.2/gen.html#tornado.gen.Return
# http://www.tornadoweb.org/en/branch3.2/gen.html#tornado.gen.Return

# return res # Python 3.3
raise gen.Return(res) # Python 2.7


class Greeting(APIHandler):
class PostIt(APIHandler):

apid = {
"get": {
"input_schema": None,
"output_schema": {
"type": "string",
apid = {}
apid["post"] = {
"input_schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"body": {"type": "string"},
"index": {"type": "number"},
},
"output_example": "Greetings, Greg!",
"input_example": None,
"doc": "Greets you.",
},
"input_example": {
"title": "Very Important Post-It Note",
"body": "Equally important message",
"index": 0
},
"output_schema": {
"type": "object",
"properties": {
"message": {"type": "string"}
}
},
"output_example": {
"message": "Very Important Post-It Note was posted."
},
"doc": """
POST the required parameters to post a Post-It note
* `title`: Title of the note
* `body`: Body of the note
* `index`: An easy index with which to find the note
"""
}

@io_schema
def post(self):
# io_schema will JSON-decode `self.request.body` for us
# and set self.body as the result, so we can use that here
return {
"message": "{} was posted.".format(self.body["title"])
}


class Greeting(APIHandler):

apid = {}
apid["get"] = {
"input_schema": None,
"output_schema": {
"type": "string",
},
"output_example": "Greetings, Named Person!",
"input_example": None,
"doc": "Greets you.",
}

# When you include extra arguments in the signature of an HTTP
# method, Tornado-JSON will generate a route that matches the extra
# arguments; here, you can GET /api/greeting/Greg and you will
# get a response back that says, "Greetings, Greg!"
# arguments; here, you can GET /api/greeting/John/Smith and you will
# get a response back that says, "Greetings, John Smith!"
# You can match the regex equivalent of `\w+`.
@io_schema
def get(self, name):
return "Greetings, {}!".format(name)
def get(self, fname, lname):
return "Greetings, {} {}!".format(fname, lname)


class FreeWilledHandler(APIHandler):

# And of course, you aren't forced to use schema validation;
# if you want your handlers to do something more custom,
# they definitely can.
def get(self):
self.success("I don't need no stinkin' schema validation.")
# If you're feeling really bold, you could even skip JSend
# altogether and do the following EVIL thing:
# self.write("I'm writing back a string that isn't JSON! Take that!")
1 change: 1 addition & 0 deletions maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
* Run tests from root project directory

```$ py.test --cov="tornado_json" --cov-report=term --cov-report=html```
```$ nosetests --with-cov --cov-report term-missing --cov tornado_json tests/```
File renamed without changes.
Loading

0 comments on commit 69dd5e6

Please sign in to comment.