Début des tests unitaires sur le service ef
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
014c2665bf
commit
0aac223ecd
@ -0,0 +1,105 @@
|
||||
using DbContextLib;
|
||||
using DbDataManager.Service;
|
||||
using Entities;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Model.OrderCriteria;
|
||||
|
||||
namespace TestEF.Service;
|
||||
|
||||
public class TestLessonDataService
|
||||
{
|
||||
private readonly UserDbContext _dbContext;
|
||||
private readonly LessonDataService _lessonDataService;
|
||||
|
||||
public TestLessonDataService()
|
||||
{
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
var options = new DbContextOptionsBuilder<UserDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
_dbContext = new UserDbContext(options);
|
||||
_lessonDataService = new LessonDataService(_dbContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLessons_ReturnsCorrectNumberOfLessons()
|
||||
{
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 3, Title = "Test3", LastPublisher = "Publisher3", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _lessonDataService.GetLessons(1, 2, LessonOrderCriteria.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLessonById_ReturnsCorrectLesson()
|
||||
{
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _lessonDataService.GetLessonById(1);
|
||||
|
||||
Assert.Equal("Test1", result.Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLessonByTitle_ReturnsCorrectLesson()
|
||||
{
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 2, Title = "Test2", LastPublisher = "Publisher2", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _lessonDataService.GetLessonByTitle("Test1");
|
||||
|
||||
Assert.Equal(1, result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateLesson_AddsNewLesson()
|
||||
{
|
||||
var result = _lessonDataService.CreateLesson(1, "Test", "Publisher", DateOnly.MinValue);
|
||||
|
||||
Assert.Equal(1, _dbContext.Lessons.Count());
|
||||
Assert.Equal("Test", result.Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteLesson_RemovesLesson()
|
||||
{
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
_lessonDataService.DeleteLesson(1);
|
||||
|
||||
Assert.Empty(_dbContext.Lessons);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateLesson_UpdatesExistingLesson()
|
||||
{
|
||||
_dbContext.Lessons.Add(new LessonEntity
|
||||
{ Id = 1, Title = "Test1", LastPublisher = "Publisher1", LastEdit = DateOnly.MinValue });
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var updatedLesson = new LessonEntity
|
||||
{ Id = 1, Title = "Updated", LastPublisher = "Publisher", LastEdit = DateOnly.MinValue };
|
||||
var result = _lessonDataService.UpdateLesson(1, updatedLesson);
|
||||
|
||||
Assert.Equal("Updated", result.Title);
|
||||
}
|
||||
}
|
@ -0,0 +1,145 @@
|
||||
using DbContextLib;
|
||||
using DbDataManager.Service;
|
||||
using Entities;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Model.OrderCriteria;
|
||||
|
||||
namespace TestEF.Service;
|
||||
|
||||
public class TestParagraphDataService
|
||||
{
|
||||
private readonly UserDbContext _dbContext;
|
||||
private readonly ParagraphDataService _paragraphDataService;
|
||||
private readonly LessonEntity _lesson;
|
||||
|
||||
public TestParagraphDataService()
|
||||
{
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
var options = new DbContextOptionsBuilder<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_ReturnsCorrectNumberOfParagraphs()
|
||||
{
|
||||
_dbContext.Lessons.Add(_lesson);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 1, ContentContent = "ContentContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||
LessonId = 1
|
||||
});
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 2, ContentContent = "ContentContent2", ContentTitle = "ContentTitl2", Title = "Test2",
|
||||
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||
LessonId = 1
|
||||
});
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 3, ContentContent = "ContentContent3", ContentTitle = "ContentTitle3", Title = "Test3",
|
||||
Content = "Content3", Info = "Info3", Query = "Query3", Comment = "Comment3",
|
||||
LessonId = 1
|
||||
});
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _paragraphDataService.GetParagraphs(1, 2, ParagraphOrderCriteria.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetParagraphById_ReturnsCorrectParagraph()
|
||||
{
|
||||
_dbContext.Lessons.Add(_lesson);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||
LessonId = 1
|
||||
});
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 2, ContentContent = "ContenContent2", ContentTitle = "ContentTitle2", Title = "Test2",
|
||||
Content = "Content2", Info = "Info2", Query = "Query2", Comment = "Comment2",
|
||||
LessonId = 1
|
||||
});
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _paragraphDataService.GetParagraphById(1);
|
||||
|
||||
Assert.Equal("Test1", result.Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateParagraph_AddsNewParagraph()
|
||||
{
|
||||
_dbContext.Lessons.Add(_lesson);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _paragraphDataService.CreateParagraph("ContentTitle", "ContentContent", "Test", "Content", "Info",
|
||||
"Query", "Comment", 1);
|
||||
|
||||
Assert.Equal(1, _dbContext.Paragraphs.Count());
|
||||
Assert.Equal("Test", result.Title);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteParagraph_RemovesParagraph()
|
||||
{
|
||||
_dbContext.Lessons.Add(_lesson);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||
LessonId = 1
|
||||
});
|
||||
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var b = _paragraphDataService.DeleteParagraph(1);
|
||||
|
||||
Assert.Empty(_dbContext.Paragraphs);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateParagraph_UpdatesExistingParagraph()
|
||||
{
|
||||
_dbContext.Lessons.Add(_lesson);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
_dbContext.Paragraphs.Add(new ParagraphEntity
|
||||
{
|
||||
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Test1",
|
||||
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||
LessonId = 1
|
||||
});
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var updatedParagraph = new ParagraphEntity
|
||||
{
|
||||
Id = 1, ContentContent = "ContenContent1", ContentTitle = "ContentTitle1", Title = "Updated",
|
||||
Content = "Content1", Info = "Info1", Query = "Query1", Comment = "Comment1",
|
||||
LessonId = 1
|
||||
};
|
||||
var result = _paragraphDataService.UpdateParagraph(1, updatedParagraph);
|
||||
|
||||
Assert.Equal("Updated", result.Title);
|
||||
}
|
||||
}
|
@ -0,0 +1,174 @@
|
||||
using DbContextLib;
|
||||
using DbDataManager.Service;
|
||||
using Entities;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Model.OrderCriteria;
|
||||
|
||||
namespace TestEF.Service;
|
||||
|
||||
public class TestSuccessDataService
|
||||
{
|
||||
private readonly UserDbContext _dbContext;
|
||||
private readonly SuccessDataService _successDataService;
|
||||
private readonly List<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_ReturnsCorrectNumberOfSuccesses()
|
||||
{
|
||||
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||
var success2 = new SuccessEntity { UserId = 2, InquiryId = 2, IsFinished = false };
|
||||
var success3 = new SuccessEntity { UserId = 3, InquiryId = 3, IsFinished = true };
|
||||
|
||||
_dbContext.Successes.AddRange(success1, success2, success3);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _successDataService.GetSuccesses(1, 2, SuccessOrderCriteria.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSuccessesByUserId_ReturnsCorrectSuccesses()
|
||||
{
|
||||
var success1 = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||
var success2 = new SuccessEntity { UserId = 1, InquiryId = 2, IsFinished = false };
|
||||
|
||||
_dbContext.Successes.AddRange(success1, success2);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _successDataService.GetSuccessesByUserId(1);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
/*
|
||||
[Fact]
|
||||
public void GetSuccessesByInquiryId_ReturnsCorrectSuccesses()
|
||||
{
|
||||
_dbContext.Inquiries.Add(
|
||||
new InquiryEntity
|
||||
{
|
||||
Id = 4,
|
||||
Title = "Title3",
|
||||
Description = "Description3",
|
||||
IsUser = false
|
||||
}
|
||||
);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var success1 = new SuccessEntity { UserId = 1, InquiryId = 4, IsFinished = true };
|
||||
var success2 = new SuccessEntity { UserId = 2, InquiryId = 4, IsFinished = false };
|
||||
|
||||
_dbContext.Successes.AddRange(success1, success2);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var result = _successDataService.GetSuccessesByInquiryId(4);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}*/
|
||||
|
||||
[Fact]
|
||||
public void DeleteSuccess_RemovesSuccess()
|
||||
{
|
||||
var success = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||
|
||||
_dbContext.Successes.Add(success);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
_successDataService.DeleteSuccess(1, 1);
|
||||
|
||||
Assert.Empty(_dbContext.Successes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateSuccess_UpdatesExistingSuccess()
|
||||
{
|
||||
var success = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = true };
|
||||
|
||||
_dbContext.Successes.Add(success);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
var updatedSuccess = new SuccessEntity { UserId = 1, InquiryId = 1, IsFinished = false };
|
||||
var result = _successDataService.UpdateSuccess(1, 1, updatedSuccess);
|
||||
|
||||
Assert.False(result.IsFinished);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateSuccess_AddsNewSuccess()
|
||||
{
|
||||
var result = _successDataService.CreateSuccess(1, 3, true);
|
||||
|
||||
Assert.Single(_dbContext.Successes);
|
||||
Assert.True(result.IsFinished);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue