From 0aac223ecd9da57ee11049a5fe36f8be71fa00d9 Mon Sep 17 00:00:00 2001 From: Nestisse Date: Sun, 17 Mar 2024 02:59:14 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20des=20tests=20unitaires=20sur=20le?= =?UTF-8?q?=20service=20ef?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/DbContextLib/UserDbContext.cs | 3 +- .../Service/LessonDataService.cs | 81 +++----- .../Service/ParagraphDataService.cs | 2 +- .../TestEF/Service/TestInquiryDataService.cs | 147 +++++++++++++++ .../TestEF/Service/TestLessonDataService.cs | 105 +++++++++++ .../Service/TestParagraphDataService.cs | 145 +++++++++++++++ .../TestEF/Service/TestSuccessDataService.cs | 174 ++++++++++++++++++ 7 files changed, 603 insertions(+), 54 deletions(-) create mode 100644 API_SQLuedo/TestEF/Service/TestInquiryDataService.cs create mode 100644 API_SQLuedo/TestEF/Service/TestLessonDataService.cs create mode 100644 API_SQLuedo/TestEF/Service/TestParagraphDataService.cs create mode 100644 API_SQLuedo/TestEF/Service/TestSuccessDataService.cs diff --git a/API_SQLuedo/DbContextLib/UserDbContext.cs b/API_SQLuedo/DbContextLib/UserDbContext.cs index b4de5c2..849189c 100644 --- a/API_SQLuedo/DbContextLib/UserDbContext.cs +++ b/API_SQLuedo/DbContextLib/UserDbContext.cs @@ -23,12 +23,11 @@ namespace DbContextLib protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + base.OnConfiguring(optionsBuilder); if (!optionsBuilder.IsConfigured) { optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=motdepasse"); } - - base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder builder) diff --git a/API_SQLuedo/DbDataManager/Service/LessonDataService.cs b/API_SQLuedo/DbDataManager/Service/LessonDataService.cs index bb7ced6..aa67a64 100644 --- a/API_SQLuedo/DbDataManager/Service/LessonDataService.cs +++ b/API_SQLuedo/DbDataManager/Service/LessonDataService.cs @@ -23,10 +23,12 @@ public class LessonDataService : ILessonService { page = 1; } + if (number <= 0) { number = 10; } + IQueryable query = DbContext.Lessons.Skip((page - 1) * number).Take(number); switch (orderCriteria) { @@ -73,64 +75,41 @@ public class LessonDataService : ILessonService public bool DeleteLesson(int id) { - using (UserDbContext context = new UserDbContext(new DbContextOptions())) - { - var lessonEntity = context.Lessons.FirstOrDefault(l => l.Id == id); - if (lessonEntity == null) - { - return false; - } - - context.Lessons.Remove(lessonEntity); - context.SaveChangesAsync(); - return true; - } + var lessonEntity = DbContext.Lessons.FirstOrDefault(l => l.Id == id); + if (lessonEntity == null) + return false; + + DbContext.Lessons.Remove(lessonEntity); + DbContext.SaveChanges(); + return true; } public LessonEntity UpdateLesson(int id, LessonEntity lesson) { - using (UserDbContext context = new UserDbContext(new DbContextOptions())) - { - var updatingLesson = context.Lessons.FirstOrDefault(l => l.Id == id); - if (updatingLesson == null) - { - throw new ArgumentException("Impossible de trouver la leçon", nameof(id)); - } - - foreach (var pptt in typeof(LessonEntity).GetProperties() - .Where(p => p.CanWrite && p.Name != nameof(LessonEntity.Id))) - { - pptt.SetValue(updatingLesson, pptt.GetValue(lesson)); - } - - context.SaveChangesAsync(); - return updatingLesson; - } + var updatingLesson = DbContext.Lessons.FirstOrDefault(l => l.Id == id); + if (updatingLesson == null) + throw new ArgumentException("Impossible de trouver la leçon", nameof(id)); + + updatingLesson.Title = lesson.Title; + updatingLesson.LastPublisher = lesson.LastPublisher; + updatingLesson.LastEdit = lesson.LastEdit; + + DbContext.SaveChanges(); + return updatingLesson; } + public LessonEntity CreateLesson(int id, string title, string lastPublisher, DateOnly lastEdit) { - using (UserDbContext context = new UserDbContext(new DbContextOptions())) + var newLessonEntity = new LessonEntity() { - DateTime date = DateTime.Now; - var newLessonEntity = new LessonEntity() - { - Title = title, - LastPublisher = lastPublisher, - LastEdit = lastEdit, - }; - var lesson = context.Lessons.FirstOrDefault(l => l.Id == id); - if (lesson == null && id > 0) - { - newLessonEntity.Id = id; - context.Lessons.Add(newLessonEntity); - context.SaveChangesAsync(); - throw new ArgumentException( - $"Erreur, l'ID {id} est déjà utilisé pour une autre leçon, un id par a été attribué."); - } - - context.Lessons.Add(newLessonEntity); - context.SaveChangesAsync(); - return newLessonEntity; - } + Id = id > 0 && DbContext.Lessons.All(l => l.Id != id) ? id : 0, + Title = title, + LastPublisher = lastPublisher, + LastEdit = lastEdit, + }; + + DbContext.Lessons.Add(newLessonEntity); + DbContext.SaveChanges(); + return newLessonEntity; } } \ No newline at end of file diff --git a/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs b/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs index 5486813..4c77183 100644 --- a/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs +++ b/API_SQLuedo/DbDataManager/Service/ParagraphDataService.cs @@ -85,7 +85,7 @@ public class ParagraphDataService : IParagraphService } DbContext.Paragraphs.Remove(paragraphEntity); - DbContext.SaveChangesAsync(); + DbContext.SaveChanges(); return true; } diff --git a/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs b/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs new file mode 100644 index 0000000..d1fe4f4 --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs @@ -0,0 +1,147 @@ +using DbContextLib; +using DbDataManager.Service; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Model.OrderCriteria; + +namespace TestEF.Service; + +public class TestInquiryDataService +{ + private readonly UserDbContext _dbContext; + private readonly InquiryDataService _inquiryDataService; + + public TestInquiryDataService() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _inquiryDataService = new InquiryDataService(_dbContext); + } + + [Fact] + public void GetInquiries_ReturnsCorrectNumberOfInquiries() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.Inquiries.Add(new InquiryEntity { Id = 2, Title = "Test2", Description = "Desc2", IsUser = false }); + _dbContext.Inquiries.Add(new InquiryEntity { Id = 3, Title = "Test3", Description = "Desc3", IsUser = true }); + _dbContext.SaveChanges(); + + var result = _inquiryDataService.GetInquiries(1, 2, InquiryOrderCriteria.None); + + Assert.Equal(2, result.Count()); + } + + [Fact] + public void GetInquiryById_ReturnsCorrectInquiry() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.Inquiries.Add(new InquiryEntity { Id = 2, Title = "Test2", Description = "Desc2", IsUser = false }); + _dbContext.SaveChanges(); + + var result = _inquiryDataService.GetInquiryById(1); + + Assert.Equal("Test1", result.Title); + } + + [Fact] + public void GetInquiryByTitle_ReturnsCorrectInquiry() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.Inquiries.Add(new InquiryEntity { Id = 2, Title = "Test2", Description = "Desc2", IsUser = false }); + _dbContext.SaveChanges(); + + var result = _inquiryDataService.GetInquiryByTitle("Test1"); + + Assert.Equal(1, result.Id); + } + + [Fact] + public void CreateInquiry_AddsNewInquiry() + { + var result = _inquiryDataService.CreateInquiry("Test", "Desc", true); + + Assert.Equal(1, _dbContext.Inquiries.Count()); + Assert.Equal("Test", result.Title); + } + + [Fact] + public void DeleteInquiry_RemovesInquiry() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.SaveChanges(); + + _inquiryDataService.DeleteInquiry(1); + + Assert.Empty(_dbContext.Inquiries); + } + + [Fact] + public void UpdateInquiry_UpdatesExistingInquiry() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.SaveChanges(); + + var updatedInquiry = new InquiryEntity { Id = 1, Title = "Updated", Description = "Desc", IsUser = false }; + var result = _inquiryDataService.UpdateInquiry(1, updatedInquiry); + + Assert.Equal("Updated", result.Title); + } + + [Fact] + public void GetInquiries_WithBadPage_ReturnsClassicInquiries() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.Inquiries.Add(new InquiryEntity { Id = 2, Title = "Test2", Description = "Desc2", IsUser = false }); + _dbContext.SaveChanges(); + + var result = _inquiryDataService.GetInquiries(-1, 2, InquiryOrderCriteria.None); + + Assert.Equal(2, result.Count()); + } + + [Fact] + public void GetInquiryById_WithoutId_ThrowsException() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.SaveChanges(); + + Assert.Throws(() => _inquiryDataService.GetInquiryById(2)); + } + + [Fact] + public void GetInquiryByTitle_WithoutTitle_ThrowsException() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.SaveChanges(); + + Assert.Throws(() => _inquiryDataService.GetInquiryByTitle("Test2")); + } + + [Fact] + public void DeleteInquiry_WithoutId_ReturnsFalse() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.SaveChanges(); + + var result = _inquiryDataService.DeleteInquiry(2); + + Assert.False(result); + } + + [Fact] + public void UpdateInquiry_WithoutId_ThrowsException() + { + _dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true }); + _dbContext.SaveChanges(); + + var updatedInquiry = new InquiryEntity { Id = 2, Title = "Updated", Description = "Desc", IsUser = false }; + + Assert.Throws(() => _inquiryDataService.UpdateInquiry(2, updatedInquiry)); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Service/TestLessonDataService.cs b/API_SQLuedo/TestEF/Service/TestLessonDataService.cs new file mode 100644 index 0000000..386d007 --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestLessonDataService.cs @@ -0,0 +1,105 @@ +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() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _lessonDataService = new LessonDataService(_dbContext); + } + + [Fact] + public void GetLessons_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 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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs b/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs new file mode 100644 index 0000000..b7a6538 --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs @@ -0,0 +1,145 @@ +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() + .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_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 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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs b/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs new file mode 100644 index 0000000..eb0aa7e --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs @@ -0,0 +1,174 @@ +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 _users = new(); + private readonly List _inquiries = new(); + + public TestSuccessDataService() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _successDataService = new SuccessDataService(_dbContext); + + _users.AddRange(new List + { + 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 + { + 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_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 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); + } +} \ No newline at end of file