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 batch update query #2752

Merged
merged 4 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -55,10 +55,12 @@ def list_cases(self, page: int = None, limit: int = None, filter: str = None):
if limit <= 0:
raise PreconditionUnsatisfiedError("limit must be >0")
predicate = CaseController.parse_filter(filter)
print(f"predicate: {predicate}")
iamleeg marked this conversation as resolved.
Show resolved Hide resolved
if predicate is None:
raise ValidationError("cannot understand query")
cases = self.store.fetch_cases(page, limit, predicate)
count = self.store.count_cases(predicate)
print(f"cases: {cases}")
nextPage = page + 1 if count > page * limit else None
return CasePage(cases, count, nextPage)

Expand Down Expand Up @@ -234,6 +236,21 @@ def remove_id(d: dict):
self.validate_updated_case(id, update)
return self.store.batch_update(update_map)

def batch_update_query(self, query: str, update: dict) -> int:
"""Update a collection of documents. Update is a description
of an update, and query indicates which cases to update.
Raises ValidationError if any update leaves a case
in an inconsistent state."""
if update is None:
raise PreconditionUnsatisfiedError("No update supplied")
cases = self.list_cases(filter=query)
updates = []
for case in cases.cases:
an_update = dict(update)
an_update["_id"] = case._id
updates.append(an_update)
return self.batch_update(updates)

def validate_updated_case(self, id: str, update: DocumentUpdate):
"""Find out whether updating a case would result in it being invalid.
Raises NotFoundError if the case doesn't exist, or ValidationError if
Expand Down
10 changes: 10 additions & 0 deletions data-serving/reusable-data-service/data_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ def batch_update():
return jsonify({"message": e.args[0]}), e.http_code


@app.route("/api/cases/batchUpdateQuery", methods=["POST"])
def batch_update_query():
try:
req = request.get_json()
count = case_controller.batch_update_query(req.get("query"), req.get("case"))
return jsonify({"numModified": count}), 200
except WebApplicationError as e:
return jsonify({"message": e.args[0]}), e.http_code


@app.route("/api/excludedCaseIds")
def excluded_case_ids():
try:
Expand Down
14 changes: 14 additions & 0 deletions data-serving/reusable-data-service/tests/test_case_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,17 @@ def test_batch_update_raises_if_case_not_found(case_controller):
update = {"_id": "1", "confirmationDate": date(2022, 5, 13)}
with pytest.raises(NotFoundError):
case_controller.batch_update([update])


def test_batch_update_query_returns_modified_count(case_controller):
for i in range(4):
_ = case_controller.create_case(
{
"confirmationDate": date(2021, 6, i + 1),
"caseReference": {"sourceId": "123ab4567890123ef4567890"},
},
)
update = {"confirmationDate": date(2022, 5, 13)}
query = None # didn't implement rich queries on the test store
modified = case_controller.batch_update_query(query, update)
assert modified == 4
28 changes: 28 additions & 0 deletions data-serving/reusable-data-service/tests/test_case_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,31 @@ def test_batch_update(client_with_patched_mongo):
)
assert post_result.status_code == 200
assert post_result.get_json()["numModified"] == 3


def test_batch_update_query(client_with_patched_mongo):
db = pymongo.MongoClient("mongodb://localhost:27017/outbreak")
inserted = db["outbreak"]["cases"].insert_many(
[
{
"confirmationDate": datetime(2022, 5, i),
"caseReference": {
"sourceId": bson.ObjectId("fedc12345678901234567890"),
"status": "EXCLUDED",
},
"caseExclusion": {
"date": datetime(2022, 6, i),
"note": f"Excluded upon this day, the {i}th of June",
},
}
for i in range(1, 4)
]
)
update = {"confirmationDate": f"2022-04-01"}

post_result = client_with_patched_mongo.post(
"/api/cases/batchUpdateQuery",
json={"case": update, "query": "dateconfirmedbefore:2022-05-03"},
)
assert post_result.status_code == 200
assert post_result.get_json()["numModified"] == 2