1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-22 20:52:57 +08:00
osu-lazer/osu.Game.Tests/Database/LegacyModelExporterTest.cs

142 lines
5.1 KiB
C#
Raw Normal View History

2023-02-16 21:26:57 +08:00
// 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.
2023-04-09 14:40:40 +08:00
using System;
2023-02-16 21:26:57 +08:00
using System.Collections.Generic;
2023-04-09 14:40:40 +08:00
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2023-02-16 21:26:57 +08:00
using NUnit.Framework;
2023-04-09 14:40:40 +08:00
using osu.Framework.Extensions;
2023-02-17 12:30:00 +08:00
using osu.Framework.Extensions.ObjectExtensions;
2023-02-16 21:26:57 +08:00
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Database;
2023-04-09 14:40:40 +08:00
using osu.Game.Overlays.Notifications;
using Realms;
2023-02-16 21:26:57 +08:00
namespace osu.Game.Tests.Database
{
2023-02-17 12:39:31 +08:00
[TestFixture]
2023-04-09 14:40:40 +08:00
public class LegacyModelExporterTest
2023-02-16 21:26:57 +08:00
{
2023-04-09 14:40:40 +08:00
private TestLegacyModelExporter legacyExporter = null!;
2023-02-17 12:30:00 +08:00
private TemporaryNativeStorage storage = null!;
2023-02-16 21:26:57 +08:00
2023-02-17 12:45:27 +08:00
private const string short_filename = "normal file name";
2023-02-17 12:39:24 +08:00
private const string long_filename =
"some file with super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name";
2023-02-16 21:26:57 +08:00
[SetUp]
2023-02-17 12:30:00 +08:00
public void SetUp()
2023-02-16 21:26:57 +08:00
{
storage = new TemporaryNativeStorage("export-storage");
2023-04-09 14:40:40 +08:00
legacyExporter = new TestLegacyModelExporter(storage);
2023-02-16 21:26:57 +08:00
}
[Test]
2023-02-17 06:44:45 +08:00
public void ExportFileWithNormalNameTest()
2023-02-16 21:26:57 +08:00
{
var item = new TestModel(short_filename);
2023-02-16 21:26:57 +08:00
2023-04-09 14:40:40 +08:00
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
2023-02-17 12:45:27 +08:00
exportItemAndAssert(item, short_filename);
2023-02-17 06:44:45 +08:00
}
[Test]
public void ExportFileWithNormalNameMultipleTimesTest()
{
var item = new TestModel(short_filename);
2023-02-17 06:44:45 +08:00
2023-04-09 14:40:40 +08:00
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
2023-02-17 06:44:45 +08:00
//Export multiple times
for (int i = 0; i < 100; i++)
2023-02-17 06:44:45 +08:00
{
2023-02-17 12:45:27 +08:00
string expectedFileName = i == 0 ? short_filename : $"{short_filename} ({i})";
exportItemAndAssert(item, expectedFileName);
2023-02-17 06:44:45 +08:00
}
}
[Test]
public void ExportFileWithSuperLongNameTest()
{
2023-04-09 14:40:40 +08:00
int expectedLength = TestLegacyModelExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
2023-02-17 12:39:24 +08:00
string expectedName = long_filename.Remove(expectedLength);
2023-02-17 06:44:45 +08:00
var item = new TestModel(long_filename);
2023-02-17 06:44:45 +08:00
2023-04-09 14:40:40 +08:00
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
exportItemAndAssert(item, expectedName);
2023-02-16 21:26:57 +08:00
}
[Test]
2023-02-17 06:44:45 +08:00
public void ExportFileWithSuperLongNameMultipleTimesTest()
2023-02-16 21:26:57 +08:00
{
2023-04-09 14:40:40 +08:00
int expectedLength = TestLegacyModelExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
2023-02-17 12:39:24 +08:00
string expectedName = long_filename.Remove(expectedLength);
2023-02-16 21:26:57 +08:00
var item = new TestModel(long_filename);
2023-02-16 21:26:57 +08:00
2023-04-09 14:40:40 +08:00
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyModelExporter.MAX_FILENAME_LENGTH));
2023-02-17 06:44:45 +08:00
//Export multiple times
for (int i = 0; i < 100; i++)
{
string expectedFilename = i == 0 ? expectedName : $"{expectedName} ({i})";
exportItemAndAssert(item, expectedFilename);
}
2023-02-17 06:44:45 +08:00
}
private void exportItemAndAssert(TestModel item, string expectedName)
2023-02-17 06:44:45 +08:00
{
2023-04-09 14:40:40 +08:00
Assert.DoesNotThrow(() =>
{
2023-05-05 15:31:48 +08:00
Task.Run(() => legacyExporter.ExportAsync(new RealmLiveUnmanaged<TestModel>(item))).WaitSafely();
2023-04-09 14:40:40 +08:00
});
Assert.That(storage.Exists($"exports/{expectedName}{legacyExporter.GetExtension()}"), Is.True);
2023-02-16 21:26:57 +08:00
}
[TearDown]
2023-02-17 12:30:00 +08:00
public void TearDown()
2023-02-16 21:26:57 +08:00
{
2023-02-17 12:30:00 +08:00
if (storage.IsNotNull())
storage.Dispose();
2023-02-16 21:26:57 +08:00
}
private class TestLegacyModelExporter : LegacyExporter<TestModel>
2023-04-09 14:40:40 +08:00
{
public TestLegacyModelExporter(Storage storage)
: base(storage)
{
}
public string GetExtension() => FileExtension;
2023-05-05 20:28:43 +08:00
public override void ExportToStream(TestModel model, Stream outputStream, ProgressNotification? notification, CancellationToken cancellationToken = default)
2023-04-09 14:40:40 +08:00
{
}
protected override string FileExtension => ".test";
}
private class TestModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
2023-02-16 21:26:57 +08:00
{
public Guid ID => Guid.Empty;
2023-02-17 12:30:00 +08:00
public string Filename { get; }
2023-02-16 21:26:57 +08:00
2023-02-17 12:30:00 +08:00
public IEnumerable<INamedFileUsage> Files { get; } = new List<INamedFileUsage>();
2023-02-16 21:26:57 +08:00
public TestModel(string filename)
2023-02-17 12:30:00 +08:00
{
Filename = filename;
}
2023-02-16 21:26:57 +08:00
2023-02-17 12:30:00 +08:00
public override string ToString() => Filename;
2023-02-16 21:26:57 +08:00
}
}
}