private static double GetRating(AdviseGroupContent adviseGroupContent) { return(adviseGroupContent.AdviseSets .Where(x => !x.IsDeleted) .Select(GetRating) .Average()); }
public void CalculateAdviseGroupRank_IfAdviseGroupContainsDeletedAdviseSets_ConsidersDeletedAdviseSetsForPlaybacksAge() { // Arrange var adviseGroupContent1 = new AdviseGroupContent("1", isFavorite: false); adviseGroupContent1.AddDisc(CreateTestDisc("1.1", new[] { CreateTestSong(11, RatingModel.R5, new DateTime(2021, 06, 26)) })); adviseGroupContent1.AddDisc(CreateTestDisc("1.2", new[] { CreateTestSong(12, RatingModel.R5, new DateTime(2021, 06, 28), isDeleted: true) })); var adviseGroupContent2 = CreateTestAdviseGroupContent("2", Enumerable.Range(1, 5).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 06, 27)))); var adviseGroupContent3 = CreateTestAdviseGroupContent("3", Enumerable.Range(6, 5).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 06, 29)))); var playbacksInfo = new PlaybacksInfo(new[] { adviseGroupContent1, adviseGroupContent2, adviseGroupContent3 }); var mocker = new AutoMocker(); var target = mocker.CreateInstance <AdviseRankCalculator>(); // Act var rank = target.CalculateAdviseGroupRank(adviseGroupContent1, playbacksInfo); // Assert // (discs number: 1 ^ 0.5) * (rating: 1.5 ^ 5) * (playbacks age: 1) rank.Should().Be(7.59375); }
public async Task GroupLibraryDiscs_IfNoFolderBelongsToAssignedGroupUpToRoot_AssignsDiscsFromSameFolderToOneAdviseGroupContent() { // Arrange var rootFolder = new FolderModel { Id = new ItemId("Root Folder") }; var folder1 = new FolderModel { Id = new ItemId("Folder 1"), Name = "Folder 1" }; var folder2 = new FolderModel { Id = new ItemId("Folder 2"), Name = "Folder 2" }; rootFolder.AddSubfolders(folder1, folder2); var folder11 = new FolderModel { Id = new ItemId("Folder 11") }; var folder21 = new FolderModel { Id = new ItemId("Folder 21") }; folder1.AddSubfolders(folder11); folder2.AddSubfolders(folder21); var disc11 = new DiscModel(); var disc12 = new DiscModel(); var disc21 = new DiscModel(); folder11.AddDiscs(disc11, disc12); folder21.AddDiscs(disc21); var mocker = new AutoMocker(); var target = mocker.CreateInstance <DiscGrouper>(); // Act var adviseGroups = await target.GroupLibraryDiscs(new[] { disc11, disc21, disc12 }, CancellationToken.None); // Assert var expectedAdviseGroup1 = new AdviseGroupContent("Folder Group: Folder 11", isFavorite: false); expectedAdviseGroup1.AddDisc(disc11); expectedAdviseGroup1.AddDisc(disc12); var expectedAdviseGroup2 = new AdviseGroupContent("Folder Group: Folder 21", isFavorite: false); expectedAdviseGroup2.AddDisc(disc21); var expectedAdviseGroups = new[] { expectedAdviseGroup1, expectedAdviseGroup2, }; adviseGroups.Should().BeEquivalentTo(expectedAdviseGroups, x => x.WithoutStrictOrdering()); }
public void CalculateAdviseGroupRank_IfAdviseGroupContainsDeletedAdviseSets_SkipsDeletedAdviseSetsWhenCountingSetsNumberAndAverageRating() { // Arrange var adviseGroupContent1 = new AdviseGroupContent("1", isFavorite: false); adviseGroupContent1.AddDisc(CreateTestDisc("1.1", new[] { CreateTestSong(11, RatingModel.R5, new DateTime(2021, 06, 26)) })); adviseGroupContent1.AddDisc(CreateTestDisc("1.2", new[] { CreateTestSong(12, RatingModel.R3, new DateTime(2021, 06, 27)) })); adviseGroupContent1.AddDisc(CreateTestDisc("1.3", new[] { CreateTestSong(13, RatingModel.R6, new DateTime(2021, 06, 28), isDeleted: true) })); var adviseGroupContent2 = CreateTestAdviseGroupContent("2", Enumerable.Range(1, 10).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 06, 29)))); var playbacksInfo = new PlaybacksInfo(new[] { adviseGroupContent1, adviseGroupContent2 }); var mocker = new AutoMocker(); var target = mocker.CreateInstance <AdviseRankCalculator>(); // Act var rank = target.CalculateAdviseGroupRank(adviseGroupContent1, playbacksInfo); // Assert // (discs number: 2 ^ 0.5) * (rating: 1.5 ^ 6) * (playbacks age: 1) rank.Should().BeApproximately(7.15945616, 0.000001); }
public void CalculateAdviseGroupRank_IfAdviseGroupContainsMultipleAdviseSets_ReturnsCorrectRank() { // Arrange var adviseGroupContent1 = new AdviseGroupContent("1", isFavorite: false); adviseGroupContent1.AddDisc(CreateTestDisc("1.1", new[] { CreateTestSong(11, RatingModel.R5, new DateTime(2021, 06, 26)) })); adviseGroupContent1.AddDisc(CreateTestDisc("1.2", new[] { CreateTestSong(12, RatingModel.R7, new DateTime(2021, 06, 27)) })); adviseGroupContent1.AddDisc(CreateTestDisc("1.3", new[] { CreateTestSong(13, RatingModel.R6, new DateTime(2021, 06, 28)) })); var adviseGroupContent2 = CreateTestAdviseGroupContent("2", Enumerable.Range(1, 10).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 06, 29)))); var playbacksInfo = new PlaybacksInfo(new[] { adviseGroupContent1, adviseGroupContent2 }); var mocker = new AutoMocker(); var target = mocker.CreateInstance <AdviseRankCalculator>(); // Act var rank = target.CalculateAdviseGroupRank(adviseGroupContent1, playbacksInfo); // Assert // (discs number: 3 ^ 0.5) * (rating: 1.5 ^ 6) * (playbacks age: 1) rank.Should().BeApproximately(19.729141, 0.000001); }
public void CalculateAdviseSetRank_ForAdviseSetWithMultipleDiscs_ReturnsCorrectRank() { // Arrange var disc1 = CreateTestDisc("1.1", new[] { CreateTestSong(1, RatingModel.R5, new DateTime(2021, 08, 28)) }); var disc2 = CreateTestDisc("1.2", new[] { CreateTestSong(2, RatingModel.R7, new DateTime(2021, 08, 28)) }); var adviseSet = new AdviseSetModel { Id = new ItemId("1") }; disc1.AdviseSetInfo = new AdviseSetInfo(adviseSet, 1); disc2.AdviseSetInfo = new AdviseSetInfo(adviseSet, 2); var adviseGroupContent1 = new AdviseGroupContent("1", isFavorite: false); adviseGroupContent1.AddDisc(disc1); adviseGroupContent1.AddDisc(disc2); var adviseGroupContent2 = CreateTestAdviseGroupContent("2", Enumerable.Range(11, 10).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 08, 27)))); var adviseGroupContent3 = CreateTestAdviseGroupContent("3", Enumerable.Range(21, 10).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 08, 29)))); var playbacksInfo = new PlaybacksInfo(new[] { adviseGroupContent1, adviseGroupContent2, adviseGroupContent3 }); var mocker = new AutoMocker(); var target = mocker.CreateInstance <AdviseRankCalculator>(); // Act var rank = target.CalculateAdviseSetRank(adviseGroupContent1.AdviseSets.Single(), playbacksInfo); // Assert // (rating: 1.5 ^ 6) * (playbacks age: 1) rank.Should().Be(11.390625); }
public double CalculateAdviseGroupRank(AdviseGroupContent adviseGroup, PlaybacksInfo playbacksInfo) { var playbacksPassed = playbacksInfo.GetPlaybacksPassed(adviseGroup); return(playbacksPassed == Int32.MaxValue ? MaxRank : GetFactorForAdviseGroupSize(adviseGroup.AdviseSets.Count(x => !x.IsDeleted)) * GetFactorForAverageRating(GetRating(adviseGroup)) * GetFactorForPlaybackAge(playbacksPassed)); }
private static AdviseGroupContent CreateTestAdviseGroupContent(string id, IEnumerable <SongModel> songs) { var disc = CreateTestDisc(id, songs); var adviseGroupContent = new AdviseGroupContent(id, isFavorite: false); adviseGroupContent.AddDisc(disc); return(adviseGroupContent); }
public static AdviseGroupContent ToAdviseGroup(this AdviseSetContent adviseSetContent) { var adviseGroup = new AdviseGroupContent(adviseSetContent.Id, isFavorite: false); foreach (var disc in adviseSetContent.Discs) { adviseGroup.AddDisc(disc); } return(adviseGroup); }
private static AdviseGroupContent CreateAdviseGroup(string id, bool isFavorite, params AdviseSetContent[] adviseSets) { var adviseGroup = new AdviseGroupContent(id, isFavorite: isFavorite); foreach (var adviseSet in adviseSets) { foreach (var disc in adviseSet.Discs) { adviseGroup.AddDisc(disc); } } return(adviseGroup); }
public async Task Advise_SortsAdviseSetsWithinAdviseGroup() { // Arrange var disc1 = CreateTestDisc("Disc 1"); var disc2 = CreateTestDisc("Disc 2"); var adviseGroup = new AdviseGroupContent("1", isFavorite: false); adviseGroup.AddDisc(disc1); adviseGroup.AddDisc(disc2); var adviseSets = adviseGroup.AdviseSets.ToList(); var playbacksInfo = new PlaybacksInfo(new[] { adviseGroup }); var adviseContentSorterStub = new Mock <IAdviseContentSorter>(); adviseContentSorterStub.Setup(x => x.SortAdviseGroups(It.IsAny <IEnumerable <AdviseGroupContent> >(), playbacksInfo)) .Returns <IEnumerable <AdviseGroupContent>, PlaybacksInfo>((adviseGroups, _) => adviseGroups); adviseContentSorterStub.Setup(x => x.SortAdviseSets(adviseSets, playbacksInfo)).Returns(new[] { adviseSets[1], adviseSets[0] }); var mocker = new AutoMocker(); mocker.Use(adviseContentSorterStub); var target = mocker.CreateInstance <RankBasedAdviser>(); // Act var advises = await target.Advise(new[] { adviseGroup }, playbacksInfo, CancellationToken.None); // Assert var expectedAdvises = new[] { AdvisedPlaylist.ForAdviseSet(adviseSets[1]), }; advises.Should().BeEquivalentTo(expectedAdvises, x => x.WithStrictOrdering()); }
public async Task Advise_IfSomeAdviseSetsAreDeleted_SkipsDeletedAdviseSets() { // Arrange var deletedDisc = CreateTestDisc("1", isDeleted: true); var activeDisc = CreateTestDisc("2", isDeleted: false); var adviseGroup = new AdviseGroupContent("1", isFavorite: false); adviseGroup.AddDisc(deletedDisc); adviseGroup.AddDisc(activeDisc); var adviseGroups = new[] { adviseGroup }; var playbacksInfo = new PlaybacksInfo(adviseGroups); var adviseContentSorterStub = new Mock <IAdviseContentSorter>(); adviseContentSorterStub.Setup(x => x.SortAdviseGroups(It.IsAny <IEnumerable <AdviseGroupContent> >(), playbacksInfo)) .Returns <IEnumerable <AdviseGroupContent>, PlaybacksInfo>((adviseGroups, _) => adviseGroups); adviseContentSorterStub.Setup(x => x.SortAdviseSets(It.IsAny <IEnumerable <AdviseSetContent> >(), playbacksInfo)) .Returns <IEnumerable <AdviseSetContent>, PlaybacksInfo>((adviseSets, _) => adviseSets); var mocker = new AutoMocker(); mocker.Use(adviseContentSorterStub); var target = mocker.CreateInstance <RankBasedAdviser>(); // Act var advises = await target.Advise(adviseGroups, playbacksInfo, CancellationToken.None); // Assert var expectedAdvises = new[] { AdvisedPlaylist.ForAdviseSet(activeDisc.ToAdviseSet()), }; advises.Should().BeEquivalentTo(expectedAdvises, x => x.WithStrictOrdering()); }
public void SortAdviseGroups_SortsAdviseGroupsByTheirRank() { // Arrange var adviseGroup1 = new AdviseGroupContent("group1", isFavorite: false); var adviseGroup2 = new AdviseGroupContent("group2", isFavorite: false); var adviseGroup3 = new AdviseGroupContent("group3", isFavorite: false); var playbacksInfo = new PlaybacksInfo(Enumerable.Empty <AdviseGroupContent>()); var adviseRankCalculatorStub = new Mock <IAdviseRankCalculator>(); adviseRankCalculatorStub.Setup(x => x.CalculateAdviseGroupRank(adviseGroup1, playbacksInfo)).Returns(0.25); adviseRankCalculatorStub.Setup(x => x.CalculateAdviseGroupRank(adviseGroup2, playbacksInfo)).Returns(0.75); adviseRankCalculatorStub.Setup(x => x.CalculateAdviseGroupRank(adviseGroup3, playbacksInfo)).Returns(0.50); var mocker = new AutoMocker(); mocker.Use(adviseRankCalculatorStub); var target = mocker.CreateInstance <RankBasedAdviseContentSorter>(); // Act var result = target.SortAdviseGroups(new[] { adviseGroup1, adviseGroup2, adviseGroup3 }, playbacksInfo).ToList(); // Assert var expectedAdviseGroups = new[] { adviseGroup2, adviseGroup3, adviseGroup1, }; result.Should().BeEquivalentTo(expectedAdviseGroups, x => x.WithStrictOrdering()); }
public void CalculateAdviseGroupRank_ForAdviseGroupWithoutPlaybacks_ReturnsMaxRank() { // Arrange var adviseGroupContent1 = new AdviseGroupContent("1", isFavorite: false); adviseGroupContent1.AddDisc(CreateTestDisc("1.1", new[] { CreateTestSong(11, lastPlaybackTime: null) })); adviseGroupContent1.AddDisc(CreateTestDisc("1.2", new[] { CreateTestSong(12, lastPlaybackTime: null) })); var adviseGroupContent2 = CreateTestAdviseGroupContent("2", Enumerable.Range(1, 10).Select(id => CreateTestSong(id, RatingModel.R5, new DateTime(2021, 06, 29)))); var playbacksInfo = new PlaybacksInfo(new[] { adviseGroupContent1, adviseGroupContent2 }); var mocker = new AutoMocker(); var target = mocker.CreateInstance <AdviseRankCalculator>(); // Act var rank = target.CalculateAdviseGroupRank(adviseGroupContent1, playbacksInfo); // Assert rank.Should().Be(Double.MaxValue); }
public int GetPlaybacksPassed(AdviseGroupContent adviseGroupContent) { return(adviseGroupContent.AdviseSets.Select(GetPlaybacksPassed).Min()); }
public async Task GroupLibraryDiscs_ForDiscsFromFolderBelongingToAdviseGroup_AssignsDiscsToSameAdviseGroupContent() { // Arrange var rootAdviseGroup = new AdviseGroupModel { Id = new ItemId("Advise Group Root") }; var adviseGroup1 = new AdviseGroupModel { Id = new ItemId("Advise Group 1") }; var adviseGroup2 = new AdviseGroupModel { Id = new ItemId("Advise Group 2") }; var adviseGroup11 = new AdviseGroupModel { Id = new ItemId("Advise Group 11") }; var adviseGroup21 = new AdviseGroupModel { Id = new ItemId("Advise Group 21"), IsFavorite = true }; var rootFolder = new FolderModel { Id = new ItemId("Root Folder"), AdviseGroup = rootAdviseGroup }; var folder1 = new FolderModel { Id = new ItemId("Folder 1"), Name = "Folder 1", AdviseGroup = adviseGroup1 }; var folder2 = new FolderModel { Id = new ItemId("Folder 2"), Name = "Folder 2", AdviseGroup = adviseGroup2 }; rootFolder.AddSubfolders(folder1, folder2); var folder11 = new FolderModel { Id = new ItemId("Folder 11"), AdviseGroup = adviseGroup11 }; var folder21 = new FolderModel { Id = new ItemId("Folder 21"), AdviseGroup = adviseGroup21 }; folder1.AddSubfolders(folder11); folder2.AddSubfolders(folder21); var disc11 = new DiscModel(); var disc12 = new DiscModel(); var disc21 = new DiscModel(); folder11.AddDiscs(disc11, disc12); folder21.AddDiscs(disc21); var mocker = new AutoMocker(); var target = mocker.CreateInstance <DiscGrouper>(); // Act var adviseGroups = await target.GroupLibraryDiscs(new[] { disc11, disc12, disc21 }, CancellationToken.None); // Assert var expectedAdviseGroup1 = new AdviseGroupContent("Advise Group: Advise Group 11", isFavorite: false); expectedAdviseGroup1.AddDisc(disc11); expectedAdviseGroup1.AddDisc(disc12); var expectedAdviseGroup2 = new AdviseGroupContent("Advise Group: Advise Group 21", isFavorite: true); expectedAdviseGroup2.AddDisc(disc21); var expectedAdviseGroups = new[] { expectedAdviseGroup1, expectedAdviseGroup2, }; adviseGroups.Should().BeEquivalentTo(expectedAdviseGroups, x => x.WithoutStrictOrdering()); }