Skip to content

test_preservation#

preservation_dataset_json#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.fixture
def preservation_dataset_json(dataset_a_json, contract_a):
    dataset = {
        **dataset_a_json,
        "preservation": {
            "contract": contract_a.json()["id"],
            "state": 0,
            "description": {"en": "Test preservation description"},
            "reason_description": "Test preservation reason description",
        },
    }
    return dataset

preservation_dataset#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.fixture
def preservation_dataset(admin_client, preservation_dataset_json):
    return admin_client.post(
        "/v3/datasets", preservation_dataset_json, content_type="application/json"
    ).json()

test_create_dataset_preservation#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_create_dataset_preservation(admin_client, preservation_dataset_json):
    resp = admin_client.post(
        "/v3/datasets", preservation_dataset_json, content_type="application/json"
    )
    assert resp.status_code == 201
    assert_nested_subdict(preservation_dataset_json["preservation"], resp.json()["preservation"])

test_create_dataset_preservation_without_contract#

Try updating preservation state without including a valid contract; this should fail.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_create_dataset_preservation_without_contract(admin_client, preservation_dataset_json):
    """
    Try updating preservation state without including a valid contract;
    this should fail.
    """
    data = preservation_dataset_json.copy()
    del data["preservation"]["contract"]
    resp = admin_client.post("/v3/datasets", data, content_type="application/json")
    assert resp.status_code == 400
    assert resp.json()["preservation"]["contract"] == [
        "Dataset in preservation process must have a contract."
    ]

test_update_dataset_preservation_state#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_update_dataset_preservation_state(
    admin_client, preservation_dataset, dataset_signal_handlers
):
    resp = admin_client.patch(
        f"/v3/datasets/{preservation_dataset['id']}/preservation",
        {"state": 20},
        content_type="application/json",
    )
    assert resp.status_code == 200

    data = resp.json()
    assert data["id"] == preservation_dataset["preservation"]["id"]
    assert data["state"] == 20
    dataset_signal_handlers.assert_call_counts(created=0, updated=1)

test_update_dataset_preservation_state_remove_contract_error#

Try updating a dataset to remove the contract while retaining the current preservation state. This should fail.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_update_dataset_preservation_state_remove_contract_error(
    admin_client, preservation_dataset
):
    """
    Try updating a dataset to remove the contract while retaining the current
    preservation state. This should fail.
    """
    resp = admin_client.patch(
        f"/v3/datasets/{preservation_dataset['id']}/preservation",
        {"contract": None},
        content_type="application/json",
    )
    assert resp.status_code == 400
    assert resp.json()["contract"] == ["Dataset in preservation process must have a contract."]

test_update_dataset_reset_preservation#

Try updating a dataset to reset the preservation process by clearing the preservation state and removing the contract

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_update_dataset_reset_preservation(admin_client, preservation_dataset):
    """
    Try updating a dataset to reset the preservation process by clearing
    the preservation state and removing the contract
    """
    resp = admin_client.patch(
        f"/v3/datasets/{preservation_dataset['id']}/preservation?include_nulls=true",
        {"state": -1, "contract": None},
        content_type="application/json",
    )
    assert resp.status_code == 200
    data = resp.json()

    assert data["id"]
    assert data["state"] == -1
    assert data["contract"] is None

test_query_datasets_preservation_state#

Query datasets that have the given preservation state

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_query_datasets_preservation_state(admin_client, preservation_dataset):
    """
    Query datasets that have the given preservation state
    """
    # One dataset with state 0
    resp = admin_client.get("/v3/datasets", {"preservation__state": ["10", "0"]})
    assert resp.status_code == 200
    assert resp.json()["count"] == 1
    assert resp.json()["results"][0]["id"] == preservation_dataset["id"]

    # No datasets with these preservation states
    resp = admin_client.get("/v3/datasets", {"preservation__state": ["10", "20"]})
    assert resp.status_code == 200
    assert resp.json()["count"] == 0

test_delete_dataset_with_preservation#

Delete a dataset containing preservation data

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_delete_dataset_with_preservation(admin_client, preservation_dataset):
    """
    Delete a dataset containing preservation data
    """
    resp = admin_client.delete(f"/v3/datasets/{preservation_dataset['id']}")
    assert resp.status_code == 204

    resp = admin_client.get(f"/v3/datasets/{preservation_dataset['id']}")
    assert resp.status_code == 404

    # Contract is NOT deleted
    resp = admin_client.get(f"/v3/contracts/{preservation_dataset['preservation']['contract']}")
    assert resp.status_code == 200

test_update_dataset_preservation_indirect#

Try updating dataset's preservation state via the /v3/datasets/<id> endpoint and ensure preservation data must be complete in order to be accepted

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_update_dataset_preservation_indirect(admin_client, preservation_dataset):
    """
    Try updating dataset's preservation state via the `/v3/datasets/<id>` endpoint
    and ensure preservation data must be complete in order to be accepted
    """
    preservation_dataset = {
        "id": preservation_dataset["id"],
        "preservation": preservation_dataset["preservation"],
    }

    # Attempt to patch only preservation state; this will fail
    resp = admin_client.patch(
        f"/v3/datasets/{preservation_dataset['id']}",
        {"id": preservation_dataset["id"], "preservation": {"state": 50}},
        content_type="application/json",
    )
    assert resp.status_code == 400
    assert resp.json() == {
        "preservation": {"contract": ["Dataset in preservation process must have a contract."]}
    }

    preservation = preservation_dataset["preservation"]

    # Provide both mandatory fields; update should now succeed
    resp = admin_client.patch(
        f"/v3/datasets/{preservation_dataset['id']}",
        {
            "id": preservation_dataset["id"],
            "preservation": {"state": 50, "contract": preservation["contract"]},
        },
        content_type="application/json",
    )
    assert resp.status_code == 200
    data = resp.json()

    assert data["preservation"]["state"] == 50
    assert data["preservation"]["contract"] == preservation["contract"]

test_delete_dataset_preservation#

