public async Task CollectResultAndLock_LocksCurrentVotingCandidates() { // Arrange var winnerOfVoting = new SongWithVoteCount { Song = new Song() }; var currentVotingCandidates = new[] { new VotingCandidate(), new VotingCandidate(), new VotingCandidate() }; _votingCandidateRepository.GetWinnerOfVotingWithVoteCountOrDefaultAsync().Returns(winnerOfVoting); _votingCandidateRepository.Get().Returns(currentVotingCandidates); // Act await _votingFinisher.CollectResultAndLockAsync(); // Assert Assert.That(currentVotingCandidates[0].IsActive, Is.False); Assert.That(currentVotingCandidates[1].IsActive, Is.False); Assert.That(currentVotingCandidates[2].IsActive, Is.False); }
private void RemoveExistingVotingCandidates() { // This will cascade delete all votes on the candidates var existingVotingCandidates = _votingCandidateRepository.Get(); foreach (var candidate in existingVotingCandidates) { _votingCandidateRepository.Remove(candidate); } }
public async Task UpdateOrCreate_RemovesAndAddsVotingCandidates() { // Arrange var existingCandidates = new[] { new VotingCandidate(), new VotingCandidate() }; var newCandidateSongs = new[] { new Song(), new Song(), new Song() }; _votingCandidateRepository.Get().Returns(existingCandidates); _votingCandidateRepository.Create().Returns(_ => new VotingCandidate()); // Act var votingCandidates = await _votingCandidateService.UpdateOrCreateAsync(newCandidateSongs); // Assert Assert.That(votingCandidates.Length, Is.EqualTo(newCandidateSongs.Length)); Assert.That(votingCandidates[0].SongId, Is.EqualTo(newCandidateSongs[0].Id)); Assert.That(votingCandidates[0].Song, Is.EqualTo(newCandidateSongs[0])); Assert.That(votingCandidates[0].DisplayOrder, Is.EqualTo(0)); Assert.That(votingCandidates[0].Votes, Is.Not.Null); Assert.That(votingCandidates[1].SongId, Is.EqualTo(newCandidateSongs[1].Id)); Assert.That(votingCandidates[1].Song, Is.EqualTo(newCandidateSongs[1])); Assert.That(votingCandidates[1].DisplayOrder, Is.EqualTo(1)); Assert.That(votingCandidates[1].Votes, Is.Not.Null); Assert.That(votingCandidates[2].SongId, Is.EqualTo(newCandidateSongs[2].Id)); Assert.That(votingCandidates[2].Song, Is.EqualTo(newCandidateSongs[2])); Assert.That(votingCandidates[2].DisplayOrder, Is.EqualTo(2)); Assert.That(votingCandidates[2].Votes, Is.Not.Null); _votingCandidateRepository.Received(1).Remove(existingCandidates[0]); _votingCandidateRepository.Received(1).Remove(existingCandidates[1]); _votingCandidateRepository.Received(3).Create(); _votingCandidateRepository.Received(1).Add(votingCandidates[0]); _votingCandidateRepository.Received(1).Add(votingCandidates[1]); _votingCandidateRepository.Received(1).Add(votingCandidates[2]); }
public async Task <SongWithVoteCount> CollectResultAndLockAsync() { var votingResult = await _votingCandidateRepository.GetWinnerOfVotingWithVoteCountOrDefaultAsync(); if (votingResult == null) { // If there are no voting candidates yet, we have to choose a random song. var randomSong = await _songRepository.GetRandomAsync(take : 1); votingResult = SongWithDefaultVoteCount(randomSong.First()); } var currentVotingCandidates = _votingCandidateRepository.Get(); foreach (var votingCandidate in currentVotingCandidates) { votingCandidate.IsActive = false; } _logger.LogInformation("The winner is {0}. Voting is locked now.", votingResult.Song.FileName); return(votingResult); }