Merge branch 'testTony'
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
commit
0affdd11d4
@ -1,7 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="UserContentModel">
|
<component name="UserContentModel">
|
||||||
<attachedFolders />
|
<attachedFolders>
|
||||||
|
<Path>../../Verax_API_EF</Path>
|
||||||
|
</attachedFolders>
|
||||||
<explicitIncludes />
|
<explicitIncludes />
|
||||||
<explicitExcludes />
|
<explicitExcludes />
|
||||||
</component>
|
</component>
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
@ -0,0 +1,130 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace API_Unit_Test;
|
||||||
|
|
||||||
|
public class DbManagerArticleTests
|
||||||
|
{
|
||||||
|
private LibraryContext GetInMemoryDbContext()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>()
|
||||||
|
.UseInMemoryDatabase(databaseName: "LibraryDbForArticleInMemory" + Guid.NewGuid()) // Ensure a unique name for each test run
|
||||||
|
.Options;
|
||||||
|
var context = new LibraryContext(options);
|
||||||
|
context.Database.EnsureDeleted();
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllArticles_ReturnsArticlesWithSpecifiedOrder()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
context.ArticleSet.AddRange(
|
||||||
|
new ArticleEntity { Title = "C", LectureTime = 30, DatePublished = "2024-03-16" },
|
||||||
|
new ArticleEntity { Title = "A", LectureTime = 10, DatePublished = "2024-03-16"},
|
||||||
|
new ArticleEntity { Title = "B", LectureTime = 20, DatePublished = "2024-03-16" }
|
||||||
|
);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerArticle(context);
|
||||||
|
|
||||||
|
// Act & Assert - Test ordering by title
|
||||||
|
var articlesByTitle = await service.GetAllArticles(0, 10, ArticleOrderCriteria.ByTitle);
|
||||||
|
Assert.Equal("A", articlesByTitle.First().Title);
|
||||||
|
|
||||||
|
// Test ordering by lecture time
|
||||||
|
var articlesByLectureTime = await service.GetAllArticles(0, 10, ArticleOrderCriteria.ByLectureTime);
|
||||||
|
Assert.Equal(10, articlesByLectureTime.First().LectureTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetArticleById_ReturnsCorrectArticle()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expectedArticle = new ArticleEntity { Title = "Test Article", Id = 1 };
|
||||||
|
context.ArticleSet.Add(expectedArticle);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerArticle(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var article = await service.GetArticleById(1);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(article);
|
||||||
|
Assert.Equal("Test Article", article.Title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CreateArticle_AddsNewArticleSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var service = new DbManagerArticle(context);
|
||||||
|
var newArticle = new Article { Title = "New Article" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var createdArticle = await service.CreateArticle(newArticle);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(createdArticle);
|
||||||
|
Assert.Equal("New Article", createdArticle.Title);
|
||||||
|
Assert.Single(context.ArticleSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateArticle_UpdatesExistingArticleSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var originalArticle = new ArticleEntity { Id = 2, Title = "Original Title" };
|
||||||
|
context.ArticleSet.Add(originalArticle);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerArticle(context);
|
||||||
|
var updatedArticle = new Article { Id = 2, Title = "Updated Title" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await service.UpdateArticle(2, updatedArticle);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("Updated Title", result.Title);
|
||||||
|
Assert.Equal(2, result.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteArticle_RemovesArticleSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var articleToDelete = new ArticleEntity { Id = 3, Title = "Delete Me" };
|
||||||
|
context.ArticleSet.Add(articleToDelete);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerArticle(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var deletedArticle = await service.DeleteArticle(3);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(deletedArticle);
|
||||||
|
Assert.DoesNotContain(context.ArticleSet, a => a.Id == 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,204 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace API_Unit_Test;
|
||||||
|
|
||||||
|
public class DbManagerFormulaireTests
|
||||||
|
{
|
||||||
|
private LibraryContext GetInMemoryDbContext()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>()
|
||||||
|
.UseInMemoryDatabase(databaseName: "LibraryDbForFormulaireInMemory" + Guid.NewGuid()) // Unique name for each test execution
|
||||||
|
.Options;
|
||||||
|
var context = new LibraryContext(options);
|
||||||
|
context.Database.EnsureDeleted();
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllForm_ReturnsForms_WithSpecifiedOrder()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
context.FormSet.AddRange(
|
||||||
|
new FormEntity { Theme = "C", DatePublication = "2021-01-01", Link = "http://c.com", UserEntityPseudo = "user1" },
|
||||||
|
new FormEntity { Theme = "A", DatePublication = "2021-01-01", Link = "http://a.com", UserEntityPseudo = "user2" },
|
||||||
|
new FormEntity { Theme = "B", DatePublication = "2021-01-01", Link = "http://b.com", UserEntityPseudo = "user3" }
|
||||||
|
);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
|
||||||
|
// Act & Assert - Test ordering by theme
|
||||||
|
var formsByTheme = await service.GetAllForm(0, 10, FormOrderCriteria.ByTheme);
|
||||||
|
Assert.Equal("A", formsByTheme.First().Theme);
|
||||||
|
|
||||||
|
// Test ordering by link
|
||||||
|
var formsByLink = await service.GetAllForm(0, 10, FormOrderCriteria.ByLien);
|
||||||
|
Assert.Equal("http://a.com", formsByLink.First().Lien);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetById_ReturnsCorrectForm()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var expectedForm = new FormEntity { Id = 1, Theme = "Test Form", Link = "http://test.com", UserEntityPseudo = "user1" };
|
||||||
|
context.FormSet.Add(expectedForm);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var form = await service.GetById(1);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(form);
|
||||||
|
Assert.Equal("Test Form", form.Theme);
|
||||||
|
Assert.Equal("http://test.com", form.Lien);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CreateForm_AddsNewFormSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
var newForm = new Formulaire { Theme = "New Form", Lien = "http://newform.com", UserPseudo = "user1", Date = "2021-01-01"};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var createdForm = await service.CreateForm(newForm);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(createdForm);
|
||||||
|
Assert.Equal("New Form", createdForm.Theme);
|
||||||
|
Assert.Equal("http://newform.com", createdForm.Lien);
|
||||||
|
Assert.Single(context.FormSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateForm_UpdatesExistingFormSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var originalForm = new FormEntity { Id = 2, Theme = "Original Theme", Link = "http://original.com",UserEntityPseudo = "user1", DatePublication = "2021-01-01" };
|
||||||
|
context.FormSet.Add(originalForm);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
var updatedForm = new Formulaire { Id = 2, Theme = "Updated Theme", Lien = "http://updated.com" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await service.UpdateForm(2, updatedForm);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("Updated Theme", result.Theme);
|
||||||
|
Assert.Equal("http://updated.com", result.Lien);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteForm_RemovesFormSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var formToDelete = new FormEntity { Id = 3, Theme = "Delete Me", Link = "http://delete.com", UserEntityPseudo = "user1" };
|
||||||
|
context.FormSet.Add(formToDelete);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var deletedForm = await service.DeleteForm(3);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(deletedForm);
|
||||||
|
Assert.DoesNotContain(context.FormSet, f => f.Id == 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetById_NonExistentId_ReturnsNull()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var form = await service.GetById(999); // Assuming this ID doesn't exist
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Null(form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateForm_NonExistentForm_ReturnsNull()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
var nonExistentForm = new Formulaire { Id = 999, Theme = "Non-Existent Theme", UserPseudo = "user1" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await service.UpdateForm(nonExistentForm.Id, nonExistentForm);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Null(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteForm_NonExistentId_ReturnsNull()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await service.DeleteForm(999); // Assuming this ID doesn't exist
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Null(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllForm_InvalidCriteria_ReturnsAllWithoutOrder()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
context.FormSet.AddRange(
|
||||||
|
new FormEntity { Theme = "C", DatePublication = "2021-01-01", Link = "http://c.com", UserEntityPseudo = "user1"},
|
||||||
|
new FormEntity { Theme = "A", DatePublication = "2021-01-01", Link = "http://a.com", UserEntityPseudo = "user2"},
|
||||||
|
new FormEntity { Theme = "B", DatePublication = "2021-01-01", Link = "http://b.com", UserEntityPseudo = "user3"}
|
||||||
|
);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerFormulaire(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var forms = await service.GetAllForm(0, 10, (FormOrderCriteria)(-1));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(3, forms.Count());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,250 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using DbDataManager;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace API_Unit_Test;
|
||||||
|
|
||||||
|
public class DbManagerUserTests
|
||||||
|
{
|
||||||
|
private LibraryContext GetInMemoryDbContext()
|
||||||
|
{
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>()
|
||||||
|
.UseInMemoryDatabase(databaseName: "LibraryDbInMemory")
|
||||||
|
.Options;
|
||||||
|
var dbContext = new LibraryContext(options);
|
||||||
|
dbContext.Database.EnsureDeleted();
|
||||||
|
dbContext.Database.EnsureCreated();
|
||||||
|
return dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAll_ReturnsCorrectUsers()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
context.UserSet.AddRange(new UserEntity { Pseudo = "user1" }, new UserEntity { Pseudo = "user2" });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var users = await service.GetAll(0, 10, UserOrderCriteria.None);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(2, users.Count());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByPseudo_UserExists_ReturnsUser()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var pseudo = "user1";
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = pseudo });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var user = await service.GetByPseudo(pseudo);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(user);
|
||||||
|
Assert.Equal(pseudo, user.Pseudo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_AddsUserSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
var newUser = new User { Pseudo = "newUser", Nom = "Doe", Prenom = "John", Mdp = "password", Mail = "mail@mail.com", Role = "Admin" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var createdUser = await service.Create(newUser);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(createdUser);
|
||||||
|
Assert.Equal("newUser", createdUser.Pseudo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Update_UpdatesUserSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var pseudo = "userToUpdate";
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = pseudo });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
var updatedInfo = new User { Pseudo = pseudo, Nom = "UpdatedLastName" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var updatedUser = await service.Update(updatedInfo, pseudo);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(updatedUser);
|
||||||
|
Assert.Equal("UpdatedLastName", updatedUser.Nom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Delete_RemovesUserSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var pseudo = "userToDelete";
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = pseudo });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var deletedUser = await service.Delete(pseudo);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(deletedUser);
|
||||||
|
Assert.Empty(context.UserSet.Where(u => u.Pseudo == pseudo));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAllArticleUsers_ReturnsCorrectUsers()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userPseudo = "user1";
|
||||||
|
var articleId = 1L;
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = userPseudo });
|
||||||
|
context.ArticleUserSet.Add(new ArticleUserEntity { UserEntityPseudo = userPseudo, ArticleEntityId = articleId });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var users = await service.GetAllArticleUsers();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Single(users);
|
||||||
|
Assert.Contains(users, u => u.Pseudo == userPseudo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetArticleUser_ReturnsCorrectArticles()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userPseudo = "userWithArticle";
|
||||||
|
var articleId = 2L;
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = userPseudo });
|
||||||
|
context.ArticleSet.Add(new ArticleEntity { Id = articleId, Title = "Test Article" });
|
||||||
|
context.ArticleUserSet.Add(new ArticleUserEntity { UserEntityPseudo = userPseudo, ArticleEntityId = articleId });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var articles = await service.GetArticleUser(userPseudo);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Single(articles);
|
||||||
|
Assert.Contains(articles, a => a.Id == articleId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CreateArticleUser_AddsRelationshipSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userPseudo = "newUserForArticle";
|
||||||
|
var articleId = 3L;
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = userPseudo });
|
||||||
|
context.ArticleSet.Add(new ArticleEntity { Id = articleId, Title = "New Article" });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
var newArticleUser = new ArticleUserEntity { UserEntityPseudo = userPseudo, ArticleEntityId = articleId };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var success = await service.CreateArticleUser(newArticleUser);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(success);
|
||||||
|
Assert.NotNull(context.ArticleUserSet.FirstOrDefault(au => au.UserEntityPseudo == userPseudo && au.ArticleEntityId == articleId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteArticleUser_RemovesRelationshipSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userPseudo = "userToDeleteArticle";
|
||||||
|
var articleId = 4L;
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = userPseudo });
|
||||||
|
context.ArticleSet.Add(new ArticleEntity { Id = articleId });
|
||||||
|
context.ArticleUserSet.Add(new ArticleUserEntity { UserEntityPseudo = userPseudo, ArticleEntityId = articleId });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var success = await service.DeleteArticleUser(userPseudo, articleId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(success);
|
||||||
|
Assert.DoesNotContain(context.ArticleUserSet, au => au.UserEntityPseudo == userPseudo && au.ArticleEntityId == articleId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UpdateArticleUser_UpdatesRelationshipSuccessfully()
|
||||||
|
{
|
||||||
|
using (var context = GetInMemoryDbContext())
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var userPseudo = "userToUpdateArticle";
|
||||||
|
var originalArticleId = 5L;
|
||||||
|
var newArticleId = 6L;
|
||||||
|
context.UserSet.Add(new UserEntity { Pseudo = userPseudo });
|
||||||
|
context.ArticleSet.Add(new ArticleEntity { Id = originalArticleId });
|
||||||
|
context.ArticleSet.Add(new ArticleEntity { Id = newArticleId });
|
||||||
|
context.ArticleUserSet.Add(new ArticleUserEntity { UserEntityPseudo = userPseudo, ArticleEntityId = originalArticleId });
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var service = new DbManagerUser(context);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var success = await service.DeleteArticleUser(userPseudo, originalArticleId);
|
||||||
|
Assert.True(success);
|
||||||
|
|
||||||
|
var updatedArticleUser = new ArticleUserEntity { UserEntityPseudo = userPseudo, ArticleEntityId = newArticleId };
|
||||||
|
success = await service.CreateArticleUser(updatedArticleUser);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.True(success);
|
||||||
|
var relationship = context.ArticleUserSet.FirstOrDefault(au => au.UserEntityPseudo == userPseudo);
|
||||||
|
Assert.NotNull(relationship);
|
||||||
|
Assert.Equal(newArticleId, relationship.ArticleEntityId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue