mirror of
https://github.com/Sonarr/Sonarr.git
synced 2026-04-27 23:06:29 -04:00
Dapper and STJson
Co-Authored-By: ta264 <ta264@users.noreply.github.com>
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Marr.Data;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
@@ -66,8 +67,7 @@ namespace NzbDrone.Core.Test.Framework
|
||||
|
||||
protected virtual ITestDatabase WithTestDb(MigrationContext migrationContext)
|
||||
{
|
||||
var factory = Mocker.Resolve<DbFactory>();
|
||||
var database = factory.Create(migrationContext);
|
||||
var database = CreateDatabase(migrationContext);
|
||||
Mocker.SetConstant(database);
|
||||
|
||||
switch (MigrationType)
|
||||
@@ -99,6 +99,48 @@ namespace NzbDrone.Core.Test.Framework
|
||||
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()
|
||||
{
|
||||
Mocker.SetConstant<ILoggerProvider>(NullLoggerProvider.Instance);
|
||||
@@ -112,7 +154,7 @@ namespace NzbDrone.Core.Test.Framework
|
||||
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
|
||||
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
|
||||
|
||||
MapRepository.Instance.EnableTraceLogging = true;
|
||||
SqlBuilderExtensions.LogSql = true;
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore;
|
||||
@@ -18,27 +17,16 @@ namespace NzbDrone.Core.Test.Framework
|
||||
|
||||
public class DirectDataMapper : IDirectDataMapper
|
||||
{
|
||||
private readonly DbProviderFactory _providerFactory;
|
||||
private readonly string _connectionString;
|
||||
private readonly IDatabase _database;
|
||||
|
||||
public DirectDataMapper(IDatabase database)
|
||||
{
|
||||
var dataMapper = database.GetDataMapper();
|
||||
_providerFactory = dataMapper.ProviderFactory;
|
||||
_connectionString = dataMapper.ConnectionString;
|
||||
}
|
||||
|
||||
private DbConnection OpenConnection()
|
||||
{
|
||||
var connection = _providerFactory.CreateConnection();
|
||||
connection.ConnectionString = _connectionString;
|
||||
connection.Open();
|
||||
return connection;
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public DataTable GetDataTable(string sql)
|
||||
{
|
||||
using (var connection = OpenConnection())
|
||||
using (var connection = _database.OpenConnection())
|
||||
{
|
||||
using (var cmd = connection.CreateCommand())
|
||||
{
|
||||
@@ -118,7 +106,19 @@ namespace NzbDrone.Core.Test.Framework
|
||||
propertyType = propertyType.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
object value = MapValue(dataRow, i, propertyType);
|
||||
object value;
|
||||
if (dataRow.ItemArray[i] == DBNull.Value)
|
||||
{
|
||||
value = null;
|
||||
}
|
||||
else if (dataRow.Table.Columns[i].DataType == typeof(string) && propertyType != typeof(string))
|
||||
{
|
||||
value = Json.Deserialize((string)dataRow.ItemArray[i], propertyType);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ChangeType(dataRow.ItemArray[i], propertyType);
|
||||
}
|
||||
|
||||
propertyInfo.SetValue(item, value, null);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using FluentMigrator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NUnit.Framework;
|
||||
@@ -11,18 +12,32 @@ namespace NzbDrone.Core.Test.Framework
|
||||
public abstract class MigrationTest<TMigration> : DbTest
|
||||
where TMigration : NzbDroneMigrationBase
|
||||
{
|
||||
protected long MigrationVersion
|
||||
protected long MigrationVersion => ((MigrationAttribute)Attribute.GetCustomAttribute(typeof(TMigration), typeof(MigrationAttribute))).Version;
|
||||
|
||||
[SetUp]
|
||||
public override void SetupDb()
|
||||
{
|
||||
get
|
||||
{
|
||||
var attrib = (MigrationAttribute)Attribute.GetCustomAttribute(typeof(TMigration), typeof(MigrationAttribute));
|
||||
return attrib.Version;
|
||||
}
|
||||
SetupContainer();
|
||||
}
|
||||
|
||||
protected virtual IDirectDataMapper WithMigrationTestDb(Action<TMigration> beforeMigration = null)
|
||||
{
|
||||
var db = WithTestDb(new MigrationContext(MigrationType, MigrationVersion)
|
||||
return WithMigrationAction(beforeMigration).GetDirectDataMapper();
|
||||
}
|
||||
|
||||
protected virtual IDbConnection WithDapperMigrationTestDb(Action<TMigration> beforeMigration = null)
|
||||
{
|
||||
return WithMigrationAction(beforeMigration).OpenConnection();
|
||||
}
|
||||
|
||||
protected override void SetupLogging()
|
||||
{
|
||||
Mocker.SetConstant<ILoggerProvider>(Mocker.Resolve<MigrationLoggerProvider>());
|
||||
}
|
||||
|
||||
private ITestDatabase WithMigrationAction(Action<TMigration> beforeMigration = null)
|
||||
{
|
||||
return WithTestDb(new MigrationContext(MigrationType, MigrationVersion)
|
||||
{
|
||||
BeforeMigration = m =>
|
||||
{
|
||||
@@ -32,19 +47,6 @@ namespace NzbDrone.Core.Test.Framework
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return db.GetDirectDataMapper();
|
||||
}
|
||||
|
||||
protected override void SetupLogging()
|
||||
{
|
||||
Mocker.SetConstant<ILoggerProvider>(Mocker.Resolve<MigrationLoggerProvider>());
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public override void SetupDb()
|
||||
{
|
||||
SetupContainer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using NzbDrone.Core.Datastore;
|
||||
@@ -21,6 +22,7 @@ namespace NzbDrone.Core.Test.Framework
|
||||
void Delete<T>(T childModel)
|
||||
where T : ModelBase, new();
|
||||
IDirectDataMapper GetDirectDataMapper();
|
||||
IDbConnection OpenConnection();
|
||||
}
|
||||
|
||||
public class TestDatabase : ITestDatabase
|
||||
@@ -74,5 +76,10 @@ namespace NzbDrone.Core.Test.Framework
|
||||
{
|
||||
return new DirectDataMapper(_dbConnection);
|
||||
}
|
||||
|
||||
public IDbConnection OpenConnection()
|
||||
{
|
||||
return _dbConnection.OpenConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user