Add test coverage for edge cases

This commit is contained in:
Bartłomiej Dach 2023-05-07 15:20:38 +02:00
parent f0ec264bbc
commit af579be0e4
No known key found for this signature in database

View File

@ -28,6 +28,7 @@ namespace osu.Game.Tests.Beatmaps
[Test]
public void TestLocalCacheQueriedFirst()
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(true);
localCachedMetadataSourceMock.Setup(src => src.Lookup(It.IsAny<BeatmapInfo>()))
.Returns(new OnlineBeatmapMetadata { BeatmapID = 123456, BeatmapStatus = BeatmapOnlineStatus.Ranked });
apiMetadataSourceMock.Setup(src => src.Available).Returns(true);
@ -46,6 +47,7 @@ namespace osu.Game.Tests.Beatmaps
[Test]
public void TestAPIQueriedSecond()
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(true);
localCachedMetadataSourceMock.Setup(src => src.Lookup(It.IsAny<BeatmapInfo>()))
.Returns((OnlineBeatmapMetadata?)null);
apiMetadataSourceMock.Setup(src => src.Available).Returns(true);
@ -66,6 +68,7 @@ namespace osu.Game.Tests.Beatmaps
[Test]
public void TestPreferOnlineFetch()
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(true);
localCachedMetadataSourceMock.Setup(src => src.Lookup(It.IsAny<BeatmapInfo>()))
.Returns(new OnlineBeatmapMetadata { BeatmapID = 123456, BeatmapStatus = BeatmapOnlineStatus.Ranked });
apiMetadataSourceMock.Setup(src => src.Available).Returns(true);
@ -86,6 +89,7 @@ namespace osu.Game.Tests.Beatmaps
[Test]
public void TestPreferOnlineFetchFallsBackToLocalCacheIfOnlineSourceUnavailable()
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(true);
localCachedMetadataSourceMock.Setup(src => src.Lookup(It.IsAny<BeatmapInfo>()))
.Returns(new OnlineBeatmapMetadata { BeatmapID = 123456, BeatmapStatus = BeatmapOnlineStatus.Ranked });
apiMetadataSourceMock.Setup(src => src.Available).Returns(false);
@ -104,6 +108,7 @@ namespace osu.Game.Tests.Beatmaps
[Test]
public void TestMetadataLookupFailed()
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(true);
localCachedMetadataSourceMock.Setup(src => src.Lookup(It.IsAny<BeatmapInfo>()))
.Returns((OnlineBeatmapMetadata?)null);
apiMetadataSourceMock.Setup(src => src.Available).Returns(true);
@ -121,5 +126,52 @@ namespace osu.Game.Tests.Beatmaps
localCachedMetadataSourceMock.Verify(src => src.Lookup(beatmap), Times.Once);
apiMetadataSourceMock.Verify(src => src.Lookup(beatmap), Times.Once);
}
/// <remarks>
/// For the time being, if we fail to find a match in the local cache but online retrieval is not available, we trust the incoming beatmap verbatim wrt online ID.
/// While this is suboptimal as it implicitly trusts the contents of the beatmap,
/// throwing away the online data would be anti-user as it would make all beatmaps imported offline stop working in online.
/// TODO: revisit if/when we have a better flow of queueing metadata retrieval.
/// </remarks>
[Test]
public void TestLocalMetadataLookupFailedAndOnlineLookupIsUnavailable([Values] bool preferOnlineFetch)
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(true);
localCachedMetadataSourceMock.Setup(src => src.Lookup(It.IsAny<BeatmapInfo>()))
.Returns((OnlineBeatmapMetadata?)null);
apiMetadataSourceMock.Setup(src => src.Available).Returns(false);
var beatmap = new BeatmapInfo { OnlineID = 123456 };
var beatmapSet = new BeatmapSetInfo(beatmap.Yield());
beatmap.BeatmapSet = beatmapSet;
metadataLookup.Update(beatmapSet, preferOnlineFetch);
Assert.That(beatmap.Status, Is.EqualTo(BeatmapOnlineStatus.None));
Assert.That(beatmap.OnlineID, Is.EqualTo(123456));
}
/// <remarks>
/// For the time being, if there are no available metadata lookup sources, we trust the incoming beatmap verbatim wrt online ID.
/// While this is suboptimal as it implicitly trusts the contents of the beatmap,
/// throwing away the online data would be anti-user as it would make all beatmaps imported offline stop working in online.
/// TODO: revisit if/when we have a better flow of queueing metadata retrieval.
/// </remarks>
[Test]
public void TestNoAvailableSources()
{
localCachedMetadataSourceMock.Setup(src => src.Available).Returns(false);
apiMetadataSourceMock.Setup(src => src.Available).Returns(false);
var beatmap = new BeatmapInfo { OnlineID = 123456 };
var beatmapSet = new BeatmapSetInfo(beatmap.Yield());
beatmap.BeatmapSet = beatmapSet;
metadataLookup.Update(beatmapSet, preferOnlineFetch: false);
Assert.That(beatmap.OnlineID, Is.EqualTo(123456));
localCachedMetadataSourceMock.Verify(src => src.Lookup(beatmap), Times.Never);
apiMetadataSourceMock.Verify(src => src.Lookup(beatmap), Times.Never);
}
}
}