Raso
Raso
CC#
Created by Raso on 12/8/2024 in #help
Issue with xUnit and Integration Test
@dreadfullydistinct thanks for the answer! Looks like I'll have to go with collections then.. right now it's not yet an application, it's literally as simple as the code above, I was just building a custom personal clean architecture template, and was adding some tests to that template.. I'm just testing simple CRUD, that's why I don't understand how I am already out of that xunit pattern.. I'll try to organize in collection, but it'll be ugly to my eyes 😂 like I'll have maybe three collections: - one for get by id, get all and update - one for delete by id - ine for delete all In this way I should be sure that all the tests run as they are intended to run, without risking to edit the database
13 replies
CC#
Created by electronic heartbreak. on 12/8/2024 in #help
Unable to properly launch Web API using Docker (Compose)
Just to be sure, Are you trying to reach the http://localhost:8080 right? Without docker the port might be the 55720 but in your docker-compose you are telling that you are mapping for http 8080 your pc -> 8080 docker container, for https 8081 your pc -> 8081 docker container
7 replies
CC#
Created by Raso on 12/8/2024 in #help
Issue with xUnit and Integration Test
In this case, I seeded the database with three entities. So in the GetAll I want to ensure that there will be at least 3 entities (at least because I have the CreateTodo test as well, which will lead it to 4, but then I have the delete by id which will lead again to 3, and then the delete all which would lead to 0).. I'm actually confused on how I should handle that without using an order to be honest
13 replies
CC#
Created by Raso on 12/8/2024 in #help
Issue with xUnit and Integration Test
This is, just for context, the GetAll test and the DeleteAll test:
C#

public class TodoTests : BaseIntegrationTest
{
private readonly List<TodoEntity> _todoEntitiesSeed =
[
new() { Title = "Test Todo 1", Description = "Description 1" },
new() { Title = "Test Todo 2", Description = "Description 2" },
new() { Title = "Test Todo 3", Description = "Description 3" },
];

private const string BaseEndpoint = "/api/todo";


...


[Fact]
public async Task GetAll_ShouldReturn_AllEntities()
{
// Arrange
var minLengthResult = _todoEntitiesSeed.Count;

// Act
var response = await HttpClient.GetAsync(BaseEndpoint);

// Assert
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Result<List<TodoDto>>>();
Assert.NotNull(result);
Assert.NotNull(result.Data);
Assert.True(result.Data.Count >= minLengthResult);
}

[Fact]
public async Task DeleteAll_ShouldReturn_True()
{
// Act
var response = await HttpClient.DeleteAsync(BaseEndpoint);

// Assert
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Result<bool>>();
Assert.NotNull(result);
Assert.True(result.Data);
}
}
C#

public class TodoTests : BaseIntegrationTest
{
private readonly List<TodoEntity> _todoEntitiesSeed =
[
new() { Title = "Test Todo 1", Description = "Description 1" },
new() { Title = "Test Todo 2", Description = "Description 2" },
new() { Title = "Test Todo 3", Description = "Description 3" },
];

private const string BaseEndpoint = "/api/todo";


...


[Fact]
public async Task GetAll_ShouldReturn_AllEntities()
{
// Arrange
var minLengthResult = _todoEntitiesSeed.Count;

// Act
var response = await HttpClient.GetAsync(BaseEndpoint);

// Assert
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Result<List<TodoDto>>>();
Assert.NotNull(result);
Assert.NotNull(result.Data);
Assert.True(result.Data.Count >= minLengthResult);
}

[Fact]
public async Task DeleteAll_ShouldReturn_True()
{
// Act
var response = await HttpClient.DeleteAsync(BaseEndpoint);

// Assert
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<Result<bool>>();
Assert.NotNull(result);
Assert.True(result.Data);
}
}
13 replies
CC#
Created by Raso on 5/9/2024 in #help
How do I do an inner GroupJoin on dotnet?
Here is what I did:
C#
public async Task<ICollection<BenefitEntity>> GetAllBenefitEntitiesAsync()
{
var benefitsQuery = Entities
.Include(a => a.CategoryEntity);

var translationsQuery = DbContext.TranslationsEntities!
.AsNoTracking()
.Where(e => e.Language.Equals(LANGUAGE));

var benefits = await benefitsQuery.ToListAsync();

var result = benefits
.GroupJoin(
translationsQuery,
benefit => benefit.Id,
translation => translation.EntityId,
(benefit, translation) => new { Benefit = benefit, Translation = translation })
.Select(result => new BenefitEntity
{
// Benefit Name and Description from joined translations
Name = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Name)))?.Value ?? result.Benefit.Name,
Description = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Description)))?.Value ?? result.Benefit.Description,
Id = result.Benefit.Id,
CategoryEntity = result.Benefit.CategoryEntity,
BenefitCategoryId = result.Benefit.BenefitCategoryId,
})
.AsQueryable();

return result.ToList();
}
C#
public async Task<ICollection<BenefitEntity>> GetAllBenefitEntitiesAsync()
{
var benefitsQuery = Entities
.Include(a => a.CategoryEntity);

var translationsQuery = DbContext.TranslationsEntities!
.AsNoTracking()
.Where(e => e.Language.Equals(LANGUAGE));

var benefits = await benefitsQuery.ToListAsync();

var result = benefits
.GroupJoin(
translationsQuery,
benefit => benefit.Id,
translation => translation.EntityId,
(benefit, translation) => new { Benefit = benefit, Translation = translation })
.Select(result => new BenefitEntity
{
// Benefit Name and Description from joined translations
Name = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Name)))?.Value ?? result.Benefit.Name,
Description = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Description)))?.Value ?? result.Benefit.Description,
Id = result.Benefit.Id,
CategoryEntity = result.Benefit.CategoryEntity,
BenefitCategoryId = result.Benefit.BenefitCategoryId,
})
.AsQueryable();

return result.ToList();
}
Starting from that point, how can I do another Join for CategoryEntity, using the same translationQuery, joining the categoryEntity.Id with translation.EntityId ?
2 replies