You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.01-QCM_MuscuMaths/WebApi/UnitTestsEntityManagers/UnitTestAnswerManager.cs

145 lines
5.2 KiB

using DbConnectionLibrairie;
using Entities;
using EntityManagers;
namespace UnitTestsEntityManagers
{
public class UnitTestAnswerManager : AbstractUnitTestEM
{
private AnswerEntityManager mgr;
public UnitTestAnswerManager()
: base()
{
mgr = new AnswerEntityManager(dbContext);
}
/// <summary>
/// test of the 'ajouterAnswer' method of an AnswerManager
/// </summary>
[Fact]
public async void TestAddAnswer()
{
var answerToAdd = new AnswerEntity { Id = 1, Content = "chateîgne" };
var a = await mgr.addAnswer(answerToAdd);
// 1) normal insertion
// WF : a is the same
// as answerToAdd
Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal(answerToAdd.Id, a.Id);
answerToAdd = new AnswerEntity { Id = 5, Content = "damien" };
a = await mgr.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
Assert.Equal(answerToAdd.Content, a.Content);
Assert.Equal((uint)2, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "chateîgne" };
a = await mgr.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
Assert.Equal("chateîgne", a.Content);
Assert.Equal((uint)1, a.Id);
answerToAdd = new AnswerEntity { Id = 7, Content = "CHATEÎGNE" };
a = await mgr.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
Assert.Equal("chateîgne", a.Content);
Assert.Equal((uint)1, a.Id);
}
/// <summary>
/// test of the 'supprimerAnswer' method of an AnswerManager
/// </summary>
[Fact]
public async Task TestRemoveAnswer()
{
// remember that we have only 2 answers in the database :
// 1) (1, "châteigne")
// 2) (2, "damien")
var a = await mgr.removeAnswer(3);
// 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);
// 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);
a = await mgr.removeAnswer(2);
// 1) same thing with the id 1 just
// to clean the database
// WF : it works so 'a' is not null,
// and since we've delete the answer with
// the id 2, the content is "damien"
Assert.NotNull(a);
Assert.Equal((uint)2, a.Id);
Assert.Equal("damien", a.Content);
// now, the database should be clean
}
// /!\ WARNING : since there was 2 answers added to the base,
// id index while now start at 3 (even though we've delete those)
/// <summary>
/// test of the 'getNbElement' method of an AnswerManager
/// </summary>
[Fact]
public async Task TestGetNbElement()
{
Assert.Equal(0, mgr.getNbElements()); // just to be sure
Assert.NotNull(fakeAnswers.datas); // just to be sure
int count = 0;
foreach (var answer in fakeAnswers.datas)
{
await mgr.addAnswer(answer);
count++;
Assert.Equal(count, mgr.getNbElements()); // ok, it's incremented
}
}
public static IEnumerable<Object?[]> TestGetAnswer_Datas()
{
var datas = fakeAnswers.datas;
uint 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 };
}
[Theory]
[MemberData(nameof(TestGetAnswer_Datas))]
public async Task TestGetAnswer(uint id, AnswerEntity? waiting)
{
var tmp = await mgr.getAnswer(id + 2);
Assert.Equal(waiting?.Content, tmp?.Content);
}
// modifierAnswer
// supprimerAnswer (les 2)
// getAnswersByIdQuestion
}
}