Add async overloaded methods for TestDatabase

This commit is contained in:
Bogdan 2026-04-27 14:19:02 +03:00
parent e965f22518
commit b1ea411400
3 changed files with 100 additions and 42 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
@ -28,22 +29,22 @@ public void Setup()
}
[Test]
public void one_to_one()
public async Task one_to_one()
{
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(c => c.Languages = new List<Language> { Language.English })
.With(c => c.Quality = new QualityModel())
.BuildNew();
Db.Insert(episodeFile);
await Db.InsertAsync(episodeFile);
var episode = Builder<Episode>.CreateNew()
.With(c => c.EpisodeFileId = episodeFile.Id)
.BuildNew();
Db.Insert(episode);
await Db.InsertAsync(episode);
var loadedEpisodeFile = Db.Single<Episode>().EpisodeFile.Value;
var loadedEpisodeFile = (await Db.SingleAsync<Episode>()).EpisodeFile.Value;
loadedEpisodeFile.Should().NotBeNull();
loadedEpisodeFile.Should().BeEquivalentTo(episodeFile,
@ -56,19 +57,19 @@ public void one_to_one()
}
[Test]
public void one_to_one_should_not_query_db_if_foreign_key_is_zero()
public async Task one_to_one_should_not_query_db_if_foreign_key_is_zero()
{
var episode = Builder<Episode>.CreateNew()
.With(c => c.EpisodeFileId = 0)
.BuildNew();
Db.Insert(episode);
await Db.InsertAsync(episode);
Db.Single<Episode>().EpisodeFile.Value.Should().BeNull();
(await Db.SingleAsync<Episode>()).EpisodeFile.Value.Should().BeNull();
}
[Test]
public void embedded_document_as_json()
public async Task embedded_document_as_json()
{
var quality = new QualityModel { Quality = Quality.Bluray720p, Revision = new Revision(version: 2) };
@ -78,14 +79,14 @@ public void embedded_document_as_json()
.With(c => c.Quality = quality)
.Build();
Db.Insert(history);
await Db.InsertAsync(history);
var loadedQuality = Db.Single<EpisodeHistory>().Quality;
var loadedQuality = (await Db.SingleAsync<EpisodeHistory>()).Quality;
loadedQuality.Should().Be(quality);
}
[Test]
public void embedded_list_of_document_with_json()
public async Task embedded_list_of_document_with_json()
{
var history = Builder<EpisodeHistory>.CreateListOfSize(2)
.All()
@ -96,9 +97,9 @@ public void embedded_list_of_document_with_json()
history[0].Quality = new QualityModel(Quality.HDTV1080p, new Revision(version: 2));
history[1].Quality = new QualityModel(Quality.Bluray720p, new Revision(version: 2));
Db.InsertMany(history);
await Db.InsertManyAsync(history);
var returnedHistory = Db.All<EpisodeHistory>();
var returnedHistory = await Db.AllAsync<EpisodeHistory>();
returnedHistory[0].Quality.Quality.Should().Be(Quality.HDTV1080p);
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
@ -25,84 +26,84 @@ public void SetUp()
}
[Test]
public void should_be_able_to_write_to_database()
public async Task should_be_able_to_write_to_database()
{
Subject.Insert(_sampleType);
Db.All<ScheduledTask>().Should().HaveCount(1);
await Subject.InsertAsync(_sampleType);
(await Db.AllAsync<ScheduledTask>()).Should().HaveCount(1);
}
[Test]
public void double_insert_should_fail()
public async Task double_insert_should_fail()
{
Subject.Insert(_sampleType);
Assert.Throws<InvalidOperationException>(() => Subject.Insert(_sampleType));
await Subject.InsertAsync(_sampleType);
Assert.ThrowsAsync<InvalidOperationException>(() => Subject.InsertAsync(_sampleType));
}
[Test]
public void update_item_with_root_index_0_should_faile()
{
_sampleType.Id = 0;
Assert.Throws<InvalidOperationException>(() => Subject.Update(_sampleType));
Assert.ThrowsAsync<InvalidOperationException>(() => Subject.UpdateAsync(_sampleType));
}
[Test]
public void should_be_able_to_store_empty_list()
public async Task should_be_able_to_store_empty_list()
{
var series = new List<ScheduledTask>();
Subject.InsertMany(series);
await Subject.InsertManyAsync(series);
}
[Test]
public void new_objects_should_get_id()
public async Task new_objects_should_get_id()
{
_sampleType.Id = 0;
Subject.Insert(_sampleType);
await Subject.InsertAsync(_sampleType);
_sampleType.Id.Should().NotBe(0);
}
[Test]
public void new_object_should_get_new_id()
public async Task new_object_should_get_new_id()
{
_sampleType.Id = 0;
Subject.Insert(_sampleType);
await Subject.InsertAsync(_sampleType);
Db.All<ScheduledTask>().Should().HaveCount(1);
(await Db.AllAsync<ScheduledTask>()).Should().HaveCount(1);
_sampleType.Id.Should().Be(1);
}
[Test]
public void should_read_and_write_in_utc()
public async Task should_read_and_write_in_utc()
{
var storedTime = DateTime.UtcNow;
_sampleType.LastExecution = storedTime;
Subject.Insert(_sampleType);
await Subject.InsertAsync(_sampleType);
StoredModel.LastExecution.Kind.Should().Be(DateTimeKind.Utc);
StoredModel.LastExecution.ToLongTimeString().Should().Be(storedTime.ToLongTimeString());
}
[Test]
public void should_convert_all_dates_to_utc()
public async Task should_convert_all_dates_to_utc()
{
var storedTime = DateTime.Now;
_sampleType.LastExecution = storedTime;
Subject.Insert(_sampleType);
await Subject.InsertAsync(_sampleType);
StoredModel.LastExecution.Kind.Should().Be(DateTimeKind.Utc);
StoredModel.LastExecution.ToLongTimeString().Should().Be(storedTime.ToUniversalTime().ToLongTimeString());
}
[Test]
public void should_have_id_when_returned_from_database()
public async Task should_have_id_when_returned_from_database()
{
_sampleType.Id = 0;
Subject.Insert(_sampleType);
var item = Db.All<ScheduledTask>();
await Subject.InsertAsync(_sampleType);
var item = await Db.AllAsync<ScheduledTask>();
item.Should().HaveCount(1);
item.First().Id.Should().NotBe(0);
@ -111,17 +112,17 @@ public void should_have_id_when_returned_from_database()
}
[Test]
public void should_be_able_to_find_object_by_id()
public async Task should_be_able_to_find_object_by_id()
{
Subject.Insert(_sampleType);
var item = Db.All<ScheduledTask>().Single(c => c.Id == _sampleType.Id);
await Subject.InsertAsync(_sampleType);
var item = (await Db.AllAsync<ScheduledTask>()).Single(c => c.Id == _sampleType.Id);
item.Id.Should().NotBe(0);
item.Id.Should().Be(_sampleType.Id);
}
[Test]
public void set_fields_should_only_update_selected_filed()
public async Task set_fields_should_only_update_selected_filed()
{
var childModel = new ScheduledTask
{
@ -129,15 +130,15 @@ public void set_fields_should_only_update_selected_filed()
Interval = 12
};
Subject.Insert(childModel);
await Subject.InsertAsync(childModel);
childModel.TypeName = "A";
childModel.Interval = 0;
Subject.SetFields(childModel, t => t.TypeName);
await Subject.SetFieldsAsync(childModel, t => t.TypeName);
Db.All<ScheduledTask>().Single().TypeName.Should().Be("A");
Db.All<ScheduledTask>().Single().Interval.Should().Be(12);
(await Db.AllAsync<ScheduledTask>()).Single().TypeName.Should().Be("A");
(await Db.AllAsync<ScheduledTask>()).Single().Interval.Should().Be(12);
}
}
}

View file

@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
@ -11,18 +13,31 @@ public interface ITestDatabase
{
void InsertMany<T>(IEnumerable<T> items)
where T : ModelBase, new();
Task InsertManyAsync<T>(IEnumerable<T> items, CancellationToken cancellationToken = default)
where T : ModelBase, new();
T Insert<T>(T item)
where T : ModelBase, new();
Task<T> InsertAsync<T>(T item, CancellationToken cancellationToken = default)
where T : ModelBase, new();
List<T> All<T>()
where T : ModelBase, new();
Task<List<T>> AllAsync<T>(CancellationToken cancellationToken = default)
where T : ModelBase, new();
T Single<T>()
where T : ModelBase, new();
Task<T> SingleAsync<T>(CancellationToken cancellationToken = default)
where T : ModelBase, new();
void Update<T>(T childModel)
where T : ModelBase, new();
Task UpdateAsync<T>(T childModel, CancellationToken cancellationToken = default)
where T : ModelBase, new();
void Delete<T>(T childModel)
where T : ModelBase, new();
Task DeleteAsync<T>(T childModel, CancellationToken cancellationToken = default)
where T : ModelBase, new();
IDirectDataMapper GetDirectDataMapper();
IDbConnection OpenConnection();
Task<IDbConnection> OpenConnectionAsync(CancellationToken cancellationToken = default);
DatabaseType DatabaseType { get; }
}
@ -45,36 +60,72 @@ public void InsertMany<T>(IEnumerable<T> items)
new BasicRepository<T>(_dbConnection, _eventAggregator).InsertMany(items.ToList());
}
public async Task InsertManyAsync<T>(IEnumerable<T> items, CancellationToken cancellationToken = default)
where T : ModelBase, new()
{
await new BasicRepository<T>(_dbConnection, _eventAggregator).InsertManyAsync(items.ToList(), cancellationToken);
}
public T Insert<T>(T item)
where T : ModelBase, new()
{
return new BasicRepository<T>(_dbConnection, _eventAggregator).Insert(item);
}
public async Task<T> InsertAsync<T>(T item, CancellationToken cancellationToken = default)
where T : ModelBase, new()
{
return await new BasicRepository<T>(_dbConnection, _eventAggregator).InsertAsync(item, cancellationToken);
}
public List<T> All<T>()
where T : ModelBase, new()
{
return new BasicRepository<T>(_dbConnection, _eventAggregator).All().ToList();
}
public async Task<List<T>> AllAsync<T>(CancellationToken cancellationToken = default)
where T : ModelBase, new()
{
return await new BasicRepository<T>(_dbConnection, _eventAggregator).AllAsync(cancellationToken).ToListAsync(cancellationToken);
}
public T Single<T>()
where T : ModelBase, new()
{
return All<T>().SingleOrDefault();
}
public async Task<T> SingleAsync<T>(CancellationToken cancellationToken = default)
where T : ModelBase, new()
{
return (await AllAsync<T>(cancellationToken)).SingleOrDefault();
}
public void Update<T>(T childModel)
where T : ModelBase, new()
{
new BasicRepository<T>(_dbConnection, _eventAggregator).Update(childModel);
}
public async Task UpdateAsync<T>(T childModel, CancellationToken cancellationToken = default)
where T : ModelBase, new()
{
await new BasicRepository<T>(_dbConnection, _eventAggregator).UpdateAsync(childModel, cancellationToken);
}
public void Delete<T>(T childModel)
where T : ModelBase, new()
{
new BasicRepository<T>(_dbConnection, _eventAggregator).Delete(childModel);
}
public async Task DeleteAsync<T>(T childModel, CancellationToken cancellationToken = default)
where T : ModelBase, new()
{
await new BasicRepository<T>(_dbConnection, _eventAggregator).DeleteAsync(childModel, cancellationToken);
}
public IDirectDataMapper GetDirectDataMapper()
{
return new DirectDataMapper(_dbConnection);
@ -84,5 +135,10 @@ public IDbConnection OpenConnection()
{
return _dbConnection.OpenConnection();
}
public async Task<IDbConnection> OpenConnectionAsync(CancellationToken cancellationToken = default)
{
return await _dbConnection.OpenConnectionAsync(cancellationToken);
}
}
}