From 18e7df02f3f15175b193b9374de4feb2e70b1c32 Mon Sep 17 00:00:00 2001 From: "Johnny.Ratton" Date: Mon, 1 Apr 2024 10:27:14 +0200 Subject: [PATCH] Ajout des test unitaires pour les nouveaux controller et services --- .../API/Controllers/InquiryTableController.cs | 2 +- .../API/Controllers/NotepadController.cs | 4 +- API_SQLuedo/TestAPI/InquiryTableUnitTest.cs | 51 +++++ API_SQLuedo/TestAPI/NotepadUnitTest.cs | 183 ++++++++++++++++++ API_SQLuedo/TestAPI/SolutionUnitTest.cs | 52 +++++ .../Service/TestInquiryTableDataService.cs | 62 ++++++ .../TestEF/Service/TestNotepadDataService.cs | 169 ++++++++++++++++ .../TestEF/Service/TestSolutionDataService.cs | 75 +++++++ 8 files changed, 595 insertions(+), 3 deletions(-) create mode 100644 API_SQLuedo/TestAPI/InquiryTableUnitTest.cs create mode 100644 API_SQLuedo/TestAPI/NotepadUnitTest.cs create mode 100644 API_SQLuedo/TestAPI/SolutionUnitTest.cs create mode 100644 API_SQLuedo/TestEF/Service/TestInquiryTableDataService.cs create mode 100644 API_SQLuedo/TestEF/Service/TestNotepadDataService.cs create mode 100644 API_SQLuedo/TestEF/Service/TestSolutionDataService.cs diff --git a/API_SQLuedo/API/Controllers/InquiryTableController.cs b/API_SQLuedo/API/Controllers/InquiryTableController.cs index fa9d3f5..a610342 100644 --- a/API_SQLuedo/API/Controllers/InquiryTableController.cs +++ b/API_SQLuedo/API/Controllers/InquiryTableController.cs @@ -26,7 +26,7 @@ public class InquiryTableController : Controller [HttpGet("database/{id:int}")] [ProducesResponseType(typeof(string), 200)] [ProducesResponseType(typeof(string), 404)] - public IActionResult GetSolutionByInquiryById(int id) + public IActionResult GetDatabaseNameByInquiryById(int id) { try { diff --git a/API_SQLuedo/API/Controllers/NotepadController.cs b/API_SQLuedo/API/Controllers/NotepadController.cs index 1eb3215..e3f46bf 100644 --- a/API_SQLuedo/API/Controllers/NotepadController.cs +++ b/API_SQLuedo/API/Controllers/NotepadController.cs @@ -50,7 +50,7 @@ public class NotepadController : Controller { try { - if (notepad.InquiryId == null || notepad.UserId == null || notepad.Notes == null) + if (notepad.InquiryId < 0 || notepad.UserId < 0 || notepad.Notes == null) { return BadRequest(); } @@ -76,7 +76,7 @@ public class NotepadController : Controller { try { - if (notepad.InquiryId == null || notepad.UserId == null || notepad.Notes == null) + if (notepad.InquiryId < 0 || notepad.UserId < 0 || notepad.Notes == null) { return BadRequest(); } diff --git a/API_SQLuedo/TestAPI/InquiryTableUnitTest.cs b/API_SQLuedo/TestAPI/InquiryTableUnitTest.cs new file mode 100644 index 0000000..a306bf6 --- /dev/null +++ b/API_SQLuedo/TestAPI/InquiryTableUnitTest.cs @@ -0,0 +1,51 @@ +using API.Controllers; +using Dto; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using Shared; + +namespace TestAPI; + +public class InquiryTableUnitTest +{ + private readonly Mock> _inquiryTableService; + + public InquiryTableUnitTest() + { + _inquiryTableService = new Mock>(); + } + + [Fact] + public void GetDatabaseNameFromInquiryId_Success() + { + var database = "Inquiry1"; + _inquiryTableService.Setup(x => x.GetDatabaseNameByInquiryId(42)) + .Returns(database); + var inquiryTableController = + new InquiryTableController(_inquiryTableService.Object, new NullLogger()); + + var inquiryTableResult = inquiryTableController.GetDatabaseNameByInquiryById(42); + + if (inquiryTableResult is OkObjectResult okObjectResult) + { + var valeur = okObjectResult.Value; + Assert.NotNull(valeur); + Assert.Equal(database, ((KeyValuePair)valeur).Value); + } + } + + [Fact] + public void GetDatabaseNameFromInquiryId_Throws_ArgumentException() + { + _inquiryTableService.Setup(x => x.GetDatabaseNameByInquiryId(42)) + .Throws(); + var inquiryTableController = + new InquiryTableController(_inquiryTableService.Object, new NullLogger()); + + var inquiryTableResult = inquiryTableController.GetDatabaseNameByInquiryById(42); + + Assert.NotNull(inquiryTableResult); + Assert.Equal(typeof(NotFoundResult), inquiryTableResult.GetType()); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestAPI/NotepadUnitTest.cs b/API_SQLuedo/TestAPI/NotepadUnitTest.cs new file mode 100644 index 0000000..1bc6ad6 --- /dev/null +++ b/API_SQLuedo/TestAPI/NotepadUnitTest.cs @@ -0,0 +1,183 @@ +using API.Controllers; +using Dto; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using Shared; +using TestAPI.Extensions; + +namespace TestAPI; + +public class NotepadUnitTest +{ + private readonly Mock> _notepadService; + + public NotepadUnitTest() + { + _notepadService = new Mock>(); + } + + [Fact] + public void GetNotepadFromUserAndInquiryId() + { + var notepad = new NotepadDto(42, 42, "These are notes example."); + _notepadService.Setup(x => x.GetNotepadFromUserAndInquiryId(42, 42)) + .Returns(notepad); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.GetNotepadByUserAndInquiryById(42,42); + + if (notepadResult is OkObjectResult okObjectResult) + { + var valeur = okObjectResult.Value; + Assert.NotNull(valeur); + Assert.Equal(notepad, valeur); + } + } + + [Fact] + public void GetNotepadFromUserAndInquiryId_ThrowingArgumentException() + { + var notepad = new NotepadDto(42, 42, "These are notes example."); + _notepadService.Setup(x => x.GetNotepadFromUserAndInquiryId(42, 10)) + .Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.GetNotepadByUserAndInquiryById(42,10); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(NotFoundResult),notepadResult.GetType()); + } + + [Fact] + public void SetNotepadFromUserAndInquiryId_Success() + { + _notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, 42,"These are notes example.")); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example.")); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(OkResult),notepadResult.GetType()); + Assert.Equal(typeof(OkObjectResult) , notepadController.GetNotepadByUserAndInquiryById(42,42).GetType()); + } + + [Fact] + public void SetNotepadFromUserAndInquiryId_Negative_UserId() + { + _notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(-42, 42,"These are notes example.")).Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(-42,42, "These are notes example.")); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(BadRequestResult),notepadResult.GetType()); + } + + [Fact] + public void SetNotepadFromUserAndInquiryId_Negative_InquiryId() + { + _notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, -42,"These are notes example.")).Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,-42, "These are notes example.")); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(BadRequestResult),notepadResult.GetType()); + } + + [Fact] + public void SetNotepadFromUserAndInquiryId_Null_Notes() + { + _notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, 42,"These are notes example.")).Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, null)); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(BadRequestResult),notepadResult.GetType()); + } + + [Fact] + public void SetNotepadFromUserAndInquiryId_Throws_ArgumentException() + { + var notepad = new NotepadDto(42, 42, "These are notes example."); + _notepadService.Setup(x => x.SetNotepadFromUserAndInquiryId(42, 42,"These are notes example.")).Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(notepad); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(NotFoundResult),notepadResult.GetType()); + } + + + [Fact] + public void UpdateNotepadFromUserAndInquiryId_Success() + { + _notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 42,"These are the new notes")); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example.")); + Assert.NotNull(notepadResult); + Assert.Equal(typeof(OkResult),notepadResult.GetType()); + Assert.Equal(typeof(OkObjectResult) , notepadController.GetNotepadByUserAndInquiryById(42,42).GetType()); + var updateResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are the new notes")); + Assert.NotNull(updateResult); + Assert.Equal(typeof(OkResult),updateResult.GetType()); + } + + [Fact] + public void UpdateNotepadFromUserAndInquiryId_Negative_UserId() + { + _notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 42,"These are the new notes")); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example.")); + var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(-42,42, "These are the new notes")); + Assert.NotNull(updateResult); + Assert.Equal(typeof(BadRequestResult),updateResult.GetType()); + } + + [Fact] + public void UpdateNotepadFromUserAndInquiryId_Negative_InquiryId() + { + _notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, -42,"These are the new notes")); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example.")); + var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(42,-42, "These are the new notes")); + Assert.NotNull(updateResult); + Assert.Equal(typeof(BadRequestResult),updateResult.GetType()); + } + + [Fact] + public void UpdateNotepadFromUserAndInquiryId_Null_Notes() + { + _notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 42,null)).Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example.")); + var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(42,42, null)); + Assert.NotNull(updateResult); + Assert.Equal(typeof(BadRequestResult),updateResult.GetType()); + } + + [Fact] + public void UpdateNotepadFromUserAndInquiryId_Throws_ArgumentException() + { + _notepadService.Setup(x => x.UpdateNotepadFromUserAndInquiryId(42, 10,"These are the new notes")).Throws(); + var notepadController = + new NotepadController(_notepadService.Object, new NullLogger()); + + var notepadResult = notepadController.SetNotepadByUserAndInquiryById(new NotepadDto(42,42, "These are notes example.")); + var updateResult = notepadController.UpdateNotepadByUserAndInquiryById(new NotepadDto(42,10, "These are the new notes")); + Assert.NotNull(updateResult); + Assert.Equal(typeof(NotFoundResult),updateResult.GetType()); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestAPI/SolutionUnitTest.cs b/API_SQLuedo/TestAPI/SolutionUnitTest.cs new file mode 100644 index 0000000..da81706 --- /dev/null +++ b/API_SQLuedo/TestAPI/SolutionUnitTest.cs @@ -0,0 +1,52 @@ +using API.Controllers; +using Dto; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using Shared; + +namespace TestAPI; + +public class SolutionUnitTest +{ + private readonly Mock> _solutionService; + + public SolutionUnitTest() + { + _solutionService = new Mock>(); + } + + [Fact] + public void GetSolutionFromInquiryId_Success() + { + var solution = new SolutionDto(42,"Maxime","Sapountzis","La cuisine","Le couteau", "L'explication"); + _solutionService.Setup(x => x.GetSolutionByInquiryId(42)) + .Returns(solution); + var solutionController = + new SolutionController(_solutionService.Object, new NullLogger()); + + var solutionResult = solutionController.GetSolutionByInquiryById(42); + + if (solutionResult is OkObjectResult okObjectResult) + { + var valeur = okObjectResult.Value; + Assert.NotNull(valeur); + Assert.Equal(solution, valeur); + } + } + + [Fact] + public void GetSolutionFromInquiryId_Throws_ArgumentException() + { + var solution = new SolutionDto(42,"Maxime","Sapountzis","La cuisine","Le couteau", "L'explication"); + _solutionService.Setup(x => x.GetSolutionByInquiryId(42)) + .Throws(); + var solutionController = + new SolutionController(_solutionService.Object, new NullLogger()); + + var solutionResult = solutionController.GetSolutionByInquiryById(42); + + Assert.NotNull(solutionResult); + Assert.Equal(typeof(NotFoundResult), solutionResult.GetType()); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Service/TestInquiryTableDataService.cs b/API_SQLuedo/TestEF/Service/TestInquiryTableDataService.cs new file mode 100644 index 0000000..63ed0cf --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestInquiryTableDataService.cs @@ -0,0 +1,62 @@ +using DbContextLib; +using DbDataManager.Service; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestEF.Service; + +public class TestInquiryTableDataService +{ + private readonly UserDbContext _dbContext; + private readonly InquiryTableDataService _inquiryTableDataService; + + public TestInquiryTableDataService() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _inquiryTableDataService = new InquiryTableDataService(_dbContext); + } + + [Fact] + public void GetDatabaseFromInquiryId_Success() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Description", IsUser = false }); + var inquiryTable = new InquiryTableEntity() + { + OwnerId = 42, + DatabaseName = "Database", + ConnectionInfo = "ConnectionString" + }; + _dbContext.InquiryTables.Add(inquiryTable); + _dbContext.SaveChanges(); + var result = _inquiryTableDataService.GetDatabaseNameByInquiryId(42); + Assert.Equal("Database",result); + } + + [Fact] + public void GetSolution_NotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Description", IsUser = false }); + var inquiryTable = new InquiryTableEntity() + { + OwnerId = 42, + DatabaseName = "Database", + ConnectionInfo = "ConnectionString" + }; + _dbContext.InquiryTables.Add(inquiryTable); + _dbContext.SaveChanges(); + + Assert.Throws(() => + { + _inquiryTableDataService.GetDatabaseNameByInquiryId(10); + }); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Service/TestNotepadDataService.cs b/API_SQLuedo/TestEF/Service/TestNotepadDataService.cs new file mode 100644 index 0000000..f67a561 --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestNotepadDataService.cs @@ -0,0 +1,169 @@ +using DbContextLib; +using DbDataManager.Service; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure.Internal; +using Model.OrderCriteria; + +namespace TestEF.Service; + +public class TestNotepadDataService +{ + private readonly UserDbContext _dbContext; + private readonly NotepadDataService _notepadDataService; + + public TestNotepadDataService() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _notepadDataService = new NotepadDataService(_dbContext); + } + + [Fact] + public void GetNotepad_Success() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" }; + _dbContext.Notepads.Add(notepad); + _dbContext.SaveChanges(); + + var result = _notepadDataService.GetNotepadFromUserAndInquiryId(42, 42); + + Assert.Equal(notepad,result); + } + + [Fact] + public void GetNotepad_Fail_UserNotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" }; + _dbContext.Notepads.Add(notepad); + _dbContext.SaveChanges(); + Assert.Throws(() => + { + _notepadDataService.GetNotepadFromUserAndInquiryId(10, 42); + }); + } + + [Fact] + public void GetNotepad_Fail_InquiryNotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" }; + _dbContext.Notepads.Add(notepad); + _dbContext.SaveChanges(); + Assert.Throws(() => + { + _notepadDataService.GetNotepadFromUserAndInquiryId(42, 10); + }); + } + + [Fact] + public void GetNotepad_Fail_NotepadNotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 1, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + _dbContext.SaveChanges(); + Assert.Throws(() => + { + _notepadDataService.GetNotepadFromUserAndInquiryId(42, 1); + }); + } + + [Fact] + public void SetNotepad_Success() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + _dbContext.SaveChanges(); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "Some notes again" }; + _notepadDataService.SetNotepadFromUserAndInquiryId(42,42, "Some notes again"); + _dbContext.SaveChanges(); + + var result = _notepadDataService.GetNotepadFromUserAndInquiryId(42, 42); + + Assert.Equal(notepad.UserId,result.UserId); + Assert.Equal(notepad.InquiryId,result.InquiryId); + Assert.Equal(notepad.Notes,result.Notes); + } + + [Fact] + public void SetNotepad_Fail_UserNotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" }; + _dbContext.SaveChanges(); + Assert.Throws(() => + { + _notepadDataService.SetNotepadFromUserAndInquiryId(10, 42, "Some notes"); + }); + } + + [Fact] + public void SetNotepad_Fail_InquiryNotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "These are some notes" }; + _dbContext.SaveChanges(); + Assert.Throws(() => + { + _notepadDataService.SetNotepadFromUserAndInquiryId(42, 10, "Some notes"); + }); + } + + [Fact] + public void SetNotepad_Fail_NotepadAlreadyExists() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "Some notes" }; + _dbContext.Notepads.Add(notepad); + _dbContext.SaveChanges(); + Assert.Throws(() => + { + _notepadDataService.SetNotepadFromUserAndInquiryId(42, 42, "Some notes"); + }); + } + + [Fact] + public void UpdateNotepad_Success() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Descritpion", IsUser = false }); + _dbContext.Users.Add(new UserEntity + { Id = 42, Username = "Username", Email = "email@example.com", Password = "password", IsAdmin = true }); + var notepad = new NotepadEntity { UserId = 42, InquiryId = 42, Notes = "Some notes" }; + _dbContext.Notepads.Add(notepad); + _dbContext.SaveChanges(); + _notepadDataService.UpdateNotepadFromUserAndInquiryId(42,42, "New Notes"); + _dbContext.SaveChanges(); + Assert.Equal("New Notes", _notepadDataService.GetNotepadFromUserAndInquiryId(42,42).Notes); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Service/TestSolutionDataService.cs b/API_SQLuedo/TestEF/Service/TestSolutionDataService.cs new file mode 100644 index 0000000..be0b73d --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestSolutionDataService.cs @@ -0,0 +1,75 @@ +using DbContextLib; +using DbDataManager.Service; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestEF.Service; + +public class TestSolutionDataService +{ + private readonly UserDbContext _dbContext; + private readonly SolutionDataService _solutionDataService; + + public TestSolutionDataService() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _solutionDataService = new SolutionDataService(_dbContext); + } + + [Fact] + public void GetSolution_Success() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Description", IsUser = false }); + var solution = new SolutionEntity + { + MurdererFirstName = "Maxime", + MurdererLastName = "Sapountzis", + MurderPlace = "La cuisine", + MurderWeapon = "Le couteau", + Explaination = "Parce que", + OwnerId = 42, + }; + _dbContext.Solutions.Add(solution); + _dbContext.SaveChanges(); + + var result = _solutionDataService.GetSolutionByInquiryId(42); + + Assert.Equal(solution.MurdererFirstName,result.MurdererFirstName); + Assert.Equal(solution.MurdererLastName,result.MurdererLastName); + Assert.Equal(solution.MurderPlace,result.MurderPlace); + Assert.Equal(solution.MurderWeapon,result.MurderWeapon); + Assert.Equal(solution.Explaination,result.Explaination); + Assert.Equal(solution.OwnerId,result.OwnerId); + } + + [Fact] + public void GetSolution_NotFound() + { + _dbContext.Inquiries.Add(new InquiryEntity + { Id = 42, Title = "Titre", Description = "Description", IsUser = false }); + var solution = new SolutionEntity + { + MurdererFirstName = "Maxime", + MurdererLastName = "Sapountzis", + MurderPlace = "La cuisine", + MurderWeapon = "Le couteau", + Explaination = "Parce que", + OwnerId = 42, + }; + _dbContext.Solutions.Add(solution); + _dbContext.SaveChanges(); + + Assert.Throws(() => + { + _solutionDataService.GetSolutionByInquiryId(10); + }); + } +} \ No newline at end of file