Skip to content

Commit

Permalink
Case fetching by ID #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jun 17, 2022
1 parent 79255e7 commit 5d0ce48
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
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
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
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 data-serving/reusable-data-service/tests/test_case_controller.py
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"

0 comments on commit 5d0ce48

Please sign in to comment.