using DataManagers; using DbConnectionLibrairie; using Entities; using EntityManagers; using ExtensionsClassLibrairie; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Model; using OrderCriterias; using StubbedDbContextLibrary; // for each test, 'WF' mean 'waiting for' /// /// test of the 'addAnswer' method of an AnswerManager /// void TestAddAnswer() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var opt = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; using (var context = new MyDbContext(opt)) { context.Database.EnsureCreated(); var manager = new AnswerDataManager(new AnswerEntityManager(context)); var answerToAdd = new Answer ("chateîgne", null, 1); var a = manager.addAnswer(answerToAdd).Result; // 1) normal insertion // WF : a is the same // as answerToAdd if (a.Content == answerToAdd.Content && a.Id == answerToAdd.Id && context.Answers.Single(e => e.Id == a.Id).Content == a.Content) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("test 1 AnswerDataManager.addAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 1 AnswerDataManager.addAnswer KO"); Console.ResetColor(); Console.WriteLine($"what we have : {a.Id} : {a.Content}"); Console.WriteLine($"WF : {answerToAdd.Id} : {answerToAdd.Content}"); } answerToAdd = new Answer("damien", null, 5); a = manager.addAnswer(answerToAdd).Result; // 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 && context.Answers.Single(e => e.Id == 2).Content == a.Content) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("test 2 AnswerDataManager.addAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 2 AnswerDataManager.addAnswer KO"); Console.ResetColor(); Console.WriteLine($"what we have : {a.Id} : {a.Content}"); Console.WriteLine($"WF : 2 : {answerToAdd.Content}"); } answerToAdd = new Answer("chateîgne", null, 7); a = manager.addAnswer(answerToAdd).Result; // 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 AnswerDataManager.addAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 3 AnswerDataManager.addAnswer KO"); Console.ResetColor(); Console.WriteLine($"what we have : {a.Id} : {a.Content}"); Console.WriteLine($"WF : 1 : {answerToAdd.Content}"); } answerToAdd = new Answer("CHATEÎGNE", null, 7); a = manager.addAnswer(answerToAdd).Result; // 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 AnswerDataManager.addAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 4 AnswerDataManager.addAnswer KO"); Console.ResetColor(); Console.WriteLine($"what we have : {a.Id} : {a.Content}"); Console.WriteLine($"WF : 1 : {answerToAdd.Content.ToLower()}"); } foreach (var item in context.Answers) context.Answers.Remove(item); // we remove all database answers context.SaveChanges(); } } /// /// test of the 'supprimerAnswer' method of an AnswerManager /// void TestRemoveAnswerById(MyDbContext context) { var mgr = new AnswerDataManager(new AnswerEntityManager(context)); var a = mgr.removeAnswer(context.Answers.Select(a => a.Id).Max() + 1).Result; // 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 AnswerDataManager.removeAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 1 AnswerDataManager.removeAnswer KO"); Console.ResetColor(); Console.WriteLine("what we have : " + a == null ? "null" : $"{a.Id} : {a.Content}"); Console.WriteLine("WF : null"); } var answer = context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()); a = mgr.removeAnswer(context.Answers.Select(a => a.Id).Min()).Result; // 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 != null && a.Content == answer.Content && a.Id == answer.Id && a.Id != context.Answers.Select(a => a.Id).Min() /* <=> he is removed */) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("test 2 AnswerDataManager.removeAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 2 AnswerDataManager.removeAnswer KO"); Console.ResetColor(); Console.WriteLine("what we have : " + a == null ? "null" : $"{a!.Id} : {a!.Content}"); Console.WriteLine("WF : " + answer == null ? "null" : $"{answer.Id} : {answer.Content}"); } } /// /// test of the 'getNbElement' method of an AnswerManager /// void TestGetNbAnswers() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var opt = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; using (var context = new MyDbContext(opt)) { context.Database.EnsureCreated(); var mgr = new AnswerDataManager(new AnswerEntityManager(context)); int count = context.Answers.CountAsync().Result; int nb = 0; var list = new List() { new Answer ("Ajout 1" ), new Answer ("Ajout 2" ), new Answer ("Ajout 3" ), new Answer ("Ajout 4" ), new Answer ("Ajout 5" ), new Answer ("Ajout 6" ), new Answer ("Ajout 7" ), new Answer ("Ajout 8" ), new Answer ("Ajout 9" ), new Answer ("Ajout 10" ) }; foreach (var answer in list) { var tmp = mgr.addAnswer(answer).Result; count++; nb++; if (mgr.getNbAnswers() == count) { // ok, it's incremented Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {nb} AnswerDataManager.getNbAnswers OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"test {nb} AnswerDataManager.getNbAnswers KO"); Console.ResetColor(); Console.WriteLine($"what we have : {mgr.getNbAnswers()}"); Console.WriteLine($"WF : {count}"); } } } } /// /// member data for the 'TestGetAnswer' test method /// /// a set of all inline datas for the test method (int id, Answer? waiting)[] TestGetAnswer_Datas(MyDbContext context) { IEnumerable datas = new List(); foreach(var answer in context.Answers) { datas.Append(answer.ToModel()); } int max = 0; (int id, Answer? waiting)[] tmp = new (int id, Answer? waiting)[] { }; foreach (var item in datas) { tmp.Append((item.Id, item)); if (max < item.Id) max = item.Id; } tmp.Append((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 void TestGetAnswer(MyDbContext context, int numTest, int id, Answer? waiting) { context.Database.EnsureCreated(); var mgr = new AnswerDataManager(new AnswerEntityManager(context)); var tmp = mgr.getAnswer(id).Result; if (tmp?.Content == waiting?.Content) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {numTest} AnswerDataManager.getAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"test {numTest} AnswerDataManager.getAnswer KO"); Console.ResetColor(); } } /// /// member data for the 'TestGetAnswer' test method /// /// a set of all inline datas for the test method (int numPage, int count, IEnumerable? waiting, AnswerOrderCriteria? orderCriteria)[] TestGetSomeAnswers_Datas(MyDbContext context) { var tmp = context.Answers; var datas = new List(); foreach (var item in tmp) datas.Add(item.ToModel()); var tmp2 = context.Answers.OrderBy(a => a.Content); var datas2 = new List(); foreach (var item in tmp2) datas2.Add(item.ToModel()); var count = datas.Count(); // remind that to add answers, we haven't ordered this collection // but we just have done a foreach return [ // 1/2 : 0 answer from the page 1 then, -1 answer from this page // WF : count < 1 so we got null (1, 0, null, null), (1, -1, null, null), // 3/4 : 1 answers from the page 0 then, 1 answers from the page -1 // WF : page < 1 so we got null (0, 1, null, null), (-1, 1, null, null), // 5 : 10 answers from the page 1 // WF : the first 10 element of datas (1, 10, datas.Take(10), null), // 6 : 10 elements from the page 1 order by id // WF : the first 10 element of datas order by id (1, 10, datas.Take(10), AnswerOrderCriteria.ById), // 7 : 10 elements from the page 1 order by content // WF : the first 10 element of datas order by content (1, 10, datas2.Take(10), AnswerOrderCriteria.ByContent), // 8/9/10 : repeat 3, 4 and 5 with the page 2 // WF : the 10 next answer from each case (2, 10, datas.Skip(10).Take(10), null), (2, 10, datas.Skip(10).Take(10), AnswerOrderCriteria.ById), (2, 10, datas2.Skip(10).Take(10), AnswerOrderCriteria.ByContent), // 11 : count/4 elements from the page 4 // WF : the lasts count/4 elements of datas (4, count / 4, datas.Skip(3*count/4).Take(count/4), null), // 12 : count/4 elements from the page 5 // WF : null since num (4) >= count / (count/4) (3, count / 2, null, null), // 13 : 7 elements from the page 5 // WF : since there's only 30 elements in fake datas, // we got the 2 last elements (5, 7, datas.Skip(28).Take(2), null) ]; } /// /// 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 void TestGetSomeAnswers(MyDbContext context, int numTest, int num, int count, List? waiting, AnswerOrderCriteria? orderCriteria) { var mgr = new AnswerDataManager(new AnswerEntityManager(context)); float nbPages = count == 0 ? -1 : (float)mgr.getNbAnswers() / count; if (nbPages != (int)nbPages) nbPages++; (int nbPages, IEnumerable? answers) tmp = orderCriteria == null ? mgr.getAnswers(num, count).Result : mgr.getAnswers(num, count, orderCriteria.Value).Result; if (tmp.nbPages == (int)nbPages) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {2 * numTest} AnswerDataManager.getSomeAnswers OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"test {2 * numTest} AnswerDataManager.getSomeAnswers KO"); Console.ResetColor(); } if (waiting == null && tmp.answers == null) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {2 * numTest + 1} AnswerDataManager.getSomeAnswers OK"); Console.ResetColor(); } else if (waiting != null && tmp.answers != null) { if (waiting.Count() == 0 && tmp.answers.Count() == 0) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {2 * numTest + 1} AnswerDataManager.getSomeAnswers OK"); Console.ResetColor(); } else if (waiting.Count() != 0 && tmp.answers.Count() != 0) { var nbFautes = 0; for (var i = 0; i < waiting.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 {2 * numTest + 1} AnswerDataManager.getSomeAnswers OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"test {2 * numTest + 1} AnswerDataManager.getSomeAnswers KO"); Console.ResetColor(); Console.WriteLine($"numPage : {num}, count : {count}"); Console.Write("what we have : "); if (tmp.answers == null) Console.WriteLine("null"); else { Console.Write("{ "); tmp.answers.ToList().ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); // I know that I use sames collections Console.WriteLine("}"); Console.WriteLine($"([{tmp.answers.Select(a => a.Id).Min()}, {tmp.answers.Select(a => a.Id).Max()}], {tmp.answers.Count()})"); } Console.WriteLine(); Console.Write("WF : "); if (waiting == null) Console.WriteLine("null"); else { Console.Write("{ "); waiting.ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); Console.WriteLine($"([{waiting.Select(a => a.Id).Min()}, {waiting.Select(a => a.Id).Max()}], {waiting.Count()})"); } } } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"test {2 * numTest + 1} AnswerDataManager.getSomeAnswers KO"); Console.ResetColor(); Console.WriteLine($"numPage : {num}, count : {count}, nbEl : {mgr.getNbAnswers()}"); Console.Write("what we have : "); if (tmp.answers == null) Console.WriteLine("null"); else { Console.Write("{ "); tmp.answers.ToList().ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); Console.WriteLine($"([{tmp.answers.Select(a => a.Id).Min()}, {tmp.answers.Select(a => a.Id).Max()}], {tmp.answers.Count()})"); } Console.WriteLine(); Console.Write("WF : "); if (waiting == null) Console.WriteLine("null"); else { Console.Write("{ "); waiting.ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); if(waiting.Count() != 0) Console.WriteLine($"([{waiting.Select(a => a.Id).Min()}, {waiting.Select(a => a.Id).Max()}], {waiting.Count()})"); } } } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"test {2 * numTest + 1} AnswerDataManager.getSomeAnswers KO"); Console.ResetColor(); Console.WriteLine($"numPage : {num}, count : {count}"); Console.Write("what we have : "); if (tmp.answers == null) Console.WriteLine("null"); else { Console.Write("{ "); tmp.answers.ToList().ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); Console.WriteLine($"([{tmp.answers.Select(a => a.Id).Min()}, {tmp.answers.Select(a => a.Id).Max()}], {tmp.answers.Count()})"); } Console.WriteLine(); Console.Write("WF : "); if (waiting == null) Console.WriteLine("null"); else { Console.Write("{ "); waiting.ForEach(a => Console.Write("{" + $"{a.Id}" + "}, ")); Console.WriteLine("}"); Console.WriteLine($"([{waiting.Select(a => a.Id).Min()}, {waiting.Select(a => a.Id).Max()}], {waiting.Count()})"); } } } /// /// test of the 'supprimerAnswer' method of an AnswerManager /// void TestRemoveAnswer(MyDbContext context) { var mgr = new AnswerDataManager(new AnswerEntityManager(context)); var a = mgr.removeAnswer(new Answer ("test remove answer")).Result; // 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 AnswerDataManager.removeAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 1 AnswerDataManager.removeAnswer KO"); Console.ResetColor(); Console.WriteLine("what we have : " + a == null ? "null" : $"{a.Id} : {a.Content}"); Console.WriteLine("WF : null"); } var answer = context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).ToModel(); a = mgr.removeAnswer(answer).Result; // 1) with a content that correspond 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 != null && a.Content == answer.Content && a.Id == answer.Id && a.Id != context.Answers.Select(a => a.Id).Min() /* <=> he is removed */) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("test 2 AnswerDataManager.removeAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 2 AnswerDataManager.removeAnswer KO"); Console.ResetColor(); Console.WriteLine("what we have : " + a == null ? "null" : $"{a!.Id} : {a!.Content}"); Console.WriteLine("WF : " + answer == null ? "null" : $"{answer.Id} : {answer.Content}"); } } (int id, Answer modified, Answer? waiting)[] TestUpdateAnswer_Datas(MyDbContext context) { return new (int, Answer, Answer?)[] { // 1) with an id that is not allowed // WF : since no answer match with this id, // the function return null ( context.Answers.Select(a => a.Id).Max() + 1, new Answer ( "we don't care about", null, 3 ), null ), // 2) with a content that correspond to the content of another answer // WF : since some answers can each have same content, it work // and the id of the answer we got is the one we give (content.Answers.Select.(a => a.id.Min()) // and his content is the one we want ( context.Answers.Select(a => a.Id).Min(), new Answer ( context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content, null, 5 ), new Answer ( context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content, null, context.Answers.Select(a => a.Id).Min() ) ), // 3) normal insertion // WF : the id of the answer we got is the one we // give (content.Answers.Select.(a => a.id.Min()) // and his content is the one we want ( context.Answers.Select(a => a.Id).Max(), new Answer ( context.Answers.First().Content, null, context.Answers.Select(a => a.Id).Min() ), new Answer ( context.Answers.First().Content, null, context.Answers.Select(a => a.Id).Max() ) ) }; } void TestUpdateAnswer(MyDbContext context, int numTest, int id, Answer modified, Answer? waiting) { var mgr = new AnswerDataManager(new AnswerEntityManager(context)); var response = mgr.updateAnswer(id, modified).Result; if ((waiting == null && response == null) || (waiting != null && response != null && response.Id == id && response.Content == waiting.Content)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"test {numTest} AnswerDataManager.updateAnswer OK"); Console.ResetColor(); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("test 2 AnswerDataManager.removeAnswer KO"); Console.ResetColor(); Console.WriteLine("what we have : " + response == null ? "null" : $"{id} : {response!.Content}"); Console.WriteLine("WF : " + waiting == null ? "null" : $"{waiting!.Id} : {waiting.Content}"); } } // Tests of an answer void TestAnswer(MyDbContext context) { TestAddAnswer(); TestRemoveAnswerById(context); TestGetNbAnswers(); { var tmp = TestGetAnswer_Datas(context); for (int i = 0; i < tmp.Count(); i++) { TestGetAnswer( context, i + 1, tmp.ElementAt(i).id, tmp.ElementAt(i).waiting ); } } { var tmp = TestGetSomeAnswers_Datas(context); for (int i = 0; i < tmp.Count(); i++) { TestGetSomeAnswers( context, i + 1, tmp.ElementAt(i).numPage, tmp.ElementAt(i).count, tmp.ElementAt(i).waiting?.ToList(), tmp.ElementAt(i).orderCriteria ); } } TestRemoveAnswer(context); { var tmp = TestUpdateAnswer_Datas(context); for (int i = 0; i < tmp.Count(); i++) { TestUpdateAnswer( context, i + 1, tmp.ElementAt(i).id, tmp.ElementAt(i).modified, tmp.ElementAt(i).waiting ); } } } void Test(MyDbContext context) { TestAnswer(context); } var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var opt = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; using var context = new StubbedDbContext(opt); context.Database.EnsureCreated(); Test(context);