From 62b3993325f41dca10e3c650ec7b40f69e2e9a6e Mon Sep 17 00:00:00 2001 From: Damien Nortier Date: Fri, 15 Mar 2024 21:22:25 +0100 Subject: [PATCH] =?UTF-8?q?tests=20unitaires=20qui=20seront=20probablement?= =?UTF-8?q?=20abandonn=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractUnitTestEM.cs | 49 ------------ .../UnitTestAnswerManager.cs | 79 +++++++++++++------ 2 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 WebApi/UnitTestsEntityManagers/AbstractUnitTestEM.cs diff --git a/WebApi/UnitTestsEntityManagers/AbstractUnitTestEM.cs b/WebApi/UnitTestsEntityManagers/AbstractUnitTestEM.cs deleted file mode 100644 index 722a36f..0000000 --- a/WebApi/UnitTestsEntityManagers/AbstractUnitTestEM.cs +++ /dev/null @@ -1,49 +0,0 @@ -using DbConnectionLibrairie; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using StubbedDbContextLibrary; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database; - -namespace UnitTestsEntityManagers -{ - /// - /// an abstract class to init the dbContext - /// (every unit tests need to implement it) - /// - public abstract class AbstractUnitTestEM - { - protected DbContextOptions opt; - - /// - /// constructor of the class : - /// initialise the database context - /// - public AbstractUnitTestEM() - { - - } - - protected void creerOpt(string provider, string connectionString) - { - if(provider == "sqlite") - { - var connection = new SqliteConnection("DataSource=:memory:"); - - connection.Open(); - - opt = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - } - else - { - throw new Exception("provider unknown"); - } - } - } -} diff --git a/WebApi/UnitTestsEntityManagers/UnitTestAnswerManager.cs b/WebApi/UnitTestsEntityManagers/UnitTestAnswerManager.cs index d9ca9f2..39968f9 100644 --- a/WebApi/UnitTestsEntityManagers/UnitTestAnswerManager.cs +++ b/WebApi/UnitTestsEntityManagers/UnitTestAnswerManager.cs @@ -1,20 +1,28 @@ using DbConnectionLibrairie; using Entities; using EntityManagers; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; using OrderCriterias; using StubbedDbContextLibrary; namespace UnitTestsEntityManagers { - public class UnitTestAnswerManager : AbstractUnitTestEM + public class UnitTestAnswerManager { - /// /// test of the 'ajouterAnswer' method of an AnswerManager /// [Fact] public async void TestAddAnswer() { + var connection = new SqliteConnection("DataSource=:memory:"); + + connection.Open(); + + var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; using (var context = new MyDbContext(opt)) { await context.Database.EnsureCreatedAsync(); @@ -35,7 +43,7 @@ namespace UnitTestsEntityManagers // and since it's the second answer // that we add, his id equal 2 Assert.Equal(answerToAdd.Content, a.Content); - Assert.Equal((uint)2, a.Id); + Assert.Equal((int)2, a.Id); answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" }; a = await manager.addAnswer(answerToAdd); @@ -44,7 +52,7 @@ namespace UnitTestsEntityManagers // have the same content so the content of 'a' is "châteigne" // and his id is 1 Assert.Equal("chateîgne", a.Content); - Assert.Equal((uint)1, a.Id); + Assert.Equal((int)1, a.Id); answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" }; a = await manager.addAnswer(answerToAdd); @@ -54,7 +62,7 @@ namespace UnitTestsEntityManagers // already have the same content so the content // of 'a' is "chateîgne" and his id is 1 Assert.Equal("chateîgne", a.Content); - Assert.Equal((uint)1, a.Id); + Assert.Equal((int)1, a.Id); } } @@ -64,46 +72,52 @@ namespace UnitTestsEntityManagers [Fact] public async Task TestRemoveAnswerById() { - using (var context = new StubbedDbContext(opt)) + var connection = new SqliteConnection("DataSource=:memory:"); + + connection.Open(); + + var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + using (var context = new StubbedDbContext()) { await context.Database.EnsureCreatedAsync(); var mgr = new AnswerEntityManager(context); - // remember that we have only 2 answers in the database : - // 1) (1, "châteigne") - // 2) (2, "damien") - var a = await mgr.removeAnswer(3); + var a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Max() + 1); // 1) with an id greater or equal // to the number of element // WF : it don't works so 'a' is null, Assert.Null(a); - a = await mgr.removeAnswer(1); + a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Min()); // 1) with an id that belongs to an answer // WF : it works so 'a' is not null, // and since we've delete the answer with // the id 1, the content is "châteigne" Assert.NotNull(a); - Assert.Equal((uint)1, a.Id); - Assert.Equal("châteigne", a.Content); + Assert.Equal(context.Answers.Select(a => a.Id).Min(), a.Id); + Assert.Equal(context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content, a.Content); a = await mgr.removeAnswer(2); } } - // /!\ WARNING : since there was 2 answers added to the base, - // id index while now start at 3 (even though we've delete those) - /// /// test of the 'getNbElement' method of an AnswerManager /// [Fact] public async Task TestGetNbElement() { - using (var context = new StubbedDbContext(opt)) + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + using (var context = new StubbedDbContext()) { await context.Database.EnsureCreatedAsync(); var mgr = new AnswerEntityManager(context); - Assert.Equal(0, mgr.getNbElements()); // just to be sure + Assert.Equal(0, mgr.getNbAnswers()); // just to be sure Assert.NotNull(fakeAnswers.datas); // just to be sure @@ -112,7 +126,7 @@ namespace UnitTestsEntityManagers { await mgr.addAnswer(answer); count++; - Assert.Equal(count, mgr.getNbElements()); // ok, it's incremented + Assert.Equal(count, mgr.getNbAnswers()); // ok, it's incremented } } } @@ -124,13 +138,13 @@ namespace UnitTestsEntityManagers public static IEnumerable TestGetAnswer_Datas() { var datas = fakeAnswers.datas; - uint max = 0; + int max = 0; foreach (var item in datas) { yield return new Object[] { item.Id, item }; if(max < item.Id) max = item.Id; } - yield return new Object[] { max+1, null }; + yield return new Object[] { max+1, null! }; } /// /// test of the 'getAnswer' method of an AnswerManager @@ -140,9 +154,17 @@ namespace UnitTestsEntityManagers /// nothing but a Task [Theory] [MemberData(nameof(TestGetAnswer_Datas))] - public async Task TestGetAnswer(uint id, AnswerEntity? waiting) + public async Task TestGetAnswer(int id, AnswerEntity? waiting) { - using (var context = new StubbedDbContext(opt)) + var connection = new SqliteConnection("DataSource=:memory:"); + + connection.Open(); + + var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new StubbedDbContext()) { await context.Database.EnsureCreatedAsync(); var mgr = new AnswerEntityManager(context); @@ -217,13 +239,20 @@ namespace UnitTestsEntityManagers [MemberData(nameof(TestGetSomeAnswers_Datas))] public async Task TestGetSomeAnswers(int num, int count, IEnumerable? waiting, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById) { + var connection = new SqliteConnection("DataSource=:memory:"); + + connection.Open(); + + var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; - using (var context = new StubbedDbContext(opt)) + using (var context = new StubbedDbContext()) { await context.Database.EnsureCreatedAsync(); var mgr = new AnswerEntityManager(context); - var nbPages = count == 0 ? -1 : mgr.getNbElements() / count; + var nbPages = count == 0 ? -1 : mgr.getNbAnswers() / count; var tmp = await mgr.getAnswers(num, count, orderCriteria); Assert.Equal(nbPages, tmp.nbPages);