-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
data-serving/reusable-data-service/reusable_data_service/__init__.py
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,3 +1,5 @@ | ||
__version__ = '0.1.0' | ||
|
||
from .model.case import Case | ||
from .controller.case_controller import CaseController | ||
from .main import app |
18 changes: 18 additions & 0 deletions
18
data-serving/reusable-data-service/reusable_data_service/controller/case_controller.py
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,18 @@ | ||
from flask import jsonify | ||
|
||
class CaseController: | ||
"""Implements CRUD operations on cases. Uses an external store | ||
class to actually work with the cases collection, so that different | ||
storage technology can be chosen. | ||
All methods return a tuple of (response, HTTP status code)""" | ||
def __init__(self, store): | ||
self._store = store | ||
|
||
def get_case(self, id: str): | ||
"""Implements get /cases/:id. Interpretation of ID is dependent | ||
on the store implementation but here it is an opaque token that | ||
should be unique to each case.""" | ||
case = self._store.case_by_id(id) | ||
if case is None: | ||
return f"No case with ID {id}", 404 | ||
return jsonify(case), 200 |
3 changes: 3 additions & 0 deletions
3
data-serving/reusable-data-service/reusable_data_service/main.py
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,3 @@ | ||
from flask import Flask | ||
|
||
app = Flask(__name__) |
39 changes: 39 additions & 0 deletions
39
data-serving/reusable-data-service/tests/test_case_controller.py
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,39 @@ | ||
import pytest | ||
|
||
from reusable_data_service import Case, CaseController, app | ||
|
||
|
||
class MemoryStore: | ||
"""Simple dictionary-based store for cases.""" | ||
def __init__(self): | ||
self.cases = dict() | ||
|
||
def case_by_id(self, id: str): | ||
return self.cases.get(id) | ||
|
||
def put_case(self, id: str, case: Case): | ||
self.cases[id] = case | ||
|
||
|
||
@pytest.fixture | ||
def app_context(): | ||
with app.app_context(): | ||
yield | ||
|
||
|
||
def test_one_present_item_should_return_200_OK(app_context): | ||
store = MemoryStore() | ||
with open('./tests/data/case.minimal.json', 'r') as minimal_file: | ||
case = Case.from_json(minimal_file.read()) | ||
store.put_case('foo', case) | ||
controller = CaseController(store) | ||
(response, status) = controller.get_case('foo') | ||
assert status == 200 | ||
assert response is not None | ||
|
||
def test_one_absent_item_should_return_400_not_found(app_context): | ||
store = MemoryStore() | ||
controller = CaseController(store) | ||
(response, status) = controller.get_case('foo') | ||
assert status == 404 | ||
assert response == "No case with ID foo" |