using DbConnectionLibrairie; using Entities; using EntityManagers; using Newtonsoft.Json; namespace UnitTestsEntityManagers { public class UnitTestAnswerManager : AbstractUnitTestEM { IEnumerable answers = JsonConvert .DeserializeObject> (File.ReadAllTextAsync("../fake-Answers.json").Result)!; // if this is null, we don't want to continue AnswerEntityManager mgr; public UnitTestAnswerManager() : base() { mgr = new AnswerEntityManager(dbContext); } /// /// test of the 'ajouterAnswer' method of an AnswerManager /// [Fact] public void TestAjouterAnswer() { var answerToAdd = new AnswerEntity { Id = -1, Content = "châteîgne" }; var a = mgr.ajouterAnswer(answerToAdd); // 1) with an id less than 0 // WF : it works so 'a' is not null, // his content is equal to the // content of 'answerToAdd' // and since it's the first answer // that we add, his id equal 0 Assert.NotNull(a); Assert.Equal(answerToAdd.Content, a.Content); Assert.NotEqual(0, a.Id); answerToAdd = new AnswerEntity { Id = 5, Content = "damien" }; a = mgr.ajouterAnswer(answerToAdd); // 2) with a random id greater than 0 // WF : it works so 'a' is not null, // his content is equal to the // content of 'answerToAdd' // and since it's the second answer // that we add, his id equal 1 Assert.NotNull(a); Assert.Equal(answerToAdd.Content, a.Content); Assert.NotEqual(1, a.Id); answerToAdd = new AnswerEntity { Id = 7, Content = "châteîgne" }; a = mgr.ajouterAnswer(answerToAdd); // 3) with a content that we already have added // WF : it don't works so 'a' is null Assert.Null(a); answerToAdd = new AnswerEntity { Id = 7, Content = "CHÂTEÎGNE" }; a = mgr.ajouterAnswer(answerToAdd); // 3) with a content that we already have added // but in upperCase instead of lowerCase // WF : it don't works so 'a' is null Assert.Null(a); } /// /// test of the 'supprimerAnswer' method of an AnswerManager /// [Fact] public void TestSupprimerAnswer() { // remember that we have only 2 answers in the database : // 1) (0, "châteigne") // 2) (1, "damien") var a = mgr.supprimerAnswer(-3); // 1) with an id less than 0 // WF : it don't work because there's no // negative id so 'a' is null Assert.Null(a); a = mgr.supprimerAnswer(3); // 2) with an id greater or equal // to the number of element // WF : it don't works so 'a' is null, Assert.Null(a); a = mgr.supprimerAnswer(0); // 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 0, the content is "châteigne" Assert.NotNull(a); Assert.Equal(0, a.Id); Assert.Equal("châteigne", a.Content); a = mgr.supprimerAnswer(0); // 1) same thing with the id 1 just // for cleaning the database // WF : it works so 'a' is not null, // and since we've delete the answer with // the id 1, the content is "damien" Assert.NotNull(a); Assert.Equal(0, 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 2 (even though we've delete those) /// /// test of the 'getNbElement' method of an AnswerManager /// [Fact] public void TestGetNbElement() { Assert.Equal(0, mgr.getNbElement()); // just to be sure Assert.NotNull(answers); // just to be sure int count = 0; foreach (var answer in answers) { mgr.ajouterAnswer(answer); count++; Assert.Equal(count, mgr.getNbElement()); } } // getAnswers // modifierAnswer // supprimerAnswer (les 2) // getAnswersByIdQuestion } }