diff --git a/.drone.yml b/.drone.yml index fb68690..6d4fb92 100644 --- a/.drone.yml +++ b/.drone.yml @@ -37,7 +37,7 @@ steps: - cd Verax_API_EF/ - cd Verax_API_EF/ - dotnet restore Verax_API_EF.sln - - dotnet sonarscanner begin /k:$${project_key} /d:sonar.host.url=$${sonar_host} /d:sonar.coverageReportPaths="coveragereport/SonarQube.xml" /d:sonar.coverage.exclusions="Unit_Test_EF/**","API_Unit_Test/**","API_Tests_Console/**,"Test_Console_EF/**,"DbContextLib/Migrations/**" /d:sonar.login=$${sonar_token} + - dotnet sonarscanner begin /k:$${project_key} /d:sonar.host.url=$${sonar_host} /d:sonar.coverageReportPaths="coveragereport/SonarQube.xml" /d:sonar.coverage.exclusions="Unit_Test_EF/**","API_Unit_Test/**","API_Tests_Console/**","Test_Console_EF/**","DbContextLib/Migrations/**","StubbedContextLib/**" /d:sonar.login=$${sonar_token} - dotnet build Verax_API_EF.sln -c Release --no-restore - dotnet test Verax_API_EF.sln --logger trx --no-restore /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --collect "XPlat Code Coverage" - reportgenerator -reports:"**/coverage.cobertura.xml" -reporttypes:SonarQube -targetdir:"coveragereport" diff --git a/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-shm b/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-shm deleted file mode 100644 index fe9ac28..0000000 Binary files a/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-shm and /dev/null differ diff --git a/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-wal b/Verax_API_EF/Verax_API_EF/API/Entity_FrameWork.Article.db-wal deleted file mode 100644 index e69de29..0000000 diff --git a/Verax_API_EF/Verax_API_EF/Unit_Test_EF/TestsArticleUserEntity.cs b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/TestsArticleUserEntity.cs new file mode 100644 index 0000000..8ed120f --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/TestsArticleUserEntity.cs @@ -0,0 +1,70 @@ +using DbContextLib; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace TestsUnitaires; + +public class TestsArticleUserEntity +{ + [Fact] + public void Add_Test() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new LibraryContext(options)) + { + context.Database.EnsureCreated(); + ArticleEntity a1 = new ArticleEntity { Title = "Breaking News Elisabeth 2 Died", Description = "The queen of England died today at the age of 95", DatePublished = "2022-02-06", LectureTime = 2, Author = "Tom Smith" }; + ArticleEntity a2 = new ArticleEntity { Title = "The new iPhone 15", Description = "The new iPhone 15 is out and it's the best phone ever", DatePublished = "2022-02-06", LectureTime = 3, Author = "Tom Smith" }; + ArticleEntity a3 = new ArticleEntity { Title = "M&M's new recipe", Description = "M&M's new recipe is out and it's the best chocolate ever", DatePublished = "2022-02-06", LectureTime = 1, Author = "M&M's Red" }; + + UserEntity u1 = new UserEntity + { + Pseudo = "Blizzard", + Prenom = "Louis", + Nom = "Laborie", + Mail = "he@gmail.com", + Mdp = "1234", + Role = "Admin" + }; + + ArticleUserEntity au1 = new ArticleUserEntity + { + UserEntityPseudo = "Blizzard", + ArticleEntityId = 1 + }; + + ArticleUserEntity au2 = new ArticleUserEntity + { + UserEntityPseudo = "Blizzard", + ArticleEntityId = 2 + }; + + ArticleUserEntity au3 = new ArticleUserEntity + { + UserEntityPseudo = "Blizzard", + ArticleEntityId = 3 + }; + + context.ArticleSet.Add(a1); + context.ArticleSet.Add(a2); + context.ArticleSet.Add(a3); + context.UserSet.Add(u1); + context.ArticleUserSet.Add(au1); + context.ArticleUserSet.Add(au2); + context.ArticleUserSet.Add(au3); + context.SaveChanges(); + + Assert.Equal(3, context.ArticleSet.Count()); + Assert.Equal(1, context.ArticleUserSet.First().ArticleEntityId); + connection.Close(); + } + } + +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/Unit_Test_EF/TestsFormEntity.cs b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/TestsFormEntity.cs new file mode 100644 index 0000000..a7cd1f8 --- /dev/null +++ b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/TestsFormEntity.cs @@ -0,0 +1,200 @@ +using DbContextLib; +using Entities; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +namespace Tests; + +public class TestsFormEntity +{ + [Fact] + public void Add_Test() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new LibraryContext(options)) + { + context.Database.EnsureCreated(); + UserEntity u1 = new UserEntity + { + Pseudo = "Blizzard", + Prenom = "Louis", + Nom = "Laborie", + Mail = "he@gmail.com", + Mdp = "1234", + Role = "Admin" + }; + FormEntity f1 = new FormEntity + { + Id = 1, + Theme = "theme", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + FormEntity f2 = new FormEntity + { + Id = 2, + Theme = "theme", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + FormEntity f3 = new FormEntity + { + Id = 3, + Theme = "theme", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + context.UserSet.Add(u1); + context.FormSet.Add(f1); + context.FormSet.Add(f2); + context.FormSet.Add(f3); + context.SaveChanges(); + + Assert.Equal(3, context.FormSet.Count()); + Assert.Equal("Blizzard", context.FormSet.First().UserEntityPseudo); + connection.Close(); + } + } + + [Fact] + public void Modify_Test() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + using (var context = new LibraryContext(options)) + { + context.Database.EnsureCreated(); + UserEntity u1 = new UserEntity + { + Pseudo = "Blizzard", + Prenom = "Louis", + Nom = "Laborie", + Mail = "he@gmail.com", + Mdp = "1234", + Role = "Admin" + }; + FormEntity f1 = new FormEntity + { + Id = 1, + Theme = "theme", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + FormEntity f2 = new FormEntity + { + Id = 2, + Theme = "theme2", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + FormEntity f3 = new FormEntity + { + Id = 3, + Theme = "theme3", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + context.UserSet.Add(u1); + context.FormSet.Add(f1); + context.FormSet.Add(f2); + context.FormSet.Add(f3); + context.SaveChanges(); + + var form = context.FormSet.First(f => f.Id == 1); + form.Theme = "Politique"; + context.SaveChanges(); + string formRemove = "theme"; + string formNew = "Politique"; + Assert.Equal(1, context.FormSet.Count(f => f.Theme == formNew)); + Assert.Equal(0, context.FormSet.Count(f => f.Theme == formRemove)); + connection.Close(); + + } + } + + [Fact] + public void Remove_Test() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + using (var context = new LibraryContext(options)) + { + context.Database.EnsureCreated(); + UserEntity u1 = new UserEntity + { + Pseudo = "Blizzard", + Prenom = "Louis", + Nom = "Laborie", + Mail = "he@gmail.com", + Mdp = "1234", + Role = "Admin" + }; + FormEntity f1 = new FormEntity + { + Id = 1, + Theme = "theme", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + FormEntity f2 = new FormEntity + { + Id = 2, + Theme = "theme2", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + FormEntity f3 = new FormEntity + { + Id = 3, + Theme = "theme3", + DatePublication = "date", + Link = "link", + UserEntityPseudo = "Blizzard", + User = u1 + }; + context.UserSet.Add(u1); + context.FormSet.Add(f1); + context.FormSet.Add(f2); + context.FormSet.Add(f3); + context.SaveChanges(); + + Assert.Equal(3, context.FormSet.Count()); + var form = context.FormSet.First(f => f.Theme == "theme2"); + context.Remove(form); + context.SaveChanges(); + Assert.Equal(2, context.FormSet.Count()); + Assert.Equal(0, context.FormSet.Count(f => f.Theme == "theme2")); + connection.Close(); + } + } +} \ No newline at end of file diff --git a/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj index 8cbb5d2..2e0344d 100644 --- a/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj +++ b/Verax_API_EF/Verax_API_EF/Unit_Test_EF/Unit_Test_EF.csproj @@ -1,4 +1,4 @@ - + net8.0