@pytest.mark.usefixtures("reference_data", "data_catalog")
def test_list_latest_versions(admin_client, user_client, dataset_a_json):
# Create published dataset + another with 2 published and 1 draft version
dataset_a_json["title"] = {"en": "dataset"}
res = admin_client.post("/v3/datasets", dataset_a_json, content_type="application/json")
assert res.status_code == 201
dataset_a_json["title"] = {"en": "version 1 something"}
res = admin_client.post("/v3/datasets", dataset_a_json, content_type="application/json")
assert res.status_code == 201
res = admin_client.post(
f"/v3/datasets/{res.data['id']}/new-version", content_type="application/json"
)
assert res.status_code == 201
res = admin_client.patch(
f"/v3/datasets/{res.data['id']}",
{"title": {"en": "version 2"}},
content_type="application/json",
)
assert res.status_code == 200
res = admin_client.post(
f"/v3/datasets/{res.data['id']}/publish",
content_type="application/json",
)
assert res.status_code == 200
res = admin_client.post(
f"/v3/datasets/{res.data['id']}/new-version", content_type="application/json"
)
assert res.status_code == 201
res = admin_client.patch(
f"/v3/datasets/{res.data['id']}",
{"title": {"en": "version 3 draft"}},
content_type="application/json",
)
assert res.status_code == 200
# Admin should see latest versions even if they are drafts
res = admin_client.get("/v3/datasets?latest_versions=true&ordering=created&pagination=false")
assert res.status_code == 200
assert len(res.data) == 2
assert res.data[0]["title"]["en"] == "dataset"
assert res.data[1]["title"]["en"] == "version 3 draft"
# Ordering should work
res = admin_client.get("/v3/datasets?latest_versions=true&ordering=-created&pagination=false")
assert res.status_code == 200
assert res.data[0]["title"]["en"] == "version 3 draft"
assert res.data[1]["title"]["en"] == "dataset"
# Filtering should match any latest dataset
res = admin_client.get("/v3/datasets?latest_versions=true&pagination=false&title=something")
assert res.status_code == 200
assert len(res.data) == 0
res = admin_client.get("/v3/datasets?latest_versions=true&pagination=false&search=draft")
assert res.status_code == 200
assert len(res.data) == 1
assert res.data[0]["title"]["en"] == "version 3 draft"
# Other user should only see published versions
res = user_client.get("/v3/datasets?latest_versions=true&ordering=created&pagination=false")
assert res.status_code == 200
assert len(res.data) == 2
assert res.data[0]["title"]["en"] == "dataset"
assert res.data[1]["title"]["en"] == "version 2"