From 0aac223ecd9da57ee11049a5fe36f8be71fa00d9 Mon Sep 17 00:00:00 2001 From: Nestisse Date: Sun, 17 Mar 2024 02:59:14 +0100 Subject: [PATCH 1/5] =?UTF-8?q?D=C3=A9but=20des=20tests=20unitaires=20sur?= =?UTF-8?q?=20le=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 -- 2.36.3 From 4ea53b072ebc54d180d2ec202ed2b9ffce7141ab Mon Sep 17 00:00:00 2001 From: "Johnny.Ratton" Date: Sun, 17 Mar 2024 10:25:38 +0100 Subject: [PATCH 2/5] Ajout des tests de UserDataService --- .../TestEF/Service/TestUserDataService.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 API_SQLuedo/TestEF/Service/TestUserDataService.cs diff --git a/API_SQLuedo/TestEF/Service/TestUserDataService.cs b/API_SQLuedo/TestEF/Service/TestUserDataService.cs new file mode 100644 index 0000000..040f600 --- /dev/null +++ b/API_SQLuedo/TestEF/Service/TestUserDataService.cs @@ -0,0 +1,152 @@ +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() + .UseSqlite(connection) + .Options; + + _dbContext = new UserDbContext(options); + _userDataService = new UserDataService(_dbContext); + } + + [Fact] + public void GetUsers_ReturnsCorrectNumberOfUsers() + { + _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); + _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); + _dbContext.Users.Add(new UserEntity() { Id = 3, Username = "Test3", Email = "example@email.com", Password = "password", IsAdmin = true }); + _dbContext.SaveChanges(); + var result = _userDataService.GetUsers(1, 2, UserOrderCriteria.None); + Assert.Equal(2, result.Count()); + } + + [Fact] + public void GetUserById_ReturnsCorrectUser() + { + _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); + _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); + _dbContext.SaveChanges(); + var result = _userDataService.GetUserById(1); + Assert.Equal("Test1", result.Username); + } + + [Fact] + public void GetUserByUsername_ReturnsCorrectUser() + { + _dbContext.Users.Add(new UserEntity() { Id = 1, Username = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); + _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@email.com", Password = "password", IsAdmin = false }); + _dbContext.SaveChanges(); + var result = _userDataService.GetUserByUsername("Test1"); + 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 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 = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); + _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@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 = "Test1", Email = "example@email.com", Password = "password", IsAdmin = true }); + _dbContext.Users.Add(new UserEntity() { Id = 2, Username = "Test2", Email = "example@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(() => _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(() => _userDataService.GetUserByUsername("Test2")); + } + + [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 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(() => _userDataService.UpdateUser(2, updatedUser)); + } +} \ No newline at end of file -- 2.36.3 From 0a2aa5ea92c34e58986ed8b596881687fac1c07d Mon Sep 17 00:00:00 2001 From: "Johnny.Ratton" Date: Sun, 17 Mar 2024 11:16:09 +0100 Subject: [PATCH 3/5] Test de toutes les classes de Model --- API_SQLuedo/Dto/UserDTO.cs | 14 ++---- API_SQLuedo/Model/Solution.cs | 10 ++--- API_SQLuedo/Shared/Mapper/SolutionMapper.cs | 4 +- API_SQLuedo/TestEF/Dto/TestUserDTO.cs | 43 +++++++++++++++++++ .../TestEF/Mapper/SolutionMapperUnitTest.cs | 4 +- API_SQLuedo/TestEF/Model/BlackListTest.cs | 32 ++++++++++++++ API_SQLuedo/TestEF/Model/InquiryTableTest.cs | 24 +++++++++++ API_SQLuedo/TestEF/Model/InquiryTest.cs | 26 +++++++++++ API_SQLuedo/TestEF/Model/LessonTest.cs | 26 +++++++++++ API_SQLuedo/TestEF/Model/NotepadTest.cs | 36 ++++++++++++++++ API_SQLuedo/TestEF/Model/ParagraphTest.cs | 32 ++++++++++++++ API_SQLuedo/TestEF/Model/SolutionTest.cs | 42 ++++++++++++++++++ API_SQLuedo/TestEF/Model/SuccessTest.cs | 24 +++++++++++ API_SQLuedo/TestEF/Model/UserTest.cs | 28 ++++++++++++ 14 files changed, 326 insertions(+), 19 deletions(-) create mode 100644 API_SQLuedo/TestEF/Model/BlackListTest.cs create mode 100644 API_SQLuedo/TestEF/Model/InquiryTableTest.cs create mode 100644 API_SQLuedo/TestEF/Model/InquiryTest.cs create mode 100644 API_SQLuedo/TestEF/Model/LessonTest.cs create mode 100644 API_SQLuedo/TestEF/Model/NotepadTest.cs create mode 100644 API_SQLuedo/TestEF/Model/ParagraphTest.cs create mode 100644 API_SQLuedo/TestEF/Model/SolutionTest.cs create mode 100644 API_SQLuedo/TestEF/Model/SuccessTest.cs create mode 100644 API_SQLuedo/TestEF/Model/UserTest.cs diff --git a/API_SQLuedo/Dto/UserDTO.cs b/API_SQLuedo/Dto/UserDTO.cs index 076daa8..e035608 100644 --- a/API_SQLuedo/Dto/UserDTO.cs +++ b/API_SQLuedo/Dto/UserDTO.cs @@ -34,28 +34,22 @@ return $"{Id}\t{Username}\t{Email}\t{IsAdmin}"; } - public override bool Equals(object obj) + public bool Equals(UserDto? other) { - if (object.ReferenceEquals(obj, null)) + if (object.ReferenceEquals(other, null)) { return false; } - if (object.ReferenceEquals(this, obj)) + if (object.ReferenceEquals(this, other)) { return true; } - if (this.GetType() != obj.GetType()) + if (this.GetType() != other.GetType()) { return false; } - - return this.Equals(obj as UserDto); - } - - public bool Equals(UserDto other) - { return (this.Id == other.Id); } diff --git a/API_SQLuedo/Model/Solution.cs b/API_SQLuedo/Model/Solution.cs index 8d167f8..72d912c 100644 --- a/API_SQLuedo/Model/Solution.cs +++ b/API_SQLuedo/Model/Solution.cs @@ -7,30 +7,30 @@ public class Solution public string MurdererLastName { get; set; } public string MurderPlace { get; set; } public string MurderWeapon { get; set; } - public string Explanation { get; set; } + public string Explaination { get; set; } public Solution() { } public Solution(int ownerId, string murdererFirstName, string murdererLastName, string murderPlace, - string murderWeapon, string explanation) + string murderWeapon, string explaination) { OwnerId = ownerId; MurdererFirstName = murdererFirstName; MurdererLastName = murdererLastName; MurderPlace = murderPlace; MurderWeapon = murderWeapon; - Explanation = explanation; + Explaination = explaination; } public Solution(string murdererFirstName, string murdererLastName, string murderPlace, string murderWeapon, - string explanation) + string explaination) { MurdererFirstName = murdererFirstName; MurdererLastName = murdererLastName; MurderPlace = murderPlace; MurderWeapon = murderWeapon; - Explanation = explanation; + Explaination = explaination; } } \ No newline at end of file diff --git a/API_SQLuedo/Shared/Mapper/SolutionMapper.cs b/API_SQLuedo/Shared/Mapper/SolutionMapper.cs index 8d7a246..8e1ccdb 100644 --- a/API_SQLuedo/Shared/Mapper/SolutionMapper.cs +++ b/API_SQLuedo/Shared/Mapper/SolutionMapper.cs @@ -21,7 +21,7 @@ public static class SolutionMapper public static SolutionDto FromModelToDto(this Solution model) { return new SolutionDto(model.OwnerId, model.MurdererFirstName, model.MurdererLastName, model.MurderPlace, - model.MurderWeapon, model.Explanation); + model.MurderWeapon, model.Explaination); } public static SolutionDto FromEntityToDto(this SolutionEntity entity) @@ -43,7 +43,7 @@ public static class SolutionMapper MurdererLastName = model.MurdererLastName, MurderPlace = model.MurderPlace, MurderWeapon = model.MurderWeapon, - Explaination = model.Explanation + Explaination = model.Explaination }; } diff --git a/API_SQLuedo/TestEF/Dto/TestUserDTO.cs b/API_SQLuedo/TestEF/Dto/TestUserDTO.cs index 97ad011..fd7ff27 100644 --- a/API_SQLuedo/TestEF/Dto/TestUserDTO.cs +++ b/API_SQLuedo/TestEF/Dto/TestUserDTO.cs @@ -42,5 +42,48 @@ namespace TestEF.Dto Assert.Equal(Password, user.Password); Assert.True(user.IsAdmin); } + + [Fact] + public void TestToString() + { + UserDto user = new UserDto(Id, Username, Password, Email, IsAdmin); + Assert.Equal($"{Id}\t{Username}\t{Email}\t{IsAdmin}", user.ToString()); + } + + [Fact] + public void TestEqualsWithSameAttributesValues() + { + UserDto user1 = new UserDto(Id, Username, Password, Email, IsAdmin); + UserDto user2 = new UserDto(Id, Username, Password, Email, IsAdmin); + Assert.True(user1.Equals(user2)); + } + + [Fact] + public void TestEqualsWithNullReference() + { + UserDto user = new UserDto(Id, Username, Password, Email, IsAdmin); + Assert.False(user.Equals(null)); + } + + [Fact] + public void TestEqualsWithSameReference() + { + UserDto user = new UserDto(Id, Username, Password, Email, IsAdmin); + Assert.True(user.Equals(user)); + } + + [Fact] + public void TestEqualsWithDifferentType() + { + UserDto user = new UserDto(Id, Username, Password, Email, IsAdmin); + Assert.False(user.Equals("Tests")); + } + + [Fact] + public void TestGetHashCode() + { + UserDto user = new UserDto(Id, Username, Password, Email, IsAdmin); + Assert.Equal(Id, user.GetHashCode()); + } } } \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Mapper/SolutionMapperUnitTest.cs b/API_SQLuedo/TestEF/Mapper/SolutionMapperUnitTest.cs index 30258d2..35a7621 100644 --- a/API_SQLuedo/TestEF/Mapper/SolutionMapperUnitTest.cs +++ b/API_SQLuedo/TestEF/Mapper/SolutionMapperUnitTest.cs @@ -52,7 +52,7 @@ namespace TestEF.Mapper Assert.Equal(_murdererLastName, solutionMod.MurdererLastName); Assert.Equal(_murderPlace, solutionMod.MurderPlace); Assert.Equal(_murderWeapon, solutionMod.MurderWeapon); - Assert.Equal(_explanation, solutionMod.Explanation); + Assert.Equal(_explanation, solutionMod.Explaination); } @@ -85,7 +85,7 @@ namespace TestEF.Mapper Assert.Equal(_murdererLastName, solutionMod.MurdererLastName); Assert.Equal(_murderPlace, solutionMod.MurderPlace); Assert.Equal(_murderWeapon, solutionMod.MurderWeapon); - Assert.Equal(_explanation, solutionMod.Explanation); + Assert.Equal(_explanation, solutionMod.Explaination); } diff --git a/API_SQLuedo/TestEF/Model/BlackListTest.cs b/API_SQLuedo/TestEF/Model/BlackListTest.cs new file mode 100644 index 0000000..e3216c3 --- /dev/null +++ b/API_SQLuedo/TestEF/Model/BlackListTest.cs @@ -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(() => + { + BlackList blackList = new BlackList(null, DateOnly.FromDateTime(DateTime.Now)); + }); + } + + [Fact] + void TestConstructorWithAnteriorDate() + { + Assert.Throws(() => + { + BlackList blackList = new BlackList("example@email.com", DateOnly.FromDateTime(DateTime.Parse("2024/01/01 00:00:00"))); + }); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/InquiryTableTest.cs b/API_SQLuedo/TestEF/Model/InquiryTableTest.cs new file mode 100644 index 0000000..2bf2735 --- /dev/null +++ b/API_SQLuedo/TestEF/Model/InquiryTableTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/InquiryTest.cs b/API_SQLuedo/TestEF/Model/InquiryTest.cs new file mode 100644 index 0000000..e1e6701 --- /dev/null +++ b/API_SQLuedo/TestEF/Model/InquiryTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/LessonTest.cs b/API_SQLuedo/TestEF/Model/LessonTest.cs new file mode 100644 index 0000000..2a081b2 --- /dev/null +++ b/API_SQLuedo/TestEF/Model/LessonTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/NotepadTest.cs b/API_SQLuedo/TestEF/Model/NotepadTest.cs new file mode 100644 index 0000000..e07909d --- /dev/null +++ b/API_SQLuedo/TestEF/Model/NotepadTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/ParagraphTest.cs b/API_SQLuedo/TestEF/Model/ParagraphTest.cs new file mode 100644 index 0000000..3507b2a --- /dev/null +++ b/API_SQLuedo/TestEF/Model/ParagraphTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/SolutionTest.cs b/API_SQLuedo/TestEF/Model/SolutionTest.cs new file mode 100644 index 0000000..95e329a --- /dev/null +++ b/API_SQLuedo/TestEF/Model/SolutionTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/SuccessTest.cs b/API_SQLuedo/TestEF/Model/SuccessTest.cs new file mode 100644 index 0000000..152979a --- /dev/null +++ b/API_SQLuedo/TestEF/Model/SuccessTest.cs @@ -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); + } +} \ No newline at end of file diff --git a/API_SQLuedo/TestEF/Model/UserTest.cs b/API_SQLuedo/TestEF/Model/UserTest.cs new file mode 100644 index 0000000..1ca3e68 --- /dev/null +++ b/API_SQLuedo/TestEF/Model/UserTest.cs @@ -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); + } +} \ No newline at end of file -- 2.36.3 From 40e422d778065470b7bbd984a16ecf60afa5b5fc Mon Sep 17 00:00:00 2001 From: Nestisse Date: Sun, 17 Mar 2024 11:29:53 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Ajout=20de=20la=20m=C3=A9thode=20equals=20d?= =?UTF-8?q?ans=20userdto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API_SQLuedo/Dto/UserDTO.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/API_SQLuedo/Dto/UserDTO.cs b/API_SQLuedo/Dto/UserDTO.cs index e035608..d3aa536 100644 --- a/API_SQLuedo/Dto/UserDTO.cs +++ b/API_SQLuedo/Dto/UserDTO.cs @@ -36,21 +36,27 @@ public bool Equals(UserDto? other) { - if (object.ReferenceEquals(other, null)) + return (this.Id == other?.Id); + } + + public override bool Equals(object? obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } - if (object.ReferenceEquals(this, other)) + if (object.ReferenceEquals(this, obj)) { return true; } - if (this.GetType() != other.GetType()) + if (this.GetType() != obj.GetType()) { return false; } - return (this.Id == other.Id); + + return this.Equals(obj as UserDto); } public override int GetHashCode() -- 2.36.3 From 80de049dc3ef995e4f68f794627a5040a1efc470 Mon Sep 17 00:00:00 2001 From: "Johnny.Ratton" Date: Sun, 17 Mar 2024 11:31:32 +0100 Subject: [PATCH 5/5] Augmentation du coverage pour tous les DataService de DbDataManager --- .../TestEF/Service/TestInquiryDataService.cs | 53 ++++- .../TestEF/Service/TestLessonDataService.cs | 82 ++++++- .../Service/TestParagraphDataService.cs | 219 +++++++++++++++++- .../TestEF/Service/TestSuccessDataService.cs | 77 +++++- 4 files changed, 427 insertions(+), 4 deletions(-) diff --git a/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs b/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs index d1fe4f4..1a6fb96 100644 --- a/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs +++ b/API_SQLuedo/TestEF/Service/TestInquiryDataService.cs @@ -25,7 +25,7 @@ public class TestInquiryDataService } [Fact] - public void GetInquiries_ReturnsCorrectNumberOfInquiries() + public void GetInquiries_WithNoneCriteria_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 }); @@ -36,6 +36,45 @@ public class TestInquiryDataService Assert.Equal(2, result.Count()); } + + [Fact] + public void GetInquiries_OrderedByTitle_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.ByTitle); + + Assert.Equal(2, result.Count()); + } + + [Fact] + public void GetInquiries_OrderedByDescription_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.ByDescription); + + Assert.Equal(2, result.Count()); + } + + [Fact] + public void GetInquiries_OrderedByIsUser_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.ByIsUser); + + Assert.Equal(2, result.Count()); + } [Fact] public void GetInquiryById_ReturnsCorrectInquiry() @@ -104,6 +143,18 @@ public class TestInquiryDataService Assert.Equal(2, result.Count()); } + + [Fact] + public void GetInquiries_WithBadNumber_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, -42, InquiryOrderCriteria.None); + + Assert.Equal(2, result.Count()); + } [Fact] public void GetInquiryById_WithoutId_ThrowsException() diff --git a/API_SQLuedo/TestEF/Service/TestLessonDataService.cs b/API_SQLuedo/TestEF/Service/TestLessonDataService.cs index 386d007..a0de30f 100644 --- a/API_SQLuedo/TestEF/Service/TestLessonDataService.cs +++ b/API_SQLuedo/TestEF/Service/TestLessonDataService.cs @@ -25,7 +25,7 @@ public class TestLessonDataService } [Fact] - public void GetLessons_ReturnsCorrectNumberOfLessons() + public void GetLessons_WithNoneCriteria_ReturnsCorrectNumberOfLessons() { _dbContext.Lessons.Add(new LessonEntity { Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue }); @@ -39,6 +39,86 @@ public class TestLessonDataService 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() diff --git a/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs b/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs index b7a6538..a160d25 100644 --- a/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs +++ b/API_SQLuedo/TestEF/Service/TestParagraphDataService.cs @@ -30,7 +30,7 @@ public class TestParagraphDataService } [Fact] - public void GetParagraphs_ReturnsCorrectNumberOfParagraphs() + public void GetParagraphs_WithoutCriteria_ReturnsCorrectNumberOfParagraphs() { _dbContext.Lessons.Add(_lesson); _dbContext.SaveChanges(); @@ -59,6 +59,223 @@ public class TestParagraphDataService 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() diff --git a/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs b/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs index eb0aa7e..4c2ce59 100644 --- a/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs +++ b/API_SQLuedo/TestEF/Service/TestSuccessDataService.cs @@ -83,7 +83,7 @@ public class TestSuccessDataService } [Fact] - public void GetSuccesses_ReturnsCorrectNumberOfSuccesses() + 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 }; @@ -96,6 +96,81 @@ public class TestSuccessDataService 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() -- 2.36.3