mirror of
https://github.com/ppy/osu.git
synced 2025-03-06 03:03:05 +08:00
fix test in #22653
This commit is contained in:
parent
52fc6de13f
commit
def6da98fc
@ -1,19 +1,26 @@
|
|||||||
// 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.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using osu.Framework.Extensions;
|
||||||
using osu.Framework.Extensions.ObjectExtensions;
|
using osu.Framework.Extensions.ObjectExtensions;
|
||||||
using osu.Framework.Platform;
|
using osu.Framework.Platform;
|
||||||
using osu.Framework.Testing;
|
using osu.Framework.Testing;
|
||||||
using osu.Game.Database;
|
using osu.Game.Database;
|
||||||
|
using osu.Game.Overlays.Notifications;
|
||||||
|
using Realms;
|
||||||
|
|
||||||
namespace osu.Game.Tests.Database
|
namespace osu.Game.Tests.Database
|
||||||
{
|
{
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class LegacyExporterTest
|
public class LegacyModelExporterTest
|
||||||
{
|
{
|
||||||
private TestLegacyExporter legacyExporter = null!;
|
private TestLegacyModelExporter legacyExporter = null!;
|
||||||
private TemporaryNativeStorage storage = null!;
|
private TemporaryNativeStorage storage = null!;
|
||||||
|
|
||||||
private const string short_filename = "normal file name";
|
private const string short_filename = "normal file name";
|
||||||
@ -25,15 +32,15 @@ namespace osu.Game.Tests.Database
|
|||||||
public void SetUp()
|
public void SetUp()
|
||||||
{
|
{
|
||||||
storage = new TemporaryNativeStorage("export-storage");
|
storage = new TemporaryNativeStorage("export-storage");
|
||||||
legacyExporter = new TestLegacyExporter(storage);
|
legacyExporter = new TestLegacyModelExporter(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ExportFileWithNormalNameTest()
|
public void ExportFileWithNormalNameTest()
|
||||||
{
|
{
|
||||||
var item = new TestPathInfo(short_filename);
|
var item = new TestRealmObject(short_filename);
|
||||||
|
|
||||||
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyExporter.MAX_FILENAME_LENGTH));
|
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
|
||||||
|
|
||||||
exportItemAndAssert(item, short_filename);
|
exportItemAndAssert(item, short_filename);
|
||||||
}
|
}
|
||||||
@ -41,9 +48,9 @@ namespace osu.Game.Tests.Database
|
|||||||
[Test]
|
[Test]
|
||||||
public void ExportFileWithNormalNameMultipleTimesTest()
|
public void ExportFileWithNormalNameMultipleTimesTest()
|
||||||
{
|
{
|
||||||
var item = new TestPathInfo(short_filename);
|
var item = new TestRealmObject(short_filename);
|
||||||
|
|
||||||
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyExporter.MAX_FILENAME_LENGTH));
|
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
|
||||||
|
|
||||||
//Export multiple times
|
//Export multiple times
|
||||||
for (int i = 0; i < 100; i++)
|
for (int i = 0; i < 100; i++)
|
||||||
@ -56,24 +63,24 @@ namespace osu.Game.Tests.Database
|
|||||||
[Test]
|
[Test]
|
||||||
public void ExportFileWithSuperLongNameTest()
|
public void ExportFileWithSuperLongNameTest()
|
||||||
{
|
{
|
||||||
int expectedLength = TestLegacyExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
|
int expectedLength = TestLegacyModelExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
|
||||||
string expectedName = long_filename.Remove(expectedLength);
|
string expectedName = long_filename.Remove(expectedLength);
|
||||||
|
|
||||||
var item = new TestPathInfo(long_filename);
|
var item = new TestRealmObject(long_filename);
|
||||||
|
|
||||||
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyExporter.MAX_FILENAME_LENGTH));
|
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
|
||||||
exportItemAndAssert(item, expectedName);
|
exportItemAndAssert(item, expectedName);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void ExportFileWithSuperLongNameMultipleTimesTest()
|
public void ExportFileWithSuperLongNameMultipleTimesTest()
|
||||||
{
|
{
|
||||||
int expectedLength = TestLegacyExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
|
int expectedLength = TestLegacyModelExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
|
||||||
string expectedName = long_filename.Remove(expectedLength);
|
string expectedName = long_filename.Remove(expectedLength);
|
||||||
|
|
||||||
var item = new TestPathInfo(long_filename);
|
var item = new TestRealmObject(long_filename);
|
||||||
|
|
||||||
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyExporter.MAX_FILENAME_LENGTH));
|
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
|
||||||
|
|
||||||
//Export multiple times
|
//Export multiple times
|
||||||
for (int i = 0; i < 100; i++)
|
for (int i = 0; i < 100; i++)
|
||||||
@ -83,9 +90,14 @@ namespace osu.Game.Tests.Database
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void exportItemAndAssert(IHasNamedFiles item, string expectedName)
|
private void exportItemAndAssert(TestRealmObject item, string expectedName)
|
||||||
{
|
{
|
||||||
Assert.DoesNotThrow(() => legacyExporter.Export(item));
|
// ReSharper disable once AsyncVoidLambda
|
||||||
|
Assert.DoesNotThrow(() =>
|
||||||
|
{
|
||||||
|
Task t = Task.Run(() => legacyExporter.ExportAsync(new TestRealmLive(item)));
|
||||||
|
t.WaitSafely();
|
||||||
|
});
|
||||||
Assert.That(storage.Exists($"exports/{expectedName}{legacyExporter.GetExtension()}"), Is.True);
|
Assert.That(storage.Exists($"exports/{expectedName}{legacyExporter.GetExtension()}"), Is.True);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,21 +108,7 @@ namespace osu.Game.Tests.Database
|
|||||||
storage.Dispose();
|
storage.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestPathInfo : IHasNamedFiles
|
private class TestLegacyModelExporter : LegacyModelExporter<TestRealmObject>
|
||||||
{
|
|
||||||
public string Filename { get; }
|
|
||||||
|
|
||||||
public IEnumerable<INamedFileUsage> Files { get; } = new List<INamedFileUsage>();
|
|
||||||
|
|
||||||
public TestPathInfo(string filename)
|
|
||||||
{
|
|
||||||
Filename = filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => Filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class TestLegacyModelExporter : LegacyModelExporter<IHasNamedFiles>
|
|
||||||
{
|
{
|
||||||
public TestLegacyModelExporter(Storage storage)
|
public TestLegacyModelExporter(Storage storage)
|
||||||
: base(storage)
|
: base(storage)
|
||||||
@ -119,7 +117,45 @@ namespace osu.Game.Tests.Database
|
|||||||
|
|
||||||
public string GetExtension() => FileExtension;
|
public string GetExtension() => FileExtension;
|
||||||
|
|
||||||
|
protected override void ExportToStream(TestRealmObject model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
protected override string FileExtension => ".test";
|
protected override string FileExtension => ".test";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class TestRealmObject : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
|
||||||
|
{
|
||||||
|
public Guid ID => throw new NotImplementedException();
|
||||||
|
public string Filename { get; }
|
||||||
|
|
||||||
|
public IEnumerable<INamedFileUsage> Files { get; } = new List<INamedFileUsage>();
|
||||||
|
|
||||||
|
public TestRealmObject(string filename)
|
||||||
|
{
|
||||||
|
Filename = filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => Filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestRealmLive : Live<TestRealmObject>
|
||||||
|
{
|
||||||
|
public override void PerformRead(Action<TestRealmObject> perform) => perform(Value);
|
||||||
|
|
||||||
|
public override TReturn PerformRead<TReturn>(Func<TestRealmObject, TReturn> perform) => perform(Value);
|
||||||
|
|
||||||
|
public override void PerformWrite(Action<TestRealmObject> perform) => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public override bool IsManaged => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public override TestRealmObject Value { get; }
|
||||||
|
|
||||||
|
public TestRealmLive(TestRealmObject model)
|
||||||
|
: base(Guid.Empty)
|
||||||
|
{
|
||||||
|
Value = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user