-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Improved JSON response (pretty'fied and added headers)
- Added Showstimes service and tests
- Loading branch information
1 parent
5ca6fdd
commit f293f67
Showing
6 changed files
with
125 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |