suite test console (fin image + character)

pull/5/head
Kevin MONDEJAR 1 month ago
parent 2738d52fa0
commit 96f55a3532

@ -1,8 +1,10 @@
using Contextlib; using Contextlib;
using Entity;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using StubbedContextLib; using StubbedContextLib;
using static System.Net.Mime.MediaTypeNames;
using static System.Net.WebRequestMethods; using static System.Net.WebRequestMethods;
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
@ -18,24 +20,115 @@ using (var _context = new StubWTFContext(options))
// ---- Test Image ---- // // ---- Test Image ---- //
var imageManager = new DbImagesManager(_context); var imageManager = new DbImagesManager(_context);
await imageManager.AddImage(new Entity.Images() { Id = 11, ImgPath = "https://www.bing.com/ck/a?!&&p=390428c2820add92760900204667aa721b17d4eb9e8537c91544d76283d06b14JmltdHM9MTc0MjQyODgwMA&ptn=3&ver=2&hsh=4&fclid=297ef5ed-ac44-66f2-2498-e058adb06776&u=a1aHR0cHM6Ly93d3cucG9rZXBlZGlhLmZyL01hamFzcGlj&ntb=1" });
await imageManager.AddImage(new Images() { Id = 11, ImgPath = "https://www.bing.com/ck/a?!&&p=390428c2820add92760900204667aa721b17d4eb9e8537c91544d76283d06b14JmltdHM9MTc0MjQyODgwMA&ptn=3&ver=2&hsh=4&fclid=297ef5ed-ac44-66f2-2498-e058adb06776&u=a1aHR0cHM6Ly93d3cucG9rZXBlZGlhLmZyL01hamFzcGlj&ntb=1" });
await imageManager.AddImage(new Images() { Id = 12, ImgPath = "https://www.bing.com/images/search?view=detailV2&ccid=t57OzeAT&id=1CCCBB65825E5FB93F10CA6D29EFDBBFEB5CDF27&thid=OIP.t57OzeATZKjBDDrzXqbc5gHaE7&mediaurl=https%3a%2f%2fimg-19.commentcamarche.net%2fP51ArxVXHJKsgdTzGDaqajlWJ3s%3d%2f1500x%2fsmart%2f7b5dd43e607643fea1a61960e3f66fc4%2fccmcms-commentcamarche%2f39481621.jpg&cdnurl=https%3a%2f%2fth.bing.com%2fth%2fid%2fR.b79ececde01364a8c10c3af35ea6dce6%3frik%3dJ99c67%252fb7yltyg%26pid%3dImgRaw%26r%3d0&exph=999&expw=1500&q=image&simid=608026907577902968&ck=0D54F216D075AD6E0ABC46B3AAB7E80A&selectedIndex=19&itb=0" });
Console.WriteLine("---- Test ajout image (id : 11, 12)");
var images = await imageManager.GetAllImage();
foreach (var image in images.items)
{
Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40)+"...")}");
}
Console.WriteLine("");
await imageManager.UpdateImage(12, new Images() { ImgPath = "https://testUpdate/stub"});
Console.WriteLine("---- Test mise a jour image (id : 12)");
images = await imageManager.GetAllImage();
foreach (var image in images.items)
{
Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40) + "...")}");
}
Console.WriteLine("");
await imageManager.RemoveImage(12);
Console.WriteLine("---- Test suppression image (id : 12)");
images = await imageManager.GetAllImage();
foreach (var image in images.items)
{
Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40) + "...")}");
}
Console.WriteLine("");
Console.WriteLine("---- Test getById image (id : 11)");
var img = await imageManager.GetImageById(11);
Console.WriteLine($"- ({img.Id}) : {(img.ImgPath.Length <= 40 ? img.ImgPath : img.ImgPath.Substring(0, 40) + "...")}");
Console.WriteLine("");
Console.WriteLine("---- Test getSomme image (nb : 5, page : 1)");
images = await imageManager.GetSomeImage(1,5);
foreach (var image in images.items)
{
Console.WriteLine($"- ({image.Id}) : {(image.ImgPath.Length <= 40 ? image.ImgPath : image.ImgPath.Substring(0, 40) + "...")}");
}
Console.WriteLine("");
Console.WriteLine("---- Test LastId image");
var id = await imageManager.GetLastImageId();
Console.WriteLine($"- Last image id : {id}");
Console.WriteLine("");
Console.WriteLine("-------------------------------------------------------------------------------");
// ---- Test Character ---- // // ---- Test Character ---- //
var characterManager = new DbCharacterManager(_context); var characterManager = new DbCharacterManager(_context);
await characterManager.AddCharacter(new Entity.Character() { Id = 11, Name = "Majespic", IdImage = 11}); await characterManager.AddCharacter(new Character() { Id = 11, Name = "Vipélière", IdImage = 11 });
Console.WriteLine("---- Test ajout charcter (id : 11)");
// recupération données
var characters = await characterManager.GetAll(); var characters = await characterManager.GetAll();
// affichage des dponnées récupérer
foreach (var charac in characters.items) foreach (var charac in characters.items)
{ {
Console.WriteLine("(" + charac.Id + ") " + charac.Name + " / Image ref :" + charac.IdImage); Console.WriteLine($"- ({charac.Id}) : {charac.Name} -> {charac.IdImage} : {(charac.Images.ImgPath.Length <= 40 ? charac.Images.ImgPath : charac.Images.ImgPath.Substring(0, 40) + "...")}");
}
Console.WriteLine("");
await characterManager.UpdateCharacter(11,new Character() {Name = "Majespic"});
Console.WriteLine("---- Test mise a jour charcter (id : 11)");
characters = await characterManager.GetAll();
foreach (var charac in characters.items)
{
Console.WriteLine($"- ({charac.Id}) : {charac.Name} -> {charac.IdImage} : {(charac.Images.ImgPath.Length <= 40 ? charac.Images.ImgPath : charac.Images.ImgPath.Substring(0, 40) + "...")}");
} }
Console.WriteLine("");
await characterManager.RemoveCharacter(11);
Console.WriteLine("---- Test sup (id : 5)");
characters = await characterManager.GetAll();
foreach (var charac in characters.items)
{
Console.WriteLine($"- ({charac.Id}) : {charac.Name} -> {charac.IdImage} : {(charac.Images.ImgPath.Length <= 40 ? charac.Images.ImgPath : charac.Images.ImgPath.Substring(0, 40) + "...")}");
}
Console.WriteLine("");
Console.WriteLine("---- Test GetById (id : 5)");
var chara = await characterManager.GetCharById(5);
Console.WriteLine($"- ({chara.Id}) : {chara.Name} -> {chara.IdImage} : {(chara.Images.ImgPath.Length <= 40 ? chara.Images.ImgPath : chara.Images.ImgPath.Substring(0, 40) + "...")}");
Console.WriteLine("");
Console.WriteLine("---- Test GetByName (name : Jon Snow)");
chara = await characterManager.GetCharByName("Jon Snow");
Console.WriteLine($"- ({chara.Id}) : {chara.Name} -> {chara.IdImage} : {(chara.Images.ImgPath.Length <= 40 ? chara.Images.ImgPath : chara.Images.ImgPath.Substring(0, 40) + "...")}");
Console.WriteLine("");
Console.WriteLine("---- Test LastId Character");
id = await characterManager.GetLastCharId();
Console.WriteLine($"- Last character id : {id}");
Console.WriteLine("");
} }

@ -41,7 +41,7 @@ namespace Contextlib
/// <returns>A task representing the asynchronous operation, with a <see cref="PaginationResult{Character}"/> as its result containing the full list of characters and pagination information.</returns> /// <returns>A task representing the asynchronous operation, with a <see cref="PaginationResult{Character}"/> as its result containing the full list of characters and pagination information.</returns>
public Task<PaginationResult<Character>> GetAll() public Task<PaginationResult<Character>> GetAll()
{ {
List<Character> charLst = _context.characters.ToList(); List<Character> charLst = _context.characters.Include(i => i.Images).ToList();
return Task.FromResult(new PaginationResult<Character>(charLst.Count, 0, charLst.Count, charLst)); return Task.FromResult(new PaginationResult<Character>(charLst.Count, 0, charLst.Count, charLst));
} }
@ -55,6 +55,7 @@ namespace Contextlib
public async Task<Character> GetCharById(int id) public async Task<Character> GetCharById(int id)
{ {
Character? character = await _context.characters Character? character = await _context.characters
.Include(i => i.Images)
.FirstOrDefaultAsync(x => x.Id == id); .FirstOrDefaultAsync(x => x.Id == id);
if (character == null) if (character == null)
@ -73,8 +74,9 @@ namespace Contextlib
/// <exception cref="KeyNotFoundException">Thrown when no character is found with the given name.</exception> /// <exception cref="KeyNotFoundException">Thrown when no character is found with the given name.</exception>
public async Task<Character> GetCharByName(string name) public async Task<Character> GetCharByName(string name)
{ {
Character? character = await _context.characters var character = await _context.characters
.FirstOrDefaultAsync(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); .Where(c => EF.Functions.Like(c.Name, name))
.FirstOrDefaultAsync();
if (character == null) if (character == null)
{ {
@ -129,17 +131,26 @@ namespace Contextlib
{ {
throw new ArgumentNullException(nameof(character), "The updated character data cannot be null."); throw new ArgumentNullException(nameof(character), "The updated character data cannot be null.");
} }
var modif = false;
Character? charUpdated = await _context.characters.FirstOrDefaultAsync(x => x.Id == id); Character? charUpdated = await _context.characters.FirstOrDefaultAsync(x => x.Id == id);
if (charUpdated == null) if (charUpdated == null)
{ {
throw new KeyNotFoundException($"Error : Unable to update, no character found with the ID: {id} ."); throw new KeyNotFoundException($"Error : Unable to update, no character found with the ID: {id} .");
} }
if (character.IdImage != 0)
charUpdated.IdImage = character.IdImage; {
charUpdated.Name = character.Name; charUpdated.IdImage = character.IdImage;
modif = true;
await _context.SaveChangesAsync(); }
if (character.Name != null)
{
charUpdated.Name = character.Name;
modif = true;
}
if (modif)
{
await _context.SaveChangesAsync();
}
} }
} }
} }

@ -1,5 +1,6 @@
using Entity; using Entity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Update;
using Shared; using Shared;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -61,7 +62,7 @@ namespace Contextlib
/// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception> /// <exception cref="KeyNotFoundException">Thrown when no comments are found for the provided user ID.</exception>
public async Task DeleteCommentForUser(int userId) public async Task DeleteCommentForUser(int userId)
{ {
var comments = await _context.comments.Where(x => x.IdUser == userId).ToListAsync(); var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync();
if (!comments.Any()) if (!comments.Any())
{ {
throw new KeyNotFoundException($"No comments found for the user ID: {userId}."); throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");
@ -73,13 +74,13 @@ namespace Contextlib
public async Task<PaginationResult<Commentary>> GetAllComment() public async Task<PaginationResult<Commentary>> GetAllComment()
{ {
var comments = await _context.comments.ToListAsync(); var comments = await _context.comments.Include(c => c.User).ToListAsync();
return new PaginationResult<Commentary>(comments.Count, 0, comments.Count, comments); return new PaginationResult<Commentary>(comments.Count, 0, comments.Count, comments);
} }
public async Task<Commentary> GetCommentById(int id) public async Task<Commentary> GetCommentById(int id)
{ {
var comment = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync(); var comment = await _context.comments.Include(c => c.User).Where(x => x.Id == id).FirstOrDefaultAsync();
if(comment == null) if(comment == null)
{ {
throw new KeyNotFoundException($"No comments found with the given ID: {id}."); throw new KeyNotFoundException($"No comments found with the given ID: {id}.");
@ -89,7 +90,7 @@ namespace Contextlib
public async Task<PaginationResult<Commentary>> GetCommentByQuote(int quoteId, int index, int pageSize) public async Task<PaginationResult<Commentary>> GetCommentByQuote(int quoteId, int index, int pageSize)
{ {
var comments = await _context.comments.Where(x => x.IdQuote == quoteId).ToListAsync(); var comments = await _context.comments.Include(c => c.User).Where(x => x.IdQuote == quoteId).ToListAsync();
if (!comments.Any()) if (!comments.Any())
{ {
throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}."); throw new KeyNotFoundException($"No comments found for the quote ID: {quoteId}.");
@ -111,7 +112,7 @@ namespace Contextlib
public async Task<PaginationResult<Commentary>> GetCommentByUser(int userId, int index, int pageSize) public async Task<PaginationResult<Commentary>> GetCommentByUser(int userId, int index, int pageSize)
{ {
var comments = await _context.comments.Where(x => x.IdUser == userId).ToListAsync(); var comments = await _context.comments.Include(c => c.User).Where(x => x.IdUser == userId).ToListAsync();
if (!comments.Any()) if (!comments.Any())
{ {
throw new KeyNotFoundException($"No comments found for the user ID: {userId}."); throw new KeyNotFoundException($"No comments found for the user ID: {userId}.");
@ -154,6 +155,7 @@ namespace Contextlib
public async Task UpdateComment(int id, Commentary comment) public async Task UpdateComment(int id, Commentary comment)
{ {
var modif = false;
var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync(); var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync();
if (comment == null) if (comment == null)
{ {
@ -163,11 +165,29 @@ namespace Contextlib
{ {
throw new KeyNotFoundException($"No comments found with the given ID: {id}."); throw new KeyNotFoundException($"No comments found with the given ID: {id}.");
} }
com.Comment = comment.Comment; if (comment.Comment != null)
com.DateCommentary = comment.DateCommentary; {
com.IdQuote = comment.IdQuote; com.Comment = comment.Comment;
com.IdUser = comment.IdUser; modif = true;
await _context.SaveChangesAsync(); }
if(comment.DateCommentary != null){
com.DateCommentary = comment.DateCommentary;
modif = true;
}
if(comment.IdQuote != 0)
{
com.IdQuote = comment.IdQuote;
modif = true;
}
if (comment.IdUser != 0)
{
com.IdUser = comment.IdUser;
modif = true;
}
if (modif)
{
await _context.SaveChangesAsync();
}
} }
} }
} }

@ -85,6 +85,7 @@ namespace Contextlib
public async Task UpdateImage(int id, Images image) public async Task UpdateImage(int id, Images image)
{ {
var modif = false;
var img = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync(); var img = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync();
if (image == null) if (image == null)
{ {
@ -94,8 +95,15 @@ namespace Contextlib
{ {
throw new KeyNotFoundException($"No image found with the given ID: {id}."); throw new KeyNotFoundException($"No image found with the given ID: {id}.");
} }
img.ImgPath = image.ImgPath; if (image.ImgPath != null)
await _context.SaveChangesAsync(); {
img.ImgPath = image.ImgPath;
modif = true;
}
if (modif)
{
await _context.SaveChangesAsync();
}
} }
} }
} }

Loading…
Cancel
Save