Skip to content

Commit

Permalink
GTC-2618 Simplify append logic
Browse files Browse the repository at this point in the history
  • Loading branch information
manukala6 committed Jun 26, 2024
1 parent 0608757 commit b414ea1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/models/pydantic/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class VersionAppendIn(StrictBaseModel):
layers: Optional[List[str]] = Field(
None,
description="List of layer names to append to version. "
"If not set, all layers in source_uri will be appended.",
"Only required for .gdb and .gpkg.",
)

class VersionResponse(Response):
Expand Down
13 changes: 8 additions & 5 deletions app/routes/datasets/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ async def append_to_version(
# file(s) with ogrinfo
# See https://gfw.atlassian.net/browse/GTC-2234

# Construct creation_options for the append request
# For the background task, we only need the new source uri from the request
input_data = {"creation_options": deepcopy(default_asset.creation_options)}
input_data["creation_options"]["source_uri"] = request.source_uri
Expand All @@ -252,20 +253,22 @@ async def append_to_version(
detail="source_driver must match the original source_driver"
)

# Use layers from request if provided, otherwise leave empty
# Use layers from request if provided, else set to None if layers are in version creation_options
if request.layers is not None:
input_data["creation_options"]["layers"] = request.layers
else:
input_data["creation_options"]["layers"] = None

if input_data["creation_options"].get("layers") is not None:
input_data["creation_options"]["layers"] = None

# Use the modified input_data to append the new data
background_tasks.add_task(
append_default_asset, dataset, version, input_data, default_asset.asset_id
)

# We now want to append the new uris to the existing ones and update the asset
# Now update the version's creation_options to reflect the changes from the append request
update_data = {"creation_options": deepcopy(default_asset.creation_options)}
update_data["creation_options"]["source_uri"] += request.source_uri
if input_data["creation_options"].get("layers") is not None:
if request.layers is not None:
if update_data["creation_options"]["layers"] is not None:
update_data["creation_options"]["layers"] += request.layers
else:
Expand Down
21 changes: 2 additions & 19 deletions tests/routes/datasets/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ async def test_version_post_append(async_client: AsyncClient):
response = await async_client.get(f"/dataset/{dataset}/{version}")
assert response.status_code == 200

# Test appending with same source uri
# Test appending existing source_uri
response = await async_client.post(
f"/dataset/{dataset}/{version}/append",
json = {
Expand All @@ -484,9 +484,6 @@ async def test_version_post_append(async_client: AsyncClient):
)
assert response.status_code == 200

# Test appending with new source uri

# Test appending with unspecified layers
# Test appending with invalid source uri
bad_uri = "s3://doesnotexist"
response = await async_client.post(
Expand Down Expand Up @@ -515,21 +512,7 @@ async def test_version_post_append(async_client: AsyncClient):
assert response.json()["status"] == "failed"
assert (response.json()["message"] == "source_driver must match the original source_driver")

# Test appending with missing source driver
response = await async_client.post(
f"/dataset/{dataset}/{version}/append",
json = {
"source_uri": [f"s3://{BUCKET}/{GPKG_NAME}"],
}
)
assert response.status_code == 400
assert response.json() == {"status": "failed"}
assert response.json()["status"] == "failed"
assert (
response.json()["message"]
== "source_driver must be specified for non-datapump requests"
)

## TODO: test with missing layers

@pytest.mark.hanging
@pytest.mark.asyncio
Expand Down

0 comments on commit b414ea1

Please sign in to comment.