commit
0cb7df701e
@ -0,0 +1,94 @@
|
|||||||
|
using Asp.Versioning;
|
||||||
|
using Dto;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Shared;
|
||||||
|
using Shared.Mapper;
|
||||||
|
|
||||||
|
namespace API.Controllers;
|
||||||
|
|
||||||
|
[Route("api/v{version:apiVersion}/[controller]")]
|
||||||
|
[Authorize]
|
||||||
|
[ApiVersion("1.0")]
|
||||||
|
[ApiController]
|
||||||
|
public class BlackListController(ILogger<UsersController> logger, IBlackListService<BlackListDto> blackListService) : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet("user/ban/{page:int}/{number:int}")]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<BlackListDto>), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 204)]
|
||||||
|
public IActionResult GetBannedUsers(int page, int number, BlackListOdrerCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
var users = blackListService.GetBannedUsers(page, number, orderCriteria).ToList();
|
||||||
|
if (users.Count == 0)
|
||||||
|
{
|
||||||
|
logger.LogError("[ERREUR] Aucun email banni trouvé.");
|
||||||
|
return StatusCode(204);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInformation("[INFORMATION] {nb} Email(s) banni(s) trouvé(s)", users.Count);
|
||||||
|
return Ok(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("user/ban/number")]
|
||||||
|
[ProducesResponseType(typeof(UserDto), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 204)]
|
||||||
|
public IActionResult GetNumberOfBannedUsers()
|
||||||
|
{
|
||||||
|
var nb = blackListService.GetNumberOfBannedUsers();
|
||||||
|
logger.LogInformation("[INFORMATION] {nb} Email(s) banni(s) trouvé(s)", nb);
|
||||||
|
return Ok(new KeyValuePair<string,int>("number",nb));
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("user/ban")]
|
||||||
|
[ProducesResponseType(typeof(UserDto), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
public IActionResult GetUserBannedByEmail([FromBody] string email)
|
||||||
|
{
|
||||||
|
var res = blackListService.GetUserBannedByEmail(email);
|
||||||
|
if (res != null)
|
||||||
|
{
|
||||||
|
logger.LogInformation("[INFORMATION] Utilisateur banni avec l'email {email} a été trouvé.", email);
|
||||||
|
return Ok(res);
|
||||||
|
}
|
||||||
|
logger.LogError("[ERREUR] Aucun utilisateur banni trouvé avec l'email {email}.", email);
|
||||||
|
return NotFound("Utilisateur non trouvé !");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("user/ban/{username:alpha}")]
|
||||||
|
[ProducesResponseType(typeof(UserDto), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
public IActionResult BanUser(string username)
|
||||||
|
{
|
||||||
|
var success = blackListService.BanUser(username);
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
logger.LogInformation("[INFORMATION] L'utilisateur avec le pseudo {username} a été banni pour 2 ans.", username);
|
||||||
|
return Ok(new KeyValuePair<string,bool>("success", true));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogError("[ERREUR] Aucun utilisateur trouvé avec le pseudo {username}.", username);
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("user/unban")]
|
||||||
|
[ProducesResponseType(typeof(UserDto), 200)]
|
||||||
|
[ProducesResponseType(typeof(string), 404)]
|
||||||
|
public IActionResult UnbanUser([FromBody] string email)
|
||||||
|
{
|
||||||
|
var success = blackListService.UnbanUser(email);
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
logger.LogInformation("[INFORMATION] L'utilisateur avec l'email {email} a été débanni.", email);
|
||||||
|
return Ok(new KeyValuePair<string,bool>("success", true));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogError("[ERREUR] Aucun utilisateur banni trouvé avec l'email {email}.", email);
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
using Dto;
|
||||||
|
using Entities;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Shared;
|
||||||
|
using Shared.Mapper;
|
||||||
|
|
||||||
|
namespace API.Service;
|
||||||
|
|
||||||
|
public class BlackListDataServiceAPI (IBlackListService<BlackListEntity> userService) : IBlackListService<BlackListDto>
|
||||||
|
{
|
||||||
|
public IEnumerable<BlackListDto> GetBannedUsers(int page, int number, BlackListOdrerCriteria orderCriteria) =>
|
||||||
|
userService.GetBannedUsers(page, number, orderCriteria).Select(b => b.FromEntityToDto());
|
||||||
|
|
||||||
|
public int GetNumberOfBannedUsers() => userService.GetNumberOfBannedUsers();
|
||||||
|
public BlackListDto? GetUserBannedByEmail(string email)
|
||||||
|
{
|
||||||
|
var res = userService.GetUserBannedByEmail(email);
|
||||||
|
if (res == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return res.FromEntityToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool BanUser(string username) => userService.BanUser(username);
|
||||||
|
public bool UnbanUser(string email) => userService.UnbanUser(email);
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
|
namespace DbDataManager.Service;
|
||||||
|
|
||||||
|
public class BlackListDataService : IBlackListService<BlackListEntity>
|
||||||
|
{
|
||||||
|
private UserDbContext DbContext { get; set; }
|
||||||
|
|
||||||
|
public BlackListDataService(UserDbContext context)
|
||||||
|
{
|
||||||
|
DbContext = context;
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetNumberOfBannedUsers()
|
||||||
|
{
|
||||||
|
return DbContext.BlackLists.Count();
|
||||||
|
}
|
||||||
|
public IEnumerable<BlackListEntity> GetBannedUsers(int page, int number, BlackListOdrerCriteria orderCriteria)
|
||||||
|
{
|
||||||
|
if (page <= 0)
|
||||||
|
{
|
||||||
|
page = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (number <= 0)
|
||||||
|
{
|
||||||
|
number = 10;
|
||||||
|
}
|
||||||
|
IQueryable<BlackListEntity> query = DbContext.BlackLists.Skip((page - 1) * number).Take(number);
|
||||||
|
switch (orderCriteria)
|
||||||
|
{
|
||||||
|
case BlackListOdrerCriteria.None:
|
||||||
|
break;
|
||||||
|
case BlackListOdrerCriteria.ByEmail:
|
||||||
|
query = query.OrderBy(s => s.Email);
|
||||||
|
break;
|
||||||
|
case BlackListOdrerCriteria.ByExpirationDate:
|
||||||
|
query = query.OrderBy(s => s.ExpirationDate);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var blackList = query.ToList();
|
||||||
|
return blackList;
|
||||||
|
}
|
||||||
|
public BlackListEntity? GetUserBannedByEmail(string email)
|
||||||
|
{
|
||||||
|
var blackListEntity = DbContext.BlackLists.FirstOrDefault(b => b.Email == email);
|
||||||
|
return blackListEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool BanUser(string username)
|
||||||
|
{
|
||||||
|
var userEntity = DbContext.Users.FirstOrDefault(u => u.Username == username);
|
||||||
|
if (userEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DbContext.BlackLists.Add(new BlackListEntity
|
||||||
|
{ Email = userEntity.Email, ExpirationDate = DateOnly.FromDateTime(DateTime.Now.AddYears(2)) });
|
||||||
|
DbContext.Users.Remove(userEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool UnbanUser(string email)
|
||||||
|
{
|
||||||
|
var blackListEntity = DbContext.BlackLists.FirstOrDefault(b => b.Email == email);
|
||||||
|
if (blackListEntity == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DbContext.BlackLists.Remove(blackListEntity);
|
||||||
|
DbContext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace Model.OrderCriteria;
|
||||||
|
|
||||||
|
public enum BlackListOdrerCriteria
|
||||||
|
{
|
||||||
|
None, ByEmail, ByExpirationDate
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface IBlackListService<TBlackList>
|
||||||
|
{
|
||||||
|
public IEnumerable<TBlackList> GetBannedUsers(int page, int number, BlackListOdrerCriteria orderCriteria);
|
||||||
|
public int GetNumberOfBannedUsers();
|
||||||
|
public TBlackList? GetUserBannedByEmail(string email);
|
||||||
|
public bool BanUser(string username);
|
||||||
|
public bool UnbanUser(string email);
|
||||||
|
}
|
@ -0,0 +1,178 @@
|
|||||||
|
using API.Controllers;
|
||||||
|
using Dto;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
using Moq;
|
||||||
|
using Shared;
|
||||||
|
using TestAPI.Extensions;
|
||||||
|
|
||||||
|
namespace TestAPI;
|
||||||
|
|
||||||
|
public class BlackListUnitTest
|
||||||
|
{
|
||||||
|
private readonly Mock<IBlackListService<BlackListDto>> _blackListService;
|
||||||
|
|
||||||
|
public BlackListUnitTest()
|
||||||
|
{
|
||||||
|
_blackListService = new Mock<IBlackListService<BlackListDto>>();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void IsBanned()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.GetUserBannedByEmail("email@example.com"))
|
||||||
|
.Returns(new BlackListDto { Email = "email@example.com", ExpirationDate = DateOnly.FromDateTime(DateTime.Now)});
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
var result = usersController.GetUserBannedByEmail("email@example.com");
|
||||||
|
Assert.Equal(typeof(OkObjectResult), result.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void IsBannedNotFound()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.GetUserBannedByEmail("example@notfound.com"))
|
||||||
|
.Returns<BlackListDto?>(null);
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
var result = usersController.GetUserBannedByEmail("example@notfound.com");
|
||||||
|
Assert.Equal(typeof(NotFoundObjectResult), result.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BanUser()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.BanUser("Test1"))
|
||||||
|
.Returns(true);
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var userResult = usersController.BanUser("Test1");
|
||||||
|
Assert.Equal(typeof(OkObjectResult), userResult.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BanUserNotFound()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.BanUser("Test1"))
|
||||||
|
.Returns(true);
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var userResult = usersController.BanUser("Test42");
|
||||||
|
Assert.Equal(typeof(NotFoundResult), userResult.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UnbanUser()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.UnbanUser("example@email.com"))
|
||||||
|
.Returns(true);
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var userResult = usersController.UnbanUser("example@email.com");
|
||||||
|
Assert.Equal(typeof(OkObjectResult), userResult.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UnbanUserNotFound()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.UnbanUser("example@email.com"))
|
||||||
|
.Returns(false);
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var userResult = usersController.UnbanUser("example@email.com");
|
||||||
|
Assert.Equal(typeof(NotFoundResult), userResult.GetType());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetBannedUsers_NoneOrderCriteria()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.GetBannedUsers(1,10,BlackListOdrerCriteria.None))
|
||||||
|
.Returns(new List<BlackListDto>()
|
||||||
|
{
|
||||||
|
new BlackListDto { Email = "example1@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example2@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example3@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) }
|
||||||
|
});
|
||||||
|
var blackListController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var result = blackListController.GetBannedUsers(1,10,BlackListOdrerCriteria.None);
|
||||||
|
Assert.Equal(typeof(OkObjectResult), result.GetType());
|
||||||
|
if (result is OkObjectResult okObjectResult)
|
||||||
|
{
|
||||||
|
var valeur = okObjectResult.Value;
|
||||||
|
|
||||||
|
Assert.NotNull(valeur);
|
||||||
|
Assert.Equal(GetBlackList().ToString(), valeur.ToString());
|
||||||
|
Assert.True(GetBlackList().SequenceEqual(valeur as IEnumerable<BlackListDto>, new BlackListDtoEqualityComparer()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetBannedUsers_OrderByEmail()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.GetBannedUsers(1,10,BlackListOdrerCriteria.ByEmail))
|
||||||
|
.Returns(new List<BlackListDto>()
|
||||||
|
{
|
||||||
|
new BlackListDto { Email = "example1@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example2@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example3@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) }
|
||||||
|
});
|
||||||
|
var blackListController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var result = blackListController.GetBannedUsers(1,10,BlackListOdrerCriteria.ByEmail);
|
||||||
|
Assert.Equal(typeof(OkObjectResult), result.GetType());
|
||||||
|
if (result is OkObjectResult okObjectResult)
|
||||||
|
{
|
||||||
|
var valeur = okObjectResult.Value;
|
||||||
|
|
||||||
|
Assert.NotNull(valeur);
|
||||||
|
Assert.Equal(GetBlackList().ToString(), valeur.ToString());
|
||||||
|
Assert.True(GetBlackList().SequenceEqual(valeur as IEnumerable<BlackListDto>, new BlackListDtoEqualityComparer()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetBannedUsers_OrderedByExpirationDate()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.GetBannedUsers(1,10,BlackListOdrerCriteria.ByExpirationDate))
|
||||||
|
.Returns(new List<BlackListDto>()
|
||||||
|
{
|
||||||
|
new BlackListDto { Email = "example1@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example2@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example3@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) }
|
||||||
|
});
|
||||||
|
var blackListController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var result = blackListController.GetBannedUsers(1,10,BlackListOdrerCriteria.ByExpirationDate);
|
||||||
|
Assert.Equal(typeof(OkObjectResult), result.GetType());
|
||||||
|
if (result is OkObjectResult okObjectResult)
|
||||||
|
{
|
||||||
|
var valeur = okObjectResult.Value;
|
||||||
|
|
||||||
|
Assert.NotNull(valeur);
|
||||||
|
Assert.Equal(GetBlackList().ToString(), valeur.ToString());
|
||||||
|
Assert.True(GetBlackList().SequenceEqual(valeur as IEnumerable<BlackListDto>, new BlackListDtoEqualityComparer()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNbBannedUsers()
|
||||||
|
{
|
||||||
|
_blackListService.Setup(x => x.GetNumberOfBannedUsers())
|
||||||
|
.Returns(10);
|
||||||
|
var usersController = new BlackListController(new NullLogger<UsersController>(), _blackListService.Object);
|
||||||
|
|
||||||
|
var userResult = usersController.GetNumberOfBannedUsers();
|
||||||
|
Assert.Equal(typeof(OkObjectResult), userResult.GetType());
|
||||||
|
Assert.Equal(10, ((KeyValuePair<string,int>)(userResult as OkObjectResult).Value).Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<BlackListDto> GetBlackList()
|
||||||
|
{
|
||||||
|
return new List<BlackListDto>()
|
||||||
|
{
|
||||||
|
new BlackListDto { Email = "example1@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example2@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) },
|
||||||
|
new BlackListDto { Email = "example3@email.com" , ExpirationDate = DateOnly.FromDateTime(DateTime.Now) }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using Dto;
|
||||||
|
|
||||||
|
namespace TestAPI.Extensions;
|
||||||
|
|
||||||
|
public class BlackListDtoEqualityComparer : EqualityComparer<BlackListDto>
|
||||||
|
{
|
||||||
|
public override bool Equals(BlackListDto x, BlackListDto y)
|
||||||
|
{
|
||||||
|
return x.Email == y.Email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode(BlackListDto obj)
|
||||||
|
{
|
||||||
|
return obj.Email.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class BlackListTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithRightParameters()
|
||||||
|
{
|
||||||
|
BlackList blackList = new BlackList("example@email.com", DateOnly.FromDateTime(DateTime.Now));
|
||||||
|
Assert.Equal("example@email.com", blackList.Email);
|
||||||
|
Assert.Equal(DateOnly.FromDateTime(DateTime.Now), blackList.ExpirationDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithNullEmail()
|
||||||
|
{
|
||||||
|
Assert.Throws<ArgumentException>(() =>
|
||||||
|
{
|
||||||
|
BlackList blackList = new BlackList(null, DateOnly.FromDateTime(DateTime.Now));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithAnteriorDate()
|
||||||
|
{
|
||||||
|
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||||||
|
{
|
||||||
|
BlackList blackList = new BlackList("example@email.com", DateOnly.FromDateTime(DateTime.Parse("2024/01/01 00:00:00")));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class InquiryTableTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestEmptyContructor()
|
||||||
|
{
|
||||||
|
InquiryTable inquiry = new InquiryTable();
|
||||||
|
Assert.Equal(0,inquiry.OwnerId);
|
||||||
|
Assert.Null(inquiry.ConnectionInfo);
|
||||||
|
Assert.Null(inquiry.DatabaseName);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestFullContructor()
|
||||||
|
{
|
||||||
|
InquiryTable inquiry = new InquiryTable(1,"Database","Connection");
|
||||||
|
Assert.Equal(1,inquiry.OwnerId);
|
||||||
|
Assert.Equal("Connection",inquiry.ConnectionInfo);
|
||||||
|
Assert.Equal("Database",inquiry.DatabaseName);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class InquiryTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestEmptyContructor()
|
||||||
|
{
|
||||||
|
Inquiry inquiry = new Inquiry();
|
||||||
|
Assert.Equal(0,inquiry.Id);
|
||||||
|
Assert.Null(inquiry.Title);
|
||||||
|
Assert.Null(inquiry.Description);
|
||||||
|
Assert.False(inquiry.IsUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestFullContructor()
|
||||||
|
{
|
||||||
|
Inquiry inquiry = new Inquiry(1,"Title","Description",true);
|
||||||
|
Assert.Equal(1,inquiry.Id);
|
||||||
|
Assert.Equal("Title",inquiry.Title);
|
||||||
|
Assert.Equal("Description",inquiry.Description);
|
||||||
|
Assert.True(inquiry.IsUser);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class LessonTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithoutId()
|
||||||
|
{
|
||||||
|
Lesson lesson = new Lesson("Title", "JohnDoe", DateOnly.FromDateTime(DateTime.Now));
|
||||||
|
Assert.Equal(0, lesson.Id);
|
||||||
|
Assert.Equal("Title", lesson.Title);
|
||||||
|
Assert.Equal("JohnDoe", lesson.LastPublisher);
|
||||||
|
Assert.Equal(DateOnly.FromDateTime(DateTime.Now), lesson.LastEdit);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithId()
|
||||||
|
{
|
||||||
|
Lesson lesson = new Lesson(42,"Title", "JohnDoe", DateOnly.FromDateTime(DateTime.Now));
|
||||||
|
Assert.Equal(42, lesson.Id);
|
||||||
|
Assert.Equal("Title", lesson.Title);
|
||||||
|
Assert.Equal("JohnDoe", lesson.LastPublisher);
|
||||||
|
Assert.Equal(DateOnly.FromDateTime(DateTime.Now), lesson.LastEdit);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class NotepadTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestEmptyConstructor()
|
||||||
|
{
|
||||||
|
Notepad notepad = new Notepad();
|
||||||
|
Assert.Equal(0, notepad.Id);
|
||||||
|
Assert.Equal(0, notepad.UserId);
|
||||||
|
Assert.Equal(0, notepad.InquiryId);
|
||||||
|
Assert.Null(notepad.Notes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithoutId()
|
||||||
|
{
|
||||||
|
Notepad notepad = new Notepad(42,42,"Notes");
|
||||||
|
Assert.Equal(0, notepad.Id);
|
||||||
|
Assert.Equal(42, notepad.UserId);
|
||||||
|
Assert.Equal(42, notepad.InquiryId);
|
||||||
|
Assert.Equal("Notes",notepad.Notes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithId()
|
||||||
|
{
|
||||||
|
Notepad notepad = new Notepad(42,42,42,"Notes");
|
||||||
|
Assert.Equal(42, notepad.Id);
|
||||||
|
Assert.Equal(42, notepad.UserId);
|
||||||
|
Assert.Equal(42, notepad.InquiryId);
|
||||||
|
Assert.Equal("Notes",notepad.Notes);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class ParagraphTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithoutId()
|
||||||
|
{
|
||||||
|
Paragraph paragraph = new Paragraph("Title", "Content","Info","Query","Comment");
|
||||||
|
Assert.Equal(0, paragraph.Id);
|
||||||
|
Assert.Equal(0, paragraph.LessonId);
|
||||||
|
Assert.Equal("Title", paragraph.ContentTitle);
|
||||||
|
Assert.Equal("Content", paragraph.ContentContent);
|
||||||
|
Assert.Equal("Info", paragraph.Info);
|
||||||
|
Assert.Equal("Query", paragraph.Query);
|
||||||
|
Assert.Equal("Comment", paragraph.Comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithId()
|
||||||
|
{
|
||||||
|
Paragraph paragraph = new Paragraph(42,"Title", "Content","Info","Query","Comment",42);
|
||||||
|
Assert.Equal(42, paragraph.Id);
|
||||||
|
Assert.Equal(42, paragraph.LessonId);
|
||||||
|
Assert.Equal("Title", paragraph.ContentTitle);
|
||||||
|
Assert.Equal("Content", paragraph.ContentContent);
|
||||||
|
Assert.Equal("Info", paragraph.Info);
|
||||||
|
Assert.Equal("Query", paragraph.Query);
|
||||||
|
Assert.Equal("Comment", paragraph.Comment);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class SolutionTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestEmptyConstructor()
|
||||||
|
{
|
||||||
|
Solution solution = new Solution();
|
||||||
|
Assert.Equal(0,solution.OwnerId);
|
||||||
|
Assert.Null(solution.MurdererFirstName);
|
||||||
|
Assert.Null(solution.MurdererLastName);
|
||||||
|
Assert.Null(solution.MurderPlace);
|
||||||
|
Assert.Null(solution.MurderWeapon);
|
||||||
|
Assert.Null(solution.Explaination);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithoutId()
|
||||||
|
{
|
||||||
|
Solution solution = new Solution("John","Doe","Bedroom","Knife","Because");
|
||||||
|
Assert.Equal(0,solution.OwnerId);
|
||||||
|
Assert.Equal("John",solution.MurdererFirstName);
|
||||||
|
Assert.Equal("Doe",solution.MurdererLastName);
|
||||||
|
Assert.Equal("Bedroom",solution.MurderPlace);
|
||||||
|
Assert.Equal("Knife",solution.MurderWeapon);
|
||||||
|
Assert.Equal("Because",solution.Explaination);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestConstructorWithId()
|
||||||
|
{
|
||||||
|
Solution solution = new Solution(42,"John","Doe","Bedroom","Knife","Because");
|
||||||
|
Assert.Equal(42,solution.OwnerId);
|
||||||
|
Assert.Equal("John",solution.MurdererFirstName);
|
||||||
|
Assert.Equal("Doe",solution.MurdererLastName);
|
||||||
|
Assert.Equal("Bedroom",solution.MurderPlace);
|
||||||
|
Assert.Equal("Knife",solution.MurderWeapon);
|
||||||
|
Assert.Equal("Because",solution.Explaination);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class SuccessTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestEmptyConstructor()
|
||||||
|
{
|
||||||
|
Success success = new Success();
|
||||||
|
Assert.Equal(0,success.UserId);
|
||||||
|
Assert.Equal(0,success.InquiryId);
|
||||||
|
Assert.False(success.IsFinished);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestFullConstructor()
|
||||||
|
{
|
||||||
|
Success success = new Success(42,42,true);
|
||||||
|
Assert.Equal(42,success.UserId);
|
||||||
|
Assert.Equal(42,success.InquiryId);
|
||||||
|
Assert.True(success.IsFinished);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace TestEF.Model;
|
||||||
|
|
||||||
|
public class UserTest
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
void TestEmptyConstructor()
|
||||||
|
{
|
||||||
|
User user = new User();
|
||||||
|
Assert.Equal(0,user.Id);
|
||||||
|
Assert.Null(user.Username);
|
||||||
|
Assert.Null(user.Email);
|
||||||
|
Assert.Null(user.Password);
|
||||||
|
Assert.False(user.IsAdmin);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
void TestFullConstructor()
|
||||||
|
{
|
||||||
|
User user = new User(42,"Username","Password","Email",true);
|
||||||
|
Assert.Equal(42,user.Id);
|
||||||
|
Assert.Equal("Username",user.Username);
|
||||||
|
Assert.Equal("Email",user.Email);
|
||||||
|
Assert.Equal("Password",user.Password);
|
||||||
|
Assert.True(user.IsAdmin);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager.Service;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace TestEF.Service;
|
||||||
|
|
||||||
|
public class TestBlackListDataService
|
||||||
|
{
|
||||||
|
private readonly UserDbContext _dbContext;
|
||||||
|
private readonly BlackListDataService _blackListDataService;
|
||||||
|
|
||||||
|
public TestBlackListDataService()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
_dbContext = new UserDbContext(options);
|
||||||
|
_blackListDataService = new BlackListDataService(_dbContext);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void BanUser_Success()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test101", Email = "example101@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test102", Email = "example102@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test103", Email = "example103@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var banResult = _blackListDataService.BanUser("Test101");
|
||||||
|
Assert.True(banResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNbBannedUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test61", Email = "example61@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test62", Email = "example62@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test63", Email = "example63@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var banResult1 = _blackListDataService.BanUser("Test61");
|
||||||
|
var banResult2 = _blackListDataService.BanUser("Test62");
|
||||||
|
var banResult3 = _blackListDataService.BanUser("Test63");
|
||||||
|
Assert.True(banResult1);
|
||||||
|
Assert.True(banResult2);
|
||||||
|
Assert.True(banResult3);
|
||||||
|
Assert.Equal(3, _dbContext.BlackLists.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void BanUser_Fail()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test71", Email = "example71@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test72", Email = "example72@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test73", Email = "example73@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var banResult = _blackListDataService.BanUser("Test42");
|
||||||
|
Assert.False(banResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void IsBanned_Success()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test81", Email = "example81@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test82", Email = "example82@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test83", Email = "example83@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var banResult = _blackListDataService.BanUser("Test81");
|
||||||
|
Assert.True(banResult);
|
||||||
|
Assert.NotNull(_blackListDataService.GetUserBannedByEmail("example81@email.com"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UnbanUser_Success()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test91", Email = "example91@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test92", Email = "example92@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test93", Email = "example93@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var banResult = _blackListDataService.BanUser("Test91");
|
||||||
|
Assert.True(banResult);
|
||||||
|
Assert.True(_blackListDataService.UnbanUser("example91@email.com"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,185 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager.Service;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace TestEF.Service;
|
||||||
|
|
||||||
|
public class TestLessonDataService
|
||||||
|
{
|
||||||
|
private readonly UserDbContext _dbContext;
|
||||||
|
private readonly LessonDataService _lessonDataService;
|
||||||
|
|
||||||
|
public TestLessonDataService()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
_dbContext = new UserDbContext(options);
|
||||||
|
_lessonDataService = new LessonDataService(_dbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessons_WithNoneCriteria_ReturnsCorrectNumberOfLessons()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessons(1, 2, LessonOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessons_OrderedByTitle_ReturnsCorrectNumberOfLessons()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessons(1, 2, LessonOrderCriteria.ByTitle);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessons_OrderedByLastPublisher_ReturnsCorrectNumberOfLessons()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessons(1, 2, LessonOrderCriteria.ByLastPublisher);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessons_OrderedByLastEdit_ReturnsCorrectNumberOfLessons()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessons(1, 2, LessonOrderCriteria.ByLastEdit);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessons_WithBadPage_ReturnsCorrectNumberOfLessons()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessons(-42, 2, LessonOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessons_WithBadNumber_ReturnsCorrectNumberOfLessons()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessons(1, -42, LessonOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(3, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessonById_ReturnsCorrectLesson()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessonById(1);
|
||||||
|
|
||||||
|
Assert.Equal("Test1", result.Title);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetLessonByTitle_ReturnsCorrectLesson()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _lessonDataService.GetLessonByTitle("Test1");
|
||||||
|
|
||||||
|
Assert.Equal(1, result.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CreateLesson_AddsNewLesson()
|
||||||
|
{
|
||||||
|
var result = _lessonDataService.CreateLesson(1, "Test", "Publisher", DateOnly.MinValue);
|
||||||
|
|
||||||
|
Assert.Equal(1, _dbContext.Lessons.Count());
|
||||||
|
Assert.Equal("Test", result.Title);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DeleteLesson_RemovesLesson()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_lessonDataService.DeleteLesson(1);
|
||||||
|
|
||||||
|
Assert.Empty(_dbContext.Lessons);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateLesson_UpdatesExistingLesson()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(new LessonEntity
|
||||||
|
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var updatedLesson = new LessonEntity
|
||||||
|
{ Id = 1, Title = "Updated", LastPublisher = "Publisher", LastEdit = DateOnly.MinValue };
|
||||||
|
var result = _lessonDataService.UpdateLesson(1, updatedLesson);
|
||||||
|
|
||||||
|
Assert.Equal("Updated", result.Title);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,362 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager.Service;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace TestEF.Service;
|
||||||
|
|
||||||
|
public class TestParagraphDataService
|
||||||
|
{
|
||||||
|
private readonly UserDbContext _dbContext;
|
||||||
|
private readonly ParagraphDataService _paragraphDataService;
|
||||||
|
private readonly LessonEntity _lesson;
|
||||||
|
|
||||||
|
public TestParagraphDataService()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
_dbContext = new UserDbContext(options);
|
||||||
|
_lesson = new LessonEntity
|
||||||
|
{
|
||||||
|
Id = 1, Title = "Test", LastPublisher = "Publisher", LastEdit = DateOnly.MinValue
|
||||||
|
};
|
||||||
|
_paragraphDataService = new ParagraphDataService(_dbContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_WithoutCriteria_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_OrderdByTitle_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.ByTitle);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_OrderedByContent_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.ByContent);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_OrderedByQuery_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.ByQuery);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_OrderedByInfo_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.ByInfo);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_OrderedByComment_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.ByComment);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_WithBadPage_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(-42, 2, ParagraphOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphs_WithBadNumber_ReturnsCorrectNumberOfParagraphs()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||||
|
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphs(1, -42, ParagraphOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(3, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetParagraphById_ReturnsCorrectParagraph()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 2, ContentContent = "ContenContent2", ContentTitle = "ContentTitle2", Title = "Test2",
|
||||||
|
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.GetParagraphById(1);
|
||||||
|
|
||||||
|
Assert.Equal("Test1", result.Title);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CreateParagraph_AddsNewParagraph()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _paragraphDataService.CreateParagraph("ContentTitle", "ContentContent", "Test", "Content", "Info",
|
||||||
|
"Query", "Comment", 1);
|
||||||
|
|
||||||
|
Assert.Equal(1, _dbContext.Paragraphs.Count());
|
||||||
|
Assert.Equal("Test", result.Title);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DeleteParagraph_RemovesParagraph()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var b = _paragraphDataService.DeleteParagraph(1);
|
||||||
|
|
||||||
|
Assert.Empty(_dbContext.Paragraphs);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateParagraph_UpdatesExistingParagraph()
|
||||||
|
{
|
||||||
|
_dbContext.Lessons.Add(_lesson);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
});
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var updatedParagraph = new ParagraphEntity
|
||||||
|
{
|
||||||
|
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Updated",
|
||||||
|
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||||
|
LessonId = 1
|
||||||
|
};
|
||||||
|
var result = _paragraphDataService.UpdateParagraph(1, updatedParagraph);
|
||||||
|
|
||||||
|
Assert.Equal("Updated", result.Title);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,249 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager.Service;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model.OrderCriteria;
|
||||||
|
|
||||||
|
namespace TestEF.Service;
|
||||||
|
|
||||||
|
public class TestSuccessDataService
|
||||||
|
{
|
||||||
|
private readonly UserDbContext _dbContext;
|
||||||
|
private readonly SuccessDataService _successDataService;
|
||||||
|
private readonly List<UserEntity> _users = new();
|
||||||
|
private readonly List<InquiryEntity> _inquiries = new();
|
||||||
|
|
||||||
|
public TestSuccessDataService()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
_dbContext = new UserDbContext(options);
|
||||||
|
_successDataService = new SuccessDataService(_dbContext);
|
||||||
|
|
||||||
|
_users.AddRange(new List<UserEntity>
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
Username = "Pseudo",
|
||||||
|
Password = "Password",
|
||||||
|
Email = "Email@gmail.com",
|
||||||
|
IsAdmin = true
|
||||||
|
},
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = 2,
|
||||||
|
Username = "Pseudo2",
|
||||||
|
Password = "Password2",
|
||||||
|
Email = "Email2@gmail.com",
|
||||||
|
IsAdmin = false
|
||||||
|
},
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = 3,
|
||||||
|
Username = "Pseudo3",
|
||||||
|
Password = "Password3",
|
||||||
|
Email = "Email3@gmail.com",
|
||||||
|
IsAdmin = false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_inquiries.AddRange(new List<InquiryEntity>
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = 1,
|
||||||
|
Title = "Title",
|
||||||
|
Description = "Description",
|
||||||
|
IsUser = false
|
||||||
|
},
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = 2,
|
||||||
|
Title = "Title2",
|
||||||
|
Description = "Description2",
|
||||||
|
IsUser = true
|
||||||
|
},
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = 3,
|
||||||
|
Title = "Title3",
|
||||||
|
Description = "Description3",
|
||||||
|
IsUser = false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_dbContext.Users.AddRange(_users);
|
||||||
|
_dbContext.Inquiries.AddRange(_inquiries);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccesses_WithoutCriteria_ReturnsCorrectNumberOfSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||||
|
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccesses(1, 2, SuccessOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccesses_OrderedByUserId_ReturnsCorrectNumberOfSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||||
|
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccesses(1, 2, SuccessOrderCriteria.ByUserId);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccesses_OrderedByInquiryId_ReturnsCorrectNumberOfSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||||
|
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccesses(1, 2, SuccessOrderCriteria.ByInquiryId);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccesses_OrderedByIsFinished_ReturnsCorrectNumberOfSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||||
|
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccesses(1, 2, SuccessOrderCriteria.ByIsFinished);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccesses_WithBadPage_ReturnsCorrectNumberOfSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||||
|
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccesses(-42, 2, SuccessOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccesses_WithBadNumber_ReturnsCorrectNumberOfSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||||
|
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccesses(1, -42, SuccessOrderCriteria.None);
|
||||||
|
|
||||||
|
Assert.Equal(3, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccessesByUserId_ReturnsCorrectSuccesses()
|
||||||
|
{
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 1, InquiryId = 2, IsFinished = false };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccessesByUserId(1);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
[Fact]
|
||||||
|
public void GetSuccessesByInquiryId_ReturnsCorrectSuccesses()
|
||||||
|
{
|
||||||
|
_dbContext.Inquiries.Add(
|
||||||
|
new InquiryEntity
|
||||||
|
{
|
||||||
|
Id = 4,
|
||||||
|
Title = "Title3",
|
||||||
|
Description = "Description3",
|
||||||
|
IsUser = false
|
||||||
|
}
|
||||||
|
);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var success1 = new SuccessEntity { UserId = 1, InquiryId = 4, IsFinished = true };
|
||||||
|
var success2 = new SuccessEntity { UserId = 2, InquiryId = 4, IsFinished = false };
|
||||||
|
|
||||||
|
_dbContext.Successes.AddRange(success1, success2);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _successDataService.GetSuccessesByInquiryId(4);
|
||||||
|
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}*/
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DeleteSuccess_RemovesSuccess()
|
||||||
|
{
|
||||||
|
var success = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.Add(success);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
_successDataService.DeleteSuccess(1, 1);
|
||||||
|
|
||||||
|
Assert.Empty(_dbContext.Successes);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UpdateSuccess_UpdatesExistingSuccess()
|
||||||
|
{
|
||||||
|
var success = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||||
|
|
||||||
|
_dbContext.Successes.Add(success);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var updatedSuccess = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = false };
|
||||||
|
var result = _successDataService.UpdateSuccess(1, 1, updatedSuccess);
|
||||||
|
|
||||||
|
Assert.False(result.IsFinished);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CreateSuccess_AddsNewSuccess()
|
||||||
|
{
|
||||||
|
var result = _successDataService.CreateSuccess(1, 3, true);
|
||||||
|
|
||||||
|
Assert.Single(_dbContext.Successes);
|
||||||
|
Assert.True(result.IsFinished);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,298 @@
|
|||||||
|
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 = "Test10", Email = "example1@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test12", Email = "example2@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test13", Email = "example3@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetUsers(1, 2, UserOrderCriteria.None);
|
||||||
|
Assert.Equal(2, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test14", Email = "example4@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test15", Email = "example5@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test16", Email = "example6@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.None);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_BadPage_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test17", Email = "example7@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test18", Email = "example8@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test19", Email = "example9@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(-42, 2, UserOrderCriteria.None);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_BadNumber_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test20", Email = "example10@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test21", Email = "example11@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test22", Email = "example12@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(1, -42, UserOrderCriteria.None);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_OrderedById_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test23", Email = "example13@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test24", Email = "example14@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test51", Email = "example15@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ById);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_OrderedByUsername_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test25", Email = "example16@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test26", Email = "example17@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test27", Email = "example18@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByUsername);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_OrderedByEmail_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test52", Email = "example45@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test53", Email = "example46@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test54", Email = "example17@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByEmail);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNotAdminUsers_OrderedByIsAdmin_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test28", Email = "example19@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test29", Email = "example20@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test30", Email = "example21@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNotAdminUsers(1, 2, UserOrderCriteria.ByIsAdmin);
|
||||||
|
Assert.Equal(1, result.Count());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetNumberOfUsers_ReturnsCorrectNumberOfUsers()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test31", Email = "example22@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test32", Email = "example23@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test33", Email = "example24@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetNumberOfUsers();
|
||||||
|
Assert.Equal(3, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetUserById_ReturnsCorrectUser()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test34", Email = "example25@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test35", Email = "example26@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetUserById(1);
|
||||||
|
Assert.Equal("Test34", result.Username);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void GetUserByUsername_ReturnsCorrectUser()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test36", Email = "example27@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test37", Email = "example28@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetUserByUsername("Test36");
|
||||||
|
Assert.Equal(1, result.Id);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void GetUserByEmail_ReturnsCorrectUser()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test38", Email = "example29@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test39", Email = "example30@email.com", Password = "password", IsAdmin = false });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
var result = _userDataService.GetUserByEmail("example29@email.com");
|
||||||
|
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 DeleteUserByUsername_RemovesUser()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
_userDataService.DeleteUserByUsername("Test1");
|
||||||
|
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 = "Test40", Email = "example31@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test41", Email = "example32@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 = "Test43", Email = "example33@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test44", Email = "example34@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 GetUserByEmail_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.GetUserByEmail("test@test"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[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 isEmailTaken_ReturnFalse()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test81", Email = "example81@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _userDataService.IsEmailTaken("example895@email.com");
|
||||||
|
|
||||||
|
Assert.False(result);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void isEmailTaken_ReturnTrue()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test82", Email = "example82@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _userDataService.IsEmailTaken("example82@email.com");
|
||||||
|
|
||||||
|
Assert.True(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DeleteUserByUsername_WithoutExisting_ReturnsFalse()
|
||||||
|
{
|
||||||
|
_dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true });
|
||||||
|
_dbContext.SaveChanges();
|
||||||
|
|
||||||
|
var result = _userDataService.DeleteUserByUsername("Test2");
|
||||||
|
|
||||||
|
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