public AnswerTO Modify(AnswerTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } if (entity.Id <= 0) { throw new ArgumentException("Answer To Update Invalid Id"); } if (!askContext.Answers.Any(x => x.Id == entity.Id)) { throw new KeyNotFoundException($"Update(AnswerTO) Can't find Answer to update."); } var editedEntity = askContext.Answers.FirstOrDefault(e => e.Id == entity.Id && e.AssociatedQuestion.IsResolved != true); if (editedEntity != default) { entity.ToTrackedEF(editedEntity); } askContext.SaveChanges(); return(editedEntity.ToTransferObject()); }
public void AnsweringQuestion_Successful() { var mockAnswerRepository = new Mock <IAnswerRepository>(); mockAnswerRepository.Setup(u => u.Create(It.IsAny <AnswerTO>())) .Returns(MockAnswer); var mockQuestionRepository = new Mock <IQuestionRepository>(); mockQuestionRepository.Setup(u => u.GetById(It.IsAny <int>())) .Returns(MockQuestion); DateTime date = DateTime.Now; var question = new QuestionTO { IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var answer2 = new AnswerTO { Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = question, }; var askUC = new AskUC(mockAnswerRepository.Object, mockQuestionRepository.Object); var addedAnswer = askUC.AnsweringQuestion(1, answer2); Assert.IsNotNull(addedAnswer); mockAnswerRepository.Verify((m => m.Create(It.IsAny <AnswerTO>())), Times.Once()); }
public AnswerTO AnsweringQuestion(int QuestionId, AnswerTO Answer) { var question = questionRepository.GetById(QuestionId); Answer.AssociatedQuestion = question; return(answerRepository.Create(Answer)); }
public void CreateAnswer_AddExistingAnswer_DoNotInsertTwiceInDb() { //ARRANGE var options = new DbContextOptionsBuilder <AskContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new AskContext(options); IQuestionRepository questionRepository = new QuestionRepository(context); IAnswerRepository answerRepository = new AnswerRepository(context); DateTime date = DateTime.Now; var question = new QuestionTO { IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var addedQuestion = questionRepository.Create(question); context.SaveChanges(); var answer = new AnswerTO { Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = addedQuestion, }; var result = answerRepository.Create(answer); context.SaveChanges(); //ACT answerRepository.Create(result); context.SaveChanges(); //ASSERT Assert.AreEqual(1, context.Answers.Count()); }
public void ToTEF_ProvidingNull_ThrowException() { //ARRANGE AnswerTO answer = null; //ACT Assert.ThrowsException <ArgumentNullException>(() => answer.ToEF()); }
public void ToTrackedEF_ProvidingNullEF_ThrowException() { //ARRANGE AnswerTO answer = null; AnswerEF answerToModify = null; //ACT Assert.ThrowsException <ArgumentNullException>(() => answer.ToTrackedEF(answerToModify)); }
public void GetALlAnswers_Successful() { //ARRANGE var options = new DbContextOptionsBuilder <AskContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new AskContext(options); IQuestionRepository questionRepository = new QuestionRepository(context); IAnswerRepository answerRepository = new AnswerRepository(context); //ACT DateTime date = DateTime.Now; var question = new QuestionTO { IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var question2 = new QuestionTO { IsResolved = false, Message = "Comment créer un projet MVC 6", Title = "MVC6", Date = date, AuthorId = 2 }; var question3 = new QuestionTO { IsResolved = false, Message = "Comment faire boucle foreach", Title = "foreach", Date = date, AuthorId = 2 }; var addedQuestion = questionRepository.Create(question); var addedQuestion2 = questionRepository.Create(question2); var addedQuestion3 = questionRepository.Create(question3); context.SaveChanges(); var answer = new AnswerTO { Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = addedQuestion, }; var answer2 = new AnswerTO { Message = "Oui c'est trop facile les tests avec InMemory", AuthorId = 3, AssociatedQuestion = addedQuestion, }; var answer3 = new AnswerTO { Message = "Tu dois d'abord créer un projet web app avec .Net Core", AuthorId = 1, AssociatedQuestion = addedQuestion2, }; var addedAnswer = answerRepository.Create(answer); var addedAnswer2 = answerRepository.Create(answer2); var addedAnswer3 = answerRepository.Create(answer3); context.SaveChanges(); //Archiving the question related to the answer 3 addedQuestion2.IsResolved = true; questionRepository.Modify(addedQuestion2); context.SaveChanges(); //ACT var test = answerRepository.GetAll(); //ASSERT Assert.AreEqual(3, test.Count()); }
public void ToTrackedEF_ProvidingNullTO_ThrowException() { //ARRANGE AnswerTO answer = null; DateTime date = DateTime.Now; var question = new QuestionEF { Id = 1, IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var answerToModify = new AnswerEF { Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = question, }; //ACT Assert.ThrowsException <ArgumentNullException>(() => answer.ToTrackedEF(answerToModify)); }
public static AnswerEF ToEF(this AnswerTO answer) { if (answer is null) { throw new ArgumentNullException(nameof(answer)); } return(new AnswerEF { Id = answer.Id, AuthorId = answer.AuthorId, Message = answer.Message, AssociatedQuestion = answer.AssociatedQuestion.ToEF(), }); }
public void ArchiveAnswer_Correct() { var options = new DbContextOptionsBuilder <QAndAContext>().UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name).Options; var context = new QAndAContext(options); IAnswerRepository ARepo = new AnswerRepository(context); IQuestionRepository QRepo = new QuestionRepository(context); var question = new QuestionTO { Questioning = "Not hungry", CreationDate = DateTime.Now, State = State.Pending, IsArchived = false, LostSoul = new UserTO { FirstName = "Don't wanna", LastName = "Eat" } }; var addedQuestion = QRepo.Insert(question); QRepo.Save(); var answer = new AnswerTO { Answering = "must be the donuts", AnswerTime = DateTime.Now.AddHours(1), QuestionId = addedQuestion.Id, Savior = new UserTO { FirstName = "Any", LastName = "Officer" } }; var addedAnswer = ARepo.Insert(answer); ARepo.Save(); Assert.AreEqual(1, ARepo.GetAll().Count()); var deletedAnswer = ARepo.Delete(addedAnswer); ARepo.Save(); var all = ARepo.GetAll(); Assert.AreEqual(0, all.Count()); Assert.IsTrue(deletedAnswer); Assert.IsTrue(ARepo.Get(addedAnswer.Id).IsDeleted); }
public void ToEF_Successful() { //ARRANGE DateTime date = DateTime.Now; var question = new QuestionTO { Id = 1, IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var answer = new AnswerTO { Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = question, }; //ACT var result = answer.ToEF(); //Assert Assert.AreEqual(answer.AuthorId, result.AuthorId); Assert.AreEqual(answer.Message, result.Message); }
public void GetAnswer_CantAddWithoutAQuestion() { var options = new DbContextOptionsBuilder <QAndAContext>().UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name).Options; var context = new QAndAContext(options); IAnswerRepository ARepo = new AnswerRepository(context); var answer = new AnswerTO { Answering = "must be the donuts", AnswerTime = DateTime.Now.AddHours(1), Savior = new UserTO { FirstName = "Any", LastName = "Officer" } }; Assert.ThrowsException <NullReferenceException>(() => ARepo.Insert(answer)); }
public bool Delete(AnswerTO entity) { if (entity is null) { throw new KeyNotFoundException(); } if (entity.Id <= 0) { throw new ArgumentException("Answer To Delete Invalid Id"); } var answer = askContext.Answers.FirstOrDefault(x => x.Id == entity.Id); askContext.Answers.Remove(answer); askContext.SaveChanges(); return(true); }
public static AnswerEF ToEF(this AnswerTO answer) { if (answer is null) { throw new ArgumentNullException(nameof(answer)); } return(new AnswerEF { Id = answer.Id, Answering = answer.Answering, AnswerTime = answer.AnswerTime, SaviorId = answer.SaviorId, QuestionId = answer.QuestionId, IsDeleted = answer.IsDeleted }); }
public void UpdateAnswer_Correct() { var options = new DbContextOptionsBuilder <QAndAContext>().UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name).Options; var context = new QAndAContext(options); IAnswerRepository ARepo = new AnswerRepository(context); IQuestionRepository QRepo = new QuestionRepository(context); var question = new QuestionTO { Questioning = "Maybe it's working", CreationDate = DateTime.Now, State = State.Pending, IsArchived = false, LostSoul = new UserTO { FirstName = "Call me", LastName = "Kevin" } }; var addedQuestion = QRepo.Insert(question); QRepo.Save(); var answer = new AnswerTO { Answering = "No shit Sherlock", AnswerTime = DateTime.Now.AddHours(1), QuestionId = addedQuestion.Id, Savior = new UserTO { FirstName = "Dr", LastName = "Watson" } }; var addedAnswer = ARepo.Insert(answer); ARepo.Save(); addedAnswer.Answering = "Can't be right"; var updatedAnswer = ARepo.Update(addedAnswer); ARepo.Save(); Assert.AreEqual(1, ARepo.GetAll().Count()); Assert.AreEqual(1, QRepo.Get(addedAnswer.QuestionId).Answers.Count()); Assert.AreEqual("Can't be right", ARepo.Get(updatedAnswer.Id).Answering); }
public void InsertAnswer_NonExistingQuestion() { var options = new DbContextOptionsBuilder <QAndAContext>().UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name).Options; var context = new QAndAContext(options); IAnswerRepository ARepo = new AnswerRepository(context); var answer = new AnswerTO { Answering = "No shit Sherlock", AnswerTime = DateTime.Now.AddHours(1), QuestionId = 20, Savior = new UserTO { FirstName = "Dr", LastName = "Watson" } }; Assert.ThrowsException <NullReferenceException>(() => ARepo.Insert(answer)); }
public static AnswerEF ToTrackedEF(this AnswerTO answer, AnswerEF answerToModify) { if (answerToModify is null) { throw new ArgumentNullException(nameof(answerToModify)); } if (answer is null) { throw new ArgumentNullException(nameof(answer)); } answerToModify.Id = answer.Id; answerToModify.AuthorId = answer.AuthorId; answerToModify.Message = answer.Message; answerToModify.AssociatedQuestion = answer.AssociatedQuestion.ToTrackedEF(answerToModify.AssociatedQuestion); return(answerToModify); }
public AnswerTO Insert(AnswerTO entity) { if (entity is null) { throw new ArgumentNullException(); } if (entity.Id != 0) { return(entity); } var question = context.Questions.FirstOrDefault(x => x.Id == entity.QuestionId); var result = context.Answers.Add(entity.ToEF()); question.Answers.Add(result.Entity); return(result.Entity.ToTO()); }
public ActionResult Answering(int id, AnswerQuestionVM answerVM) { try { var currentUser = _userManager.GetUserAsync(User).Result; var associatedQuestion = _askUC.ShowThisQuestion(id); var answerTO = new AnswerTO { Message = answerVM.Answer.Message, AssociatedQuestion = associatedQuestion, AuthorId = currentUser.Id, }; _askUC.AnsweringQuestion(id, answerTO); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public AnswerTO Create(AnswerTO entity) { if (entity is null) { throw new ArgumentNullException(); } if (entity.Id != 0) { return(entity); } var answerEF = entity.ToEF(); answerEF.AssociatedQuestion = askContext.Questions.First(x => x.Id == entity.AssociatedQuestion.Id && entity.AssociatedQuestion.IsResolved != true); var result = askContext.Answers.Add(answerEF); askContext.SaveChanges(); return(result.Entity.ToTransferObject()); }
public void ModifyAnswer_Successful() { var options = new DbContextOptionsBuilder <AskContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new AskContext(options); IQuestionRepository questionRepository = new QuestionRepository(context); IAnswerRepository answerRepository = new AnswerRepository(context); //ACT DateTime date = DateTime.Now; var question = new QuestionTO { IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var question2 = new QuestionTO { IsResolved = false, Message = "Comment créer un projet MVC 6", Title = "MVC6", Date = date, AuthorId = 2 }; var question3 = new QuestionTO { IsResolved = false, Message = "Comment faire boucle foreach", Title = "foreach", Date = date, AuthorId = 2 }; var addedQuestion = questionRepository.Create(question); var addedQuestion2 = questionRepository.Create(question2); var addedQuestion3 = questionRepository.Create(question3); context.SaveChanges(); var answer = new AnswerTO { Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = addedQuestion, }; var result = answerRepository.Create(answer); context.SaveChanges(); //ACT result.Message = "Message modifié"; var test = answerRepository.Modify(result); context.SaveChanges(); //ASSERT Assert.AreEqual("Message modifié", test.Message); }
private static List <AnswerTO> GetAnswers(int id) { List <AnswerTO> list = new List <AnswerTO>(); DataTable table = ExecuteCommand( String.Format( " SELECT ID, Text " + " FROM answer WHERE QuestionID = {0} ", id)); foreach (DataRow item in table.Rows) { AnswerTO element = new AnswerTO() { ID = Convert.ToInt32(item["ID"]), Text = Convert.ToString(item["Text"]) }; list.Add(element); } return(list); }
public AnswerTO Update(AnswerTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } if (entity.Id <= 0) { throw new ArgumentException(nameof(entity)); } var updated = context.Answers.FirstOrDefault(e => e.Id == entity.Id); if (updated != default) { updated.UpdateFromDetached(entity.ToEF()); } Save(); return(context.Answers.Update(updated).Entity.ToTO()); }
public bool Delete(AnswerTO entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } if (entity.Id <= 0) { throw new ArgumentException(nameof(entity)); } if (entity.IsDeleted) { throw new ArgumentException(nameof(entity)); } entity.IsDeleted = true; var result = Update(entity); return(result != null); }
public void ModifyAnswer_ProvidingNonExistingId_ThrowException() { var options = new DbContextOptionsBuilder <AskContext>() .UseInMemoryDatabase(databaseName: MethodBase.GetCurrentMethod().Name) .Options; using var context = new AskContext(options); IQuestionRepository questionRepository = new QuestionRepository(context); IAnswerRepository answerRepository = new AnswerRepository(context); DateTime date = DateTime.Now; var question = new QuestionTO { IsResolved = false, Message = "Je n'arrive pas à faire un test", Title = "Problème avec Tests", Date = date, AuthorId = 1 }; var addedQuestion = questionRepository.Create(question); context.SaveChanges(); var answer = new AnswerTO { Id = 16783, Message = "En fait, c'est facile il faut toujorus faire des tests", AuthorId = 2, AssociatedQuestion = addedQuestion, }; Assert.ThrowsException <KeyNotFoundException>(() => answerRepository.Modify(answer)); }
public AnswerTO AddAnswer(int questionId, AnswerTO answer) { throw new NotImplementedException(); }
public void GetAllAnswers_Correct() { var options = new DbContextOptionsBuilder <QAndAContext>().UseInMemoryDatabase(MethodBase.GetCurrentMethod().Name).Options; var context = new QAndAContext(options); IAnswerRepository ARepo = new AnswerRepository(context); IQuestionRepository QRepo = new QuestionRepository(context); var question1 = new QuestionTO { Questioning = "How can I stop being Rickrolled ?", CreationDate = DateTime.Now, State = State.Pending, IsArchived = false, LostSoul = new UserTO { FirstName = "Don't wanna", LastName = "Eat" } }; var question2 = new QuestionTO { Questioning = "Never Gonna what?", CreationDate = DateTime.Now, State = State.Pending, IsArchived = false, LostSoul = new UserTO { FirstName = "Rick", LastName = "Ashley" } }; var addedQuestion1 = QRepo.Insert(question1); var addedQuestion2 = QRepo.Insert(question2); QRepo.Save(); var answer1 = new AnswerTO { Answering = "Maybe Someone Else know this", AnswerTime = DateTime.Now.AddHours(1), QuestionId = addedQuestion1.Id, Savior = new UserTO { FirstName = "Random", LastName = "Joe" } }; var answer2 = new AnswerTO { Answering = "Why should I know this ? ", AnswerTime = DateTime.Now.AddHours(1), QuestionId = addedQuestion1.Id, Savior = new UserTO { FirstName = "Someone", LastName = "Else" } }; var answer3 = new AnswerTO { Answering = "Give you up, Let you down", AnswerTime = DateTime.Now.AddHours(1), QuestionId = addedQuestion2.Id, Savior = new UserTO { FirstName = "A specific", LastName = "Asshole" } }; var addedAnswer1 = ARepo.Insert(answer1); var addedAnswer2 = ARepo.Insert(answer2); var addedAnswer3 = ARepo.Insert(answer3); ARepo.Save(); Assert.AreEqual(3, ARepo.GetAll().Count()); }