Skip to content

Commit

Permalink
Add list fields to the schema #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jul 28, 2022
1 parent 7a2832b commit 0426295
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def restore_saved_fields(self) -> None:
field.required,
field.default,
field.values,
field.is_list,
False,
)

Expand All @@ -45,6 +46,7 @@ def add_field(
required: bool = False,
default: Optional[Union[bool, str, int, date]] = None,
values: Optional[List[Any]] = None,
is_list: bool = False,
store_field: bool = True,
):
global Case
Expand All @@ -63,7 +65,9 @@ def add_field(
If a field is required, set required = True. You must also set a default value so that
existing cases have an initial setting for the field."""
required = required if required is not None else False
field_model = Field(name, type_name, description, required, default, values)
field_model = Field(
name, type_name, description, required, default, values, is_list
)
add_field_to_case_class(field_model)
if store_field:
self.store.add_field(field_model)
1 change: 1 addition & 0 deletions data-serving/reusable-data-service/data_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def add_field_to_case_schema():
req.get("required"),
req.get("default"),
req.get("values"),
req.get("is_list"),
)
return "", 201
except WebApplicationError as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def field_values(self) -> List[str]:
fields += value.custom_field_values()
else:
fields += f.type.custom_none_field_values()
elif issubclass(f.type, list):
fields.append(",".join(value))
else:
fields.append(str(value) if value is not None else "")
return fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Field(Document):
init=True, default=None
)
values: Optional[List[Any]] = dataclasses.field(init=True, default=None)
is_list: bool = dataclasses.field(init=True, default=False)

STRING = "string"
DATE = "date"
Expand Down Expand Up @@ -55,10 +56,14 @@ def from_dict(cls, dictionary):
dictionary.get("required"),
dictionary.get("default", None),
dictionary.get("values", None),
dictionary.get("is_list", False),
)

def python_type(self) -> type:
return self.model_type(self.type)
if self.is_list:
return list
else:
return self.model_type(self.type)

def dataclasses_tuples(self):
# Note that the default value here is always None, even if I have a default value!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def get_case_fields(self):
doc["data_dictionary_text"],
doc["required"],
doc["default"],
doc["is_list"],
)
for doc in self.get_schema_collection().find({})
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,36 @@ def test_adding_enumerated_field_with_other_value(client_with_patched_mongo):
assert len(cases) == 1
case = cases[0]
assert case["customPathogenStatus_other"] == "Neopanspermia"


def test_adding_list_field(client_with_patched_mongo):
with open("./tests/data/case.minimal.json") as json_file:
case_doc = json.load(json_file)
response = client_with_patched_mongo.post(
"/api/schema",
json={
"name": "extraSymptoms",
"type": "string",
"description": "Additional symptoms outside the main list",
"required": False,
"is_list": True,
},
)
assert response.status_code == 201

case_doc["extraSymptoms"] = ["ickiness", "pustulation"]
response = client_with_patched_mongo.post(
"/api/cases",
json=case_doc,
)
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["extraSymptoms"] == "ickiness,pustulation"

0 comments on commit 0426295

Please sign in to comment.