Exemplo n.º 1
0
        public async Task QueryStateAsync_ValidateResult()
        {
            await using var client = TestClient.CreateForDaprClient();

            var queryJson = "{'query':{'filter':{ 'EQ': {'value':'test'}}}}";
            var request   = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.QueryStateAsync <Widget>("testStore", queryJson, new Dictionary <string, string>()));
            });

            // Validate request.
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.QueryStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Query.Should().Be(queryJson);
            envelope.Metadata.Should().BeEmpty();

            // Validate response.
            var testData = new Widget()
            {
                Color = "Green", Size = "Small"
            };
            var wireResponse = new Autogenerated.QueryStateResponse();

            wireResponse.Results.Add(MakeQueryStateItem("test", testData, "an etag"));

            var response = await request.CompleteWithMessageAsync(wireResponse);

            response.Results.Count.Should().Be(1);
            response.Results[0].Key.Should().Be("test");
            response.Results[0].Data.Should().Be(testData);
            response.Results[0].ETag.Should().Be("an etag");
            response.Results[0].Error.Should().BeNullOrEmpty();
        }
Exemplo n.º 2
0
        public async Task QueryStateAsync_EncountersError_ValidatePartialResult()
        {
            await using var client = TestClient.CreateForDaprClient();

            var queryJson = "{'query':{'filter':{ 'EQ': {'value':'test'}}}}";
            var request   = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return(await daprClient.QueryStateAsync <Widget>("testStore", queryJson, new Dictionary <string, string>()));
            });

            // Validate request.
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.QueryStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Query.Should().Be(queryJson);
            envelope.Metadata.Should().BeEmpty();

            // Validate response, we expect to only get the first object as the 2nd will present an error.
            var testData1 = new Widget()
            {
                Color = "Green", Size = "Small"
            };
            var testData2 = new Widget()
            {
                Color = "Green", Size = "Medium"
            };
            var testData3 = new Widget()
            {
                Color = "Green", Size = "Large"
            };
            var wireResponse = new Autogenerated.QueryStateResponse();

            wireResponse.Results.Add(MakeQueryStateItem("test1", testData1));
            wireResponse.Results.Add(MakeQueryStateItem("test2", testData2, string.Empty, "An error!"));
            wireResponse.Results.Add(MakeQueryStateItem("test3", testData3));

            var ex = await Assert.ThrowsAsync <StateQueryException <Widget> >(() => request.CompleteWithMessageAsync(wireResponse));

            ex.Message.Should().Be("Encountered an error while processing state query results.");
            var response = ex.Response;

            response.Results.Count.Should().Be(2);
            response.Results[0].Key.Should().Be("test1");
            response.Results[0].Data.Should().Be(testData1);
            response.Results[0].ETag.Should().BeNullOrEmpty();
            response.Results[0].Error.Should().BeNullOrEmpty();
            response.Results[1].Key.Should().Be("test3");
            response.Results[1].Data.Should().Be(testData3);
            response.Results[1].ETag.Should().BeNullOrEmpty();
            response.Results[1].Error.Should().BeNullOrEmpty();

            var failedKeys = ex.FailedKeys;

            failedKeys.Count.Should().Be(1);
            failedKeys[0].Should().Be("test2");
        }