Try deleting dataset preservation using the endpoint.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_delete_dataset_preservation(admin_client, preservation_dataset):
    """Try deleting dataset preservation using the endpoint."""
    resp = admin_client.delete(
        f"/v3/datasets/{preservation_dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 204

    # The preservation endpoint should return default values when preservation does not exist
    resp = admin_client.get(
        f"/v3/datasets/{preservation_dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 200

test_get_dataset_preservation_anonymous#

Try retrieving dataset preservation as an anonymous user.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_get_dataset_preservation_anonymous(client, preservation_dataset):
    """Try retrieving dataset preservation as an anonymous user."""
    resp = client.get(
        f"/v3/datasets/{preservation_dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 200

    # Nested endpoint of should not be visible if dataset is not visible
    Dataset.objects.filter(id=preservation_dataset["id"]).update(state="draft")
    resp = client.get(
        f"/v3/datasets/{preservation_dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 404

test_get_dataset_nonexisting_preservation#

Preservation should return default values when preservation does not exist.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_get_dataset_nonexisting_preservation(
    admin_client, dataset_a_json, dataset_signal_handlers
):
    """Preservation should return default values when preservation does not exist."""
    dataset = admin_client.post(
        "/v3/datasets", dataset_a_json, content_type="application/json"
    ).json()

    dataset_signal_handlers.reset()
    resp = admin_client.get(
        f"/v3/datasets/{dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 200
    assert resp.data == {
        "reason_description": "",
        "state": -1,
    }
    dataset_signal_handlers.assert_call_counts(created=0, updated=0)

test_patch_dataset_nonexisting_preservation#

Try patching preservation object that does not exist yet. Should work like put.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_patch_dataset_nonexisting_preservation(
    admin_client, dataset_a_json, dataset_signal_handlers
):
    """Try patching preservation object that does not exist yet. Should work like put."""
    dataset = admin_client.post(
        "/v3/datasets", dataset_a_json, content_type="application/json"
    ).json()

    dataset_signal_handlers.reset()
    resp = admin_client.patch(
        f"/v3/datasets/{dataset['id']}/preservation",
        {"reason_description": "testing patch"},
        content_type="application/json",
    )
    assert resp.status_code == 201
    assert resp.data == {
        "id": matchers.Any(),
        "reason_description": "testing patch",
        "state": -1,
    }
    dataset_signal_handlers.assert_call_counts(created=0, updated=1)

    # Check the created preservation is associated to the dataset
    resp = admin_client.get(
        f"/v3/datasets/{dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 200

test_put_dataset_nonexisting_preservation#

Try creating preservation object using put.

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.mark.usefixtures("data_catalog", "reference_data")
def test_put_dataset_nonexisting_preservation(
    admin_client, dataset_a_json, dataset_signal_handlers
):
    """Try creating preservation object using put."""
    dataset = admin_client.post(
        "/v3/datasets", dataset_a_json, content_type="application/json"
    ).json()

    dataset_signal_handlers.reset()
    resp = admin_client.put(
        f"/v3/datasets/{dataset['id']}/preservation",
        {"state": -1},
        content_type="application/json",
    )
    assert resp.status_code == 201
    dataset_signal_handlers.assert_call_counts(created=0, updated=1)

    # Check the created preservation is associated to the dataset
    resp = admin_client.get(
        f"/v3/datasets/{dataset['id']}/preservation", content_type="application/json"
    )
    assert resp.status_code == 200

preservation_dataset_with_fileset#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
@pytest.fixture
def preservation_dataset_with_fileset(admin_client, preservation_dataset_json):
    preservation_dataset_json["fileset"] = {"storage_service": "ida", "csc_project": "project"}
    return admin_client.post(
        "/v3/datasets", preservation_dataset_json, content_type="application/json"
    ).json()

test_create_dataset_preservation_version#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
def test_create_dataset_preservation_version(
    pas_client, preservation_dataset_with_fileset, data_catalog_pas
):
    origin_id = preservation_dataset_with_fileset["id"]
    origin_pid = preservation_dataset_with_fileset["persistent_identifier"]
    res = pas_client.post(
        f"/v3/datasets/{origin_id}/create-preservation-version",
        content_type="application/json",
    )
    assert res.status_code == 201, res.data
    data = res.json()
    preserved_id = data["id"]
    preserved_pid = data["persistent_identifier"]
    assert data["data_catalog"] == "urn:nbn:fi:att:data-catalog-pas"
    assert data["fileset"]["storage_service"] == "pas"
    assert data["fileset"]["csc_project"] == "project"
    assert data["preservation"]["dataset_origin_version"]["id"] == origin_id
    assert data["other_identifiers"][0]["notation"] == origin_pid

    # Check original version has new dataset relations but is otherwise unchanged
    res = pas_client.get(f"/v3/datasets/{origin_id}", content_type="application/json")
    assert res.status_code == 200, res.data
    data = res.json()
    assert data["data_catalog"] == "urn:nbn:fi:att:data-catalog-ida"
    assert data["fileset"]["storage_service"] == "ida"
    assert data["fileset"]["csc_project"] == "project"
    assert data["preservation"]["dataset_version"]["id"] == preserved_id
    assert data["other_identifiers"][0]["notation"] == preserved_pid

test_create_dataset_preservation_version_no_preservation#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
def test_create_dataset_preservation_version_no_preservation(
    pas_client, preservation_dataset_json, data_catalog_pas
):
    preservation_dataset_json.pop("preservation")
    preservation_dataset = pas_client.post(
        "/v3/datasets", preservation_dataset_json, content_type="application/json"
    ).json()
    origin_id = preservation_dataset["id"]
    res = pas_client.post(
        f"/v3/datasets/{origin_id}/create-preservation-version",
        content_type="application/json",
    )
    assert res.status_code == 400, res.data
    data = res.json()
    assert "not in preservation" in data["detail"]

test_create_dataset_preservation_version_no_preservation#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
def test_create_dataset_preservation_version_no_preservation(
    pas_client, preservation_dataset_json, data_catalog_pas
):
    preservation_dataset_json.pop("preservation")
    preservation_dataset = pas_client.post(
        "/v3/datasets", preservation_dataset_json, content_type="application/json"
    ).json()
    origin_id = preservation_dataset["id"]
    res = pas_client.post(
        f"/v3/datasets/{origin_id}/create-preservation-version",
        content_type="application/json",
    )
    assert res.status_code == 400, res.data
    data = res.json()
    assert "not in preservation" in data["detail"]

test_create_dataset_preservation_version_twice#

Source code in tests/unit/apps/core/api/dataset/test_preservation.py
def test_create_dataset_preservation_version_twice(
    admin_client, pas_client, preservation_dataset_with_fileset, data_catalog_pas
):
    origin_id = preservation_dataset_with_fileset["id"]
    res = pas_client.post(
        f"/v3/datasets/{origin_id}/create-preservation-version",
        content_type="application/json",
    )
    assert res.status_code == 201, res.data
    res = pas_client.patch(
        f"/v3/datasets/{origin_id}/preservation", {"state": 0}, content_type="application/json"
    )
    assert res.status_code == 200, res.data
    res = pas_client.post(
        f"/v3/datasets/{origin_id}/create-preservation-version",
        content_type="application/json",
    )
    assert res.status_code == 400, res.data
    assert "already has a PAS version" in res.json()["detail"]