Exemplo n.º 1
0
        public async Task VoteAsync_ShouldThrowNullIpException_WhenVoteIsNull()
        {
            await this.dbContext.Polls.AddAsync(new Poll
            {
                Id       = "1",
                IsActive = true,
                Options  = new List <PollOption>
                {
                    new PollOption {
                        Id = 1, Votes = 0
                    },
                    new PollOption {
                        Id = 2, Votes = 0
                    },
                    new PollOption {
                        Id = 3, Votes = 0
                    },
                }
            });

            await this.dbContext.SaveChangesAsync();

            var pollService = new PollService(this.dbContext);

            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = 1,
                VoteIp           = null
            };

            Assert.Throws <NullIpException>(() => pollService.VoteAsync(activePollVoteInputModel).GetAwaiter().GetResult());
        }
Exemplo n.º 2
0
        public async Task VoteAsync(ActivePollVoteInputModel activePollVoteInputModel)
        {
            CoreValidator.ThrowIfNull(activePollVoteInputModel.VoteIp, new NullIpException());

            var poll = await this.GetAsync(activePollVoteInputModel.Id);

            var isAlreadyVoted = poll.Votes
                                 .Any(v => v.Ip == activePollVoteInputModel.VoteIp);

            if (isAlreadyVoted)
            {
                throw new AlreadyVotedException();
            }

            var option = poll.Options
                         .SingleOrDefault(o => o.Id == activePollVoteInputModel.SelectedOptionId);

            CoreValidator.ThrowIfNull(option, new InvalidPollOptionException());

            poll.Votes.Add(new PollVote {
                Ip = activePollVoteInputModel.VoteIp
            });
            option.Votes++;
            await this.db.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task VoteAsync_ShouldThrowInvalidPollOptionException_WhenSelectedOptionNotExists()
        {
            await this.dbContext.Polls.AddAsync(new Poll
            {
                Id       = "1",
                IsActive = true,
                Options  = new List <PollOption>
                {
                    new PollOption {
                        Id = 1, Votes = 0
                    },
                    new PollOption {
                        Id = 2, Votes = 0
                    },
                    new PollOption {
                        Id = 3, Votes = 0
                    },
                }
            });

            await this.dbContext.SaveChangesAsync();

            var pollService = new PollService(this.dbContext);


            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = 5,
                VoteIp           = Guid.NewGuid().ToString()
            };

            Assert.Throws <InvalidPollOptionException>(() => pollService.VoteAsync(activePollVoteInputModel).GetAwaiter().GetResult());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Vote(ActivePollVoteInputModel activePollViewModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.Content(GetModelStateErrorMessages()));
            }

            await this.pollService.VoteAsync(activePollViewModel);

            return(this.Content(GlobalConstants.PollVoteThanks));
        }
Exemplo n.º 5
0
        public void VoteAsync_ShouldThrowInvalidPollException_WhenPollNotExists()
        {
            var pollService = new PollService(this.dbContext);

            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = 1,
                VoteIp           = Guid.NewGuid().ToString()
            };

            Assert.Throws <InvalidPollException>(() => pollService.VoteAsync(activePollVoteInputModel).GetAwaiter().GetResult());
        }
Exemplo n.º 6
0
        public async Task VoteAsync_ShouldAddNewVoteToCorrectOption_WhenPollAndSelectedOptionExists()
        {
            await this.dbContext.Polls.AddAsync(new Poll
            {
                Id       = "1",
                IsActive = true,
                Options  = new List <PollOption>
                {
                    new PollOption {
                        Id = 1, Votes = 0
                    },
                    new PollOption {
                        Id = 2, Votes = 0
                    },
                    new PollOption {
                        Id = 3, Votes = 0
                    },
                }
            });

            await this.dbContext.SaveChangesAsync();

            var pollService = new PollService(this.dbContext);

            var optionIdToVote           = 2;
            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = optionIdToVote,
                VoteIp           = Guid.NewGuid().ToString()
            };

            await pollService.VoteAsync(activePollVoteInputModel);

            var pollFromDb = this.dbContext.Polls.First();

            var optionToVote = pollFromDb.Options.First(o => o.Id == optionIdToVote);

            var expectedVotesCount = 1;
            var actualVotesCount   = optionToVote.Votes;

            Assert.Equal(expectedVotesCount, actualVotesCount);
        }
Exemplo n.º 7
0
        public async Task VoteAsync_ShouldThrowAlreadyVotedException_WhenVoteIpIsAlreadyAddedToThePoll()
        {
            var voteIp = Guid.NewGuid().ToString();

            await this.dbContext.Polls.AddAsync(new Poll
            {
                Id       = "1",
                IsActive = true,
                Options  = new List <PollOption>
                {
                    new PollOption {
                        Id = 1, Votes = 0
                    },
                    new PollOption {
                        Id = 2, Votes = 0
                    },
                    new PollOption {
                        Id = 3, Votes = 0
                    },
                },
                Votes = new List <PollVote>
                {
                    new PollVote {
                        Ip = voteIp
                    }
                }
            });

            await this.dbContext.SaveChangesAsync();

            var pollService = new PollService(this.dbContext);

            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = 1,
                VoteIp           = voteIp
            };

            Assert.Throws <AlreadyVotedException>(() => pollService.VoteAsync(activePollVoteInputModel).GetAwaiter().GetResult());
        }
Exemplo n.º 8
0
        public async Task VoteAsync_ShouldAssignIpToNewVote_WhenPollAndSelectedOptionExists()
        {
            await this.dbContext.Polls.AddAsync(new Poll
            {
                Id       = "1",
                IsActive = true,
                Options  = new List <PollOption>
                {
                    new PollOption {
                        Id = 1, Votes = 0
                    },
                    new PollOption {
                        Id = 2, Votes = 0
                    },
                    new PollOption {
                        Id = 3, Votes = 0
                    },
                }
            });

            await this.dbContext.SaveChangesAsync();

            var pollService = new PollService(this.dbContext);

            var voteIp = Guid.NewGuid().ToString();

            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = 1,
                VoteIp           = voteIp
            };

            await pollService.VoteAsync(activePollVoteInputModel);

            var pollFromDb = this.dbContext.Polls.First();
            var vote       = pollFromDb.Votes.SingleOrDefault(v => v.Ip == voteIp);

            Assert.NotNull(vote);
        }
Exemplo n.º 9
0
        public async Task VoteAsync_ShouldNotAddNewVoteToOtherOptions_WhenPollAndSelectedOptionExists()
        {
            await this.dbContext.Polls.AddAsync(new Poll
            {
                Id       = "1",
                IsActive = true,
                Options  = new List <PollOption>
                {
                    new PollOption {
                        Id = 1, Votes = 0
                    },
                    new PollOption {
                        Id = 2, Votes = 0
                    },
                    new PollOption {
                        Id = 3, Votes = 0
                    },
                }
            });

            await this.dbContext.SaveChangesAsync();

            var pollService = new PollService(this.dbContext);

            var optionIdToVote           = 2;
            var activePollVoteInputModel = new ActivePollVoteInputModel
            {
                Id = "1",
                SelectedOptionId = optionIdToVote,
                VoteIp           = Guid.NewGuid().ToString()
            };

            await pollService.VoteAsync(activePollVoteInputModel);

            var pollFromDb = this.dbContext.Polls.First();

            var notVotedOptions = pollFromDb.Options.Where(o => o.Id != optionIdToVote);

            Assert.True(notVotedOptions.All(o => o.Votes == 0));
        }