UnitTestsEF #44

Merged
clement.chieu merged 6 commits from UnitTestsEF into master 1 year ago

@ -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)

@ -23,10 +23,12 @@ public class LessonDataService : ILessonService<LessonEntity>
{
page = 1;
}
if (number <= 0)
{
number = 10;
}
IQueryable<LessonEntity> query = DbContext.Lessons.Skip((page - 1) * number).Take(number);
switch (orderCriteria)
{
@ -73,64 +75,41 @@ public class LessonDataService : ILessonService<LessonEntity>
public bool DeleteLesson(int id)
{
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
{
var lessonEntity = context.Lessons.FirstOrDefault(l => l.Id == id);
var lessonEntity = DbContext.Lessons.FirstOrDefault(l => l.Id == id);
if (lessonEntity == null)
{
return false;
}
context.Lessons.Remove(lessonEntity);
context.SaveChangesAsync();
DbContext.Lessons.Remove(lessonEntity);
DbContext.SaveChanges();
return true;
}
}
public LessonEntity UpdateLesson(int id, LessonEntity lesson)
{
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
{
var updatingLesson = context.Lessons.FirstOrDefault(l => l.Id == id);
var updatingLesson = DbContext.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));
}
updatingLesson.Title = lesson.Title;
updatingLesson.LastPublisher = lesson.LastPublisher;
updatingLesson.LastEdit = lesson.LastEdit;
context.SaveChangesAsync();
DbContext.SaveChanges();
return updatingLesson;
}
}
public LessonEntity CreateLesson(int id, string title, string lastPublisher, DateOnly lastEdit)
{
using (UserDbContext context = new UserDbContext(new DbContextOptions<UserDbContext>()))
{
DateTime date = DateTime.Now;
var newLessonEntity = new LessonEntity()
{
Id = id > 0 && DbContext.Lessons.All(l => l.Id != id) ? id : 0,
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();
DbContext.Lessons.Add(newLessonEntity);
DbContext.SaveChanges();
return newLessonEntity;
}
}
}

@ -85,7 +85,7 @@ public class ParagraphDataService : IParagraphService<ParagraphEntity>
}
DbContext.Paragraphs.Remove(paragraphEntity);
DbContext.SaveChangesAsync();
DbContext.SaveChanges();
return true;
}

@ -34,7 +34,12 @@
return $"{Id}\t{Username}\t{Email}\t{IsAdmin}";
}
public override bool Equals(object obj)
public bool Equals(UserDto? other)
{
return (this.Id == other?.Id);
}
public override bool Equals(object? obj)
{
if (object.ReferenceEquals(obj, null))
{
@ -54,11 +59,6 @@
return this.Equals(obj as UserDto);
}
public bool Equals(UserDto other)
{
return (this.Id == other.Id);
}
public override int GetHashCode()
{
return Id;

@ -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;
}
}

@ -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
};
}

@ -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());
}
}
}

@ -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);
}

@ -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<ArgumentException>(() =>
{
BlackList blackList = new BlackList(null, DateOnly.FromDateTime(DateTime.Now));
});
}
[Fact]
void TestConstructorWithAnteriorDate()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
BlackList blackList = new BlackList("example@email.com", DateOnly.FromDateTime(DateTime.Parse("2024/01/01 00:00:00")));
});
}
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}
}

@ -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);
}
}

@ -0,0 +1,198 @@
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<UserDbContext>()
.UseSqlite(connection)
.Options;
_dbContext = new UserDbContext(options);
_inquiryDataService = new InquiryDataService(_dbContext);
}
[Fact]
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 });
_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 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()
{
_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 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()
{
_dbContext.Inquiries.Add(new InquiryEntity { Id = 1, Title = "Test1", Description = "Desc1", IsUser = true });
_dbContext.SaveChanges();
Assert.Throws<ArgumentException>(() => _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<ArgumentException>(() => _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<ArgumentException>(() => _inquiryDataService.UpdateInquiry(2, updatedInquiry));
}
}

@ -0,0 +1,185 @@
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<UserDbContext>()
.UseSqlite(connection)
.Options;
_dbContext = new UserDbContext(options);
_lessonDataService = new LessonDataService(_dbContext);
}
[Fact]
public void GetLessons_WithNoneCriteria_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 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()
{
_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);
}
}

@ -0,0 +1,362 @@
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<UserDbContext>()
.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_WithoutCriteria_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 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()
{
_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);
}
}

@ -0,0 +1,249 @@
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<UserEntity> _users = new();
private readonly List<InquiryEntity> _inquiries = new();
public TestSuccessDataService()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<UserDbContext>()
.UseSqlite(connection)
.Options;
_dbContext = new UserDbContext(options);
_successDataService = new SuccessDataService(_dbContext);
_users.AddRange(new List<UserEntity>
{
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<InquiryEntity>
{
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_WithoutCriteria_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 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()
{
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);
}
}

@ -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<UserDbContext>()
.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<ArgumentException>(() => _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<ArgumentException>(() => _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<ArgumentException>(() => _userDataService.UpdateUser(2, updatedUser));
}
}
Loading…
Cancel
Save