public void SetMaxValues_SetsMaxValues() { IDbSet <Poll> existingPolls = DbSetTestHelper.CreateMockDbSet <Poll>(); Poll existingPoll = CreatePoll(); existingPolls.Add(existingPoll); IDbSet <Ballot> existingBallots = DbSetTestHelper.CreateMockDbSet <Ballot>(); IDbSet <Vote> existingVotes = DbSetTestHelper.CreateMockDbSet <Vote>(); int maxPerVote = 15; int maxPoints = 17; IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(existingPolls, existingBallots, existingVotes); ManagePollTypeRequest request = new ManagePollTypeRequest { PollType = "UpDown", MaxPerVote = maxPerVote, MaxPoints = maxPoints }; ManagePollTypeController controller = CreateManagePollTypeController(contextFactory); controller.Put(PollManageGuid, request); Assert.AreEqual(maxPerVote, existingPoll.MaxPerVote); Assert.AreEqual(maxPoints, existingPoll.MaxPoints); }
public void InvalidPollType_ReturnsBadRequest() { IDbSet <Poll> existingPolls = DbSetTestHelper.CreateMockDbSet <Poll>(); Poll existingPoll = CreatePoll(); existingPolls.Add(existingPoll); Vote testVote = new Vote() { Poll = existingPoll }; IDbSet <Ballot> existingBallots = DbSetTestHelper.CreateMockDbSet <Ballot>(); IDbSet <Vote> existingVotes = DbSetTestHelper.CreateMockDbSet <Vote>(); existingVotes.Add(testVote); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(existingPolls, existingBallots, existingVotes); ManagePollTypeRequest request = new ManagePollTypeRequest { PollType = "NotAnChoice", MaxPerVote = 1, MaxPoints = 1 }; ManagePollTypeController controller = CreateManagePollTypeController(contextFactory); controller.Put(PollManageGuid, request); Assert.AreEqual(0, existingVotes.Local.Count); }
public void ChangingToPointsPollWithoutChangingPointsValuesGeneratesMetric() { // Arrange IDbSet <Poll> existingPolls = DbSetTestHelper.CreateMockDbSet <Poll>(); Poll existingPoll = CreatePoll(); existingPoll.PollType = PollType.Basic; existingPoll.MaxPerVote = 3; existingPoll.MaxPoints = 7; existingPolls.Add(existingPoll); IDbSet <Vote> votes = DbSetTestHelper.CreateMockDbSet <Vote>(); Vote existingVote = new Vote() { Poll = existingPoll }; votes.Add(existingVote); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(existingPolls, DbSetTestHelper.CreateMockDbSet <Ballot>(), votes); ManagePollTypeRequest request = new ManagePollTypeRequest { PollType = "Points", MaxPerVote = 3, MaxPoints = 7 }; Mock <IMetricHandler> mockMetricHandler = new Mock <IMetricHandler>(); ManagePollTypeController controller = CreateManagePollTypeController(contextFactory, mockMetricHandler.Object); // Act controller.Put(PollManageGuid, request); // Assert mockMetricHandler.Verify(m => m.HandlePollTypeChangedEvent(PollType.Points, 3, 7, existingPoll.UUID), Times.Once()); mockMetricHandler.Verify(m => m.HandleVoteDeletedEvent(existingVote, existingPoll.UUID), Times.Once()); }
public void IdenticalPollTypeDoesNotGenerateMetric() { // Arrange IDbSet <Poll> existingPolls = DbSetTestHelper.CreateMockDbSet <Poll>(); Poll existingPoll = CreatePoll(); existingPoll.PollType = PollType.Points; existingPoll.MaxPerVote = 3; existingPoll.MaxPoints = 7; existingPolls.Add(existingPoll); IDbSet <Vote> votes = DbSetTestHelper.CreateMockDbSet <Vote>(); Vote existingVote = new Vote() { Poll = existingPoll }; votes.Add(existingVote); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(existingPolls, DbSetTestHelper.CreateMockDbSet <Ballot>(), votes); ManagePollTypeRequest request = new ManagePollTypeRequest { PollType = "Points", MaxPerVote = 3, MaxPoints = 7 }; Mock <IMetricHandler> mockMetricHandler = new Mock <IMetricHandler>(); ManagePollTypeController controller = CreateManagePollTypeController(contextFactory, mockMetricHandler.Object); // Act controller.Put(PollManageGuid, request); // Assert mockMetricHandler.Verify(m => m.HandlePollTypeChangedEvent(It.IsAny <PollType>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <Guid>()), Times.Never()); mockMetricHandler.Verify(m => m.HandleVoteDeletedEvent(It.IsAny <Vote>(), It.IsAny <Guid>()), Times.Never()); }
public void UnChangedPollType_LeavesVotes() { IDbSet <Poll> existingPolls = DbSetTestHelper.CreateMockDbSet <Poll>(); Poll existingPoll = CreatePoll(); existingPoll.PollType = PollType.Basic; existingPoll.MaxPoints = 1; existingPoll.MaxPerVote = 1; existingPolls.Add(existingPoll); Vote testVote = new Vote() { Poll = existingPoll }; IDbSet <Ballot> existingBallots = DbSetTestHelper.CreateMockDbSet <Ballot>(); IDbSet <Vote> existingVotes = DbSetTestHelper.CreateMockDbSet <Vote>(); existingVotes.Add(testVote); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(existingPolls, existingBallots, existingVotes); ManagePollTypeRequest request = new ManagePollTypeRequest { PollType = "Basic", MaxPerVote = 1, MaxPoints = 1 }; ManagePollTypeController controller = CreateManagePollTypeController(contextFactory); controller.Put(PollManageGuid, request); Assert.AreEqual(1, existingVotes.Local.Count); }
public void UnknownManageId_ReturnsNotFound() { IDbSet <Poll> existingPolls = DbSetTestHelper.CreateMockDbSet <Poll>(); IContextFactory contextFactory = ContextFactoryTestHelper.CreateContextFactory(existingPolls); ManagePollTypeRequest request = new ManagePollTypeRequest { }; ManagePollTypeController controller = CreateManagePollTypeController(contextFactory); controller.Put(Guid.NewGuid(), request); }
public void Put(Guid manageId, ManagePollTypeRequest updateRequest) { using (IVotingContext context = _contextFactory.CreateContext()) { Poll poll = PollByManageId(manageId, context); PollType pollType; if (!Enum.TryParse <PollType>(updateRequest.PollType, true, out pollType)) { ModelState.AddModelError("PollType", "Invalid PollType"); } if (!ModelState.IsValid) { ThrowError(HttpStatusCode.BadRequest, ModelState); } if (poll.PollType == pollType) { if (pollType != PollType.Points || (poll.MaxPoints == updateRequest.MaxPoints && poll.MaxPerVote == updateRequest.MaxPerVote)) { return; } } List <Vote> removedVotes = context.Votes.Include(v => v.Poll) .Where(v => v.Poll.UUID == poll.UUID) .ToList(); foreach (Vote oldVote in removedVotes) { _metricHandler.HandleVoteDeletedEvent(oldVote, poll.UUID); context.Votes.Remove(oldVote); } _metricHandler.HandlePollTypeChangedEvent(pollType, updateRequest.MaxPerVote ?? 0, updateRequest.MaxPoints ?? 0, poll.UUID); poll.PollType = pollType; poll.MaxPerVote = updateRequest.MaxPerVote; poll.MaxPoints = updateRequest.MaxPoints; poll.LastUpdatedUtc = DateTime.UtcNow; context.SaveChanges(); } }