Skip to content

Commit

Permalink
- Improved JSON response (pretty'fied and added headers)
Browse files Browse the repository at this point in the history
- Added Showstimes service and tests
  • Loading branch information
umermansoor committed Dec 1, 2015
1 parent 5ca6fdd commit f293f67
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion services/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import os

import json
from flask import make_response

def root_dir():
""" Returns root director for this project """
return os.path.dirname(os.path.realpath(__file__ + '/..'))

def nice_json(arg):
response = make_response(json.dumps(arg, sort_keys = True, indent=4))
response.headers['Content-type'] = "application/json"
return response
25 changes: 12 additions & 13 deletions services/movies.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from services import root_dir
from flask import Flask, json, jsonify
from services import root_dir, nice_json
from flask import Flask
from werkzeug.exceptions import NotFound
import json


app = Flask(__name__)

with open("{}/database/movies.json".format(root_dir()), 'r') as f:
with open("{}/database/movies.json".format(root_dir()), "r") as f:
movies = json.load(f)

@app.route("/")
def hello():
return jsonify({
'uri': "/",
'subresource_uris': {
'movies': '/movies',
'movie': '/movies</id>'
return nice_json({
"uri": "/",
"subresource_uris": {
"movies": "/movies",
"movie": "/movies</id>"
}
})

Expand All @@ -23,13 +24,11 @@ def movie_info(movieid):
if movieid not in movies:
raise NotFound

return jsonify(movies[movieid])
return nice_json(movies[movieid])

@app.route("/movies")
def movie_list():
return jsonify(movies)


def movie_record():
return nice_json(movies)


if __name__ == "__main__":
Expand Down
34 changes: 34 additions & 0 deletions services/showtimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from services import root_dir, nice_json
from flask import Flask
from werkzeug.exceptions import NotFound
import json


app = Flask(__name__)

with open("{}/database/showtimes.json".format(root_dir()), "r") as f:
showtimes = json.load(f)

@app.route("/")
def hello():
return nice_json({
"uri": "/",
"subresource_uris": {
"showtimes": "/showtimes",
"showtime": "/showtimes</date>"
}
})

@app.route("/showtimes")
def showtimes_list():
return nice_json(showtimes)

@app.route("/showtimes/<date>")
def showtimes_record(date):
if date not in showtimes:
raise NotFound
print showtimes[date]
return nice_json(showtimes[date])

if __name__ == "__main__":
app.run(port=5002, debug = True)
Empty file added tests/movies.py
Empty file.
66 changes: 66 additions & 0 deletions tests/showtimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import unittest
import requests

class TestShowTimesService(unittest.TestCase):
def setUp(self):
self.url = "http://127.0.0.1:5002/showtimes"

def test_all_showtimes_records(self):
""" Test /showtimes for all known showtimes"""
for date, expected in GOOD_RESPONSES.iteritems():
reply = requests.get("{}/{}".format(self.url, date))
actual_reply = reply.json()

self.assertEqual(len(actual_reply), len(expected),
"Got {} showtimes but expected {}".format(
len(actual_reply), len(expected)
))

# Use set because the order doesn't matter
self.assertEqual(set(actual_reply), set(expected),
"Got {} but expected {}".format(
actual_reply, expected))

def test_not_found(self):
""" Test /showtimes/<date> for non-existent dates"""
future_date = 20490101
actual_reply = requests.get("{}/{}".format(self.url, future_date))
self.assertEqual(actual_reply.status_code, 404,
"Got {} but expected 404".format(
actual_reply.status_code))

GOOD_RESPONSES = {
"20151130": [
"720d006c-3a57-4b6a-b18f-9b713b073f3c",
"a8034f44-aee4-44cf-b32c-74cf452aaaae",
"39ab85e5-5e8e-4dc5-afea-65dc368bd7ab"
],
"20151201": [
"267eedb8-0f5d-42d5-8f43-72426b9fb3e6",
"7daf7208-be4d-4944-a3ae-c1c2f516f3e6",
"39ab85e5-5e8e-4dc5-afea-65dc368bd7ab",
"a8034f44-aee4-44cf-b32c-74cf452aaaae"
],
"20151202": [
"a8034f44-aee4-44cf-b32c-74cf452aaaae",
"96798c08-d19b-4986-a05d-7da856efb697",
"39ab85e5-5e8e-4dc5-afea-65dc368bd7ab",
"276c79ec-a26a-40a6-b3d3-fb242a5947b6"
],
"20151203": [
"720d006c-3a57-4b6a-b18f-9b713b073f3c",
"39ab85e5-5e8e-4dc5-afea-65dc368bd7ab"
],
"20151204": [
"96798c08-d19b-4986-a05d-7da856efb697",
"a8034f44-aee4-44cf-b32c-74cf452aaaae",
"7daf7208-be4d-4944-a3ae-c1c2f516f3e6"
],
"20151205": [
"96798c08-d19b-4986-a05d-7da856efb697",
"a8034f44-aee4-44cf-b32c-74cf452aaaae",
"7daf7208-be4d-4944-a3ae-c1c2f516f3e6",
"276c79ec-a26a-40a6-b3d3-fb242a5947b6",
"39ab85e5-5e8e-4dc5-afea-65dc368bd7ab"
]
}

0 comments on commit f293f67

Please sign in to comment.