From 6d4b58db029ced2f922b0d1c205aa974c865f239 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:22:22 -0500 Subject: [PATCH] tests: make sure SdnId is set for dependent entities --- src/SanctionsSearch.Worker.Tests/Faker/SdnFaker.cs | 5 ++++- .../Integration/AddressRepositoryTests.cs | 3 +++ .../Integration/AliasRepositoryTests.cs | 3 +++ .../Integration/CommentRepositoryTests.cs | 3 +++ .../Integration/RepositoryTest.cs | 6 ++++++ 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/SanctionsSearch.Worker.Tests/Faker/SdnFaker.cs b/src/SanctionsSearch.Worker.Tests/Faker/SdnFaker.cs index 1d0f766..50dccf4 100644 --- a/src/SanctionsSearch.Worker.Tests/Faker/SdnFaker.cs +++ b/src/SanctionsSearch.Worker.Tests/Faker/SdnFaker.cs @@ -2,9 +2,12 @@ namespace SanctionsSearch.Worker.Tests.Faker; class SdnFaker : Faker { + public const int ReservedId = 1; + private readonly int _minimumId = ReservedId + 1; + public SdnFaker() { - RuleFor(x => x.Id, f => f.Random.Int(1, int.MaxValue)); + RuleFor(x => x.Id, f => f.Random.Int(_minimumId, int.MaxValue)); RuleFor(x => x.Name, f => f.Person.FullName); RuleFor(x => x.Type, f => f.Lorem.Word()); RuleFor(x => x.Program, f => f.Lorem.Word()); diff --git a/src/SanctionsSearch.Worker.Tests/Integration/AddressRepositoryTests.cs b/src/SanctionsSearch.Worker.Tests/Integration/AddressRepositoryTests.cs index 1439c8b..2f887ca 100644 --- a/src/SanctionsSearch.Worker.Tests/Integration/AddressRepositoryTests.cs +++ b/src/SanctionsSearch.Worker.Tests/Integration/AddressRepositoryTests.cs @@ -18,6 +18,7 @@ public class AddressRepositoryTests : RepositoryTest _timeProvider.Setup(x => x.GetUtcNow()).Returns(now); var entity = _faker.Generate(); + entity.SdnId = SdnId; await _repository.Upsert(entity); await _context.SaveChangesAsync(); @@ -38,6 +39,7 @@ public class AddressRepositoryTests : RepositoryTest _timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp); var entity = _faker.Generate(); + entity.SdnId = SdnId; // Insert the entity await _repository.Upsert(entity); @@ -72,6 +74,7 @@ public class AddressRepositoryTests : RepositoryTest foreach (var entity in entities) { + entity.SdnId = SdnId; await _repository.Upsert(entity); } diff --git a/src/SanctionsSearch.Worker.Tests/Integration/AliasRepositoryTests.cs b/src/SanctionsSearch.Worker.Tests/Integration/AliasRepositoryTests.cs index c419fe7..ed0ab53 100644 --- a/src/SanctionsSearch.Worker.Tests/Integration/AliasRepositoryTests.cs +++ b/src/SanctionsSearch.Worker.Tests/Integration/AliasRepositoryTests.cs @@ -18,6 +18,7 @@ public class AliasRepositoryTests : RepositoryTest _timeProvider.Setup(x => x.GetUtcNow()).Returns(now); var entity = _faker.Generate(); + entity.SdnId = SdnId; await _repository.Upsert(entity); await _context.SaveChangesAsync(); @@ -38,6 +39,7 @@ public class AliasRepositoryTests : RepositoryTest _timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp); var entity = _faker.Generate(); + entity.SdnId = SdnId; // Insert the entity await _repository.Upsert(entity); @@ -72,6 +74,7 @@ public class AliasRepositoryTests : RepositoryTest foreach (var entity in entities) { + entity.SdnId = SdnId; await _repository.Upsert(entity); } diff --git a/src/SanctionsSearch.Worker.Tests/Integration/CommentRepositoryTests.cs b/src/SanctionsSearch.Worker.Tests/Integration/CommentRepositoryTests.cs index 6745411..dfa3f23 100644 --- a/src/SanctionsSearch.Worker.Tests/Integration/CommentRepositoryTests.cs +++ b/src/SanctionsSearch.Worker.Tests/Integration/CommentRepositoryTests.cs @@ -18,6 +18,7 @@ public class CommentRepositoryTests : RepositoryTest _timeProvider.Setup(x => x.GetUtcNow()).Returns(now); var entity = _faker.Generate(); + entity.SdnId = SdnId; await _repository.Upsert(entity); await _context.SaveChangesAsync(); @@ -38,6 +39,7 @@ public class CommentRepositoryTests : RepositoryTest _timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp); var entity = _faker.Generate(); + entity.SdnId = SdnId; // Insert the entity await _repository.Upsert(entity); @@ -72,6 +74,7 @@ public class CommentRepositoryTests : RepositoryTest foreach (var entity in entities) { + entity.SdnId = SdnId; await _repository.Upsert(entity); } diff --git a/src/SanctionsSearch.Worker.Tests/Integration/RepositoryTest.cs b/src/SanctionsSearch.Worker.Tests/Integration/RepositoryTest.cs index be0aa1d..2562c9c 100644 --- a/src/SanctionsSearch.Worker.Tests/Integration/RepositoryTest.cs +++ b/src/SanctionsSearch.Worker.Tests/Integration/RepositoryTest.cs @@ -3,8 +3,11 @@ namespace SanctionsSearch.Worker.Tests.Integration; public class RepositoryTest : IAsyncLifetime { private readonly AppDbContext _appDbContext; + private readonly SdnFaker _faker = new(); + private readonly Sdn _sdn; protected readonly Mock _timeProvider = new(); protected readonly DbContext _context; + protected int SdnId => _sdn.Id; public RepositoryTest() { @@ -13,11 +16,14 @@ public class RepositoryTest : IAsyncLifetime _appDbContext = context; _context = context; + _sdn = _faker.Generate(); + _sdn.Id = SdnFaker.ReservedId; } public async Task InitializeAsync() { await _context.Database.MigrateAsync(); + await _context.Set().AddAsync(_sdn); } public async Task DisposeAsync()