Skip to content

Commit

Permalink
Adding tests for updating entities
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmadonna committed Jan 2, 2025
1 parent a712ca6 commit b60c5a2
Showing 1 changed file with 221 additions and 0 deletions.
221 changes: 221 additions & 0 deletions test/test_update_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
from test.helpers.database import create_provenance, get_entity
from test.helpers.response import mock_response

import pytest


@pytest.fixture()
def app(auth):
import app as app_module

app_module.app.config.update({"TESTING": True})
app_module.auth_helper_instance = auth
app_module.schema_manager._auth_helper = auth
# other setup
yield app_module.app
# clean up


# Update Entity Tests


@pytest.mark.usefixtures("lab")
def test_update_source(app, requests, db_session):
# Create provenance in test database
test_entities = create_provenance(db_session, ["source"])
test_source = test_entities["source"]

# uuid and search api mock responses
uuid_api_url = app.config["UUID_API_URL"]
search_api_url = app.config["SEARCH_API_URL"]
requests.add_response(
f"{uuid_api_url}/uuid/{test_source['uuid']}",
"get",
mock_response(200, {k: test_source[k] for k in ["uuid", "sennet_id", "base_id"]}),
)
requests.add_response(f"{search_api_url}/reindex/{test_source['uuid']}", "put", mock_response(202))

with app.test_client() as client:
data = {
"description": "New Testing lab notes",
"lab_source_id": "new_test_lab_source_id",
}

res = client.put(
f"/entities/{test_source['uuid']}?return_all_properties=true",
json=data,
headers={"Authorization": "Bearer test_token"},
)

assert res.status_code == 200
assert res.json["uuid"] == test_source["uuid"]
assert res.json["sennet_id"] == test_source["sennet_id"]

assert res.json["description"] == data["description"]
assert res.json["lab_source_id"] == data["lab_source_id"]

# check database
db_entity = get_entity(test_source["uuid"], db_session)
assert db_entity["description"] == data["description"]
assert db_entity["lab_source_id"] == data["lab_source_id"]


@pytest.mark.usefixtures("lab")
def test_update_organ_sample(app, requests, db_session):
# Create provenance in test database
test_entities = create_provenance(db_session, ["source", "organ"])
test_organ = test_entities["organ"]

# uuid and search api mock responses
uuid_api_url = app.config["UUID_API_URL"]
search_api_url = app.config["SEARCH_API_URL"]
requests.add_response(
f"{uuid_api_url}/uuid/{test_organ['uuid']}",
"get",
mock_response(200, {k: test_organ[k] for k in ["uuid", "sennet_id", "base_id"]}),
)
requests.add_response(f"{search_api_url}/reindex/{test_organ['uuid']}", "put", mock_response(202))

with app.test_client() as client:
data = {
"description": "New Testing lab notes",
"lab_tissue_sample_id": "new_test_lab_tissue_organ_id",
}

res = client.put(
f"/entities/{test_organ['uuid']}?return_all_properties=true",
json=data,
headers={"Authorization": "Bearer test_token"},
)

assert res.status_code == 200
assert res.json["uuid"] == test_organ["uuid"]
assert res.json["sennet_id"] == test_organ["sennet_id"]

assert res.json["description"] == data["description"]
assert res.json["lab_tissue_sample_id"] == data["lab_tissue_sample_id"]

# check database
db_entity = get_entity(test_organ["uuid"], db_session)
assert db_entity["description"] == data["description"]
assert db_entity["lab_tissue_sample_id"] == data["lab_tissue_sample_id"]


@pytest.mark.usefixtures("lab")
def test_update_block_sample(app, requests, db_session):
# Create provenance in test database
test_entities = create_provenance(db_session, ["source", "organ", "block"])
test_block = test_entities["block"]

# uuid and search api mock responses
uuid_api_url = app.config["UUID_API_URL"]
search_api_url = app.config["SEARCH_API_URL"]
requests.add_response(
f"{uuid_api_url}/uuid/{test_block['uuid']}",
"get",
mock_response(200, {k: test_block[k] for k in ["uuid", "sennet_id", "base_id"]}),
)
requests.add_response(f"{search_api_url}/reindex/{test_block['uuid']}", "put", mock_response(202))

with app.test_client() as client:
data = {
"description": "New Testing lab notes",
"lab_tissue_sample_id": "new_test_lab_tissue_block_id",
}

res = client.put(
f"/entities/{test_block['uuid']}?return_all_properties=true",
json=data,
headers={"Authorization": "Bearer test_token"},
)

assert res.status_code == 200
assert res.json["uuid"] == test_block["uuid"]
assert res.json["sennet_id"] == test_block["sennet_id"]

assert res.json["description"] == data["description"]
assert res.json["lab_tissue_sample_id"] == data["lab_tissue_sample_id"]

# check database
db_entity = get_entity(test_block["uuid"], db_session)
assert db_entity["description"] == data["description"]
assert db_entity["lab_tissue_sample_id"] == data["lab_tissue_sample_id"]


@pytest.mark.usefixtures("lab")
def test_update_section_sample(app, requests, db_session):
# Create provenance in test database
test_entities = create_provenance(db_session, ["source", "organ", "block", "section"])
test_section = test_entities["section"]

# uuid and search api mock responses
uuid_api_url = app.config["UUID_API_URL"]
search_api_url = app.config["SEARCH_API_URL"]
requests.add_response(
f"{uuid_api_url}/uuid/{test_section['uuid']}",
"get",
mock_response(200, {k: test_section[k] for k in ["uuid", "sennet_id", "base_id"]}),
)
requests.add_response(f"{search_api_url}/reindex/{test_section['uuid']}", "put", mock_response(202))

with app.test_client() as client:
data = {
"description": "New Testing lab notes",
"lab_tissue_sample_id": "new_test_lab_tissue_section_id",
}

res = client.put(
f"/entities/{test_section['uuid']}?return_all_properties=true",
json=data,
headers={"Authorization": "Bearer test_token"},
)

assert res.status_code == 200
assert res.json["uuid"] == test_section["uuid"]
assert res.json["sennet_id"] == test_section["sennet_id"]

assert res.json["description"] == data["description"]
assert res.json["lab_tissue_sample_id"] == data["lab_tissue_sample_id"]

# check database
db_entity = get_entity(test_section["uuid"], db_session)
assert db_entity["description"] == data["description"]
assert db_entity["lab_tissue_sample_id"] == data["lab_tissue_sample_id"]


@pytest.mark.usefixtures("lab")
def test_update_dataset(app, requests, db_session):
# Create provenance in test database
test_entities = create_provenance(db_session, ["source", "organ", "block", "section", "dataset"])
test_dataset = test_entities["dataset"]

# uuid and search api mock responses
uuid_api_url = app.config["UUID_API_URL"]
search_api_url = app.config["SEARCH_API_URL"]
requests.add_response(
f"{uuid_api_url}/uuid/{test_dataset['uuid']}",
"get",
mock_response(200, {k: test_dataset[k] for k in ["uuid", "sennet_id", "base_id"]}),
)
requests.add_response(f"{search_api_url}/reindex/{test_dataset['uuid']}", "put", mock_response(202))

with app.test_client() as client:
data = {
"description": "New Testing lab notes",
}

res = client.put(
f"/entities/{test_dataset['uuid']}?return_all_properties=true",
json=data,
headers={"Authorization": "Bearer test_token"},
)

assert res.status_code == 200
assert res.json["uuid"] == test_dataset["uuid"]
assert res.json["sennet_id"] == test_dataset["sennet_id"]

assert res.json["description"] == data["description"]

# check database
db_entity = get_entity(test_dataset["uuid"], db_session)
assert db_entity["description"] == data["description"]

0 comments on commit b60c5a2

Please sign in to comment.