From c82927eb387d8188eca66e3ac189442f66d6c6c3 Mon Sep 17 00:00:00 2001 From: ahmad Date: Mon, 23 Sep 2024 17:16:22 +0200 Subject: [PATCH] Fix Product based GraphQl, Rest, Grpc tests --- .../GraphQlClientTests.cs | 10 ++-- .../GrpcWithGrpcClientTests.cs | 47 ++++++++++--------- .../GrpcWithRestClientTests.cs | 39 ++++++++------- .../RestClientTests.cs | 20 ++++---- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/test/KurzSharp.Integration.Tests/GraphQlClientTests.cs b/test/KurzSharp.Integration.Tests/GraphQlClientTests.cs index c3707fd..a0dadb5 100644 --- a/test/KurzSharp.Integration.Tests/GraphQlClientTests.cs +++ b/test/KurzSharp.Integration.Tests/GraphQlClientTests.cs @@ -43,7 +43,7 @@ await client.PostAsync(BaseUrl, } // READ - var results = await GetAll(); + var results = await GetAll(dataIds); results.Select(i => i.Id).ToList().Should().Contain(dataIds); @@ -70,7 +70,7 @@ await client.PostAsync(BaseUrl, new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json")); } - var updatedResults = await GetAll(); + var updatedResults = await GetAll(dataIds); updatedResults.Select(i => i.Id).Should().Contain(dataIds); @@ -93,12 +93,12 @@ await client.PostAsync(BaseUrl, new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json")); } - var afterDeletedRes = await GetAll(); + var afterDeletedRes = await GetAll(dataIds); afterDeletedRes.Should().NotContain(updatedResults); } - private async Task> GetAll() + private async Task> GetAll(List relatedIds) { var client = _factory.CreateClient(); @@ -121,7 +121,7 @@ query Query { var nodes = await ParseProductDtos(response, "products"); - return nodes.ToList(); + return nodes.Where(r => relatedIds.Contains(r.Id)).ToList(); } private static async Task> ParseProductDtos(HttpResponseMessage response, string dataKey) diff --git a/test/KurzSharp.Integration.Tests/GrpcWithGrpcClientTests.cs b/test/KurzSharp.Integration.Tests/GrpcWithGrpcClientTests.cs index a3438f6..03022fa 100644 --- a/test/KurzSharp.Integration.Tests/GrpcWithGrpcClientTests.cs +++ b/test/KurzSharp.Integration.Tests/GrpcWithGrpcClientTests.cs @@ -22,41 +22,42 @@ public async Task Operations(List data, List updatedData var client = factory.Services.GetRequiredService(); // INSERT - foreach (var dto in data) - { - await client.AddProduct(dto, CancellationToken.None); - } + await Task.WhenAll(data.Select(dto => client.AddProduct(dto, CancellationToken.None))); // READ - var serviceResult = await client.GetProducts(CancellationToken.None); - var results = serviceResult.ToList(); + var afterAddDtos = await GetAll(dataIds); - results.Select(i => i.Id).ToList().Should().Contain(dataIds); + afterAddDtos.Select(i => i.Id).ToList().Should().Contain(dataIds); // UPDATE - for (var i = 0; i < data.Count; i++) + await Task.WhenAll(afterAddDtos.Select((dto, i) => { - var dto = results[i]; - dto.Name = updatedData[i].Name; - dto.Password = updatedData[i].Password; + var updateDto = updatedData[i]; - await client.UpdateProduct(dto, CancellationToken.None); - } + dto.Name = updateDto.Name; + dto.Password = updateDto.Password; - serviceResult = await client.GetProducts(CancellationToken.None); - var updatedResults = serviceResult.ToList(); + return client.UpdateProduct(dto, CancellationToken.None); + })); - updatedResults.Select(i => i.Id).ToList().Should().Contain(dataIds); + var afterUpdateDtos = await GetAll(dataIds); + + afterUpdateDtos.Select(i => i.Id).ToList().Should().Contain(dataIds); // DELETE - foreach (var dto in data) - { - await client.DeleteProduct(dto, CancellationToken.None); - } + await Task.WhenAll(data.Select(dto => client.DeleteProduct(dto, CancellationToken.None))); + + var afterDeletedRes = await GetAll(dataIds); + + afterDeletedRes.Should().NotContain(afterUpdateDtos); + } + + private async Task> GetAll(List relatedIds) + { + var client = factory.Services.GetRequiredService(); - serviceResult = await client.GetProducts(CancellationToken.None); - var afterDeletedRes = serviceResult.ToList(); + var result = await client.GetProducts(CancellationToken.None); - afterDeletedRes.Should().NotContain(updatedResults); + return result?.Where(r => relatedIds.Contains(r.Id)).ToList() ?? []; } } diff --git a/test/KurzSharp.Integration.Tests/GrpcWithRestClientTests.cs b/test/KurzSharp.Integration.Tests/GrpcWithRestClientTests.cs index fe2b139..76a2c16 100644 --- a/test/KurzSharp.Integration.Tests/GrpcWithRestClientTests.cs +++ b/test/KurzSharp.Integration.Tests/GrpcWithRestClientTests.cs @@ -25,34 +25,33 @@ public async Task Operations(List data, List updatedData var client = factory.CreateClient(); // INSERT - foreach (var dto in data) - { - await client.PostAsync(BaseUrl, - new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json")); - } + await Task.WhenAll(data.Select(dto => client.PostAsync(BaseUrl, + new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json")))); // READ - var results = await GetAll(); + var afterAddDtos = await GetAll(dataIds); - results.Select(i => i.Id).ToList().Should().Contain(dataIds); + afterAddDtos.Select(i => i.Id).ToList().Should().Contain(dataIds); // UPDATE - for (var i = 0; i < data.Count; i++) + await Task.WhenAll(afterAddDtos.Select((dto, i) => { - var dto = results[i]; - dto.Name = updatedData[i].Name; - dto.Password = updatedData[i].Password; + var updateDto = updatedData[i]; + + dto.Name = updateDto.Name; + dto.Password = updateDto.Password; - await client.PutAsync(BaseUrl, + return client.PutAsync(BaseUrl, new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json")); - } + })); + - var updatedResults = await GetAll(); + var updatedResults = await GetAll(dataIds); updatedResults.Select(i => i.Id).ToList().Should().Contain(dataIds); // DELETE - foreach (var dto in data) + await Task.WhenAll(data.Select(dto => { var request = new HttpRequestMessage { @@ -61,15 +60,15 @@ await client.PutAsync(BaseUrl, RequestUri = new Uri(client.BaseAddress!, BaseUrl) }; - await client.SendAsync(request); - } + return client.SendAsync(request); + })); - var afterDeletedRes = await GetAll(); + var afterDeletedRes = await GetAll(dataIds); afterDeletedRes.Should().NotContain(updatedResults); } - private async Task> GetAll() + private async Task> GetAll(List relatedIds) { var client = factory.CreateClient(); @@ -77,6 +76,6 @@ private async Task> GetAll() var result = await response.Content.ReadFromJsonAsync>(); - return result ?? ArraySegment.Empty; + return result?.Where(r => relatedIds.Contains(r.Id)).ToList() ?? []; } } diff --git a/test/KurzSharp.Integration.Tests/RestClientTests.cs b/test/KurzSharp.Integration.Tests/RestClientTests.cs index 0bad4bd..5a66ed3 100644 --- a/test/KurzSharp.Integration.Tests/RestClientTests.cs +++ b/test/KurzSharp.Integration.Tests/RestClientTests.cs @@ -33,14 +33,14 @@ await client.PostAsync(BaseUrl, } // READ - var results = await GetAll(); + var afterAddDtos = await GetAll(dataIds); - results.Select(i => i.Id).ToList().Should().Contain(dataIds); + afterAddDtos.Select(i => i.Id).ToList().Should().Contain(dataIds); // UPDATE for (var i = 0; i < data.Count; i++) { - var dto = results[i]; + var dto = afterAddDtos[i]; dto.Name = updatedData[i].Name; dto.Password = updatedData[i].Password; @@ -48,12 +48,12 @@ await client.PutAsync(BaseUrl, new StringContent(JsonSerializer.Serialize(dto), Encoding.UTF8, "application/json")); } - var updatedResults = await GetAll(); + var afterUpdateDtos = await GetAll(dataIds); - updatedResults.Select(i => i.Id).Should().Contain(dataIds); + afterUpdateDtos.Select(i => i.Id).Should().Contain(dataIds); // DELETE - foreach (var dto in updatedResults) + foreach (var dto in afterUpdateDtos) { var request = new HttpRequestMessage { @@ -65,12 +65,12 @@ await client.PutAsync(BaseUrl, await client.SendAsync(request); } - var afterDeletedRes = await GetAll(); + var afterDeletedRes = await GetAll(dataIds); - afterDeletedRes.Should().NotContain(updatedResults); + afterDeletedRes.Should().NotContain(afterUpdateDtos); } - private async Task> GetAll() + private async Task> GetAll(List relatedIds) { var client = _factory.CreateClient(); @@ -78,6 +78,6 @@ private async Task> GetAll() var result = await response.Content.ReadFromJsonAsync>(); - return result ?? ArraySegment.Empty; + return result?.Where(r => relatedIds.Contains(r.Id)).ToList() ?? []; } }