using DbConnectionLibrairie;
using Entities;
using EntityManagers;
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 AnswerEntityManager(context);
var answerToAdd = new AnswerEntity { Id = 1, Content = "chateîgne" };
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 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 1 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
Console.WriteLine($"what we have : {a.Id} : {a.Content}");
Console.WriteLine($"WF : {answerToAdd.Id} : {answerToAdd.Content}");
}
answerToAdd = new AnswerEntity { Id = 5, Content = "damien" };
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 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 2 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
Console.WriteLine($"what we have : {a.Id} : {a.Content}");
Console.WriteLine($"WF : 2 : {answerToAdd.Content}");
}
answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" };
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 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 3 AnswerEntityManager.addAnswer KO");
Console.ResetColor();
Console.WriteLine($"what we have : {a.Id} : {a.Content}");
Console.WriteLine($"WF : 1 : {answerToAdd.Content}");
}
answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" };
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 AnswerEntityManager.addAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 4 AnswerEntityManager.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 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 AnswerEntityManager.removeAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 1 AnswerEntityManager.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 AnswerEntityManager.removeAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 2 AnswerEntityManager.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 AnswerEntityManager(context);
int count = context.Answers.CountAsync().Result;
int nb = 0;
var list = new List()
{
new AnswerEntity { Content = "Ajout 1" },
new AnswerEntity { Content = "Ajout 2" },
new AnswerEntity { Content = "Ajout 3" },
new AnswerEntity { Content = "Ajout 4" },
new AnswerEntity { Content = "Ajout 5" },
new AnswerEntity { Content = "Ajout 6" },
new AnswerEntity { Content = "Ajout 7" },
new AnswerEntity { Content = "Ajout 8" },
new AnswerEntity { Content = "Ajout 9" },
new AnswerEntity { Content = "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} AnswerEntityManager.getNbAnswers OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {nb} AnswerEntityManager.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, AnswerEntity? waiting)[] TestGetAnswer_Datas(MyDbContext context)
{
var datas = context.Answers;
int max = 0;
(int id, AnswerEntity? waiting)[] tmp = new (int id, AnswerEntity? 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, AnswerEntity? waiting)
{
context.Database.EnsureCreated();
var mgr = new AnswerEntityManager(context);
var tmp = mgr.getAnswer(id).Result;
if (tmp?.Content == waiting?.Content)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {numTest} AnswerEntityManager.getAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {numTest} AnswerEntityManager.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()
{
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 [
// 1 : 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),
// 2 : 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),
// 3 : 10 answers from the page 1
// WF : the first 10 element of datas
(1, 10, datas.Take(10), null),
// 4 : 10 elements from the page 1 order by id
// WF : the first 10 element of datas order by id
(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
(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
(2, 10, datas.Skip(10).Take(10), null),
(1, 10, datas.OrderBy(e => e.Id).Skip(10).Take(10), AnswerOrderCriteria.ById),
(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
(4, count / 4, datas.TakeLast(count / 4), null),
// 8 : count/4 elements from the page 5
// WF : null since num (4) >= count / (count/4)
(5, count / 4, null, null),
// 9 : 10 elements from the page 4
// WF : since there's only 31 elements in fake datas,
// we got the last element
(4, 10, datas.TakeLast(1), 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)
{
context.Database.EnsureCreated();
var mgr = new AnswerEntityManager(context);
var nbPages = count == 0 ? -1 : mgr.getNbAnswers() / count;
(int nbPages, IEnumerable? answers) tmp = orderCriteria == null ? mgr.getAnswers(num, count).Result : mgr.getAnswers(num, count, orderCriteria.Value).Result;
if(tmp.nbPages == nbPages){
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {2*numTest} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {2*numTest} AnswerEntityManager.getSomeAnswers KO");
Console.ResetColor();
}
if (waiting == null && tmp.answers == null)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"test {2*numTest + 1} AnswerEntityManager.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} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else if(waiting.Count() != 0 && tmp.answers.Count() == 0)
{
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 {2*numTest + 1} AnswerEntityManager.getSomeAnswers OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {2*numTest + 1} AnswerEntityManager.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("{" + $"id : {a.Id}, content : {a.Content}" + "}, "));
Console.WriteLine("}");
}
Console.Write("WF : ");
if (waiting == null) Console.WriteLine("null");
else
{
Console.Write("{ ");
waiting.ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, "));
Console.WriteLine("}");
}
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {2*numTest + 1} AnswerEntityManager.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("{" + $"id : {a.Id}, content : {a.Content}" + "}, "));
Console.WriteLine("}");
}
Console.Write("WF : ");
if (waiting == null) Console.WriteLine("null");
else
{
Console.Write("{ ");
waiting.ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, "));
Console.WriteLine("}");
}
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"test {2*numTest + 1} AnswerEntityManager.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("{" + $"id : {a.Id}, content : {a.Content}" + "}, "));
Console.WriteLine("}");
}
Console.Write("WF : ");
if (waiting == null) Console.WriteLine("null");
else
{
Console.Write("{ ");
waiting.ForEach(a => Console.Write("{" + $"id : {a.Id}, content : {a.Content}" + "}, "));
Console.WriteLine("}");
}
}
}
///
/// test of the 'supprimerAnswer' method of an AnswerManager
///
void TestRemoveAnswer(MyDbContext context)
{
var mgr = new AnswerEntityManager(context);
var a = mgr.removeAnswer(new AnswerEntity { Content = "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 AnswerEntityManager.removeAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 1 AnswerEntityManager.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(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 AnswerEntityManager.removeAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 2 AnswerEntityManager.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, AnswerEntity modified, AnswerEntity? waiting)[] TestUpdateAnswer_Datas(MyDbContext context)
{
return new (int, AnswerEntity, AnswerEntity?)[]
{
// 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 AnswerEntity
{
Id = 3,
Content = "we don't care about"
},
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 AnswerEntity
{
Id = 5,
Content = context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content,
},
new AnswerEntity
{
Id = context.Answers.Select(a => a.Id).Min(),
Content = context.Answers.Single(a => a.Id == context.Answers.Select(a => a.Id).Min()).Content,
}
),
// 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 AnswerEntity
{
Id = context.Answers.Select(a => a.Id).Min(),
Content = context.Answers.First().Content
},
new AnswerEntity
{
Id = context.Answers.Select(a => a.Id).Max(),
Content = context.Answers.First().Content
}
)
};
}
void TestUpdateAnswer(MyDbContext context, int numTest, int id, AnswerEntity modified, AnswerEntity? waiting)
{
var mgr = 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} AnswerEntityManager.updateAnswer OK");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("test 2 AnswerEntityManager.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();
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);