From c2f7f8c8161749ec1b51d1383c401f6e8699b2a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 8 Feb 2024 14:06:51 +0100 Subject: [PATCH 01/17] dependency: add EntityFrameworkCore.Triggered --- Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj b/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj index 1829f89c25..6c710e3bc4 100644 --- a/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj +++ b/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj @@ -6,6 +6,7 @@ + From 9fcdc968f21ee076033f0b39e4a40bc2173365aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 8 Feb 2024 14:07:54 +0100 Subject: [PATCH 02/17] fix: set AfterSave trigger that deletes orphaned tier quota definitions --- .../Triggers/DeleteTierQuotaDefinitionsTrigger.cs | 14 ++++++++++++++ .../Database/IServiceCollectionExtensions.cs | 5 +++++ 2 files changed, 19 insertions(+) create mode 100644 Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs diff --git a/Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs b/Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs new file mode 100644 index 0000000000..4cd81bf79f --- /dev/null +++ b/Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs @@ -0,0 +1,14 @@ +using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; +using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; +using EntityFrameworkCore.Triggered; + +namespace Backbone.Modules.Quotas.Application.Tiers.Triggers; + +public class DeleteTierQuotaDefinitionsTrigger(ITiersRepository tiersRepository) : IAfterSaveTrigger +{ + public async Task AfterSave(ITriggerContext context, CancellationToken cancellationToken) + { + if (context.ChangeType == ChangeType.Modified) + await tiersRepository.RemoveTierQuotaDefinitionIfOrphaned(context.Entity.Id); + } +} diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs index ed8282747a..d4651a7f69 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Application.Metrics; +using Backbone.Modules.Quotas.Application.Tiers.Triggers; using Backbone.Modules.Quotas.Domain.Metrics; using Backbone.Modules.Quotas.Infrastructure.Persistence.Repository; using Microsoft.EntityFrameworkCore; @@ -51,6 +52,10 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio default: throw new Exception($"Unsupported database provider: {options.Provider}"); } + + dbContextOptions.UseTriggers(triggerOptions => { + triggerOptions.AddTrigger(); + }); }); services.AddTransient(); services.AddTransient(); From faf3fa664e45b3adb8aca6872b3ce80c88bfd455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 8 Feb 2024 14:08:19 +0100 Subject: [PATCH 03/17] feat: add RemoveTierQuotaDefinitionIfOrphaned --- .../Repository/ITiersRepository.cs | 1 + .../Persistence/Repository/TiersRepository.cs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs index 4128d0053c..ec20e7558d 100644 --- a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs @@ -7,5 +7,6 @@ public interface ITiersRepository Task Find(string id, CancellationToken cancellationToken, bool track = false); Task FindTierQuotaDefinition(string id, CancellationToken cancellationToken, bool track = false); Task RemoveById(TierId tierId); + Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId); Task Update(Tier tier, CancellationToken cancellationToken); } diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs index 3d49c6c7e8..47384e4e64 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs @@ -1,4 +1,6 @@ -using Backbone.BuildingBlocks.Application.Extensions; +using System.Threading; +using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.Persistence.Database; +using Backbone.BuildingBlocks.Application.Extensions; using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; @@ -53,6 +55,21 @@ public async Task RemoveById(TierId tierId) await _tiers.Where(t => t.Id == tierId).ExecuteDeleteAsync(); } + public async Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) + { + //await _tierQuotaDefinitions.Where(t => t.Id == tierQuotaDefinitionId).ExecuteDeleteAsync(); + + //var allTierQuotaDefinitions = await _tierQuotaDefinitions.ToListAsync(); + //var tierQuotaDefinitionsWithTierIdNull = allTierQuotaDefinitions.Where(t => _dbContext.Entry(t).Property("TierId").CurrentValue == null).ToList(); + //var idsOfTierQuotasWithTierIdNull = tierQuotaDefinitionsWithTierIdNull.Select(t => t.Id).ToList(); + //await _tierQuotaDefinitions.Where(t => idsOfTierQuotasWithTierIdNull.Contains(t.Id)).ExecuteDeleteAsync(); + + var tierQuotaDefinition = await _tierQuotaDefinitions.FirstWithId(tierQuotaDefinitionId, CancellationToken.None); + + if (_dbContext.Entry(tierQuotaDefinition).Property("TierId").CurrentValue == null) + await _tierQuotaDefinitions.Where(t => t.Id == tierQuotaDefinitionId).ExecuteDeleteAsync(); + } + public async Task Update(Tier tier, CancellationToken cancellationToken) { _tiers.Update(tier); From cb83f26a13532049f5544868f077a98e1ca9dc57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 8 Feb 2024 14:16:40 +0100 Subject: [PATCH 04/17] chore: fix failing pipeline tests --- .../TestDoubles/FindTierQuotaDefinitionsStubRepository.cs | 5 +++++ .../TestDoubles/FindTiersStubRepository.cs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs index ca156fa7ac..58e46f919a 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTierQuotaDefinitionsStubRepository.cs @@ -32,6 +32,11 @@ public Task RemoveById(TierId tierId) throw new NotImplementedException(); } + public Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) + { + throw new NotImplementedException(); + } + public Task Update(Tier tier, CancellationToken cancellationToken) { throw new NotImplementedException(); diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs index 4f1d9c20d8..ade4ad7ba4 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/FindTiersStubRepository.cs @@ -32,6 +32,11 @@ public Task RemoveById(TierId tierId) throw new NotImplementedException(); } + public Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) + { + throw new NotImplementedException(); + } + public Task Update(Tier tier, CancellationToken cancellationToken) { throw new NotImplementedException(); From 89f4c4f734bcb4ed37f6e4fdf24d52bfead1b483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 8 Feb 2024 14:19:25 +0100 Subject: [PATCH 05/17] chore: fix formatting --- .../Persistence/Database/IServiceCollectionExtensions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs index d4651a7f69..02dc9112f9 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs @@ -53,7 +53,8 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio throw new Exception($"Unsupported database provider: {options.Provider}"); } - dbContextOptions.UseTriggers(triggerOptions => { + dbContextOptions.UseTriggers(triggerOptions => + { triggerOptions.AddTrigger(); }); }); From 7750272f4801e3c137a02735daa7d104410e5690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 8 Feb 2024 14:22:24 +0100 Subject: [PATCH 06/17] chore: fix failing ci/cd tests --- .../TestDoubles/AddMockTiersRepository.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs index f8b3ce2325..2603ce2865 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs @@ -29,6 +29,11 @@ public Task RemoveById(TierId tierId) throw new NotImplementedException(); } + public Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) + { + throw new NotImplementedException(); + } + public Task Update(Tier tier, CancellationToken cancellationToken) { throw new NotImplementedException(); From ed6e1dcec9d32c5d38acf138df8ee9919a505934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Tue, 20 Feb 2024 11:52:49 +0100 Subject: [PATCH 07/17] feat: remove orphaned TierQuotaDefinition entries using ChangeTracker --- .../Persistence/Repository/TiersRepository.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs index 95145d5821..43ddb58543 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs @@ -1,6 +1,4 @@ -using System.Threading; -using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.Persistence.Database; -using Backbone.BuildingBlocks.Application.Extensions; +using Backbone.BuildingBlocks.Application.Extensions; using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; @@ -57,13 +55,6 @@ public async Task RemoveById(TierId tierId) public async Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) { - //await _tierQuotaDefinitions.Where(t => t.Id == tierQuotaDefinitionId).ExecuteDeleteAsync(); - - //var allTierQuotaDefinitions = await _tierQuotaDefinitions.ToListAsync(); - //var tierQuotaDefinitionsWithTierIdNull = allTierQuotaDefinitions.Where(t => _dbContext.Entry(t).Property("TierId").CurrentValue == null).ToList(); - //var idsOfTierQuotasWithTierIdNull = tierQuotaDefinitionsWithTierIdNull.Select(t => t.Id).ToList(); - //await _tierQuotaDefinitions.Where(t => idsOfTierQuotasWithTierIdNull.Contains(t.Id)).ExecuteDeleteAsync(); - var tierQuotaDefinition = await _tierQuotaDefinitions.FirstWithId(tierQuotaDefinitionId, CancellationToken.None); if (_dbContext.Entry(tierQuotaDefinition).Property("TierId").CurrentValue == null) @@ -72,6 +63,12 @@ public async Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tier public async Task Update(Tier tier, CancellationToken cancellationToken) { + _dbContext.RemoveRange(_dbContext.ChangeTracker + .Entries() + .Where(e => e.State == EntityState.Modified) + .Select(e => e.Entity) + .ToList()); + _tiers.Update(tier); await _dbContext.SaveChangesAsync(cancellationToken); } From a81194f8743bf16e56770d36e9855fb91f3e4688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Tue, 20 Feb 2024 11:55:42 +0100 Subject: [PATCH 08/17] chore: remove EntityFrameworkCore.Triggered dependency --- .../Triggers/DeleteTierQuotaDefinitionsTrigger.cs | 14 -------------- .../Quotas/src/Quotas.Domain/Quotas.Domain.csproj | 1 - .../Database/IServiceCollectionExtensions.cs | 6 ------ 3 files changed, 21 deletions(-) delete mode 100644 Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs diff --git a/Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs b/Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs deleted file mode 100644 index 4cd81bf79f..0000000000 --- a/Modules/Quotas/src/Quotas.Application/Tiers/Triggers/DeleteTierQuotaDefinitionsTrigger.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; -using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; -using EntityFrameworkCore.Triggered; - -namespace Backbone.Modules.Quotas.Application.Tiers.Triggers; - -public class DeleteTierQuotaDefinitionsTrigger(ITiersRepository tiersRepository) : IAfterSaveTrigger -{ - public async Task AfterSave(ITriggerContext context, CancellationToken cancellationToken) - { - if (context.ChangeType == ChangeType.Modified) - await tiersRepository.RemoveTierQuotaDefinitionIfOrphaned(context.Entity.Id); - } -} diff --git a/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj b/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj index 6c710e3bc4..1829f89c25 100644 --- a/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj +++ b/Modules/Quotas/src/Quotas.Domain/Quotas.Domain.csproj @@ -6,7 +6,6 @@ - diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs index 98f9ede867..091bdfde68 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Database/IServiceCollectionExtensions.cs @@ -1,6 +1,5 @@ using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository; using Backbone.Modules.Quotas.Application.Metrics; -using Backbone.Modules.Quotas.Application.Tiers.Triggers; using Backbone.Modules.Quotas.Domain.Metrics; using Backbone.Modules.Quotas.Infrastructure.Persistence.Repository; using Microsoft.EntityFrameworkCore; @@ -52,11 +51,6 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio default: throw new Exception($"Unsupported database provider: {options.Provider}"); } - - dbContextOptions.UseTriggers(triggerOptions => - { - triggerOptions.AddTrigger(); - }); }); services.AddTransient(); services.AddTransient(); From 3540b688bf76d40b0098ef387d5baa56d934b3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Wed, 21 Feb 2024 09:34:25 +0100 Subject: [PATCH 09/17] chore: remove unnecessary method --- .../Persistence/Repository/ITiersRepository.cs | 1 - .../TestDoubles/AddMockTiersRepository.cs | 5 ----- 2 files changed, 6 deletions(-) diff --git a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs index 6625496e12..3210a561bf 100644 --- a/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Application/Infrastructure/Persistence/Repository/ITiersRepository.cs @@ -7,6 +7,5 @@ public interface ITiersRepository Task Find(string id, CancellationToken cancellationToken, bool track = false); Task FindTierQuotaDefinition(string id, CancellationToken cancellationToken, bool track = false); Task RemoveById(TierId tierId); - Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId); Task Update(Tier tier, CancellationToken cancellationToken); } diff --git a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs index e2031988ce..382312bc4a 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/TestDoubles/AddMockTiersRepository.cs @@ -30,11 +30,6 @@ public Task RemoveById(TierId tierId) throw new NotImplementedException(); } - public Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) - { - throw new NotImplementedException(); - } - public Task Update(Tier tier, CancellationToken cancellationToken) { throw new NotImplementedException(); From 01e890104772dc3b552fedb65604b44b160d825f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Wed, 21 Feb 2024 09:35:33 +0100 Subject: [PATCH 10/17] feat: ensure only modified and orphaned TierQuotaDefinitions are deleted --- .../Persistence/Repository/TiersRepository.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs index 43ddb58543..e4f841f422 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs @@ -53,23 +53,23 @@ public async Task RemoveById(TierId tierId) await _tiers.Where(t => t.Id == tierId).ExecuteDeleteAsync(); } - public async Task RemoveTierQuotaDefinitionIfOrphaned(TierQuotaDefinitionId tierQuotaDefinitionId) + public async Task Update(Tier tier, CancellationToken cancellationToken) { - var tierQuotaDefinition = await _tierQuotaDefinitions.FirstWithId(tierQuotaDefinitionId, CancellationToken.None); - - if (_dbContext.Entry(tierQuotaDefinition).Property("TierId").CurrentValue == null) - await _tierQuotaDefinitions.Where(t => t.Id == tierQuotaDefinitionId).ExecuteDeleteAsync(); + RemoveModifiedAndOrphanedTierQuotaDefinitions(); + _tiers.Update(tier); + await _dbContext.SaveChangesAsync(cancellationToken); } - public async Task Update(Tier tier, CancellationToken cancellationToken) + private void RemoveModifiedAndOrphanedTierQuotaDefinitions() { - _dbContext.RemoveRange(_dbContext.ChangeTracker + var modifiedTierQuotaDefinitions = _dbContext.ChangeTracker .Entries() .Where(e => e.State == EntityState.Modified) .Select(e => e.Entity) - .ToList()); + .ToList(); - _tiers.Update(tier); - await _dbContext.SaveChangesAsync(cancellationToken); + _dbContext.RemoveRange(modifiedTierQuotaDefinitions + .Where(tqd => _dbContext.Entry(tqd).Property("TierId").CurrentValue == null) + .ToList()); } } From d7ef9736876f2a09cd42ac563f04ddede35c2f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 22 Feb 2024 10:54:30 +0100 Subject: [PATCH 11/17] test: add TierQuotaDefinition deletion test --- .../Tests/Tiers/TierQuotaDeletionTests.cs | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs new file mode 100644 index 0000000000..494efa5694 --- /dev/null +++ b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs @@ -0,0 +1,137 @@ +using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; +using Backbone.Modules.Quotas.Domain.Aggregates.Identities; +using Backbone.Modules.Quotas.Domain.Aggregates.Metrics; +using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; +using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; +using Backbone.Modules.Quotas.Infrastructure.Persistence.Repository; +using Backbone.UnitTestTools.TestDoubles.Fakes; +using FluentAssertions; +using Xunit; + +namespace Backbone.Modules.Quotas.Application.Tests.Tests.Tiers; + +public class TierQuotaDeletionTests +{ + private readonly QuotasDbContext _actContext; + private readonly QuotasDbContext _arrangeContext; + private readonly QuotasDbContext _assertContext; + + private TierId? _tierId; + private TierQuotaDefinitionId? _tierQuotaDefinitionId; + + public TierQuotaDeletionTests() + { + (_arrangeContext, _actContext, _assertContext) = FakeDbContextFactory.CreateDbContexts(); + } + + [Fact] + public async void Tier_quota_definition_is_deleted_when_removed_from_its_tier() + { + // Arrange + await CreateTier(ContextToUse.Act); + + // Act + await DeleteTierQuotaDefinition(ContextToUse.Act, _tierId!, _tierQuotaDefinitionId!); + + // Assert + var repository = CreateRepository(ContextToUse.Assert); + var asserting = + async () => await repository.FindTierQuotaDefinition(_tierQuotaDefinitionId!.Value, CancellationToken.None); + + await asserting.Should().ThrowAsync(); + } + + [Fact] + public async void Tier_quota_definition_is_deleted_when_its_tier_is_deleted() + { + // Arrange + await CreateTier(ContextToUse.Act); + + // Act + await DeleteTier(ContextToUse.Act, _tierId!); + + // Assert + var repository = CreateRepository(ContextToUse.Assert); + var asserting = + async () => await repository.FindTierQuotaDefinition(_tierQuotaDefinitionId!.Value, CancellationToken.None); + + await asserting.Should().ThrowAsync(); + } + + private async Task CreateTier(ContextToUse contextToUse) + { + var repository = CreateRepository(contextToUse); + + _tierId = new TierId("TIR00000000000000000"); + var tier = new Tier(_tierId, "Test"); + + var metricKey = MetricKey.NumberOfSentMessages; + const int max = 5; + const QuotaPeriod period = QuotaPeriod.Month; + + tier.CreateQuota(metricKey, max, period); + _tierQuotaDefinitionId = tier.Quotas.First().Id; + + await repository.Add(tier, CancellationToken.None); + await _arrangeContext.SaveChangesAsync(); + } + + private async Task DeleteTierQuotaDefinition(ContextToUse contextToUse, TierId tierId, TierQuotaDefinitionId tierQuotaDefinitionId) + { + var repository = CreateRepository(contextToUse); + var context = GetContext(contextToUse); + + var tier = await repository.Find(tierId, CancellationToken.None, true) ?? throw new NotFoundException(nameof(Tier)); + + tier.DeleteQuota(tierQuotaDefinitionId); + + await repository.Update(tier, CancellationToken.None); + await context.SaveChangesAsync(); + } + + private async Task DeleteTier(ContextToUse contextToUse, TierId tierId) + { + var repository = CreateRepository(contextToUse); + var context = GetContext(contextToUse); + + await repository.RemoveById(tierId); + await context.SaveChangesAsync(); + } + + private QuotasDbContext GetContext(ContextToUse contextToUse) + { + switch (contextToUse) + { + case ContextToUse.Arrange: + return _arrangeContext; + case ContextToUse.Act: + return _actContext; + case ContextToUse.Assert: + return _assertContext; + default: + throw new ArgumentOutOfRangeException(nameof(contextToUse), contextToUse, null); + } + } + + private TiersRepository CreateRepository(ContextToUse contextToUse) + { + switch (contextToUse) + { + case ContextToUse.Arrange: + return new TiersRepository(_arrangeContext); + case ContextToUse.Act: + return new TiersRepository(_actContext); + case ContextToUse.Assert: + return new TiersRepository(_assertContext); + default: + throw new ArgumentOutOfRangeException(nameof(contextToUse), contextToUse, null); + } + } + + private enum ContextToUse + { + Arrange, + Act, + Assert + } +} From ab2827ce306f193b5c335f3bd62cecb65346170e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Vetni=C4=87?= Date: Thu, 22 Feb 2024 11:00:20 +0100 Subject: [PATCH 12/17] chore: fix formatting --- .../Tests/Tiers/TierQuotaDeletionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs index 494efa5694..6ad899bc45 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs +++ b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs @@ -35,7 +35,7 @@ public async void Tier_quota_definition_is_deleted_when_removed_from_its_tier() // Assert var repository = CreateRepository(ContextToUse.Assert); - var asserting = + var asserting = async () => await repository.FindTierQuotaDefinition(_tierQuotaDefinitionId!.Value, CancellationToken.None); await asserting.Should().ThrowAsync(); From 3d4047c3edfa1f7430ba58bd20e385f19f24e409 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Thu, 22 Feb 2024 13:10:03 +0100 Subject: [PATCH 13/17] refactor: simplify RemoveOrphanedTierQuotaDefinitions --- .../Persistence/Repository/TiersRepository.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs index e4f841f422..7d76a93fdc 100644 --- a/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs +++ b/Modules/Quotas/src/Quotas.Infrastructure/Persistence/Repository/TiersRepository.cs @@ -55,21 +55,19 @@ public async Task RemoveById(TierId tierId) public async Task Update(Tier tier, CancellationToken cancellationToken) { - RemoveModifiedAndOrphanedTierQuotaDefinitions(); + RemoveOrphanedTierQuotaDefinitions(); _tiers.Update(tier); await _dbContext.SaveChangesAsync(cancellationToken); } - private void RemoveModifiedAndOrphanedTierQuotaDefinitions() + private void RemoveOrphanedTierQuotaDefinitions() { - var modifiedTierQuotaDefinitions = _dbContext.ChangeTracker + var removedQuotas = _dbContext.ChangeTracker .Entries() .Where(e => e.State == EntityState.Modified) - .Select(e => e.Entity) - .ToList(); + .Where(e => _dbContext.Entry(e.Entity).Property("TierId").CurrentValue == null) + .Select(e => e.Entity); - _dbContext.RemoveRange(modifiedTierQuotaDefinitions - .Where(tqd => _dbContext.Entry(tqd).Property("TierId").CurrentValue == null) - .ToList()); + _dbContext.RemoveRange(removedQuotas); } } From bd58afa6d1ec7c8e402e39e412fc0e460bf9cfe3 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Thu, 22 Feb 2024 15:42:59 +0100 Subject: [PATCH 14/17] test: improve/rewrite test --- Backbone.sln | 9 +- .../Tests/Tiers/TierQuotaDeletionTests.cs | 137 ------------------ .../Quotas.Infrastructure.Tests.csproj | 29 ++++ .../Repositories/TiersRepositoryTests.cs | 45 ++++++ 4 files changed, 82 insertions(+), 138 deletions(-) delete mode 100644 Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs create mode 100644 Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj create mode 100644 Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/TiersRepositoryTests.cs diff --git a/Backbone.sln b/Backbone.sln index 565d6bccca..3bd3e578fd 100644 --- a/Backbone.sln +++ b/Backbone.sln @@ -265,7 +265,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdminUi.Infrastructure.Data EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdminUi.Infrastructure", "AdminUi\src\AdminUi.Infrastructure\AdminUi.Infrastructure.csproj", "{5028E85D-E115-4E02-AC94-2B441686E44E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HealthCheck", "HealthCheck\HealthCheck.csproj", "{EE910828-296B-45CD-BA01-DCABE27BCC4C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthCheck", "HealthCheck\HealthCheck.csproj", "{EE910828-296B-45CD-BA01-DCABE27BCC4C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Quotas.Infrastructure.Tests", "Modules\Quotas\test\Quotas.Infrastructure.Tests\Quotas.Infrastructure.Tests.csproj", "{FB38C7C5-9F11-43BB-871F-E2E0360FD993}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -649,6 +651,10 @@ Global {EE910828-296B-45CD-BA01-DCABE27BCC4C}.Debug|Any CPU.Build.0 = Debug|Any CPU {EE910828-296B-45CD-BA01-DCABE27BCC4C}.Release|Any CPU.ActiveCfg = Release|Any CPU {EE910828-296B-45CD-BA01-DCABE27BCC4C}.Release|Any CPU.Build.0 = Release|Any CPU + {FB38C7C5-9F11-43BB-871F-E2E0360FD993}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB38C7C5-9F11-43BB-871F-E2E0360FD993}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB38C7C5-9F11-43BB-871F-E2E0360FD993}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB38C7C5-9F11-43BB-871F-E2E0360FD993}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -773,6 +779,7 @@ Global {FEBD339A-7343-47EF-84F1-770EDF7E5E94} = {A8C20813-97C8-42A9-B45A-4A0D650DA647} {4D9FCC6B-0958-45A2-85EA-3992CE29B5CB} = {A8C20813-97C8-42A9-B45A-4A0D650DA647} {5028E85D-E115-4E02-AC94-2B441686E44E} = {A8C20813-97C8-42A9-B45A-4A0D650DA647} + {FB38C7C5-9F11-43BB-871F-E2E0360FD993} = {4192A28C-45C0-4D20-B880-F417B8AB752F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {1F3BD2C6-7CB3-450F-A21A-23EA520D5B7A} diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs b/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs deleted file mode 100644 index 6ad899bc45..0000000000 --- a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Tiers/TierQuotaDeletionTests.cs +++ /dev/null @@ -1,137 +0,0 @@ -using Backbone.BuildingBlocks.Application.Abstractions.Exceptions; -using Backbone.Modules.Quotas.Domain.Aggregates.Identities; -using Backbone.Modules.Quotas.Domain.Aggregates.Metrics; -using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; -using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; -using Backbone.Modules.Quotas.Infrastructure.Persistence.Repository; -using Backbone.UnitTestTools.TestDoubles.Fakes; -using FluentAssertions; -using Xunit; - -namespace Backbone.Modules.Quotas.Application.Tests.Tests.Tiers; - -public class TierQuotaDeletionTests -{ - private readonly QuotasDbContext _actContext; - private readonly QuotasDbContext _arrangeContext; - private readonly QuotasDbContext _assertContext; - - private TierId? _tierId; - private TierQuotaDefinitionId? _tierQuotaDefinitionId; - - public TierQuotaDeletionTests() - { - (_arrangeContext, _actContext, _assertContext) = FakeDbContextFactory.CreateDbContexts(); - } - - [Fact] - public async void Tier_quota_definition_is_deleted_when_removed_from_its_tier() - { - // Arrange - await CreateTier(ContextToUse.Act); - - // Act - await DeleteTierQuotaDefinition(ContextToUse.Act, _tierId!, _tierQuotaDefinitionId!); - - // Assert - var repository = CreateRepository(ContextToUse.Assert); - var asserting = - async () => await repository.FindTierQuotaDefinition(_tierQuotaDefinitionId!.Value, CancellationToken.None); - - await asserting.Should().ThrowAsync(); - } - - [Fact] - public async void Tier_quota_definition_is_deleted_when_its_tier_is_deleted() - { - // Arrange - await CreateTier(ContextToUse.Act); - - // Act - await DeleteTier(ContextToUse.Act, _tierId!); - - // Assert - var repository = CreateRepository(ContextToUse.Assert); - var asserting = - async () => await repository.FindTierQuotaDefinition(_tierQuotaDefinitionId!.Value, CancellationToken.None); - - await asserting.Should().ThrowAsync(); - } - - private async Task CreateTier(ContextToUse contextToUse) - { - var repository = CreateRepository(contextToUse); - - _tierId = new TierId("TIR00000000000000000"); - var tier = new Tier(_tierId, "Test"); - - var metricKey = MetricKey.NumberOfSentMessages; - const int max = 5; - const QuotaPeriod period = QuotaPeriod.Month; - - tier.CreateQuota(metricKey, max, period); - _tierQuotaDefinitionId = tier.Quotas.First().Id; - - await repository.Add(tier, CancellationToken.None); - await _arrangeContext.SaveChangesAsync(); - } - - private async Task DeleteTierQuotaDefinition(ContextToUse contextToUse, TierId tierId, TierQuotaDefinitionId tierQuotaDefinitionId) - { - var repository = CreateRepository(contextToUse); - var context = GetContext(contextToUse); - - var tier = await repository.Find(tierId, CancellationToken.None, true) ?? throw new NotFoundException(nameof(Tier)); - - tier.DeleteQuota(tierQuotaDefinitionId); - - await repository.Update(tier, CancellationToken.None); - await context.SaveChangesAsync(); - } - - private async Task DeleteTier(ContextToUse contextToUse, TierId tierId) - { - var repository = CreateRepository(contextToUse); - var context = GetContext(contextToUse); - - await repository.RemoveById(tierId); - await context.SaveChangesAsync(); - } - - private QuotasDbContext GetContext(ContextToUse contextToUse) - { - switch (contextToUse) - { - case ContextToUse.Arrange: - return _arrangeContext; - case ContextToUse.Act: - return _actContext; - case ContextToUse.Assert: - return _assertContext; - default: - throw new ArgumentOutOfRangeException(nameof(contextToUse), contextToUse, null); - } - } - - private TiersRepository CreateRepository(ContextToUse contextToUse) - { - switch (contextToUse) - { - case ContextToUse.Arrange: - return new TiersRepository(_arrangeContext); - case ContextToUse.Act: - return new TiersRepository(_actContext); - case ContextToUse.Assert: - return new TiersRepository(_assertContext); - default: - throw new ArgumentOutOfRangeException(nameof(contextToUse), contextToUse, null); - } - } - - private enum ContextToUse - { - Arrange, - Act, - Assert - } -} diff --git a/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj new file mode 100644 index 0000000000..54e24925a6 --- /dev/null +++ b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/TiersRepositoryTests.cs b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/TiersRepositoryTests.cs new file mode 100644 index 0000000000..d43a6012b0 --- /dev/null +++ b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/TiersRepositoryTests.cs @@ -0,0 +1,45 @@ +using Backbone.Modules.Quotas.Domain.Aggregates.Identities; +using Backbone.Modules.Quotas.Domain.Aggregates.Metrics; +using Backbone.Modules.Quotas.Domain.Aggregates.Tiers; +using Backbone.Modules.Quotas.Infrastructure.Persistence.Database; +using Backbone.Modules.Quotas.Infrastructure.Persistence.Repository; +using Backbone.UnitTestTools.TestDoubles.Fakes; +using FluentAssertions; +using Xunit; + +namespace Backbone.Modules.Quotas.Infrastructure.Tests.Tests.Repositories; + +public class TiersRepositoryTests +{ + /** + * This test makes sure that when the Update method is called on the TiersRepository with a Tier from which + * TierQuotaDefinitions were removed, the TierQuotaDefinitions were removed from the database as well. + * That's not the case by default, because EF Core only sets the foreign key to null, but doesn't remove the + * lines from the TierQuotaDefinitions table. + */ + [Fact] + public async Task Updating_a_Tier_deletes_unassigned_quotas_from_the_TierQuotaDefinitions_table() + { + // Arrange + var (arrangeContext, actContext, assertContext) = FakeDbContextFactory.CreateDbContexts(); + + var arrangedTier = new Tier(new TierId("TIR00000000000000000"), "Test"); + var tierQuotaDefinitionToBeDeleted = arrangedTier.CreateQuota(MetricKey.NumberOfSentMessages, 5, QuotaPeriod.Month).Value; + var otherTierQuotaDefinition = arrangedTier.CreateQuota(MetricKey.NumberOfFiles, 5, QuotaPeriod.Month).Value; + + await arrangeContext.Tiers.AddAsync(arrangedTier); + await arrangeContext.SaveChangesAsync(); + + var repository = new TiersRepository(actContext); + + var actTier = (await repository.Find(arrangedTier.Id, CancellationToken.None, true))!; + actTier.DeleteQuota(tierQuotaDefinitionToBeDeleted.Id); + + // Act + await repository.Update(actTier, CancellationToken.None); + + // Assert + assertContext.Set().Should().NotContain(q => q.Id == tierQuotaDefinitionToBeDeleted.Id); + assertContext.Set().Should().Contain(q => q.Id == otherTierQuotaDefinition.Id); + } +} From d1ae496bbd9be180c56d9df17108ca6cd270a00f Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Thu, 22 Feb 2024 15:43:39 +0100 Subject: [PATCH 15/17] chore: move MessagesRepositoryTests to Quotas.Infrastructure.Tests --- .../Quotas.Application.Tests.csproj | 2 -- .../Repositories/MessagesRepositoryTests.cs | 23 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) rename Modules/Quotas/test/{Quotas.Application.Tests => Quotas.Infrastructure.Tests}/Tests/Repositories/MessagesRepositoryTests.cs (93%) diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Quotas.Application.Tests.csproj b/Modules/Quotas/test/Quotas.Application.Tests/Quotas.Application.Tests.csproj index 5bceadc4df..6616e43652 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/Quotas.Application.Tests.csproj +++ b/Modules/Quotas/test/Quotas.Application.Tests/Quotas.Application.Tests.csproj @@ -17,9 +17,7 @@ - - diff --git a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Repositories/MessagesRepositoryTests.cs b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/MessagesRepositoryTests.cs similarity index 93% rename from Modules/Quotas/test/Quotas.Application.Tests/Tests/Repositories/MessagesRepositoryTests.cs rename to Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/MessagesRepositoryTests.cs index ab2f04ed08..5dc4bb828d 100644 --- a/Modules/Quotas/test/Quotas.Application.Tests/Tests/Repositories/MessagesRepositoryTests.cs +++ b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Tests/Repositories/MessagesRepositoryTests.cs @@ -11,10 +11,10 @@ using FluentAssertions.Execution; using Xunit; -namespace Backbone.Modules.Quotas.Application.Tests.Tests.Repositories; +namespace Backbone.Modules.Quotas.Infrastructure.Tests.Tests.Repositories; + public class MessagesRepositoryTests { - private readonly IdentityAddress _identityAddress1 = TestDataGenerator.CreateRandomIdentityAddress(); private readonly IdentityAddress _identityAddress2 = TestDataGenerator.CreateRandomIdentityAddress(); @@ -28,6 +28,7 @@ public class MessagesRepositoryTests * */ private static readonly DateTime TODAY = new(2020, 02, 15, 10, 30, 00); + private static readonly DateTime YESTERDAY = TODAY.AddDays(-1); private static readonly DateTime TOMORROW = TODAY.AddDays(1); private static readonly DateTime LAST_YEAR = TODAY.AddYears(-1); @@ -48,7 +49,8 @@ public MessagesRepositoryTests() public async Task Counts_entities_within_timeframe_hour_quotaPeriod() { // Arrange - var messages = new List() { + var messages = new List() + { CreateMessage(TODAY, _identityAddress1), CreateMessage(YESTERDAY, _identityAddress1), CreateMessage(TOMORROW, _identityAddress1) @@ -70,7 +72,8 @@ public async Task Counts_entities_within_timeframe_hour_quotaPeriod() public async Task Counts_entities_within_timeframe_month_quotaPeriod() { // Arrange - var messages = new List() { + var messages = new List() + { CreateMessage(TODAY, _identityAddress1), CreateMessage(YESTERDAY, _identityAddress1), CreateMessage(TOMORROW, _identityAddress1), @@ -94,7 +97,8 @@ public async Task Counts_entities_within_timeframe_month_quotaPeriod() public async Task Counts_entities_total_quotaPeriod() { // Arrange - var messages = new List() { + var messages = new List() + { CreateMessage(TODAY, _identityAddress1), CreateMessage(TOMORROW, _identityAddress1), CreateMessage(NEXT_YEAR, _identityAddress1) @@ -116,7 +120,8 @@ public async Task Counts_entities_total_quotaPeriod() public async Task Counts_entities_only_for_requested_identityAddress() { // Arrange - var messages = new List() { + var messages = new List() + { CreateMessage(TODAY, _identityAddress1), CreateMessage(TOMORROW, _identityAddress2), CreateMessage(NEXT_YEAR, _identityAddress1) @@ -145,9 +150,9 @@ private static Message CreateMessage(DateTime createdAt, IdentityAddress identit DeviceId.New(), null, Array.Empty(), - new List(), - new List() - ); + [], + [] + ); SystemTime.Set(savedDateTime); From df7f138659e948557e33860bf985ce62cfb5f17d Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Thu, 22 Feb 2024 15:52:53 +0100 Subject: [PATCH 16/17] refactor: simplify "IsNullOrEmpty" extension method --- AdminUi/test/AdminUi.Tests.Integration/HttpClientFactory.cs | 2 +- BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs | 5 +++-- ConsumerApi.Tests.Integration/HttpClientFactory.cs | 2 +- ConsumerApi/Controllers/AuthorizationController.cs | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/AdminUi/test/AdminUi.Tests.Integration/HttpClientFactory.cs b/AdminUi/test/AdminUi.Tests.Integration/HttpClientFactory.cs index f7b6d99d36..d2bb38c3c0 100644 --- a/AdminUi/test/AdminUi.Tests.Integration/HttpClientFactory.cs +++ b/AdminUi/test/AdminUi.Tests.Integration/HttpClientFactory.cs @@ -14,7 +14,7 @@ internal HttpClientFactory(CustomWebApplicationFactory factory) internal HttpClient CreateClient() { var baseAddress = Environment.GetEnvironmentVariable("ADMIN_API_BASE_ADDRESS"); - return baseAddress.IsNullOrEmpty() ? _factory.CreateClient() : new HttpClient() { BaseAddress = new Uri(baseAddress!) }; + return baseAddress.IsNullOrEmpty() ? _factory.CreateClient() : new HttpClient() { BaseAddress = new Uri(baseAddress) }; } } diff --git a/BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs b/BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs index 9b8443a1b7..d5bdea90ce 100644 --- a/BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs +++ b/BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Diagnostics.CodeAnalysis; +using System.Text.RegularExpressions; namespace Backbone.Tooling.Extensions; @@ -9,7 +10,7 @@ public static bool IsEmpty(this string @string) return @string == string.Empty; } - public static bool IsNullOrEmpty(this string? @string) + public static bool IsNullOrEmpty([NotNullWhen(false)] this string? @string) { return string.IsNullOrEmpty(@string); } diff --git a/ConsumerApi.Tests.Integration/HttpClientFactory.cs b/ConsumerApi.Tests.Integration/HttpClientFactory.cs index d037f901a8..c0a1401467 100644 --- a/ConsumerApi.Tests.Integration/HttpClientFactory.cs +++ b/ConsumerApi.Tests.Integration/HttpClientFactory.cs @@ -14,6 +14,6 @@ internal HttpClientFactory(CustomWebApplicationFactory factory) internal HttpClient CreateClient() { var baseAddress = Environment.GetEnvironmentVariable("CONSUMER_API_BASE_ADDRESS"); - return baseAddress.IsNullOrEmpty() ? _factory.CreateClient() : new HttpClient() { BaseAddress = new Uri(baseAddress!) }; + return baseAddress.IsNullOrEmpty() ? _factory.CreateClient() : new HttpClient() { BaseAddress = new Uri(baseAddress) }; } } diff --git a/ConsumerApi/Controllers/AuthorizationController.cs b/ConsumerApi/Controllers/AuthorizationController.cs index e6373a8f52..88bc1a7db2 100644 --- a/ConsumerApi/Controllers/AuthorizationController.cs +++ b/ConsumerApi/Controllers/AuthorizationController.cs @@ -56,7 +56,7 @@ public async Task Exchange() throw new OperationFailedException( ApplicationErrors.Authentication.InvalidOAuthRequest("missing password")); - var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password!, lockoutOnFailure: true); + var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, lockoutOnFailure: true); if (result.IsLockedOut) return UserLockedOut(); if (!result.Succeeded) From fe63a1686a0e8ee57ae33f6fd75e32bd734f0e59 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Thu, 22 Feb 2024 15:55:09 +0100 Subject: [PATCH 17/17] chore: remove redundant properties from Quotas.Infrastructure.Tests.csproj --- .../Quotas.Infrastructure.Tests.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj index 54e24925a6..a75f9ee293 100644 --- a/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj +++ b/Modules/Quotas/test/Quotas.Infrastructure.Tests/Quotas.Infrastructure.Tests.csproj @@ -1,10 +1,6 @@  - net8.0 - enable - enable - false true