testing feature injector and factories

This commit is contained in:
andy 2021-10-15 23:38:24 +01:00
parent d139dce719
commit c6439db00b
8 changed files with 189 additions and 6 deletions

View File

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class AudioInjectorTests
{
[Fact]
public void Subscribe()
{
var watcherMock = new Mock<IPlayerWatcher>();
var spotifyMock = new Mock<ITracksClient>();
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
featureInjector.Subscribe();
watcherMock.VerifyAdd(m => m.ItemChange += It.IsAny<EventHandler<ListeningChangeEventArgs>>());
}
[Fact]
public void Unsubscribe()
{
var watcherMock = new Mock<IPlayerWatcher>();
var spotifyMock = new Mock<ITracksClient>();
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
featureInjector.Unsubscribe();
watcherMock.VerifyRemove(m => m.ItemChange -= It.IsAny<EventHandler<ListeningChangeEventArgs>>());
}
[Fact]
public void SubscribeFuncArg()
{
var watcherMock = new Mock<IPlayerWatcher>();
var watcherFuncArgMock = new Mock<IPlayerWatcher>();
var spotifyMock = new Mock<ITracksClient>();
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
featureInjector.Subscribe(watcherFuncArgMock.Object);
watcherFuncArgMock.VerifyAdd(m => m.ItemChange += It.IsAny<EventHandler<ListeningChangeEventArgs>>());
watcherMock.VerifyNoOtherCalls();
}
[Fact]
public void UnsubscribeFuncArg()
{
var watcherMock = new Mock<IPlayerWatcher>();
var watcherFuncArgMock = new Mock<IPlayerWatcher>();
var spotifyMock = new Mock<ITracksClient>();
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object);
featureInjector.Unsubscribe(watcherFuncArgMock.Object);
watcherFuncArgMock.VerifyRemove(m => m.ItemChange -= It.IsAny<EventHandler<ListeningChangeEventArgs>>());
watcherMock.VerifyNoOtherCalls();
}
[Fact]
public async void Callback()
{
var watcherMock = new Mock<IPlayerWatcher>();
var spotifyMock = new Mock<ITracksClient>();
var timelineMock = new Mock<AnalysedTrackTimeline>();
var eventArgsMock = new Mock<ListeningChangeEventArgs>();
var playingMock = new Mock<CurrentlyPlayingContext>();
var trackMock = new Mock<FullTrack>();
var featureMock = new Mock<TrackAudioFeatures>();
eventArgsMock.Object.Current = playingMock.Object;
playingMock.Object.Item = trackMock.Object;
spotifyMock.Setup(m => m.GetAudioFeatures(It.IsAny<string>()).Result).Returns(() => featureMock.Object);
var featureInjector = new AudioFeatureInjector(watcherMock.Object, spotifyMock.Object)
{
Timeline = timelineMock.Object
};
await featureInjector.AsyncCallback(eventArgsMock.Object);
spotifyMock.Verify(m => m.GetAudioFeatures(It.IsAny<string>()));
spotifyMock.VerifyNoOtherCalls();
timelineMock.Verify(m => m.Add(It.IsAny<AnalysedTrack>(), It.IsAny<DateTime>()), Times.Once);
timelineMock.VerifyNoOtherCalls();
}
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class AudioInjectorFactoryTests
{
[Fact]
public void Get()
{
var logMock = new Mock<ILoggerFactory>();
var configFactoryMock = new Mock<ISpotifyConfigFactory>();
var factory = new AudioFeatureInjectorFactory(logMock.Object);
var consumer = factory.Get(configFactoryMock.Object);
configFactoryMock.Verify(m => m.GetConfig(), Times.Once);
consumer.Should().NotBeNull();
}
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Xunit;
using Moq;
using FluentAssertions;
using SpotifyAPI.Web;
using Selector;
namespace Selector.Tests
{
public class RefreshTokenFactoryProviderTests
{
[Fact]
public void Init()
{
var provider = new RefreshTokenFactoryProvider();
provider.Initialised.Should().BeFalse();
provider.Initialise("a", "b");
provider.Initialised.Should().BeTrue();
provider.Initialise("a", "");
provider.Initialised.Should().BeFalse();
provider.Initialise(null, "b");
provider.Initialised.Should().BeFalse();
}
[Fact]
public void Get()
{
var provider = new RefreshTokenFactoryProvider();
provider.Initialise("a", "b");
var consumer = provider.GetFactory("a");
consumer.Should().NotBeNull();
consumer.Result.Should().NotBeNull();
}
}
}

View File

@ -38,7 +38,7 @@ namespace Selector
Task.Run(() => { return AsyncCallback(e); }, CancelToken); Task.Run(() => { return AsyncCallback(e); }, CancelToken);
} }
private async Task AsyncCallback(ListeningChangeEventArgs e) public async Task AsyncCallback(ListeningChangeEventArgs e)
{ {
if (e.Current.Item is FullTrack track) if (e.Current.Item is FullTrack track)
{ {

View File

@ -9,11 +9,11 @@ namespace Selector
{ {
public static class SpotifyExtensions public static class SpotifyExtensions
{ {
public static string DisplayString(this FullTrack track) => $"{track.Name} / {track.Album.Name} / {track.Artists.DisplayString()}"; public static string DisplayString(this FullTrack track) => $"{track.Name} / {track.Album?.Name} / {track.Artists?.DisplayString()}";
public static string DisplayString(this SimpleAlbum album) => $"{album.Name} / {album.Artists.DisplayString()}"; public static string DisplayString(this SimpleAlbum album) => $"{album.Name} / {album.Artists?.DisplayString()}";
public static string DisplayString(this SimpleArtist artist) => artist.Name; public static string DisplayString(this SimpleArtist artist) => artist.Name;
public static string DisplayString(this FullEpisode ep) => $"{ep.Name} / {ep.Show.DisplayString()}"; public static string DisplayString(this FullEpisode ep) => $"{ep.Name} / {ep.Show?.DisplayString()}";
public static string DisplayString(this SimpleShow show) => $"{show.Name} / {show.Publisher}"; public static string DisplayString(this SimpleShow show) => $"{show.Name} / {show.Publisher}";
@ -21,11 +21,11 @@ namespace Selector
if (currentPlaying.Item is FullTrack track) if (currentPlaying.Item is FullTrack track)
{ {
return $"{currentPlaying.IsPlaying}, {track.DisplayString()}, {currentPlaying.Device.DisplayString()}"; return $"{currentPlaying.IsPlaying}, {track.DisplayString()}, {currentPlaying.Device?.DisplayString()}";
} }
else if (currentPlaying.Item is FullEpisode episode) else if (currentPlaying.Item is FullEpisode episode)
{ {
return $"{currentPlaying.IsPlaying}, {episode.DisplayString()}, {currentPlaying.Device.DisplayString()}"; return $"{currentPlaying.IsPlaying}, {episode.DisplayString()}, {currentPlaying.Device?.DisplayString()}";
} }
else else
{ {