@ -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,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…
Reference in new issue