Skip to content

Commit

Permalink
fix update_test_instance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
apdavison committed Oct 7, 2024
1 parent eeda99e commit ee1ec54
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
11 changes: 6 additions & 5 deletions validation_service_api/validation_service/resources/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,18 +578,19 @@ def update_test_instance(
kg_client = get_kg_client_for_user_account(token)
test_instance_kg = _get_test_instance_by_id(test_instance_id, kg_client, scope="any")
test_definition_kg = _get_test_by_id_or_alias(test_id, kg_client, scope="any")
return _update_test_instance(test_instance_kg, test_definition_kg, test_instance_patch, kg_client)
test_definition = ValidationTest.from_kg_object(test_definition_kg, kg_client)
return _update_test_instance(test_instance_kg, test_definition, test_instance_patch, kg_client)


def _update_test_instance(test_instance, test_definition_kg, test_instance_patch, kg_client):
stored_test_instance = ValidationTestInstance.from_kg_object(test_instance, test_definition_kg.uuid, kg_client)
def _update_test_instance(test_instance, test_definition, test_instance_patch, kg_client):
stored_test_instance = ValidationTestInstance.from_kg_object(test_instance, test_definition.id, kg_client)
update_data = test_instance_patch.dict(exclude_unset=True)
updated_test_instance = stored_test_instance.copy(update=update_data)
test_instance_kg = updated_test_instance.to_kg_object(test_definition_kg)
test_instance_kg = updated_test_instance.to_kg_object(test_definition)
assert test_instance_kg.id == test_instance.id
assert test_instance.space is not None
test_instance_kg.save(kg_client, recursive=True, space=test_instance.space)
return ValidationTestInstance.from_kg_object(test_instance_kg, test_definition_kg.uuid, kg_client)
return ValidationTestInstance.from_kg_object(test_instance_kg, test_definition.id, kg_client)


@router.delete("/tests/query/instances/{test_instance_id}", status_code=status.HTTP_200_OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ def test_list_validation_tests_filter_by_species():
assert validation_test["species"] == "Rattus norvegicus"


def test_list_validation_tests_filter_by_cell_type():
response = client.get(f"/tests/?size=5&cell_type=hippocampus%20CA1%20pyramidal%20neuron", headers=AUTH_HEADER)
assert response.status_code == 200
validation_tests = response.json()
assert len(validation_tests) == 5
for validation_test in validation_tests:
check_validation_test(validation_test)
assert validation_test["cell_type"] == "hippocampus CA1 pyramidal neuron"


def test_list_validation_tests_filter_by_author():
response = client.get(f"/tests/?size=5&author=Appukuttan", headers=AUTH_HEADER)
assert response.status_code == 200
Expand Down Expand Up @@ -548,42 +558,42 @@ def test_create_validation_test_instance():
# assert response.status_code == 200


# def test_update_test_instance():
# # first create a test project
# payload1 = _build_sample_validation_test()
# response = client.post(f"/tests/", json=payload1, headers=AUTH_HEADER)
# assert response.status_code == 201
# posted_test = response.json()
# check_validation_test(posted_test)
# assert len(posted_test["instances"]) == 1
# test_uuid = posted_test["id"]
# test_instance_uuid = posted_test["instances"][0]["id"]
def test_update_test_instance():
# first create a test project
payload1 = _build_sample_validation_test()
response = client.post(f"/tests/", json=payload1, headers=AUTH_HEADER)
assert response.status_code == 201
posted_test = response.json()
check_validation_test(posted_test)
assert len(posted_test["instances"]) == 1
test_uuid = posted_test["id"]
test_instance_uuid = posted_test["instances"][0]["id"]

# # now edit the instance
# payload2 = {
# "description": "a more detailed description of this version",
# "repository": "http://example.com/my_code_in_a_new_location.py",
# }
# response = client.put(
# f"/tests/{test_uuid}/instances/{test_instance_uuid}", json=payload2, headers=AUTH_HEADER
# )
# assert response.status_code == 200
# now edit the instance
payload2 = {
"description": "a more detailed description of this version",
"repository": "http://example.com/my_code_in_a_new_location.py",
}
response = client.put(
f"/tests/{test_uuid}/instances/{test_instance_uuid}", json=payload2, headers=AUTH_HEADER
)
assert response.status_code == 200

# # now retrieve the test and check the instance has been updated
# sleep(15) # need to wait a short time to allow Nexus to become consistent
# response = client.get(f"/tests/{test_uuid}", headers=AUTH_HEADER)
# assert response.status_code == 200
# retrieved_test = response.json()
# assert len(retrieved_test["instances"]) == 1
# assert (
# retrieved_test["instances"][0]["version"] == payload1["instances"][0]["version"]
# ) # should be unchanged
# assert retrieved_test["instances"][0]["repository"] == payload2["repository"]
# assert retrieved_test["instances"][0]["description"] == payload2["description"]
# now retrieve the test and check the instance has been updated
sleep(15) # need to wait a short time to allow Nexus to become consistent
response = client.get(f"/tests/{test_uuid}", headers=AUTH_HEADER)
assert response.status_code == 200
retrieved_test = response.json()
assert len(retrieved_test["instances"]) == 1
assert (
retrieved_test["instances"][0]["version"] == payload1["instances"][0]["version"]
) # should be unchanged
assert retrieved_test["instances"][0]["repository"] == payload2["repository"]
assert retrieved_test["instances"][0]["description"] == payload2["description"]

# # delete again
# response = client.delete(f"/tests/{test_uuid}", headers=AUTH_HEADER)
# assert response.status_code == 200
# delete again
response = client.delete(f"/tests/{test_uuid}", headers=AUTH_HEADER)
assert response.status_code == 200


# def test_update_test_instance_version():
Expand Down

0 comments on commit ee1ec54

Please sign in to comment.