Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktAlkin committed Apr 2, 2022
1 parent 17d3f2b commit ddec832
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 9 deletions.
1 change: 1 addition & 0 deletions Backend.Tests.Spotify/SpotifyOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SpotifyOperationsTests : BaseTests
public override void SetUp()
{
base.SetUp();
Assert.IsNotNull(ConnectionManager.Instance.GetSavedToken());
ConnectionManager.Instance.TryInitFromSavedToken().Wait();
}

Expand Down
6 changes: 6 additions & 0 deletions Backend.Tests/SpotifyOperationsMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace Backend.Tests
{
public class SpotifyOperationsMockTests : BaseTests
{
[SetUp]
public override void SetUp()
{
REQUIRES_DB = true;
base.SetUp();
}
[Test]
public async Task GetPlaylistItems()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterAcousticnessNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.AcousticnessPercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.AcousticnessPercent;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterDanceabilityNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.DanceabilityPercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.DanceabilityPercent;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterEnergyNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.EnergyPercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.EnergyPercent;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterInstrumentalnessNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.InstrumentalnessPercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.InstrumentalnessPercent;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterLivenessNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.LivenessPercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.LivenessPercent;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterLoudnessNode : FilterRangeNode
{
protected override int? GetValue(Track t) => (int)t.AudioFeatures.Loudness;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => (int)t.AudioFeatures?.Loudness;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterSpeechinessNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.SpeechinessPercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.SpeechinessPercent;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterTempoNode : FilterRangeNode
{
protected override int? GetValue(Track t) => (int)t.AudioFeatures.Tempo;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => (int)t.AudioFeatures?.Tempo;
public override bool RequiresAudioFeatures => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Backend.Entities.GraphNodes.AudioFeaturesFilters
{
public class FilterValenceNode : FilterRangeNode
{
protected override int? GetValue(Track t) => t.AudioFeatures.ValencePercent;
// if db is pre-AudioFeatures even including AudioFeatures results in AudioFeature being null
protected override int? GetValue(Track t) => t.AudioFeatures?.ValencePercent;
public override bool RequiresAudioFeatures => true;
}
}
48 changes: 48 additions & 0 deletions Tests.Util/SpotifyClientMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,65 @@ public ISpotifyClient SetUp(
trackMock.Setup(track => track.Get(It.IsAny<string>(), It.IsAny<TrackRequest>()))
.Returns((string trackId, TrackRequest req) =>
Task.FromResult(Tracks.FirstOrDefault(t => t.Id == trackId)));
trackMock.Setup(track => track.GetSeveralAudioFeatures(It.IsAny<TracksAudioFeaturesRequest>()))
.Returns((TracksAudioFeaturesRequest req) =>
{
var response = new TracksAudioFeaturesResponse
{
// TODO this is not tested yet and is only here to avoid SyncLibrary failing
AudioFeatures = req.Ids.Select(id => new TrackAudioFeatures
{
Id = id,
Acousticness = 0.5f,
AnalysisUrl = "http://some.url/",
Danceability = 0.5f,
DurationMs = 123,
Energy = 0.5f,
Instrumentalness = 0.5f,
Key = 3,
Liveness = 0.5f,
Loudness = 0.5f,
Mode = 0,
Speechiness = 0.5f,
Tempo = 123,
TimeSignature = 4,
TrackHref = "http://some.href/",
Type = "SomeType",
Uri = "http://some.uri",
Valence = 0.5f,
}).ToList(),
};
return Task.FromResult(response);
});

var userMock = new Mock<IUserProfileClient>();
userMock.Setup(user => user.Current())
.Returns(() => Task.FromResult(User));

// TODO this was not tested yet
var artistMock = new Mock<IArtistsClient>();
artistMock.Setup(artist => artist.GetSeveral(It.IsAny<ArtistsRequest>()))
.Returns((ArtistsRequest req) =>
{
var response = new ArtistsResponse
{
Artists = req.Ids.Select(id => new FullArtist
{
Id = id, // TODO this is technically not needed (only genres are needed)
Genres = new List<string> { "genre1", "genre2" },
Name = $"{id}.Name", // TODO this is technically not needed (only genres are needed)
}).ToList(),
};
return Task.FromResult(response);
});

mock.SetupGet(client => client.Library).Returns(libraryMock.Object);
mock.SetupGet(client => client.Playlists).Returns(playlistsMock.Object);
mock.SetupGet(client => client.Follow).Returns(followMock.Object);
mock.SetupGet(client => client.Albums).Returns(albumMock.Object);
mock.SetupGet(client => client.Tracks).Returns(trackMock.Object);
mock.SetupGet(client => client.UserProfile).Returns(userMock.Object);
mock.SetupGet(client => client.Artists).Returns(artistMock.Object);


SpotifyClient = mock.Object;
Expand Down

0 comments on commit ddec832

Please sign in to comment.