Move database isolation logic to OsuTestScene for easier reuse

This commit is contained in:
Dean Herbert
2019-07-29 16:55:19 +09:00
parent 2318402292
commit c14c3ba8ec
3 changed files with 19 additions and 36 deletions

View File

@ -15,6 +15,7 @@ using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Tests.Beatmaps;
@ -43,6 +44,9 @@ namespace osu.Game.Tests.Visual
private readonly Lazy<Storage> localStorage;
protected Storage LocalStorage => localStorage.Value;
private readonly Lazy<DatabaseContextFactory> contextFactory;
protected DatabaseContextFactory ContextFactory => contextFactory.Value;
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
// This is the earliest we can get OsuGameBase, which is used by the dummy working beatmap to find textures
@ -59,6 +63,14 @@ namespace osu.Game.Tests.Visual
protected OsuTestScene()
{
localStorage = new Lazy<Storage>(() => new NativeStorage($"{GetType().Name}-{Guid.NewGuid()}"));
contextFactory = new Lazy<DatabaseContextFactory>(() =>
{
var factory = new DatabaseContextFactory(LocalStorage);
factory.ResetDatabase();
using (var usage = factory.Get())
usage.Migrate();
return factory;
});
}
[Resolved]
@ -85,6 +97,9 @@ namespace osu.Game.Tests.Visual
if (beatmap?.Value.TrackLoaded == true)
beatmap.Value.Track.Stop();
if (contextFactory.IsValueCreated)
contextFactory.Value.ResetDatabase();
if (localStorage.IsValueCreated)
{
try