mirror of https://github.com/lidarr/Lidarr
Cache database for Unit tests to avoid repeated migrations
(cherry picked from commit f3308827d0ede7895b0b8b3b251a17cda3a54120)
This commit is contained in:
parent
11ba745439
commit
f7839adc38
|
@ -1,10 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||||
|
|
||||||
|
@ -65,8 +67,7 @@ namespace NzbDrone.Core.Test.Framework
|
||||||
|
|
||||||
protected virtual ITestDatabase WithTestDb(MigrationContext migrationContext)
|
protected virtual ITestDatabase WithTestDb(MigrationContext migrationContext)
|
||||||
{
|
{
|
||||||
var factory = Mocker.Resolve<DbFactory>();
|
var database = CreateDatabase(migrationContext);
|
||||||
var database = factory.Create(migrationContext);
|
|
||||||
Mocker.SetConstant(database);
|
Mocker.SetConstant(database);
|
||||||
|
|
||||||
switch (MigrationType)
|
switch (MigrationType)
|
||||||
|
@ -98,6 +99,48 @@ namespace NzbDrone.Core.Test.Framework
|
||||||
return testDb;
|
return testDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IDatabase CreateDatabase(MigrationContext migrationContext)
|
||||||
|
{
|
||||||
|
var factory = Mocker.Resolve<DbFactory>();
|
||||||
|
|
||||||
|
// If a special migration test or log migration then create new
|
||||||
|
if (migrationContext.BeforeMigration != null)
|
||||||
|
{
|
||||||
|
return factory.Create(migrationContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise try to use a cached migrated db
|
||||||
|
var cachedDb = GetCachedDatabase(migrationContext.MigrationType);
|
||||||
|
var testDb = GetTestDb(migrationContext.MigrationType);
|
||||||
|
if (File.Exists(cachedDb))
|
||||||
|
{
|
||||||
|
TestLogger.Info($"Using cached initial database {cachedDb}");
|
||||||
|
File.Copy(cachedDb, testDb);
|
||||||
|
return factory.Create(migrationContext);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var db = factory.Create(migrationContext);
|
||||||
|
GC.Collect();
|
||||||
|
GC.WaitForPendingFinalizers();
|
||||||
|
SQLiteConnection.ClearAllPools();
|
||||||
|
|
||||||
|
TestLogger.Info("Caching database");
|
||||||
|
File.Copy(testDb, cachedDb);
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetCachedDatabase(MigrationType type)
|
||||||
|
{
|
||||||
|
return Path.Combine(TestContext.CurrentContext.TestDirectory, $"cached_{type}.db");
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetTestDb(MigrationType type)
|
||||||
|
{
|
||||||
|
return type == MigrationType.Main ? TestFolderInfo.GetDatabase() : TestFolderInfo.GetLogDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual void SetupLogging()
|
protected virtual void SetupLogging()
|
||||||
{
|
{
|
||||||
Mocker.SetConstant<ILoggerProvider>(NullLoggerProvider.Instance);
|
Mocker.SetConstant<ILoggerProvider>(NullLoggerProvider.Instance);
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
using System.IO;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test
|
||||||
|
{
|
||||||
|
[SetUpFixture]
|
||||||
|
public class RemoveCachedDatabase
|
||||||
|
{
|
||||||
|
[OneTimeSetUp]
|
||||||
|
[OneTimeTearDown]
|
||||||
|
public void ClearCachedDatabase()
|
||||||
|
{
|
||||||
|
var mainCache = Path.Combine(TestContext.CurrentContext.TestDirectory, $"cached_Main.db");
|
||||||
|
if (File.Exists(mainCache))
|
||||||
|
{
|
||||||
|
File.Delete(mainCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
var logCache = Path.Combine(TestContext.CurrentContext.TestDirectory, $"cached_Log.db");
|
||||||
|
if (File.Exists(logCache))
|
||||||
|
{
|
||||||
|
File.Delete(logCache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue