Add logger Entity + Unit Test for API
continuous-integration/drone/push Build is failing Details

pagination
Tony Fages 1 year ago
parent 2a5cfc1d12
commit d620088c63

@ -21,6 +21,7 @@ namespace API.Controllers
this._logger = logger;
}
[Route("/articles")]
[HttpGet]
public async Task<IActionResult> GetAllArticles([FromQuery] int index = 0, [FromQuery] int count = 10, [FromQuery] ArticleOrderCriteria orderCriterium = ArticleOrderCriteria.None)
{
@ -109,8 +110,8 @@ namespace API.Controllers
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(UpdateArticle), id, a);
try
{
var result = await _dataManager.ArticleService.UpdateArticle(id, a);
if (result == false)
var result = (await _dataManager.ArticleService.UpdateArticle(id, a)).ToDTO();
if (result == null)
{
return NotFound($"Article ID {id} not found");
}

@ -91,8 +91,8 @@ namespace API.Controllers
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteForm), id);
try
{
var result = await _dataManager.FormulaireService.DeleteForm(id);
if (result == false)
var result = (await _dataManager.FormulaireService.DeleteForm(id)).ToDTO();
if (result == null)
{
return NotFound($"Form Id {id} not found");
}
@ -112,8 +112,8 @@ namespace API.Controllers
try
{
var result = await _dataManager.FormulaireService.UpdateForm(id, formulaire);
if (result == false)
var result = (await _dataManager.FormulaireService.UpdateForm(id, formulaire)).ToDTO();
if (result == null)
{
return NotFound($"form Id {id} not found");
}

@ -68,8 +68,8 @@ namespace API.Controllers
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(Create), user);
try
{
var result = await _dataManager.UserService.Create(user);
if (result == false)
var result = (await _dataManager.UserService.Create(user)).ToDTO();
if (result == null)
{
return BadRequest($"User {user.Pseudo} already exists");
}
@ -90,8 +90,8 @@ namespace API.Controllers
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(Update), user, pseudo);
try
{
var result = await _dataManager.UserService.Update(user,pseudo);
if (result == false)
var result = (await _dataManager.UserService.Update(user, pseudo)).ToDTO();
if (result == null)
{
return NotFound();
}
@ -111,8 +111,8 @@ namespace API.Controllers
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(Delete), pseudo);
try
{
var result = await _dataManager.UserService.Delete(pseudo);
if (result == false)
var result = (await _dataManager.UserService.Delete(pseudo)).ToDTO();
if (result == null)
{
return NotFound();
}
@ -186,16 +186,16 @@ namespace API.Controllers
}
}
[HttpDelete("/user/{pseudo}/article")]
public async Task<IActionResult> DeleteArticleUser(string pseudo)
[HttpDelete("/user/{pseudo}/{id}")]
public async Task<IActionResult> DeleteArticleUser(string pseudo, long id)
{
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteArticleUser), pseudo);
try
{
var result = await _dataManager.UserService.DeleteArticleUser(pseudo);
var result = await _dataManager.UserService.DeleteArticleUser(pseudo, id);
if (!result)
{
return BadRequest($"ArticleUser {pseudo} does not exist");
return BadRequest($"User {pseudo} or {id} does not exist");
}
return Ok(result);
}
@ -212,7 +212,7 @@ namespace API.Controllers
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(UpdateArticleUser), articleUser);
try
{
var result = await _dataManager.UserService.UpdateArticleUser(articleUser);
var result = (await _dataManager.UserService.UpdateArticleUser(articleUser));
if (!result)
{
return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} does not exist");

@ -0,0 +1,17 @@
info: 03/15/2024 16:59:22.173 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: 03/15/2024 16:59:22.176 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: 03/15/2024 16:59:22.182 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "MigrationId", "ProductVersion"
FROM "__EFMigrationsHistory"
ORDER BY "MigrationId";
info: 03/15/2024 16:59:22.190 RelationalEventId.MigrationsNotApplied[20405] (Microsoft.EntityFrameworkCore.Migrations)
No migrations were applied. The database is already up to date.
info: 03/15/2024 16:59:30.063 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "a"."Id", "a"."Author", "a"."DatePublished", "a"."Description", "a"."LectureTime", "a"."Title"
FROM "ArticleSet" AS "a"

@ -63,8 +63,8 @@ public class DbManagerArticle : IArticleService
LectureTime = article.LectureTime,
};
_context.ArticleSet.Add(entity);
await _context.SaveChangesAsync();
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<Article?>(null);
return entity.ToModel();
}
@ -74,21 +74,23 @@ public class DbManagerArticle : IArticleService
Console.WriteLine(entity);
if (entity == null) return null;
_context.ArticleSet.Remove(entity);
await _context.SaveChangesAsync();
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<Article?>(null);
return entity.ToModel();
}
public async Task<bool> UpdateArticle(long id, Article? a)
public async Task<Article?> UpdateArticle(long id, Article? a)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
if (entity == null) return false;
if (entity == null) return await Task.FromResult<Article?>(null);
entity.Title = a.Title;
entity.Description = a.Description;
entity.Author = a.Author;
entity.DatePublished = a.DatePublished;
entity.LectureTime = a.LectureTime;
await _context.SaveChangesAsync();
return true;
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<Article?>(null);
return entity.ToModel();
}

@ -59,28 +59,31 @@ public class DbManagerFormulaire : IFormulaireService
};
_context.FormSet.Add(entity);
await _context.SaveChangesAsync();
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<Formulaire?>(null);
return entity.ToModel();
}
public async Task<bool> DeleteForm(long id)
public async Task<Formulaire?> DeleteForm(long id)
{
var entity = _context.FormSet.FirstOrDefault(f => f.Id == id);
if (entity == null) return false;
if (entity == null) return Task.FromResult<Formulaire?>(null).Result;
_context.FormSet.Remove(entity);
await _context.SaveChangesAsync();
return true;
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<Formulaire?>(null);
return entity.ToModel();
}
public async Task<bool> UpdateForm(long id, Formulaire formulaire)
public async Task<Formulaire?> UpdateForm(long id, Formulaire formulaire)
{
var entity = _context.FormSet.FirstOrDefault(f => f.Id == id);
if (entity == null) return false;
if (entity == null) return Task.FromResult<Formulaire?>(null).Result;
entity.Theme = formulaire.Theme;
entity.DatePublication = formulaire.Date;
entity.Link = formulaire.Lien;
entity.UserEntityPseudo = formulaire.UserPseudo;
await _context.SaveChangesAsync();
return true;
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<Formulaire?>(null);
return entity.ToModel();
}
}

@ -43,7 +43,7 @@ public class DbManagerUser: IUserService
return await Task.FromResult(entity.ToModel());
}
public async Task<bool> Create(User user)
public async Task<User?> Create(User user)
{
var entity = new UserEntity()
{
@ -55,30 +55,33 @@ public class DbManagerUser: IUserService
Role = user.Role
};
_context.UserSet.Add(entity);
await _context.SaveChangesAsync();
return true;
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<User?>(null);
return await Task.FromResult(entity.ToModel());
}
public async Task<bool> Update(User user, string pseudo)
public async Task<User?> Update(User user, string pseudo)
{
var entity = _context.UserSet.FirstOrDefault(u => u.Pseudo == pseudo);
if (entity == null) return false;
if (entity == null) return await Task.FromResult<User?>(null);
entity.Mdp = user.Mdp;
entity.Mail = user.Mail;
entity.Role = user.Role;
entity.Prenom = user.Prenom;
entity.Nom = user.Nom;
await _context.SaveChangesAsync();
return true;
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<User?>(null);
return await Task.FromResult(entity.ToModel());
}
public async Task<bool> Delete(string pseudo)
public async Task<User?> Delete(string pseudo)
{
var entity = _context.UserSet.FirstOrDefault(u => u.Pseudo == pseudo);
if (entity == null) return await Task.FromResult(false);
if (entity == null) return await Task.FromResult<User?>(null);
_context.UserSet.Remove(entity);
await _context.SaveChangesAsync();
return await Task.FromResult(true);
var result = await _context.SaveChangesAsync();
if (result == 0) return await Task.FromResult<User?>(null);
return await Task.FromResult(entity.ToModel());
}
public async Task<IEnumerable<User?>> GetAllArticleUsers()
@ -124,9 +127,10 @@ public class DbManagerUser: IUserService
return await Task.FromResult(true);
}
public async Task<bool> DeleteArticleUser(string pseudo)
public async Task<bool> DeleteArticleUser(string pseudo, long id)
{
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(pseudo));
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(pseudo) && a.ArticleEntityId.Equals(id));
if (entity == null) return await Task.FromResult(false);
_context.ArticleUserSet.Remove(entity);
await _context.SaveChangesAsync();

@ -5,7 +5,7 @@ namespace API_Mapping;
public static class FormulaireMapping
{
public static FormulaireDTO ToDTO(this Formulaire f) => new()
public static FormulaireDTO ToDTO(this Formulaire? f) => new()
{
Id = f.Id,
Theme = f.Theme,

@ -5,7 +5,7 @@ namespace API_Mapping;
public static class UserMapping
{
public static UserDTO ToDTO(this User u) => new()
public static UserDTO ToDTO(this User? u) => new()
{
Pseudo = u.Pseudo,
Mdp = u.Mdp,

@ -14,7 +14,7 @@ namespace API_Services
Task<Article?> DeleteArticle(long id);
Task<bool> UpdateArticle(long id, Article? a);
Task<Article?> UpdateArticle(long id, Article? a);
}

@ -1,9 +0,0 @@
using Entities;
using Model;
namespace API_Services;
public interface IArticleUserService
{
}

@ -12,7 +12,7 @@ public interface IFormulaireService
Task<Formulaire?> CreateForm(Formulaire formulaire);
Task<bool> DeleteForm(long id);
Task<Formulaire?> DeleteForm(long id);
Task<bool> UpdateForm(long id, Formulaire formulaire);
Task<Formulaire?> UpdateForm(long id, Formulaire formulaire);
}

@ -8,17 +8,17 @@ namespace API_Services
Task<IEnumerable<User?>> GetAll(int index, int count, UserOrderCriteria orderCriteria);
Task<User?> GetByPseudo(string pseudo);
Task<bool> Create(User user);
Task<bool> Update(User user, string pseudo);
Task<User?> Create(User user);
Task<User?> Update(User user, string pseudo);
Task<bool> Delete(string pseudo);
Task<User?> Delete(string pseudo);
Task<IEnumerable<User?>> GetAllArticleUsers();
Task<IEnumerable<Article?>> GetArticleUser(string pseudo);
Task<bool> CreateArticleUser(ArticleUserEntity articleUser);
Task<bool> DeleteArticleUser(string pseudo);
Task<bool> DeleteArticleUser(string pseudo, long id);
Task<bool> UpdateArticleUser(ArticleUserEntity articleUser);

@ -13,13 +13,13 @@ class Tests_Console
static async Task Main(string[] args)
{
//await TestUser();
//await TestFormulaire();
await TestFormulaire();
//await TestArticle();
}
private static async Task TestFormulaire()
{
//await TestFormulaireGetAll();
await TestFormulaireGetAll();
//await TestFormulaireGetId();
//await TestFormulaireCreate();
//await TestFormulaireDelete();
@ -37,7 +37,7 @@ class Tests_Console
//await TestGetArticleByUser();
//await TestCreateArticleUser();
//await TestDeleteArticleUser();
await TestUpdateArticleUser();
//await TestUpdateArticleUser();
}

@ -100,9 +100,9 @@ public class UnitTest_Article
DatePublished = "Test",
LectureTime = 10
};
mockArticleService.Setup(x => x.UpdateArticle(1, updated)).ReturnsAsync(true);
mockArticleService.Setup(x => x.UpdateArticle(1, updated)).ReturnsAsync(updated);
var resultUpdated = mockArticleService.Object.UpdateArticle(1, updated);
Assert.True(resultUpdated.Result);
Assert.Equal(updated ,resultUpdated.Result);
}
[Fact]

