mirror of
https://github.com/ppy/osu.git
synced 2024-12-15 05:02:55 +08:00
Refactor new tests to not suck as much as the old importer tests
This commit is contained in:
parent
9939866f7d
commit
846291d203
@ -1,15 +1,19 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using osu.Framework.Allocation;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Database;
|
using osu.Game.Database;
|
||||||
using osu.Game.Overlays.Notifications;
|
using osu.Game.Overlays.Notifications;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Tests.Resources;
|
using osu.Game.Tests.Resources;
|
||||||
|
using Realms;
|
||||||
using SharpCompress.Archives;
|
using SharpCompress.Archives;
|
||||||
using SharpCompress.Archives.Zip;
|
using SharpCompress.Archives.Zip;
|
||||||
using SharpCompress.Common;
|
using SharpCompress.Common;
|
||||||
@ -24,58 +28,85 @@ namespace osu.Game.Tests.Database
|
|||||||
public class BeatmapImporterUpdateTests : RealmTest
|
public class BeatmapImporterUpdateTests : RealmTest
|
||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
public void TestImportThenUpdateWithNewDifficulty()
|
public void TestNewDifficultyAdded()
|
||||||
{
|
{
|
||||||
RunTestWithRealmAsync(async (realm, storage) =>
|
RunTestWithRealmAsync(async (realm, storage) =>
|
||||||
{
|
{
|
||||||
var importer = new BeatmapImporter(storage, realm);
|
var importer = new BeatmapImporter(storage, realm);
|
||||||
using var store = new RealmRulesetStore(realm, storage);
|
using var rulesets = new RealmRulesetStore(realm, storage);
|
||||||
|
|
||||||
string? pathOriginal = TestResources.GetTestBeatmapForImport();
|
using var __ = getBeatmapArchive(out string pathOriginal);
|
||||||
|
using var _ = getBeatmapArchiveWithModifications(out string pathMissingOneBeatmap, directory =>
|
||||||
|
{
|
||||||
|
// remove one difficulty before first import
|
||||||
|
directory.GetFiles("*.osu").First().Delete();
|
||||||
|
});
|
||||||
|
|
||||||
string pathMissingOneBeatmap = pathOriginal.Replace(".osz", "_missing_difficulty.osz");
|
var importBeforeUpdate = await importer.Import(new ImportTask(pathMissingOneBeatmap));
|
||||||
|
|
||||||
string extractedFolder = $"{pathOriginal}_extracted";
|
Assert.That(importBeforeUpdate, Is.Not.Null);
|
||||||
|
Debug.Assert(importBeforeUpdate != null);
|
||||||
|
|
||||||
|
checkCount<BeatmapSetInfo>(realm, 1, s => !s.DeletePending);
|
||||||
|
Assert.That(importBeforeUpdate.Value.Beatmaps, Has.Count.EqualTo(11));
|
||||||
|
|
||||||
|
// Second import matches first but contains one extra .osu file.
|
||||||
|
var importAfterUpdate = await importer.ImportAsUpdate(new ProgressNotification(), new ImportTask(pathOriginal), importBeforeUpdate.Value);
|
||||||
|
|
||||||
|
Assert.That(importAfterUpdate, Is.Not.Null);
|
||||||
|
Debug.Assert(importAfterUpdate != null);
|
||||||
|
|
||||||
|
checkCount<BeatmapInfo>(realm, 12);
|
||||||
|
checkCount<BeatmapMetadata>(realm, 12);
|
||||||
|
checkCount<BeatmapSetInfo>(realm, 1);
|
||||||
|
|
||||||
|
// check the newly "imported" beatmap is not the original.
|
||||||
|
Assert.That(importBeforeUpdate.ID, Is.Not.EqualTo(importAfterUpdate.ID));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkCount<T>(RealmAccess realm, int expected, Expression<Func<T, bool>>? condition = null) where T : RealmObject
|
||||||
|
{
|
||||||
|
var query = realm.Realm.All<T>();
|
||||||
|
|
||||||
|
if (condition != null)
|
||||||
|
query = query.Where(condition);
|
||||||
|
|
||||||
|
Assert.That(query, Has.Count.EqualTo(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IDisposable getBeatmapArchiveWithModifications(out string path, Action<DirectoryInfo> applyModifications)
|
||||||
|
{
|
||||||
|
var cleanup = getBeatmapArchive(out path);
|
||||||
|
|
||||||
|
string extractedFolder = $"{path}_extracted";
|
||||||
Directory.CreateDirectory(extractedFolder);
|
Directory.CreateDirectory(extractedFolder);
|
||||||
|
|
||||||
try
|
using (var zip = ZipArchive.Open(path))
|
||||||
{
|
|
||||||
using (var zip = ZipArchive.Open(pathOriginal))
|
|
||||||
zip.WriteToDirectory(extractedFolder);
|
zip.WriteToDirectory(extractedFolder);
|
||||||
|
|
||||||
// remove one difficulty before first import
|
applyModifications(new DirectoryInfo(extractedFolder));
|
||||||
new FileInfo(Directory.GetFiles(extractedFolder, "*.osu").First()).Delete();
|
|
||||||
|
File.Delete(path);
|
||||||
|
|
||||||
using (var zip = ZipArchive.Create())
|
using (var zip = ZipArchive.Create())
|
||||||
{
|
{
|
||||||
zip.AddAllFromDirectory(extractedFolder);
|
zip.AddAllFromDirectory(extractedFolder);
|
||||||
zip.SaveTo(pathMissingOneBeatmap, new ZipWriterOptions(CompressionType.Deflate));
|
zip.SaveTo(path, new ZipWriterOptions(CompressionType.Deflate));
|
||||||
}
|
}
|
||||||
|
|
||||||
var firstImport = await importer.Import(new ImportTask(pathMissingOneBeatmap));
|
|
||||||
Assert.That(firstImport, Is.Not.Null);
|
|
||||||
Debug.Assert(firstImport != null);
|
|
||||||
|
|
||||||
Assert.That(realm.Realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending), Has.Count.EqualTo(1));
|
|
||||||
Assert.That(realm.Realm.All<BeatmapSetInfo>().First(s => !s.DeletePending).Beatmaps, Has.Count.EqualTo(11));
|
|
||||||
|
|
||||||
// Second import matches first but contains one extra .osu file.
|
|
||||||
var secondImport = await importer.ImportAsUpdate(new ProgressNotification(), new ImportTask(pathOriginal), firstImport.Value);
|
|
||||||
Assert.That(secondImport, Is.Not.Null);
|
|
||||||
Debug.Assert(secondImport != null);
|
|
||||||
|
|
||||||
Assert.That(realm.Realm.All<BeatmapInfo>(), Has.Count.EqualTo(12));
|
|
||||||
Assert.That(realm.Realm.All<BeatmapMetadata>(), Has.Count.EqualTo(12));
|
|
||||||
Assert.That(realm.Realm.All<BeatmapSetInfo>(), Has.Count.EqualTo(1));
|
|
||||||
|
|
||||||
// check the newly "imported" beatmap is not the original.
|
|
||||||
Assert.That(firstImport.ID, Is.Not.EqualTo(secondImport.ID));
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Directory.Delete(extractedFolder, true);
|
Directory.Delete(extractedFolder, true);
|
||||||
|
|
||||||
|
return cleanup;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
private static IDisposable getBeatmapArchive(out string path, bool quick = true)
|
||||||
|
{
|
||||||
|
string beatmapPath = TestResources.GetTestBeatmapForImport(quick);
|
||||||
|
|
||||||
|
path = beatmapPath;
|
||||||
|
|
||||||
|
return new InvokeOnDisposal(() => File.Delete(beatmapPath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user