Ajout des tests de UserDataService
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
0aac223ecd
commit
4ea53b072e
@ -0,0 +1,152 @@
|
||||
using DbContextLib;
|
||||
using DbDataManager.Service;
|
||||
using Entities;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Model.OrderCriteria;
|
||||
|
||||
namespace TestEF.Service;
|
||||
|
||||
public class TestUserDataService
|
||||
{
|
||||
private readonly UserDbContext _dbContext;
|
||||
private readonly UserDataService _userDataService;
|
||||
|
||||
public TestUserDataService()
|
||||
{
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
_dbContext = new UserDbContext(options);
|
||||
_userDataService = new UserDataService(_dbContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsers_ReturnsCorrectNumberOfUsers()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false });
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
var result = _userDataService.GetUsers(1, 2, UserOrderCriteria.None);
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserById_ReturnsCorrectUser()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false });
|
||||
_dbContext.SaveChanges();
|
||||
var result = _userDataService.GetUserById(1);
|
||||
Assert.Equal("Test1", result.Username);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserByUsername_ReturnsCorrectUser()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false });
|
||||
_dbContext.SaveChanges();
|
||||
var result = _userDataService.GetUserByUsername("Test1");
|
||||
Assert.Equal(1, result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateUser_AddsNewUser()
|
||||
{
|
||||
var result = _userDataService.CreateUser("Test42", "password", "eamil@example1.com", true);
|
||||
Assert.Equal(1, _dbContext.Users.Count());
|
||||
Assert.Equal("Test42", result.Username);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteUser_RemovesUser()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
_userDataService.DeleteUser(1);
|
||||
Assert.Empty(_dbContext.Inquiries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateUser_UpdatesExistingUser()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var updatedUser = new UserEntity() { Id = 1, Username = "Updated", Email = "updated@example.com", Password = "updated", IsAdmin = false };
|
||||
var result = _userDataService.UpdateUser(1, updatedUser);
|
||||
|
||||
Assert.Equal("Updated", result.Username);
|
||||
Assert.Equal("updated@example.com", result.Email);
|
||||
Assert.Equal("updated", result.Password);
|
||||
Assert.False(result.IsAdmin);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsers_WithBadPage_ReturnsClassicUsers()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _userDataService.GetUsers(-1, 2, UserOrderCriteria.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsers_WithBadNumber_ReturnsClassicUsers()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _userDataService.GetUsers(1, -42, UserOrderCriteria.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserById_WithoutId_ThrowsException()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
Assert.Throws<ArgumentException>(() => _userDataService.GetUserById(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserByUsername_WithoutUsername_ThrowsException()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
Assert.Throws<ArgumentException>(() => _userDataService.GetUserByUsername("Test2"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteUser_WithoutId_ReturnsFalse()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _userDataService.DeleteUser(2);
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateUser_WithoutId_ThrowsException()
|
||||
{
|
||||
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var updatedUser = new UserEntity() { Id = 1, Username = "Updated", Email = "updated@email.com", Password = "updated", IsAdmin = false };
|
||||
|
||||
Assert.Throws<ArgumentException>(() => _userDataService.UpdateUser(2, updatedUser));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue