Skip to content

Commit

Permalink
Fix delete behavior in Cosmos adapter (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
brfrn169 committed Nov 18, 2024
1 parent 721dbca commit 2328900
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.implementation.NotFoundException;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.PartitionKey;
import com.scalar.db.api.Delete;
Expand Down Expand Up @@ -42,7 +43,11 @@ private void execute(Mutation mutation, TableMetadata tableMetadata) throws Cosm
PartitionKey partitionKey = cosmosMutation.getCosmosPartitionKey();
CosmosItemRequestOptions options = new CosmosItemRequestOptions();

getContainer(mutation).deleteItem(id, partitionKey, options);
try {
getContainer(mutation).deleteItem(id, partitionKey, options);
} catch (NotFoundException ignored) {
// don't throw an exception if the item is not found
}
} else {
// clustering key is not fully specified
executeStoredProcedure(mutation, tableMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.CosmosScripts;
import com.azure.cosmos.CosmosStoredProcedure;
import com.azure.cosmos.implementation.NotFoundException;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosStoredProcedureRequestOptions;
Expand Down Expand Up @@ -121,6 +122,19 @@ public void handle_DeleteWithoutConditionsCosmosExceptionThrown_ShouldThrowExecu
.hasCause(toThrow);
}

@Test
public void handle_DeleteWithoutConditionsNotFoundExceptionThrown_ShouldNotThrowAnyException() {
// Arrange
doThrow(NotFoundException.class)
.when(container)
.deleteItem(anyString(), any(PartitionKey.class), any(CosmosItemRequestOptions.class));

Delete delete = prepareDelete();

// Act Assert
assertThatCode(() -> handler.handle(delete)).doesNotThrowAnyException();
}

@Test
public void handle_DeleteWithoutClusteringKeyGiven_ShouldCallStoredProcedure() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,17 @@ public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProper
assertThat(results.size()).isEqualTo(0);
}

@Test
public void delete_ForNonExistingRecord_ShouldDoNothing() throws ExecutionException {
// Arrange

// Act Assert
assertThatCode(() -> storage.delete(prepareDeletes().get(0))).doesNotThrowAnyException();

Optional<Result> result = storage.get(prepareGet(0, 0));
assertThat(result).isNotPresent();
}

@Test
public void mutate_MultiplePutGiven_ShouldStoreProperly() throws ExecutionException, IOException {
// Arrange
Expand Down

0 comments on commit 2328900

Please sign in to comment.