From 8bbe15057121920098a51331ae27716b0b5823e1 Mon Sep 17 00:00:00 2001 From: Damien Nortier Date: Fri, 15 Mar 2024 21:20:43 +0100 Subject: [PATCH] =?UTF-8?q?feat=20:=20d=C3=A9but=20des=20tests=20unitaires?= =?UTF-8?q?=20version=20console?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebApi/TestEntityManagers/Program.cs | 379 ++++++++++++++++++ .../TestEntityManagers.csproj | 25 ++ WebApi/WebApi.sln | 8 +- 3 files changed, 411 insertions(+), 1 deletion(-) create mode 100644 WebApi/TestEntityManagers/Program.cs create mode 100644 WebApi/TestEntityManagers/TestEntityManagers.csproj diff --git a/WebApi/TestEntityManagers/Program.cs b/WebApi/TestEntityManagers/Program.cs new file mode 100644 index 0000000..46b718a --- /dev/null +++ b/WebApi/TestEntityManagers/Program.cs @@ -0,0 +1,379 @@ +using DbConnectionLibrairie; +using Entities; +using EntityManagers; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using OrderCriterias; +using StubbedDbContextLibrary; + +var connection = new SqliteConnection("DataSource=:memory:"); + +connection.Open(); + +var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; +using var context = new StubbedDbContext(); + +/// +/// test of the 'addAnswer' method of an AnswerManager +/// +async Task TestAddAnswer() +{ + var connection = new SqliteConnection("DataSource=:memory:"); + + connection.Open(); + + var opt = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + using (var dbContext = new MyDbContext(opt)) + { + await dbContext.Database.EnsureCreatedAsync(); + var manager = new AnswerEntityManager(dbContext); + var answerToAdd = new AnswerEntity { Id = 1, Content = "chateîgne" }; + var a = await manager.addAnswer(answerToAdd); + // 1) normal insertion + // WF : a is the same + // as answerToAdd + if (a.Content == answerToAdd.Content && a.Id == answerToAdd.Id) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("test 1 AnswerEntityManager.addAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("test 1 AnswerEntityManager.addAnswer KO"); + Console.ResetColor(); + } + + answerToAdd = new AnswerEntity { Id = 5, Content = "damien" }; + a = await manager.addAnswer(answerToAdd); + // 2) with a random id greater than 0 + // WF : 'a' content is equal to the + // content of 'answerToAdd' + // and since it's the second answer + // that we add, his id equal 2 + if (a.Content == answerToAdd.Content && a.Id == 2) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("test 2 AnswerEntityManager.addAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("test 2 AnswerEntityManager.addAnswer KO"); + Console.ResetColor(); + } + + answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" }; + a = await manager.addAnswer(answerToAdd); + // 3) with a content that we already have added + // WF : the function return the answer which already + // have the same content so the content of 'a' is "châteigne" + // and his id is 1 + if (a.Content == "chateîgne" && a.Id == 1) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("test 3 AnswerEntityManager.addAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("test 3 AnswerEntityManager.addAnswer KO"); + Console.ResetColor(); + } + + answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" }; + a = await manager.addAnswer(answerToAdd); + // 3) with a content that we already have added + // but in upperCase instead of lowerCase + // WF : the function return the answer which + // already have the same content so the content + // of 'a' is "chateîgne" and his id is 1 + if (a.Content == "chateîgne" && a.Id == 1) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("test 4 AnswerEntityManager.addAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("test 4 AnswerEntityManager.addAnswer KO"); + Console.ResetColor(); + } + } +} + +/// +/// test of the 'supprimerAnswer' method of an AnswerManager +/// +async Task TestRemoveAnswerById(MyDbContext context) +{ + var mgr = new AnswerEntityManager(context); + 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, + if(a == null) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("test 1 AnswerEntityManager.RemoveAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("test 1 AnswerEntityManager.RemoveAnswer KO"); + Console.ResetColor(); + } + + a = await mgr.removeAnswer(context.Answers.Select(a => a.Id).Min()); + // 1) with an id that is allowed 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" + if (a.Content == context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content + && a.Id == context.Answers.Select(a => a.Id).Min()) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("test 2 AnswerEntityManager.RemoveAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("test 2 AnswerEntityManager.RemoveAnswer KO"); + Console.ResetColor(); + } +} + +/// +/// test of the 'getNbElement' method of an AnswerManager +/// +async Task TestGetNbAnswers(MyDbContext context) +{ + await context.Database.EnsureCreatedAsync(); + var mgr = new AnswerEntityManager(context); + if (mgr.getNbAnswers() != 0 || fakeAnswers.datas == null) throw new Exception("not ready"); + int count = 0; + foreach (var answer in fakeAnswers.datas) + { + await mgr.addAnswer(answer); + count++; + if (mgr.getNbAnswers() == count) + { // ok, it's incremented + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"test {count} AnswerEntityManager.addAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"test {count} AnswerEntityManager.addAnswer KO"); + Console.ResetColor(); + } + } +} + +/// +/// member data for the 'TestGetAnswer' test method +/// +/// a set of all inline datas for the test method +IEnumerable TestGetAnswer_Datas() +{ + var datas = fakeAnswers.datas; + int max = 0; + List tmp = new List(); + foreach (var item in datas) + { + tmp.Add(new Object[] { item.Id, item }); + if (max < item.Id) max = item.Id; + } + tmp.Add(new Object[] { max + 1, null! }); + return tmp; +} + +/// +/// test of the 'getAnswer' method of an AnswerManager +/// +/// identifiant of the answer to get +/// answer expected +/// nothing but a Task +async Task TestGetAnswer(MyDbContext context, int numTest, int id, AnswerEntity? waiting) +{ + await context.Database.EnsureCreatedAsync(); + var mgr = new AnswerEntityManager(context); + + var tmp = await mgr.getAnswer(id + 2); + if (tmp?.Content == waiting?.Content) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"test {numTest} AnswerEntityManager.addAnswer OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"test {numTest} AnswerEntityManager.addAnswer KO"); + Console.ResetColor(); + } +} + +/// +/// member data for the 'TestGetAnswer' test method +/// +/// a set of all inline datas for the test method +static IEnumerable TestGetSomeAnswers_Datas() +{ + var datas = fakeAnswers.datas; + var count = datas.Count(); + // remind that to add answers, we haven't ordered this collection + // but we just have done a foreach + + return new List + { + // 1 : 0 answer from the page 1 then, -1 answer from this page + // WF : count < 1 so we got null + new object?[] { 1, 0, null }, + new object?[] { 1, -1, null }, + // 2 : 1 answers from the page 0 then, 1 answers from the page -1 + // WF : page < 1 so we got null + new object?[] { 0, 1, null }, + new object?[] { -1, 1, null }, + // 3 : 10 answers from the page 1 + // WF : the first 10 element of datas + new object?[] { 1, 10, datas.Take(10) }, + // 4 : 10 elements from the page 1 order by id + // WF : the first 10 element of datas order by id + new object?[] { 1, 10, datas.OrderBy(e => e.Id).Take(10), AnswerOrderCriteria.ById }, + // 5 : 10 elements from the page 1 order by content + // WF : the first 10 element of datas order by content + new object?[] { 1, 10, datas.OrderBy(e => e.Content).Take(10), AnswerOrderCriteria.ById }, + // 6 : repeat 3, 4 and 5 with the page 2 + // WF : the 10 next answer from each case + new object?[] { 2, 10, datas.Skip(10).Take(10) }, + new object?[] { 1, 10, datas.OrderBy(e => e.Id).Skip(10).Take(10), AnswerOrderCriteria.ById }, + new object?[] { 1, 10, datas.OrderBy(e => e.Content).Skip(10).Take(10), AnswerOrderCriteria.ByContent }, + // 7 : count/4 elements from the page 4 + // WF : the lasts count/4 elements of datas + new object?[] { 4, count / 4, datas.TakeLast(count / 4) }, + // 8 : count/4 elements from the page 5 + // WF : null since num (4) >= count / (count/4) + new object?[] { 5, count / 4, null }, + // 9 : 10 elements from the page 4 + // WF : since there's only 31 elements in fake datas, + // we got the last element + new object?[] { 4, 10, datas.TakeLast(1) } + }; +} + +/// +/// test of the 'getAnswer' method of an AnswerManager +/// +/// the page number +/// page elements number +/// set of answers expected +/// the order criteria +/// nothing but a Task +async Task TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, IEnumerable? waiting, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById) +{ + await context.Database.EnsureCreatedAsync(); + var mgr = new AnswerEntityManager(context); + + var nbPages = count == 0 ? -1 : mgr.getNbAnswers() / count; + var tmp = await mgr.getAnswers(num, count, orderCriteria); + if(tmp.nbPages == nbPages){ + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"test {numTest} AnswerEntityManager.getSomeAnswers OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"test {numTest} AnswerEntityManager.getSomeAnswers KO"); + Console.ResetColor(); + } + + if (waiting == null && tmp.answers == null) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers OK"); + Console.ResetColor(); + } + else if (waiting != null && tmp.answers != null) + { + if (waiting?.Count() == 0) + { + if(tmp.answers == null) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers KO"); + Console.ResetColor(); + } + } + else + { + var nbFautes = 0; + for (var i = 0; i < count; i++) + { + var e1 = waiting.ElementAt(i); + var e2 = tmp.answers.ElementAt(i); + if(e2.Content != e1.Content || e2.IdQuestion != e1.IdQuestion) nbFautes++; + } + if (nbFautes == 0) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers OK"); + Console.ResetColor(); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers KO"); + Console.ResetColor(); + } + } + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"test {numTest + 1} AnswerEntityManager.getSomeAnswers KO"); + Console.ResetColor(); + } +} +{ + await TestAddAnswer(); + await TestRemoveAnswerById(context); + await TestGetNbAnswers(context); + var tmp = TestGetAnswer_Datas(); + for (int i = 0; i < tmp.Count(); i++) + { + await TestGetAnswer(context, i, (int)tmp.ElementAt(i).ElementAt(0)!, (AnswerEntity)tmp.ElementAt(i).ElementAt(1)!); + } + tmp = TestGetSomeAnswers_Datas(); + for (int i = 0; i < tmp.Count(); i++) + { + await TestGetSomeAnswers(context, i, (int)tmp.ElementAt(i).ElementAt(0)!, (int)tmp.ElementAt(i).ElementAt(1)!, + (IEnumerable?)tmp.ElementAt(i).ElementAtOrDefault(2), + (AnswerOrderCriteria)(tmp.ElementAt(i).ElementAtOrDefault(i) ?? AnswerOrderCriteria.ById) + ); + } +} + + + +// removeAnswer by element + +// updateAnswer \ No newline at end of file diff --git a/WebApi/TestEntityManagers/TestEntityManagers.csproj b/WebApi/TestEntityManagers/TestEntityManagers.csproj new file mode 100644 index 0000000..3871665 --- /dev/null +++ b/WebApi/TestEntityManagers/TestEntityManagers.csproj @@ -0,0 +1,25 @@ + + + + Exe + net8.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/WebApi/WebApi.sln b/WebApi/WebApi.sln index 86d27ca..5ad8e61 100644 --- a/WebApi/WebApi.sln +++ b/WebApi/WebApi.sln @@ -29,7 +29,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "WebApi\WebApi.csp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FakeDatas", "FakeDatas\FakeDatas.csproj", "{3C930FB5-2F1E-463E-978B-F0B5C7A58307}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedDbContextLibrary", "StubbedDbContextLibrary\StubbedDbContextLibrary.csproj", "{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedDbContextLibrary", "StubbedDbContextLibrary\StubbedDbContextLibrary.csproj", "{6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEntityManagers", "TestEntityManagers\TestEntityManagers.csproj", "{F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -93,6 +95,10 @@ Global {6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}.Debug|Any CPU.Build.0 = Debug|Any CPU {6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}.Release|Any CPU.ActiveCfg = Release|Any CPU {6A0D9093-EAA4-45A0-8813-ED5BB4E1EA3E}.Release|Any CPU.Build.0 = Release|Any CPU + {F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F0758B69-FD20-4BC4-BE0F-5868DB8BF74E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE