Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2714 integrate schema case controllers #2771

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import date
from typing import List, Optional

from data_service.model.case import Case
from data_service.model.case import observe_case_class
from data_service.model.case_exclusion_metadata import CaseExclusionMetadata
from data_service.model.case_page import CasePage
from data_service.model.case_reference import CaseReference
Expand All @@ -24,6 +24,13 @@
ValidationError,
)

Case = None


def case_observer(cls):
global Case
Case = cls


class CaseController:
"""Implements CRUD operations on cases. Uses an external store
Expand All @@ -36,6 +43,7 @@ def __init__(self, store, outbreak_date: date):
outbreak_date is the earliest date on which this instance should accept cases."""
self.store = store
self.outbreak_date = outbreak_date
observe_case_class(case_observer)

def get_case(self, id: str):
"""Implements get /cases/:id. Interpretation of ID is dependent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import copy
import csv
import dataclasses
import datetime
import io
import operator

from data_service.model.document_update import DocumentUpdate
Expand Down Expand Up @@ -108,7 +110,10 @@ def include_dataclass_fields(aType: type):

def delimiter_separated_values(self, sep: str) -> str:
"""Create a line listing all of the fields in me and my member dataclasses."""
return sep.join(self.field_values()) + "\n"
f = io.StringIO()
csv_writer = csv.writer(f, delimiter=sep)
csv_writer.writerow(self.field_values())
return f.getvalue()

def to_tsv(self) -> str:
"""Generate a row in a CSV file representing myself."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import os
import pymongo
from data_service.model.case import Case
from data_service.model.case import observe_case_class
from data_service.model.case_exclusion_metadata import CaseExclusionMetadata
from data_service.model.document_update import DocumentUpdate
from data_service.model.field import Field
Expand All @@ -19,6 +19,14 @@
from typing import List, Tuple


Case = None


def case_observer(cls):
global Case
Case = cls


class MongoStore:
"""A line list store backed by mongodb."""

Expand All @@ -33,6 +41,7 @@ def __init__(
self.database_name = database_name
self.case_collection_name = case_collection_name
self.schema_collection_name = schema_collection_name
observe_case_class(case_observer)

def get_client(self):
return self.client
Expand Down
4 changes: 2 additions & 2 deletions data-serving/reusable-data-service/tests/test_case_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_csv_row_with_no_id():
case.confirmationDate = date(2022, 6, 13)
case.caseReference = ref
csv = case.to_csv()
assert csv == ",2022-06-13,abcd12903478565647382910,UNVERIFIED\n"
assert csv == ",2022-06-13,abcd12903478565647382910,UNVERIFIED\r\n"


def test_csv_row_with_id():
Expand All @@ -49,7 +49,7 @@ def test_csv_row_with_id():
case.confirmationDate = date(2022, 6, 13)
case.caseReference = ref
csv = case.to_csv()
assert csv == f"{id1},2022-06-13,{id2},UNVERIFIED\n"
assert csv == f"{id1},2022-06-13,{id2},UNVERIFIED\r\n"


def test_apply_update_to_case():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_csv_row_unexcluded():
ref = CaseReference()
ref.sourceId = oid
csv = ref.to_csv()
assert csv == "abcd12903478565647382910,UNVERIFIED\n"
assert csv == "abcd12903478565647382910,UNVERIFIED\r\n"


def test_csv_row_excluded():
Expand All @@ -20,7 +20,7 @@ def test_csv_row_excluded():
ref.sourceId = oid
ref.status = "EXCLUDED"
csv = ref.to_csv()
assert csv == "abcd12903478565647382910,EXCLUDED\n"
assert csv == "abcd12903478565647382910,EXCLUDED\r\n"


def test_reference_must_have_valid_status():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import csv
import io

from tests.end_to_end_fixture import client_with_patched_mongo


def test_adding_field_then_downloading_case(client_with_patched_mongo):
response = client_with_patched_mongo.post(
"/api/schema",
json={
"name": "mySymptoms",
"type": "string",
"description": "A custom, comma-separated list of symptoms",
},
)
assert response.status_code == 201
response = client_with_patched_mongo.post(
"/api/cases",
json={
"confirmationDate": "2022-06-01T00:00:00.000Z",
"caseReference": {
"status": "UNVERIFIED",
"sourceId": "24680135792468013579fedc",
},
"mySymptoms": "coughs, sneezles",
},
)
assert response.status_code == 201
response = client_with_patched_mongo.get("/api/cases")
assert response.status_code == 200
case_list = response.get_json()
assert case_list["total"] == 1
assert len(case_list["cases"]) == 1
assert case_list["cases"][0]["mySymptoms"] == "coughs, sneezles"


def test_adding_field_then_downloading_csv(client_with_patched_mongo):
response = client_with_patched_mongo.post(
"/api/schema",
json={
"name": "someField",
"type": "string",
"description": "Another custom field",
},
)
assert response.status_code == 201
response = client_with_patched_mongo.post(
"/api/cases",
json={
"confirmationDate": "2022-06-01T00:00:00.000Z",
"caseReference": {
"status": "UNVERIFIED",
"sourceId": "24680135792468013579fedc",
},
"someField": "well, what have we here",
},
)
assert response.status_code == 201
response = client_with_patched_mongo.post(
"/api/cases/download", json={"format": "csv"}
)
assert response.status_code == 200
csv_file = io.StringIO(response.get_data().decode("utf-8"))
csv_reader = csv.DictReader(csv_file)
cases = [c for c in csv_reader]
assert len(cases) == 1
case = cases[0]
assert case["someField"] == "well, what have we here"