From f16cc430f8f85ea62f1585c16f7ba4534a428f05 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 27 Oct 2016 17:08:53 +0900 Subject: [PATCH] Stop using memory database. Load sample data local to TestCase. Also makes the connection private. --- .../Tests/TestCasePlaySongSelect.cs | 91 ++++++++++++++++--- osu.Desktop.VisualTests/VisualTestGame.cs | 57 ------------ osu.Game/Database/BeatmapDatabase.cs | 58 +++++++----- osu.Game/GameModes/Play/PlaySongSelect.cs | 4 +- 4 files changed, 118 insertions(+), 92 deletions(-) diff --git a/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs b/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs index 7dcc7026b1..204fe88ee1 100644 --- a/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs +++ b/osu.Desktop.VisualTests/Tests/TestCasePlaySongSelect.cs @@ -1,31 +1,96 @@ //Copyright (c) 2007-2016 ppy Pty Ltd . //Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using OpenTK; using osu.Framework.GameModes.Testing; -using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; -using osu.Framework.Threading; -using osu.Game; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using osu.Framework.Graphics.Sprites; -using osu.Game.Online.Chat.Display; -using osu.Framework; +using osu.Game.Database; using osu.Game.GameModes.Play; +using SQLiteNetExtensions.Extensions; +using osu.Framework; +using osu.Game; namespace osu.Desktop.Tests { class TestCasePlaySongSelect : TestCase { + private BeatmapDatabase db; public override string Name => @"Song Select"; - public override string Description => @"Testing song selection UI"; - + public override string Description => @"Testing song selection UI"; + + public override void Load(BaseGame game) + { + base.Load(game); + + db = (game as OsuGameBase).Beatmaps; + } + public override void Reset() { - base.Reset(); + base.Reset(); + + db.Reset(); + + var sets = new List(); + + for (int i = 0; i < 100; i += 10) + sets.Add(createTestBeatmapSet(i)); + + db.Import(sets); + Add(new PlaySongSelect()); + } + + private BeatmapSetInfo createTestBeatmapSet(int i) + { + return new BeatmapSetInfo + { + BeatmapSetID = 1234 + i, + Hash = "d8e8fca2dc0f896fd7cb4cb0031ba249", + Path = string.Empty, + Metadata = new BeatmapMetadata + { + BeatmapSetID = 1234 + i, + Artist = "MONACA", + Title = "Black Song", + Author = "Some Guy", + }, + Beatmaps = new List(new[] + { + new BeatmapInfo + { + BeatmapID = 1234 + i, + Mode = PlayMode.Osu, + Path = "normal.osu", + Version = "Normal", + BaseDifficulty = new BaseDifficulty + { + OverallDifficulty = 3.5f, + } + }, + new BeatmapInfo + { + BeatmapID = 1235 + i, + Mode = PlayMode.Osu, + Path = "hard.osu", + Version = "Hard", + BaseDifficulty = new BaseDifficulty + { + OverallDifficulty = 5, + } + }, + new BeatmapInfo + { + BeatmapID = 1236 + i, + Mode = PlayMode.Osu, + Path = "insane.osu", + Version = "Insane", + BaseDifficulty = new BaseDifficulty + { + OverallDifficulty = 7, + } + }, + }), + }; } } } diff --git a/osu.Desktop.VisualTests/VisualTestGame.cs b/osu.Desktop.VisualTests/VisualTestGame.cs index 967965a91d..6618427bed 100644 --- a/osu.Desktop.VisualTests/VisualTestGame.cs +++ b/osu.Desktop.VisualTests/VisualTestGame.cs @@ -18,66 +18,9 @@ namespace osu.Desktop.VisualTests { class VisualTestGame : OsuGameBase { - private void InsertTestMap(int i) - { - var beatmapSet = new BeatmapSetInfo - { - BeatmapSetID = 1234 + i, - Hash = "d8e8fca2dc0f896fd7cb4cb0031ba249", - Path = "/foo/bar/baz", - Metadata = new BeatmapMetadata - { - BeatmapSetID = 1234 + i, - Artist = "MONACA", - Title = "Black Song", - Author = "Some Guy", - }, - Beatmaps = new List(new[] - { - new BeatmapInfo - { - BeatmapID = 1234 + i, - Mode = PlayMode.Osu, - Path = "normal.osu", - Version = "Normal", - BaseDifficulty = new BaseDifficulty - { - OverallDifficulty = 3.5f, - } - }, - new BeatmapInfo - { - BeatmapID = 1235 + i, - Mode = PlayMode.Osu, - Path = "hard.osu", - Version = "Hard", - BaseDifficulty = new BaseDifficulty - { - OverallDifficulty = 5, - } - }, - new BeatmapInfo - { - BeatmapID = 1236 + i, - Mode = PlayMode.Osu, - Path = "insane.osu", - Version = "Insane", - BaseDifficulty = new BaseDifficulty - { - OverallDifficulty = 7, - } - }, - }), - }; - BeatmapDatabase.Connection.InsertWithChildren(beatmapSet, true); - } - public override void Load(BaseGame game) { - Host.Storage = new TestStorage(@"visual-tests"); base.Load(game); - for (int i = 0; i < 100; i += 10) - InsertTestMap(i); Add(new TestBrowser()); } } diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index 512ca32a75..896daf100e 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -17,7 +17,7 @@ namespace osu.Game.Database { public class BeatmapDatabase { - public static SQLiteConnection Connection { get; set; } + private static SQLiteConnection connection { get; set; } private BasicStorage storage; public event Action BeatmapSetAdded; @@ -29,25 +29,28 @@ namespace osu.Game.Database ipc = new BeatmapImporter(host, this); - if (Connection == null) + if (connection == null) { - Connection = storage.GetDatabase(@"beatmaps"); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); + connection = storage.GetDatabase(@"beatmaps"); + connection.CreateTable(); + connection.CreateTable(); + connection.CreateTable(); + connection.CreateTable(); } } public void Reset() { foreach (var setInfo in Query()) - storage.Delete(setInfo.Path); + { + if (storage.Exists(setInfo.Path)) + storage.Delete(setInfo.Path); + } - Connection.DeleteAll(); - Connection.DeleteAll(); - Connection.DeleteAll(); - Connection.DeleteAll(); + connection.DeleteAll(); + connection.DeleteAll(); + connection.DeleteAll(); + connection.DeleteAll(); } public void Import(params string[] paths) @@ -62,7 +65,7 @@ namespace osu.Game.Database using (var reader = ArchiveReader.GetReader(storage, path)) metadata = reader.ReadMetadata(); - if (Connection.Table().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) + if (connection.Table().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) return; // TODO: Update this beatmap instead if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader @@ -104,11 +107,24 @@ namespace osu.Game.Database } } } - Connection.InsertWithChildren(beatmapSet, true); - BeatmapSetAdded?.Invoke(beatmapSet); + + Import(new[] { beatmapSet }); } } + public void Import(IEnumerable beatmapSets) + { + connection.BeginTransaction(); + + foreach (var s in beatmapSets) + { + connection.InsertWithChildren(s, true); + BeatmapSetAdded?.Invoke(s); + } + + connection.Commit(); + } + public ArchiveReader GetReader(BeatmapSetInfo beatmapSet) { return ArchiveReader.GetReader(storage, beatmapSet.Path); @@ -136,25 +152,25 @@ namespace osu.Game.Database public TableQuery Query() where T : class { - return Connection.Table(); + return connection.Table(); } public T GetWithChildren(object id) where T : class { - return Connection.GetWithChildren(id); + return connection.GetWithChildren(id); } public List GetAllWithChildren(Expression> filter = null, bool recursive = true) where T : class { - return Connection.GetAllWithChildren(filter, recursive); + return connection.GetAllWithChildren(filter, recursive); } public T GetChildren(T item, bool recursive = true) { if (item == null) return default(T); - Connection.GetChildren(item, recursive); + connection.GetChildren(item, recursive); return item; } @@ -171,9 +187,9 @@ namespace osu.Game.Database if (!validTypes.Any(t => t == typeof(T))) throw new ArgumentException(nameof(T), "Must be a type managed by BeatmapDatabase"); if (cascade) - Connection.UpdateWithChildren(record); + connection.UpdateWithChildren(record); else - Connection.Update(record); + connection.Update(record); } } } diff --git a/osu.Game/GameModes/Play/PlaySongSelect.cs b/osu.Game/GameModes/Play/PlaySongSelect.cs index ec0b6b5478..92422138a5 100644 --- a/osu.Game/GameModes/Play/PlaySongSelect.cs +++ b/osu.Game/GameModes/Play/PlaySongSelect.cs @@ -2,6 +2,7 @@ //Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; +using System.Diagnostics; using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -16,6 +17,7 @@ using OpenTK.Graphics; using osu.Framework.Graphics.UserInterface; using System.Threading.Tasks; using osu.Game.Beatmaps.Drawable; +using osu.Framework.Extensions.IEnumerableExtensions; namespace osu.Game.GameModes.Play { @@ -122,7 +124,7 @@ namespace osu.Game.GameModes.Play } beatmaps = (game as OsuGameBase).Beatmaps; - beatmaps.BeatmapSetAdded += bset => Scheduler.Add(() => addBeatmapSet(bset)); + beatmaps.BeatmapSetAdded += s => Scheduler.Add(() => addBeatmapSet(s)); Task.Factory.StartNew(addBeatmapSets); }