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

126 lines
4.6 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.
using System.Collections.Generic;
using NUnit.Framework;
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;
namespace osu.Game.Tests.Database
{
2023-02-17 12:39:31 +08:00
[TestFixture]
2023-02-16 21:26:57 +08:00
public class LegacyExporterTest
{
2023-02-17 12:30:00 +08:00
private TestLegacyExporter legacyExporter = null!;
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");
legacyExporter = new TestLegacyExporter(storage);
}
[Test]
2023-02-17 06:44:45 +08:00
public void ExportFileWithNormalNameTest()
2023-02-16 21:26:57 +08:00
{
2023-02-17 12:45:27 +08:00
var item = new TestPathInfo(short_filename);
2023-02-16 21:26:57 +08:00
2023-02-17 12:39:24 +08:00
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyExporter.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()
{
2023-02-17 12:45:27 +08:00
var item = new TestPathInfo(short_filename);
2023-02-17 06:44:45 +08:00
2023-02-17 12:45:27 +08:00
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyExporter.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-02-17 12:39:24 +08:00
int expectedLength = TestLegacyExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
string expectedName = long_filename.Remove(expectedLength);
2023-02-17 06:44:45 +08:00
2023-02-17 12:39:24 +08:00
var item = new TestPathInfo(long_filename);
2023-02-17 06:44:45 +08:00
2023-02-17 12:45:27 +08:00
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyExporter.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-02-17 12:39:24 +08:00
int expectedLength = TestLegacyExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
string expectedName = long_filename.Remove(expectedLength);
2023-02-16 21:26:57 +08:00
2023-02-17 12:39:24 +08:00
var item = new TestPathInfo(long_filename);
2023-02-16 21:26:57 +08:00
2023-02-17 12:45:27 +08:00
Assert.That(item.Filename.Length, Is.GreaterThan(TestLegacyExporter.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(IHasNamedFiles item, string expectedName)
2023-02-17 06:44:45 +08:00
{
2023-02-17 12:30:00 +08:00
Assert.DoesNotThrow(() => legacyExporter.Export(item));
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 TestPathInfo : IHasNamedFiles
{
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
2023-02-17 12:30:00 +08:00
public TestPathInfo(string filename)
{
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
}
private class TestLegacyExporter : LegacyExporter<IHasNamedFiles>
{
2023-02-16 21:42:07 +08:00
public TestLegacyExporter(Storage storage)
: base(storage)
{
}
2023-02-16 21:26:57 +08:00
public string GetExtension() => FileExtension;
protected override string FileExtension => ".test";
2023-02-16 21:26:57 +08:00
}
}
}