예제 #1
0
        public void ReturnCorrectPlaytime_WhenParamsAreValid()
        {
            // Arrange
            var options = Utils.GetOptions(nameof(ReturnCorrectPlaytime_WhenParamsAreValid));

            Track firstTrack = new Track()
            {
                Id            = 5,
                ArtistId      = 1,
                TrackDuration = 218
            };

            Track secondTrack = new Track()
            {
                Id            = 6,
                ArtistId      = 1,
                TrackDuration = 257
            };

            var playlist = new List <Track>()
            {
                firstTrack, secondTrack
            };

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            using (var assertContext = new RidePalDbContext(options))
            {
                //Act
                var sut      = new GeneratePlaylistService(assertContext, dateTimeProviderMock.Object);
                var result   = sut.CalculatePlaytime(playlist);
                var expected = 475.0;

                //Assert
                Assert.AreEqual(result, expected);
            }
        }
        public void ReturnPlaylistWithCorrectPlaytime()
        {
            // Arrange
            var options = Utils.GetOptions(nameof(ReturnPlaylistWithCorrectPlaytime));

            Track firstTrack = new Track()
            {
                Id            = 7,
                ArtistId      = 1,
                TrackDuration = 249
            };

            Track secondTrack = new Track()
            {
                Id            = 8,
                ArtistId      = 1,
                TrackDuration = 272
            };

            Track thirdTrack = new Track()
            {
                Id            = 9,
                ArtistId      = 1,
                TrackDuration = 262
            };

            Track fourthTrack = new Track()
            {
                Id            = 10,
                ArtistId      = 1,
                TrackDuration = 246
            };

            Track fifthTrack = new Track()
            {
                Id            = 11,
                ArtistId      = 1,
                TrackDuration = 218
            };

            var playlist = new List <Track>()
            {
                firstTrack, secondTrack, thirdTrack, fourthTrack, fifthTrack
            };

            double travelDuration = 900.0;

            var genrePercentage = new Dictionary <string, int>()
            {
                { "rock", 20 },
                { "metal", 20 },
                { "pop", 40 },
                { "jazz", 20 },
            };

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            using (var assertContext = new RidePalDbContext(options))
            {
                //Act
                var sut           = new GeneratePlaylistService(assertContext, dateTimeProviderMock.Object);
                var result        = sut.FinetunePlaytime(travelDuration, playlist, genrePercentage).ToList();
                var finalPlaytime = sut.CalculatePlaytime(result);
                var minPlaytime   = travelDuration - 300;
                var maxPlaytime   = travelDuration + 300;

                //Assert
                Assert.IsTrue(finalPlaytime > minPlaytime);
                Assert.IsTrue(finalPlaytime < maxPlaytime);
            }
        }
예제 #3
0
        public void ReturnPlaylistWithCorrectPlaytime()
        {
            // Arrange
            var options = Utils.GetOptions(nameof(ReturnPlaylistWithCorrectPlaytime));

            Genre rock = new Genre()
            {
                Id   = 30,
                Name = "rock"
            };

            Genre pop = new Genre()
            {
                Id   = 31,
                Name = "pop"
            };

            Track firstTrack = new Track()
            {
                Id            = 17,
                ArtistId      = 1,
                TrackDuration = 249,
                GenreId       = 30,
                Genre         = rock
            };

            Track secondTrack = new Track()
            {
                Id            = 18,
                ArtistId      = 1,
                TrackDuration = 272,
                GenreId       = 30,
                Genre         = rock
            };

            Track thirdTrack = new Track()
            {
                Id            = 19,
                ArtistId      = 1,
                TrackDuration = 262,
                GenreId       = 30,
                Genre         = rock
            };

            Track fourthTrack = new Track()
            {
                Id            = 20,
                ArtistId      = 1,
                TrackDuration = 246,
                GenreId       = 30,
                Genre         = rock
            };

            Track fifthTrack = new Track()
            {
                Id            = 21,
                ArtistId      = 1,
                TrackDuration = 218,
                GenreId       = 31,
                Genre         = pop
            };

            Track sixthTrack = new Track()
            {
                Id            = 22,
                ArtistId      = 1,
                TrackDuration = 252,
                GenreId       = 31,
                Genre         = pop
            };

            Track seventhTrack = new Track()
            {
                Id            = 23,
                ArtistId      = 1,
                TrackDuration = 218,
                GenreId       = 31,
                Genre         = pop
            };

            Track eightTrack = new Track()
            {
                Id            = 24,
                ArtistId      = 1,
                TrackDuration = 252,
                GenreId       = 31,
                Genre         = pop
            };

            using (var arrangeContext = new RidePalDbContext(options))
            {
                arrangeContext.Genres.Add(rock);
                arrangeContext.Genres.Add(pop);
                arrangeContext.Tracks.Add(firstTrack);
                arrangeContext.Tracks.Add(secondTrack);
                arrangeContext.Tracks.Add(thirdTrack);
                arrangeContext.Tracks.Add(fourthTrack);
                arrangeContext.Tracks.Add(fifthTrack);
                arrangeContext.Tracks.Add(sixthTrack);
                arrangeContext.Tracks.Add(seventhTrack);
                arrangeContext.Tracks.Add(eightTrack);
                arrangeContext.SaveChanges();
            }

            double travelDuration = 660.0;

            var genrePercentage = new Dictionary <string, int>()
            {
                { "rock", 50 },
                { "metal", 0 },
                { "pop", 50 },
                { "jazz", 0 },
            };

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            string genre = "pop";

            using (var assertContext = new RidePalDbContext(options))
            {
                //Act
                var sut    = new GeneratePlaylistService(assertContext, dateTimeProviderMock.Object);
                var result = sut.GetTracks(genre, travelDuration, genrePercentage, true).Result.ToList();

                var finalPlaytime = sut.CalculatePlaytime(result);

                //Assert
                Assert.IsTrue(finalPlaytime >= travelDuration / 2);
                Assert.IsTrue(result.Count > 1);
            }
        }