From b1ea4114007f75f9f4b052e9779c3eadcbb570cb Mon Sep 17 00:00:00 2001 From: Bogdan Date: Mon, 27 Apr 2026 14:19:02 +0300 Subject: [PATCH] Add async overloaded methods for TestDatabase --- .../Datastore/DatabaseRelationshipFixture.cs | 27 +++++---- .../Datastore/ObjectDatabaseFixture.cs | 59 ++++++++++--------- .../Framework/TestDatabase.cs | 56 ++++++++++++++++++ 3 files changed, 100 insertions(+), 42 deletions(-) diff --git a/src/NzbDrone.Core.Test/Datastore/DatabaseRelationshipFixture.cs b/src/NzbDrone.Core.Test/Datastore/DatabaseRelationshipFixture.cs index e40f9a0fa..b78119834 100644 --- a/src/NzbDrone.Core.Test/Datastore/DatabaseRelationshipFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/DatabaseRelationshipFixture.cs @@ -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.CreateNew() .With(c => c.Languages = new List { Language.English }) .With(c => c.Quality = new QualityModel()) .BuildNew(); - Db.Insert(episodeFile); + await Db.InsertAsync(episodeFile); var episode = Builder.CreateNew() .With(c => c.EpisodeFileId = episodeFile.Id) .BuildNew(); - Db.Insert(episode); + await Db.InsertAsync(episode); - var loadedEpisodeFile = Db.Single().EpisodeFile.Value; + var loadedEpisodeFile = (await Db.SingleAsync()).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.CreateNew() .With(c => c.EpisodeFileId = 0) .BuildNew(); - Db.Insert(episode); + await Db.InsertAsync(episode); - Db.Single().EpisodeFile.Value.Should().BeNull(); + (await Db.SingleAsync()).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().Quality; + var loadedQuality = (await Db.SingleAsync()).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.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(); + var returnedHistory = await Db.AllAsync(); returnedHistory[0].Quality.Quality.Should().Be(Quality.HDTV1080p); } diff --git a/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index 08577f42a..5a906b293 100644 --- a/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -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().Should().HaveCount(1); + await Subject.InsertAsync(_sampleType); + (await Db.AllAsync()).Should().HaveCount(1); } [Test] - public void double_insert_should_fail() + public async Task double_insert_should_fail() { - Subject.Insert(_sampleType); - Assert.Throws(() => Subject.Insert(_sampleType)); + await Subject.InsertAsync(_sampleType); + Assert.ThrowsAsync(() => Subject.InsertAsync(_sampleType)); } [Test] public void update_item_with_root_index_0_should_faile() { _sampleType.Id = 0; - Assert.Throws(() => Subject.Update(_sampleType)); + Assert.ThrowsAsync(() => 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(); - 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().Should().HaveCount(1); + (await Db.AllAsync()).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(); + await Subject.InsertAsync(_sampleType); + var item = await Db.AllAsync(); 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().Single(c => c.Id == _sampleType.Id); + await Subject.InsertAsync(_sampleType); + var item = (await Db.AllAsync()).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().Single().TypeName.Should().Be("A"); - Db.All().Single().Interval.Should().Be(12); + (await Db.AllAsync()).Single().TypeName.Should().Be("A"); + (await Db.AllAsync()).Single().Interval.Should().Be(12); } } } diff --git a/src/NzbDrone.Core.Test/Framework/TestDatabase.cs b/src/NzbDrone.Core.Test/Framework/TestDatabase.cs index 5391cfb1f..c0e09cb80 100644 --- a/src/NzbDrone.Core.Test/Framework/TestDatabase.cs +++ b/src/NzbDrone.Core.Test/Framework/TestDatabase.cs @@ -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(IEnumerable items) where T : ModelBase, new(); + Task InsertManyAsync(IEnumerable items, CancellationToken cancellationToken = default) + where T : ModelBase, new(); T Insert(T item) where T : ModelBase, new(); + Task InsertAsync(T item, CancellationToken cancellationToken = default) + where T : ModelBase, new(); List All() where T : ModelBase, new(); + Task> AllAsync(CancellationToken cancellationToken = default) + where T : ModelBase, new(); T Single() where T : ModelBase, new(); + Task SingleAsync(CancellationToken cancellationToken = default) + where T : ModelBase, new(); void Update(T childModel) where T : ModelBase, new(); + Task UpdateAsync(T childModel, CancellationToken cancellationToken = default) + where T : ModelBase, new(); void Delete(T childModel) where T : ModelBase, new(); + Task DeleteAsync(T childModel, CancellationToken cancellationToken = default) + where T : ModelBase, new(); IDirectDataMapper GetDirectDataMapper(); IDbConnection OpenConnection(); + Task OpenConnectionAsync(CancellationToken cancellationToken = default); DatabaseType DatabaseType { get; } } @@ -45,36 +60,72 @@ public void InsertMany(IEnumerable items) new BasicRepository(_dbConnection, _eventAggregator).InsertMany(items.ToList()); } + public async Task InsertManyAsync(IEnumerable items, CancellationToken cancellationToken = default) + where T : ModelBase, new() + { + await new BasicRepository(_dbConnection, _eventAggregator).InsertManyAsync(items.ToList(), cancellationToken); + } + public T Insert(T item) where T : ModelBase, new() { return new BasicRepository(_dbConnection, _eventAggregator).Insert(item); } + public async Task InsertAsync(T item, CancellationToken cancellationToken = default) + where T : ModelBase, new() + { + return await new BasicRepository(_dbConnection, _eventAggregator).InsertAsync(item, cancellationToken); + } + public List All() where T : ModelBase, new() { return new BasicRepository(_dbConnection, _eventAggregator).All().ToList(); } + public async Task> AllAsync(CancellationToken cancellationToken = default) + where T : ModelBase, new() + { + return await new BasicRepository(_dbConnection, _eventAggregator).AllAsync(cancellationToken).ToListAsync(cancellationToken); + } + public T Single() where T : ModelBase, new() { return All().SingleOrDefault(); } + public async Task SingleAsync(CancellationToken cancellationToken = default) + where T : ModelBase, new() + { + return (await AllAsync(cancellationToken)).SingleOrDefault(); + } + public void Update(T childModel) where T : ModelBase, new() { new BasicRepository(_dbConnection, _eventAggregator).Update(childModel); } + public async Task UpdateAsync(T childModel, CancellationToken cancellationToken = default) + where T : ModelBase, new() + { + await new BasicRepository(_dbConnection, _eventAggregator).UpdateAsync(childModel, cancellationToken); + } + public void Delete(T childModel) where T : ModelBase, new() { new BasicRepository(_dbConnection, _eventAggregator).Delete(childModel); } + public async Task DeleteAsync(T childModel, CancellationToken cancellationToken = default) + where T : ModelBase, new() + { + await new BasicRepository(_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 OpenConnectionAsync(CancellationToken cancellationToken = default) + { + return await _dbConnection.OpenConnectionAsync(cancellationToken); + } } }