コード例 #1
0
        private static void CreateAndAddVotingCandidate(int number, int numberOfVotes, IVotingCandidateRepository votingCandidateRepository, IVoteRepository voteRepository, ISongRepository songRepository)
        {
            var song = songRepository.Create();

            song.Title             = "SongRepositoryTests" + number;
            song.DurationInSeconds = 120;
            song.FileName          = "SongRepositoryTests.mp3";

            songRepository.Add(song);

            var votingCandidate = votingCandidateRepository.Create();

            votingCandidate.SongId       = song.Id;
            votingCandidate.Song         = song;
            votingCandidate.DisplayOrder = number;

            votingCandidateRepository.Add(votingCandidate);

            foreach (var index in Enumerable.Range(0, numberOfVotes))
            {
                var vote = voteRepository.Create();
                vote.VotingCandidateId = votingCandidate.Id;
                vote.VotingCandidate   = votingCandidate;
                vote.UserIdentifier    = Guid.NewGuid();

                voteRepository.Add(vote);
            }
        }
コード例 #2
0
        private IEnumerable <VotingCandidate> AddNewVotingCandidates(IEnumerable <Song> songs)
        {
            foreach (var item in songs.Select((song, index) => new { song, index }))
            {
                var votingCandidate = _votingCandidateRepository.Create();
                _votingCandidateRepository.Add(votingCandidate);

                votingCandidate.Map(song: item.song, displayOrder: item.index);

                yield return(votingCandidate);
            }
        }
コード例 #3
0
        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]);
        }