Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ private async ValueTask<ConversionResult> ConvertFromBindingDataAsync(ConverterC

return ConversionResult.Success(result);
}
catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return ConversionResult.Success(null);
}
catch (Exception ex)
{
return ConversionResult.Failed(ex);
Expand Down
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Microsoft.Azure.Functions.Worker (metapackage) <version>

- `AZURE_FUNCTIONS_` environment variables are now loaded correctly when using `FunctionsApplicationBuilder`. (#2878)
- Return null when cosmos db input can't find a document (#2545)
Comment thread
florianlenz96 marked this conversation as resolved.
Outdated

### Microsoft.Azure.Functions.Worker.Core <version>

Expand Down
37 changes: 34 additions & 3 deletions test/Worker.Extensions.Tests/Cosmos/CosmosDBConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -359,14 +360,18 @@ public async Task ConvertAsync_CosmosContainerIsNull_ThrowsException_ReturnsFail
Assert.Equal($"Unable to create Cosmos container client for 'myContainer'.", conversionResult.Error.Message);
}

[Fact]
public async Task ConvertAsync_POCO_IdProvided_StatusNot200_ThrowsException_ReturnsFailure()
[Theory]
Comment thread
florianlenz96 marked this conversation as resolved.
[InlineData(HttpStatusCode.Conflict)]
[InlineData(HttpStatusCode.Forbidden)]
[InlineData(HttpStatusCode.InternalServerError)]
[InlineData(HttpStatusCode.BadRequest)]
public async Task ConvertAsync_POCO_IdProvided_NotSuccessStatus_ThrowsException_ReturnsFailure(HttpStatusCode httpStatusCode)
{
object grpcModelBindingData = GrpcTestHelper.GetTestGrpcModelBindingData(GetTestBinaryData(id: "1", partitionKey: "1"), "CosmosDB");
var context = new TestConverterContext(typeof(ToDoItem), grpcModelBindingData);

var mockResponse = new Mock<ResponseMessage>();
var cosmosException = new CosmosException("test failure", System.Net.HttpStatusCode.NotFound, 0, "test", 0);
var cosmosException = new CosmosException("test failure", httpStatusCode, 0, "test", 0);
mockResponse.Setup(x => x.EnsureSuccessStatusCode()).Throws(cosmosException);

var mockContainer = new Mock<Container>();
Expand All @@ -384,6 +389,32 @@ public async Task ConvertAsync_POCO_IdProvided_StatusNot200_ThrowsException_Retu
Assert.Equal("test failure", conversionResult.Error.Message);
}

[Fact]
public async Task ConvertAsync_POCO_IdProvided_Status404_ThrowsException_ReturnsFailure()
Comment thread
florianlenz96 marked this conversation as resolved.
Outdated
{
object grpcModelBindingData = GrpcTestHelper.GetTestGrpcModelBindingData(GetTestBinaryData(id: "1", partitionKey: "1"), "CosmosDB");
var context = new TestConverterContext(typeof(ToDoItem), grpcModelBindingData);

var mockResponse = new Mock<ResponseMessage>();
mockResponse.Setup(x => x.IsSuccessStatusCode).Returns(false);
var cosmosException = new CosmosException("test failure", HttpStatusCode.NotFound, 0, "test", 0);
mockResponse.Setup(x => x.EnsureSuccessStatusCode()).Throws(cosmosException);

var mockContainer = new Mock<Container>();
mockContainer
.Setup(m => m.ReadItemStreamAsync(It.IsAny<string>(), It.IsAny<PartitionKey>(), null, default))
.ReturnsAsync(mockResponse.Object);

_mockCosmosClient
.Setup(m => m.GetContainer(It.IsAny<string>(), It.IsAny<string>()))
.Returns(mockContainer.Object);

var conversionResult = await _cosmosDBConverter.ConvertAsync(context);

Assert.Equal(ConversionStatus.Succeeded, conversionResult.Status);
Assert.Null(conversionResult.Value);
}

private BinaryData GetTestBinaryData(string db = "testDb", string container = "testContainer", string connection = "cosmosConnection", string id = "", string partitionKey = "", string query = "", string location = "", string queryParams = "{}")
{
string jsonData = $@"{{
Expand Down