Skip to content

Commit

Permalink
Fix Product based GraphQl, Rest, Grpc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad2smile committed Sep 23, 2024
1 parent 46b69dc commit c82927e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
10 changes: 5 additions & 5 deletions test/KurzSharp.Integration.Tests/GraphQlClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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<IList<ProductDto>> GetAll()
private async Task<IList<ProductDto>> GetAll(List<Guid> relatedIds)
{
var client = _factory.CreateClient();

Expand All @@ -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<List<ProductDto>> ParseProductDtos(HttpResponseMessage response, string dataKey)
Expand Down
47 changes: 24 additions & 23 deletions test/KurzSharp.Integration.Tests/GrpcWithGrpcClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,42 @@ public async Task Operations(List<ProductDto> data, List<ProductDto> updatedData
var client = factory.Services.GetRequiredService<IProductGrpcService>();

// 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<IList<ProductDto>> GetAll(List<Guid> relatedIds)
{
var client = factory.Services.GetRequiredService<IProductGrpcService>();

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() ?? [];
}
}
39 changes: 19 additions & 20 deletions test/KurzSharp.Integration.Tests/GrpcWithRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,33 @@ public async Task Operations(List<ProductDto> data, List<ProductDto> 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
{
Expand All @@ -61,22 +60,22 @@ 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<IList<ProductDto>> GetAll()
private async Task<IList<ProductDto>> GetAll(List<Guid> relatedIds)
{
var client = factory.CreateClient();

var response = await client.GetAsync(BaseUrl);

var result = await response.Content.ReadFromJsonAsync<IList<ProductDto>>();

return result ?? ArraySegment<ProductDto>.Empty;
return result?.Where(r => relatedIds.Contains(r.Id)).ToList() ?? [];
}
}
20 changes: 10 additions & 10 deletions test/KurzSharp.Integration.Tests/RestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ 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;

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
{
Expand All @@ -65,19 +65,19 @@ 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<IList<ProductDto>> GetAll()
private async Task<IList<ProductDto>> GetAll(List<Guid> relatedIds)
{
var client = _factory.CreateClient();

var response = await client.GetAsync(BaseUrl);

var result = await response.Content.ReadFromJsonAsync<IList<ProductDto>>();

return result ?? ArraySegment<ProductDto>.Empty;
return result?.Where(r => relatedIds.Contains(r.Id)).ToList() ?? [];
}
}

0 comments on commit c82927e

Please sign in to comment.