Skip to content

Commit

Permalink
add some missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Apr 28, 2024
1 parent 18126d7 commit dbbd221
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 26 deletions.
25 changes: 25 additions & 0 deletions CleanAspCore.Api.Tests/Features/Employees/CreateEmployeeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,29 @@ public async Task CreateEmployee_IsAdded()
context.Employees.Should().BeEquivalentTo(new[] { new { Id = createdId } });
});
}

[Test]
public async Task CreateEmployee_InvalidRequest_ReturnsBadRequest()
{
//Arrange
var createEmployeeRequest = new CreateEmployeeRequestFaker()
.RuleFor(x => x.FirstName, string.Empty)
.Generate();

Sut.SeedData(context =>
{
context.Departments.Add(new DepartmentFaker().RuleFor(x => x.Id, createEmployeeRequest.DepartmentId).Generate());
context.Jobs.Add(new JobFaker().RuleFor(x => x.Id, createEmployeeRequest.JobId).Generate());
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().CreateEmployee(createEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.BadRequest);
Sut.AssertDatabase(context =>
{
context.Employees.Should().BeEmpty();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ public async Task DeleteEmployeeById_IsDeleted()
await response.AssertStatusCode(HttpStatusCode.OK);
Sut.AssertDatabase(context => { context.Employees.Should().BeEmpty(); });
}

[Test]
public async Task DeleteEmployeeById_DoesNotExist_ReturnsNotFound()
{
//Arrange
var id = Guid.NewGuid();

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().DeleteEmployeeById(id);

//Assert
await response.AssertStatusCode(HttpStatusCode.NotFound);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,22 @@ public async Task UpdateEmployeeById_IsUpdated()
});
});
}

[Test]
public async Task UpdateEmployeeById_DoesNotExist_ReturnsNotFound()
{
//Arrange
var employee = new EmployeeFaker().Generate();

UpdateEmployeeRequest updateEmployeeRequest = new()
{
FirstName = "Updated"
};

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().UpdateEmployeeById(employee.Id, updateEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.NotFound);
}
}
11 changes: 2 additions & 9 deletions CleanAspCore/Features/Employees/Endpoints/UpdateEmployeeById.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ public sealed class UpdateEmployeeRequest

internal static class UpdateEmployeeById
{
internal static async Task<Results<NoContent, NotFound, ValidationProblem>> Handle(Guid id,
[FromBody] UpdateEmployeeRequest updateEmployeeRequest, HrContext context, [FromServices] IValidator<UpdateEmployeeRequest> validator, CancellationToken cancellationToken)
internal static async Task<Results<NoContent, NotFound, ValidationProblem>> Handle(
Guid id, [FromBody] UpdateEmployeeRequest updateEmployeeRequest, HrContext context, CancellationToken cancellationToken)
{
var validationResult = await validator.ValidateAsync(updateEmployeeRequest, cancellationToken);

if (!validationResult.IsValid)
{
return TypedResults.ValidationProblem(validationResult.ToDictionary());
}

var builder = new SetPropertyBuilder<Employee>()
.SetPropertyIfNotNull(x => x.FirstName, updateEmployeeRequest.FirstName)
.SetPropertyIfNotNull(x => x.LastName, updateEmployeeRequest.LastName)
Expand Down
2 changes: 1 addition & 1 deletion CleanAspCore/Features/Employees/IEmployeeApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IEmployeeApiClient
[Post("/employees")]
Task<HttpResponseMessage> CreateEmployee(CreateEmployeeRequest createEmployeeRequest);

[Put("/employees/{id}")]
[Patch("/employees/{id}")]
Task<HttpResponseMessage> UpdateEmployeeById(Guid id, UpdateEmployeeRequest updateEmployeeRequest);

[Delete("/employees/{id}")]
Expand Down
3 changes: 2 additions & 1 deletion CleanAspCore/Features/Employees/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static void AddEmployeesRoutes(this IEndpointRouteBuilder host)

employeeGroup.MapDelete("/{id:guid}", DeleteEmployeeById.Handle);

employeeGroup.MapPut("/{id:guid}", UpdateEmployeeById.Handle);
employeeGroup.MapPatch("/{id:guid}", UpdateEmployeeById.Handle)
.WithRequestValidation<UpdateEmployeeRequest>();;
}
}
15 changes: 0 additions & 15 deletions CleanAspCore/Features/Import/EmployeeFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,3 @@ public EmployeeFaker()
});
}
}

public sealed class EmployeeDtoFaker : Faker<EmployeeDto>
{
public EmployeeDtoFaker()
{
UseSeed(3);
RuleFor(x => x.FirstName, f => f.Name.FirstName());
RuleFor(x => x.LastName, f => f.Name.LastName());
RuleFor(x => x.Email, f => f.Internet.Email());
RuleFor(x => x.Gender, f => f.PickRandom("Male", "Female"));
RuleFor(x => x.DepartmentId, f => f.Random.Guid());
RuleFor(x => x.JobId, f => f.Random.Guid());

}
}

0 comments on commit dbbd221

Please sign in to comment.