1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 08:22:56 +08:00

Stop using memory database. Load sample data local to TestCase.

Also makes the connection private.
This commit is contained in:
Dean Herbert 2016-10-27 17:08:53 +09:00
parent e333f97364
commit f16cc430f8
4 changed files with 118 additions and 92 deletions

View File

@ -1,31 +1,96 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//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 void Load(BaseGame game)
{
base.Load(game);
db = (game as OsuGameBase).Beatmaps;
}
public override void Reset()
{
base.Reset();
db.Reset();
var sets = new List<BeatmapSetInfo>();
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<BeatmapInfo>(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,
}
},
}),
};
}
}
}

View File

@ -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<BeatmapInfo>(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());
}
}

View File

@ -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<BeatmapSetInfo> 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<BeatmapMetadata>();
Connection.CreateTable<BaseDifficulty>();
Connection.CreateTable<BeatmapSetInfo>();
Connection.CreateTable<BeatmapInfo>();
connection = storage.GetDatabase(@"beatmaps");
connection.CreateTable<BeatmapMetadata>();
connection.CreateTable<BaseDifficulty>();
connection.CreateTable<BeatmapSetInfo>();
connection.CreateTable<BeatmapInfo>();
}
}
public void Reset()
{
foreach (var setInfo in Query<BeatmapSetInfo>())
storage.Delete(setInfo.Path);
{
if (storage.Exists(setInfo.Path))
storage.Delete(setInfo.Path);
}
Connection.DeleteAll<BeatmapMetadata>();
Connection.DeleteAll<BaseDifficulty>();
Connection.DeleteAll<BeatmapSetInfo>();
Connection.DeleteAll<BeatmapInfo>();
connection.DeleteAll<BeatmapMetadata>();
connection.DeleteAll<BaseDifficulty>();
connection.DeleteAll<BeatmapSetInfo>();
connection.DeleteAll<BeatmapInfo>();
}
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<BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
if (connection.Table<BeatmapSetInfo>().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<BeatmapSetInfo> 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<T> Query<T>() where T : class
{
return Connection.Table<T>();
return connection.Table<T>();
}
public T GetWithChildren<T>(object id) where T : class
{
return Connection.GetWithChildren<T>(id);
return connection.GetWithChildren<T>(id);
}
public List<T> GetAllWithChildren<T>(Expression<Func<T, bool>> filter = null,
bool recursive = true) where T : class
{
return Connection.GetAllWithChildren<T>(filter, recursive);
return connection.GetAllWithChildren<T>(filter, recursive);
}
public T GetChildren<T>(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);
}
}
}

View File

@ -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);
}