You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi.Core.Tests/Engine/V2/MusicSearchEngineV2Tests.cs

160 lines
8.8 KiB

using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Linq;
using AutoFixture;
using Hqub.MusicBrainz.API.Entities;
using Microsoft.EntityFrameworkCore.Internal;
using Moq;
using NUnit.Framework;
using Ombi.Api.MusicBrainz;
using Ombi.Core.Engine.V2;
using Ombi.Core.Models.Requests;
using Ombi.Core.Models.Search.V2.Music;
using Ombi.Core.Rule.Interfaces;
using Ombi.Core.Settings;
using Ombi.Helpers;
using Ombi.Settings.Settings.Models;
using Ombi.Settings.Settings.Models.External;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using Ombi.Test.Common;
namespace Ombi.Core.Tests.Engine.V2
{
[TestFixture]
public class MusicSearchEngineV2Tests
{
private MusicSearchEngineV2 _engine;
private Mock<IMusicBrainzApi> _musicApi;
private Fixture F;
[SetUp]
public void Setup()
{
F = new Fixture();
F.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
.ForEach(b => F.Behaviors.Remove(b));
F.Behaviors.Add(new OmitOnRecursionBehavior());
var principle = new Mock<IPrincipal>();
var requestService = new Mock<IRequestServiceMain>();
var ruleEval = new Mock<IRuleEvaluator>();
var um = MockHelper.MockUserManager(new List<OmbiUser>());
var cache = new Mock<ICacheService>();
var ombiSettings = new Mock<ISettingsService<OmbiSettings>>();
var requestSub = new Mock<IRepository<RequestSubscription>>();
_musicApi = new Mock<IMusicBrainzApi>();
var lidarrSettings = new Mock<ISettingsService<LidarrSettings>>();
_engine = new MusicSearchEngineV2(principle.Object, requestService.Object, ruleEval.Object,
um.Object, cache.Object, ombiSettings.Object, requestSub.Object, _musicApi.Object,
lidarrSettings.Object);
}
[Test]
public async Task GetBasicArtistInformation_SingleArtist_Test()
{
_musicApi.Setup(x => x.GetArtistInformation("pretend-artist-id")).ReturnsAsync(F.Create<Artist>());
var result = await _engine.GetArtistInformation("pretend-artist-id");
Assert.That(result, Is.Not.Null);
Assert.That(result.ReleaseGroups.Any(), Is.True, "Release Groups are null");
Assert.That(result.Members.Any(), Is.False, "Members somehow populated?");
}
[Test]
public async Task GetBasicArtistInformation_Group_Test()
{
var musicReturnVal = F.Build<Artist>().With(x => x.Relations, new List<Relation>
{
new Relation
{
TypeId = RelationLinks.BandMember,
Artist = new Artist
{
Name = "Mr Artist"
},
Attributes = new []{"a nobody"},
Begin = "1992",
End = "2019",
Ended = true
},
new Relation
{
TypeId = RelationLinks.BandMember,
Artist = new Artist
{
Name = "Mr Artist2"
},
Attributes = new []{"a nobody2"},
Begin = "1993",
}
});
_musicApi.Setup(x => x.GetArtistInformation("pretend-artist-id")).ReturnsAsync(musicReturnVal.Create());
var result = await _engine.GetArtistInformation("pretend-artist-id");
Assert.That(result, Is.Not.Null);
Assert.That(result.ReleaseGroups.Any(), Is.True, "Release Groups are null");
Assert.That(result.Members.Any(), Is.True, "Members IS NULL!");
Assert.That(result.Members.FirstOrDefault(x => x.Name == "Mr Artist").End, Is.EqualTo("2019"));
Assert.That(result.Members.FirstOrDefault(x => x.Name == "Mr Artist").Attributes.Length, Is.EqualTo(1));
Assert.That(result.Members.FirstOrDefault(x => x.Name == "Mr Artist").IsCurrentMember, Is.EqualTo(false));
Assert.That(result.Members.FirstOrDefault(x => x.Name == "Mr Artist").Start, Is.EqualTo("1992"));
Assert.That(result.Members.FirstOrDefault(x => x.Name == "Mr Artist2").IsCurrentMember, Is.EqualTo(true));
}
[TestCaseSource(nameof(LinksData))]
public async Task<string> GetBasicArtistInformation_Links_Test(string url, string typeId, Func<ArtistInformation, string> func)
{
var musicReturnVal = F.Build<Artist>().With(x => x.Relations, new List<Relation>
{
new Relation
{
TypeId = typeId,
Url = new Url
{
Resource = url
}
},
});
_musicApi.Setup(x => x.GetArtistInformation("pretend-artist-id")).ReturnsAsync(musicReturnVal.Create());
var result = await _engine.GetArtistInformation("pretend-artist-id");
Assert.That(result, Is.Not.Null);
return func(result);
}
private static IEnumerable<TestCaseData> LinksData
{
get
{
yield return new TestCaseData("twitter.com", RelationLinks.SocialNetwork, new Func<ArtistInformation, string>(artist => artist.Links.Twitter)).Returns("twitter.com").SetName("ArtistInformation_Links_Twitter");
yield return new TestCaseData("allmusic", RelationLinks.AllMusic, new Func<ArtistInformation, string>(artist => artist.Links.AllMusic)).Returns("allmusic").SetName("ArtistInformation_Links_AllMusic");
yield return new TestCaseData("bbcmusic", RelationLinks.BbcMusic, new Func<ArtistInformation, string>(artist => artist.Links.BbcMusic)).Returns("bbcmusic").SetName("ArtistInformation_Links_BbcMusic");
yield return new TestCaseData("discogs", RelationLinks.Discogs, new Func<ArtistInformation, string>(artist => artist.Links.Discogs)).Returns("discogs").SetName("ArtistInformation_Links_Discogs");
yield return new TestCaseData("homepage", RelationLinks.Homepage, new Func<ArtistInformation, string>(artist => artist.Links.HomePage)).Returns("homepage").SetName("ArtistInformation_Links_Homepage");
yield return new TestCaseData("imdb", RelationLinks.Imdb, new Func<ArtistInformation, string>(artist => artist.Links.Imdb)).Returns("imdb").SetName("ArtistInformation_Links_Imdb");
yield return new TestCaseData("lastfm", RelationLinks.LastFm, new Func<ArtistInformation, string>(artist => artist.Links.LastFm)).Returns("lastfm").SetName("ArtistInformation_Links_LastFm");
yield return new TestCaseData("myspace", RelationLinks.MySpace, new Func<ArtistInformation, string>(artist => artist.Links.MySpace)).Returns("myspace").SetName("ArtistInformation_Links_MySpace");
yield return new TestCaseData("onlinecommunity", RelationLinks.OnlineCommunity, new Func<ArtistInformation, string>(artist => artist.Links.OnlineCommunity)).Returns("onlinecommunity").SetName("ArtistInformation_Links_OnlineCommunity");
yield return new TestCaseData("www.facebook.com", RelationLinks.SocialNetwork, new Func<ArtistInformation, string>(artist => artist.Links.Facebook)).Returns("www.facebook.com").SetName("ArtistInformation_Links_Facebook");
yield return new TestCaseData("www.instagram.com", RelationLinks.SocialNetwork, new Func<ArtistInformation, string>(artist => artist.Links.Instagram)).Returns("www.instagram.com").SetName("ArtistInformation_Links_insta");
yield return new TestCaseData("www.vk.com", RelationLinks.SocialNetwork, new Func<ArtistInformation, string>(artist => artist.Links.Vk)).Returns("www.vk.com").SetName("ArtistInformation_Links_vk");
yield return new TestCaseData("app.spotify.com", RelationLinks.Streams, new Func<ArtistInformation, string>(artist => artist.Links.Spotify)).Returns("app.spotify.com").SetName("ArtistInformation_Links_Spotify");
yield return new TestCaseData("deezer.com", RelationLinks.Streams, new Func<ArtistInformation, string>(artist => artist.Links.Deezer)).Returns("deezer.com").SetName("ArtistInformation_Links_Deezer");
yield return new TestCaseData("play.google.com", RelationLinks.Download, new Func<ArtistInformation, string>(artist => artist.Links.Google)).Returns("play.google.com").SetName("ArtistInformation_Links_Google");
yield return new TestCaseData("itunes.apple.com", RelationLinks.Download, new Func<ArtistInformation, string>(artist => artist.Links.Apple)).Returns("itunes.apple.com").SetName("ArtistInformation_Links_Apple");
}
}
}
}