@ -1,17 +1,99 @@
using API_Services;
using Model;
using Moq;
namespace API_Unit_Test;
public class UnitTest_Form
{
[Fact]
public void TestGetAllForm()
{
var mockFormService = new Mock<IFormulaireService>();
var expected = new List<Formulaire>()
{
new Formulaire()
{
Lien = "Test",
Theme = "Test",
Date = "Test",
UserPseudo = "Test"
},
new Formulaire()
{
Lien = "Test",
Theme = "Test",
Date = "Test",
UserPseudo = "Test"
}
};
mockFormService.Setup(x => x.GetAllForm(0, 10, FormOrderCriteria.None)).ReturnsAsync(expected);
var result = mockFormService.Object.GetAllForm(0, 10, FormOrderCriteria.None);
Assert.Equal(expected, result.Result);
}
[Fact]
static void TestAllUser()
public void TestGetFormById()
{
var mockFormService = new Mock<IFormulaireService>();
var expected = new Formulaire()
{
var mockUserService = new Mock<IUserService>();
// TODO
Lien = "Test",
Theme = "Test",
Date = "Test",
UserPseudo = "Test"
};
mockFormService.Setup(x => x.GetById(1)).ReturnsAsync(expected);
var result = mockFormService.Object.GetById(1);
Assert.Equal(expected, result.Result);
}
}
[Fact]
public void TestCreateForm()
{
var mockFormService = new Mock<IFormulaireService>();
var expected = new Formulaire()
{
Lien = "Test",
Theme = "Test",
Date = "Test",
UserPseudo = "Test"
};
mockFormService.Setup(x => x.CreateForm(expected)).ReturnsAsync(expected);
var result = mockFormService.Object.CreateForm(expected);
Assert.Equal(expected, result.Result);
}
[Fact]
public void TestUpdateForm()
{
var mockFormService = new Mock<IFormulaireService>();
var expected = new Formulaire()
{
Lien = "Test",
Theme = "Test",
Date = "Test",
UserPseudo = "Test"
};
mockFormService.Setup(x => x.CreateForm(expected)).ReturnsAsync(expected);
var result = mockFormService.Object.CreateForm(expected);
Assert.Equal(expected, result.Result);
}
[Fact]
public void TestDeleteForm()
{
var mockFormService = new Mock<IFormulaireService>();
var expected = new Formulaire()
{
Lien = "Test",
Theme = "Test",
Date = "Test",
UserPseudo = "Test"
};
mockFormService.Setup(x => x.DeleteForm(1)).ReturnsAsync(expected);
var result = mockFormService.Object.DeleteForm(1);
Assert.Equal(expected, result.Result);
}
}

@ -0,0 +1,215 @@
using API_Services;
using Entities;
using Model;
using Moq;
namespace API_Unit_Test;
public class UnitTest_User
{
[Fact]
static void TestAllUser()
{
var mockUserService = new Mock<IUserService>();
var expected = new List<User>()
{
new User()
{
Pseudo = "Tofgasy",
Prenom = "Tony",
Nom = "Fages",
Mail = "mail@mail.com",
Mdp = "1234",
Role = "Admin"
},
new User()
{
Pseudo = "Blizzard",
Prenom = "Louis",
Nom = "Laborie",
Mail = "mail@mail.com",
Mdp = "1234",
Role = "Admin",
},
};
mockUserService.Setup(x => x.GetAll(0, 10, UserOrderCriteria.None)).ReturnsAsync(expected);
var result = mockUserService.Object.GetAll(0, 10, UserOrderCriteria.None);
Assert.Equal(expected, result.Result);
}
[Fact]
static void TestGetUserByPseudo()
{
var mockUserService = new Mock<IUserService>();
var expected = new User()
{
Pseudo = "Tofgasy",
Prenom = "Tony",
Nom = "Fages",
Mail = "mail@mail.com",
Mdp = "1234",
Role = "Admin"
};
mockUserService.Setup(x => x.GetByPseudo("Tofgasy")).ReturnsAsync(expected);
var result = mockUserService.Object.GetByPseudo("Tofgasy");
Assert.Equal(expected, result.Result);
}
[Fact]
static void TestCreateUser()
{
var mockUserService = new Mock<IUserService>();
var user = new User()
{
Pseudo = "Tofgasy",
Prenom = "Tony",
Nom = "Fages",
Mail = "mail@mail.com",
Mdp = "1234",
Role = "Admin"
};
mockUserService.Setup(x => x.Create(user)).ReturnsAsync(user);
var result = mockUserService.Object.Create(user);
Assert.Equal( user,result.Result);
}
[Fact]
static void TestUpdateUser()
{
var mockUserService = new Mock<IUserService>();
var user = new User()
{
Pseudo = "Tofgasy",
Prenom = "Tonio",
Nom = "Fages",
Mail = "mail@mail.com",
Mdp = "1234",
Role = "Admin"
};
mockUserService.Setup(x => x.Update(user, "Tofgasy")).ReturnsAsync(user);
var result = mockUserService.Object.Update(user, "Tofgasy");
Assert.Equal( user,result.Result);
}
[Fact]
static void TestDeleteUser()
{
var mockUserService = new Mock<IUserService>();
var user = new User()
{
Pseudo = "Tofgasy",
Prenom = "Tonio",
Nom = "Fages",
Mail = "mail@mail.com",
Mdp = "1234",
Role = "Admin"
};
mockUserService.Setup(x => x.Delete("Tofgasy")).ReturnsAsync(user);
var result = mockUserService.Object.Delete("Tofgasy");
Assert.Equal( user,result.Result);
}
[Fact]
static void TestGetAllArticleUsers()
{
var mockUserService = new Mock<IUserService>();
var expected = new List<User>()
{
new User()
{
Pseudo = "Tofgasy",
Prenom = "Tony",
Nom = "Fages",
Mail = "",
Mdp = "",
Role = "",
},
new User()
{
Pseudo = "Blizzard",
Prenom = "Louis",
Nom = "Laborie",
Mail = "",
Mdp = "",
Role = "",
},
};
mockUserService.Setup(x => x.GetAllArticleUsers()).ReturnsAsync(expected);
var result = mockUserService.Object.GetAllArticleUsers();
Assert.Equal(expected, result.Result);
}
[Fact]
static void TestGetArticleUser()
{
var mockUserService = new Mock<IUserService>();
var expected = new List<Article>()
{
new Article()
{
Id = 1,
Title = "Test",
Description = "Test",
Author = "Test",
DatePublished = "Test",
LectureTime = 10
},
new Article()
{
Id = 2,
Title = "Test",
Description = "Test",
Author = "Test",
DatePublished = "Test",
LectureTime = 10
}
};
mockUserService.Setup(x => x.GetArticleUser("Tofgasy")).ReturnsAsync(expected);
var result = mockUserService.Object.GetArticleUser("Tofgasy");
Assert.Equal(expected, result.Result);
}
[Fact]
static void TestCreateArticleUser()
{
var mockUserService = new Mock<IUserService>();
var articleUser = new ArticleUserEntity()
{
ArticleEntityId = 1,
UserEntityPseudo = "Tofgasy"
};
mockUserService.Setup(x => x.CreateArticleUser(articleUser)).ReturnsAsync(true);
var result = mockUserService.Object.CreateArticleUser(articleUser);
Assert.True(result.Result);
}
[Fact]
static void TestDeleteArticleUser()
{
var mockUserService = new Mock<IUserService>();
mockUserService.Setup(x => x.DeleteArticleUser("Tofgasy", 1)).ReturnsAsync(true);
var result = mockUserService.Object.DeleteArticleUser("Tofgasy", 1);
Assert.True(result.Result);
}
[Fact]
static void TestUpdateArticleUser()
{
var mockUserService = new Mock<IUserService>();
var articleUser = new ArticleUserEntity()
{
ArticleEntityId = 1,
UserEntityPseudo = "Tofgasy"
};
mockUserService.Setup(x => x.UpdateArticleUser(articleUser)).ReturnsAsync(true);
var result = mockUserService.Object.UpdateArticleUser(articleUser);
Assert.True(result.Result);
}
}

@ -26,7 +26,7 @@ public class LibraryContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.LogTo( LogFile.WriteLine, LogLevel.Error).EnableDetailedErrors().EnableDetailedErrors();
optionsBuilder.LogTo(message => LogFile.WriteLine(message), LogLevel.Information);
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite($"Data Source=Entity_FrameWork.Article.db");

Loading…
Cancel
Save