mirror of
https://github.com/Sonarr/Sonarr
synced 2026-05-08 13:01:10 +02:00
Add async overloaded methods for MigrationTest
This commit is contained in:
parent
ef76ee3897
commit
beab42841e
2 changed files with 46 additions and 0 deletions
|
|
@ -2,6 +2,8 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
|
|
@ -10,9 +12,13 @@ namespace NzbDrone.Core.Test.Framework
|
|||
public interface IDirectDataMapper
|
||||
{
|
||||
List<Dictionary<string, object>> Query(string sql);
|
||||
Task<List<Dictionary<string, object>>> QueryAsync(string sql, CancellationToken cancellationToken = default);
|
||||
List<T> Query<T>(string sql)
|
||||
where T : new();
|
||||
Task<List<T>> QueryAsync<T>(string sql, CancellationToken cancellationToken = default)
|
||||
where T : new();
|
||||
T QueryScalar<T>(string sql);
|
||||
Task<T> QueryScalarAsync<T>(string sql, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
public class DirectDataMapper : IDirectDataMapper
|
||||
|
|
@ -38,6 +44,18 @@ public DataTable GetDataTable(string sql)
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<DataTable> GetDataTableAsync(string sql, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var connection = await _database.OpenConnectionAsync(cancellationToken);
|
||||
await using var cmd = connection.CreateCommand();
|
||||
|
||||
var dataTable = new DataTable();
|
||||
cmd.CommandText = sql;
|
||||
dataTable.Load(await cmd.ExecuteReaderAsync(cancellationToken));
|
||||
|
||||
return dataTable;
|
||||
}
|
||||
|
||||
public List<Dictionary<string, object>> Query(string sql)
|
||||
{
|
||||
var dataTable = GetDataTable(sql);
|
||||
|
|
@ -45,6 +63,13 @@ public List<Dictionary<string, object>> Query(string sql)
|
|||
return dataTable.Rows.Cast<DataRow>().Select(MapToDictionary).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<Dictionary<string, object>>> QueryAsync(string sql, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dataTable = await GetDataTableAsync(sql, cancellationToken);
|
||||
|
||||
return dataTable.Rows.Cast<DataRow>().Select(MapToDictionary).ToList();
|
||||
}
|
||||
|
||||
public List<T> Query<T>(string sql)
|
||||
where T : new()
|
||||
{
|
||||
|
|
@ -53,6 +78,14 @@ public List<T> Query<T>(string sql)
|
|||
return dataTable.Rows.Cast<DataRow>().Select(MapToObject<T>).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<T>> QueryAsync<T>(string sql, CancellationToken cancellationToken = default)
|
||||
where T : new()
|
||||
{
|
||||
var dataTable = await GetDataTableAsync(sql, cancellationToken);
|
||||
|
||||
return dataTable.Rows.Cast<DataRow>().Select(MapToObject<T>).ToList();
|
||||
}
|
||||
|
||||
public T QueryScalar<T>(string sql)
|
||||
{
|
||||
var dataTable = GetDataTable(sql);
|
||||
|
|
@ -60,6 +93,13 @@ public T QueryScalar<T>(string sql)
|
|||
return dataTable.Rows.Cast<DataRow>().Select(d => MapValue(d, 0, typeof(T))).Cast<T>().FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task<T> QueryScalarAsync<T>(string sql, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dataTable = await GetDataTableAsync(sql, cancellationToken);
|
||||
|
||||
return dataTable.Rows.Cast<DataRow>().Select(d => MapValue(d, 0, typeof(T))).Cast<T>().FirstOrDefault();
|
||||
}
|
||||
|
||||
protected Dictionary<string, object> MapToDictionary(DataRow dataRow)
|
||||
{
|
||||
var item = new Dictionary<string, object>();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
using FluentMigrator;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
|
|
@ -31,6 +32,11 @@ protected virtual IDbConnection WithDapperMigrationTestDb(Action<TMigration> bef
|
|||
return WithMigrationAction(beforeMigration).OpenConnection();
|
||||
}
|
||||
|
||||
protected virtual async Task<IDbConnection> WithDapperMigrationTestDbAsync(Action<TMigration> beforeMigration = null)
|
||||
{
|
||||
return await WithMigrationAction(beforeMigration).OpenConnectionAsync();
|
||||
}
|
||||
|
||||
protected override void SetupLogging()
|
||||
{
|
||||
Mocker.SetConstant<ILoggerProvider>(Mocker.Resolve<NLogLoggerProvider>());
|
||||
|
|
|
|||
Loading…
Reference in a new issue