public void ChoiceAddingAllowedForPoll_AddsChoice() { const string optionName = "new option"; const string optionDescription = "description"; IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>(); IDbSet <Choice> options = DbSetTestHelper.CreateMockDbSet <Choice>(); polls.Add(new Poll() { UUID = PollManageGuid, ChoiceAdding = true }); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls, options); PollChoiceController controller = CreatePollChoiceController(contextFactory); ChoiceCreationRequestModel optionCreationRequestModel = new ChoiceCreationRequestModel() { Name = optionName, Description = optionDescription, }; controller.Post(PollManageGuid, optionCreationRequestModel); Assert.AreEqual(1, options.Count()); Assert.AreEqual(optionName, options.First().Name); Assert.AreEqual(optionDescription, options.First().Description); }
public void AddingChoiceGeneratesMetric() { // Arrange IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>(); var contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls); var metricHandler = new Mock <IMetricHandler>(); PollChoiceController controller = CreatePollChoiceController(contextFactory, metricHandler.Object); Poll existingPoll = new Poll() { Choices = new List <Choice>(), UUID = Guid.NewGuid(), ChoiceAdding = true }; polls.Add(existingPoll); ChoiceCreationRequestModel request = new ChoiceCreationRequestModel() { Name = "New Choice" }; // Act controller.Post(existingPoll.UUID, request); // Assert metricHandler.Verify(m => m.HandleChoiceAddedEvent(It.Is <Choice>(o => o.Name == "New Choice"), existingPoll.UUID), Times.Once()); }
private static Choice CreateChoiceFromRequest(ChoiceCreationRequestModel requestModel) { return(new Choice { Name = requestModel.Name, Description = requestModel.Description }); }
public void UnknownPoll_ThrowsNotFound() { IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>(); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls); PollChoiceController controller = CreatePollChoiceController(contextFactory); ChoiceCreationRequestModel optionCreationRequestModel = new ChoiceCreationRequestModel(); controller.Post(PollManageGuid, optionCreationRequestModel); }
public void ChoiceAddingNotAllowedForPoll_ThrowsMethodNotAllowed() { IDbSet <Poll> polls = DbSetTestHelper.CreateMockDbSet <Poll>(); polls.Add(new Poll() { UUID = PollManageGuid, ChoiceAdding = false }); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(polls); PollChoiceController controller = CreatePollChoiceController(contextFactory); ChoiceCreationRequestModel optionCreationRequestModel = new ChoiceCreationRequestModel(); controller.Post(PollManageGuid, optionCreationRequestModel); }
public void Post(Guid pollId, ChoiceCreationRequestModel choiceCreationRequest) { using (IVotingContext context = _contextFactory.CreateContext()) { if (choiceCreationRequest == null) { ThrowError(HttpStatusCode.BadRequest); } Poll poll = context .Polls .Include(p => p.Choices) .SingleOrDefault(p => p.UUID == pollId); if (poll == null) { ThrowError(HttpStatusCode.NotFound, string.Format("Poll {0} does not exist", pollId)); } if (!poll.ChoiceAdding) { ThrowError(HttpStatusCode.MethodNotAllowed, string.Format("Option adding not allowed for poll {0}", pollId)); } if (!ModelState.IsValid) { ThrowError(HttpStatusCode.BadRequest, ModelState); } Choice newOption = CreateChoiceFromRequest(choiceCreationRequest); _metricHandler.HandleChoiceAddedEvent(newOption, pollId); poll.Choices.Add(newOption); context.Choices.Add(newOption); poll.LastUpdatedUtc = DateTime.UtcNow; context.SaveChanges(); ClientSignaller.SignalUpdate(poll.UUID.ToString()); } }