Skip to content

Instantly share code, notes, and snippets.

@Msirkovsky
Created March 17, 2024 05:33
Show Gist options
  • Save Msirkovsky/fe518a751b0ec861f5a8f491807bdf6f to your computer and use it in GitHub Desktop.
Save Msirkovsky/fe518a751b0ec861f5a8f491807bdf6f to your computer and use it in GitHub Desktop.
Tabnine - DeleteUserCommandHandlerTests
public class DeleteUserCommandHandlerTests
{
private readonly AssistantsAIDbContext _context;
private readonly DeleteUserCommandHandler _handler;
public DeleteUserCommandHandlerTests()
{
var options = new DbContextOptionsBuilder<AssistantsAIDbContext>()
.UseInMemoryDatabase(databaseName: "AssistantsAITest")
.Options;
_context = new AssistantsAIDbContext(options);
_handler = new DeleteUserCommandHandler(_context);
}
[Fact]
public async Task Handle_WhenUserExistsAndIsNotLocked_ShouldDeleteUser()
{
// Arrange
var userId = 1;
var command = new DeleteUserCommandInput(userId);
var user = new User { User_Id = userId, Locked = false };
_context.Users.Add(user);
await _context.SaveChangesAsync();
// Act
await _handler.Handle(command, default);
// Assert
var deletedUser = await _context.Users.FirstOrDefaultAsync(u => u.User_Id == userId);
deletedUser.Should().BeNull();
}
[Fact]
public async Task Handle_WhenUserExistsAndIsLocked_ShouldNotDeleteUser()
{
// Arrange
var userId = 1;
var command = new DeleteUserCommandInput(userId);
var user = new User { User_Id = userId, Locked = true };
_context.Users.Add(user);
await _context.SaveChangesAsync();
// Act
await _handler.Handle(command, default);
// Assert
var deletedUser = await _context.Users.FirstOrDefaultAsync(u => u.User_Id == userId);
deletedUser.Should().NotBeNull();
}
[Fact]
public async Task Handle_WhenUserDoesNotExist_ShouldNotDeleteUser()
{
// Arrange
var userId = 1;
var command = new DeleteUserCommandInput(userId);
_context.Users.Add(new User { User_Id = 2, Locked = false });
await _context.SaveChangesAsync();
// Act
await _handler.Handle(command, default);
// Assert
var deletedUser = await _context.Users.FirstOrDefaultAsync(u => u.User_Id == userId);
deletedUser.Should().BeNull();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment