From 417406d306c8d6b3038b318dd8879044e2d660d8 Mon Sep 17 00:00:00 2001 From: Marcel Kummerow Date: Tue, 26 May 2026 16:21:06 +0200 Subject: [PATCH] refactor: cleanup code across fakes and tests Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../FakeOrganizationService.cs | 6 +- .../OrganizationRequests/AssociateFake.cs | 2 +- .../DeepInsertProcessor.cs | 2 +- .../OrganizationRequests/DisassociateFake.cs | 2 +- .../Fixtures/AccountExtensions.cs | 4 - .../Logic/TemporalOperatorTests.cs | 84 +++++++++---------- .../AnyAllFilterLinkEntityTests.cs | 16 ++-- .../AssociateFakeTests.cs | 31 ++----- .../CreateFakeDeepInsertTests.cs | 31 ++++--- .../DisassociateFakeTests.cs | 18 ++-- .../ExecuteTransactionFakeTests.cs | 30 ++----- .../OrganizationRequests/ExistsJoinTests.cs | 30 +++---- 12 files changed, 107 insertions(+), 149 deletions(-) diff --git a/src/Digitall.Dataverse.Testing/FakeOrganizationService.cs b/src/Digitall.Dataverse.Testing/FakeOrganizationService.cs index 5f09532..ef4ff96 100644 --- a/src/Digitall.Dataverse.Testing/FakeOrganizationService.cs +++ b/src/Digitall.Dataverse.Testing/FakeOrganizationService.cs @@ -136,7 +136,7 @@ public void AddRelationships(params RelationshipMetadataBase[] relationships) } } - private static readonly Lazy> DefaultFakes = new(() => + private static readonly Lazy> s_defaultFakes = new(() => { var assembly = typeof(IOrganizationRequestFake).Assembly; var fakeTypes = assembly.GetTypes().Where(type => @@ -161,7 +161,7 @@ public void AddRelationships(params RelationshipMetadataBase[] relationships) /// public void AddDefaultRequests() { - foreach (var (type, fake) in DefaultFakes.Value) + foreach (var (type, fake) in s_defaultFakes.Value) { if (!OrganizationRequestFakes.ContainsKey(type)) { @@ -477,7 +477,7 @@ public OrganizationResponse Execute(OrganizationRequest request) if (OrganizationRequestFakes.TryGetValue(request.GetType(), out var fake)) return fake.Execute(request, this); - if (DefaultFakes.Value.TryGetValue(request.GetType(), out var defaultFake)) + if (s_defaultFakes.Value.TryGetValue(request.GetType(), out var defaultFake)) return defaultFake.Execute(request, this); ErrorFactory.ThrowFault(ErrorCodes.MessageDoesNotExist, $"No implementation found for request of type {request.GetType().Name}"); diff --git a/src/Digitall.Dataverse.Testing/OrganizationRequests/AssociateFake.cs b/src/Digitall.Dataverse.Testing/OrganizationRequests/AssociateFake.cs index 39f1966..ae4fa9f 100644 --- a/src/Digitall.Dataverse.Testing/OrganizationRequests/AssociateFake.cs +++ b/src/Digitall.Dataverse.Testing/OrganizationRequests/AssociateFake.cs @@ -74,7 +74,7 @@ public override AssociateResponse Execute(AssociateRequest organizationRequest, break; } default: - ErrorFactory.ThrowFault(ErrorCodes.InvalidArgument, $"Relationship metadata type '{relationshipMetadata!.GetType().Name}' is not supported for Associate"); + ErrorFactory.ThrowFault(ErrorCodes.InvalidArgument, $"Relationship metadata type '{relationshipMetadata.GetType().Name}' is not supported for Associate"); break; } } diff --git a/src/Digitall.Dataverse.Testing/OrganizationRequests/DeepInsertProcessor.cs b/src/Digitall.Dataverse.Testing/OrganizationRequests/DeepInsertProcessor.cs index 6f8b99c..1d2468a 100644 --- a/src/Digitall.Dataverse.Testing/OrganizationRequests/DeepInsertProcessor.cs +++ b/src/Digitall.Dataverse.Testing/OrganizationRequests/DeepInsertProcessor.cs @@ -55,7 +55,7 @@ internal static void Process( default: ErrorFactory.ThrowFault( ErrorCodes.InvalidArgument, - $"Relationship metadata type '{relationshipMetadata!.GetType().Name}' is not supported for deep insert"); + $"Relationship metadata type '{relationshipMetadata.GetType().Name}' is not supported for deep insert"); break; } } diff --git a/src/Digitall.Dataverse.Testing/OrganizationRequests/DisassociateFake.cs b/src/Digitall.Dataverse.Testing/OrganizationRequests/DisassociateFake.cs index e9d1558..6d99c11 100644 --- a/src/Digitall.Dataverse.Testing/OrganizationRequests/DisassociateFake.cs +++ b/src/Digitall.Dataverse.Testing/OrganizationRequests/DisassociateFake.cs @@ -46,7 +46,7 @@ public override DisassociateResponse Execute(DisassociateRequest organizationReq } else { - ErrorFactory.ThrowFault(ErrorCodes.InvalidArgument, $"Disassociate only supports ManyToMany relationships; '{relationship.SchemaName}' is of type '{relationshipMetadata!.GetType().Name}'"); + ErrorFactory.ThrowFault(ErrorCodes.InvalidArgument, $"Disassociate only supports ManyToMany relationships; '{relationship.SchemaName}' is of type '{relationshipMetadata.GetType().Name}'"); } } diff --git a/tests/Digitall.Dataverse.Testing.Tests/Fixtures/AccountExtensions.cs b/tests/Digitall.Dataverse.Testing.Tests/Fixtures/AccountExtensions.cs index f464f62..e453b39 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/Fixtures/AccountExtensions.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/Fixtures/AccountExtensions.cs @@ -10,10 +10,6 @@ public partial class Account [AttributeLogicalName("new_accountcategorycodemultiple")] public OptionSetValueCollection AccountCategoryCodeMultiple { - get - { - return GetAttributeValue("new_accountcategorycodemultiple"); - } set { OnPropertyChanging(); diff --git a/tests/Digitall.Dataverse.Testing.Tests/Logic/TemporalOperatorTests.cs b/tests/Digitall.Dataverse.Testing.Tests/Logic/TemporalOperatorTests.cs index 01568b4..9a84acd 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/Logic/TemporalOperatorTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/Logic/TemporalOperatorTests.cs @@ -14,11 +14,11 @@ namespace Digitall.Dataverse.Testing.Tests.Logic; /// public class TemporalOperatorTests { - private static readonly DateTime Now = new(2024, 6, 12, 12, 0, 0, DateTimeKind.Utc); // Wednesday + private static readonly DateTime s_now = new(2024, 6, 12, 12, 0, 0, DateTimeKind.Utc); // Wednesday private static FakeOrganizationService CreateService(params Entity[] entities) { - var sut = new FakeOrganizationService(new FakeTimeProvider(Now)); + var sut = new FakeOrganizationService(new FakeTimeProvider(s_now)); sut.AddRange(entities); return sut; } @@ -30,8 +30,8 @@ private static FakeOrganizationService CreateService(params Entity[] entities) [Test] public async Task LastXHours_MatchesRecordWithinLastXHours() { - var inside = AccountAt(Now.AddHours(-2)); - var outside = AccountAt(Now.AddHours(-10)); + var inside = AccountAt(s_now.AddHours(-2)); + var outside = AccountAt(s_now.AddHours(-10)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -45,8 +45,8 @@ public async Task LastXHours_MatchesRecordWithinLastXHours() [Test] public async Task LastXDays_MatchesRecordWithinLastXDays() { - var inside = AccountAt(Now.AddDays(-3)); - var outside = AccountAt(Now.AddDays(-10)); + var inside = AccountAt(s_now.AddDays(-3)); + var outside = AccountAt(s_now.AddDays(-10)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -60,8 +60,8 @@ public async Task LastXDays_MatchesRecordWithinLastXDays() [Test] public async Task Last7Days_MatchesRecordWithinLast7Days() { - var inside = AccountAt(Now.AddDays(-5)); - var outside = AccountAt(Now.AddDays(-10)); + var inside = AccountAt(s_now.AddDays(-5)); + var outside = AccountAt(s_now.AddDays(-10)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -75,8 +75,8 @@ public async Task Last7Days_MatchesRecordWithinLast7Days() [Test] public async Task LastXWeeks_MatchesRecordWithinLastXWeeks() { - var inside = AccountAt(Now.AddDays(-10)); // ~1.4 weeks ago - var outside = AccountAt(Now.AddDays(-30)); // ~4.3 weeks ago + var inside = AccountAt(s_now.AddDays(-10)); // ~1.4 weeks ago + var outside = AccountAt(s_now.AddDays(-30)); // ~4.3 weeks ago var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -90,8 +90,8 @@ public async Task LastXWeeks_MatchesRecordWithinLastXWeeks() [Test] public async Task LastXMonths_MatchesRecordWithinLastXMonths() { - var inside = AccountAt(Now.AddMonths(-2)); - var outside = AccountAt(Now.AddMonths(-6)); + var inside = AccountAt(s_now.AddMonths(-2)); + var outside = AccountAt(s_now.AddMonths(-6)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -105,8 +105,8 @@ public async Task LastXMonths_MatchesRecordWithinLastXMonths() [Test] public async Task LastXYears_MatchesRecordWithinLastXYears() { - var inside = AccountAt(Now.AddYears(-1)); - var outside = AccountAt(Now.AddYears(-5)); + var inside = AccountAt(s_now.AddYears(-1)); + var outside = AccountAt(s_now.AddYears(-5)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -124,8 +124,8 @@ public async Task LastXYears_MatchesRecordWithinLastXYears() [Test] public async Task NextXHours_MatchesRecordWithinNextXHours() { - var inside = AccountAt(Now.AddHours(3)); - var outside = AccountAt(Now.AddHours(10)); + var inside = AccountAt(s_now.AddHours(3)); + var outside = AccountAt(s_now.AddHours(10)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -139,8 +139,8 @@ public async Task NextXHours_MatchesRecordWithinNextXHours() [Test] public async Task NextXDays_MatchesRecordWithinNextXDays() { - var inside = AccountAt(Now.AddDays(3)); - var outside = AccountAt(Now.AddDays(10)); + var inside = AccountAt(s_now.AddDays(3)); + var outside = AccountAt(s_now.AddDays(10)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -154,8 +154,8 @@ public async Task NextXDays_MatchesRecordWithinNextXDays() [Test] public async Task Next7Days_MatchesRecordWithinNext7Days() { - var inside = AccountAt(Now.AddDays(5)); - var outside = AccountAt(Now.AddDays(10)); + var inside = AccountAt(s_now.AddDays(5)); + var outside = AccountAt(s_now.AddDays(10)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -169,8 +169,8 @@ public async Task Next7Days_MatchesRecordWithinNext7Days() [Test] public async Task NextXWeeks_MatchesRecordWithinNextXWeeks() { - var inside = AccountAt(Now.AddDays(10)); - var outside = AccountAt(Now.AddDays(30)); + var inside = AccountAt(s_now.AddDays(10)); + var outside = AccountAt(s_now.AddDays(30)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -184,8 +184,8 @@ public async Task NextXWeeks_MatchesRecordWithinNextXWeeks() [Test] public async Task NextXMonths_MatchesRecordWithinNextXMonths() { - var inside = AccountAt(Now.AddMonths(2)); - var outside = AccountAt(Now.AddMonths(6)); + var inside = AccountAt(s_now.AddMonths(2)); + var outside = AccountAt(s_now.AddMonths(6)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -199,8 +199,8 @@ public async Task NextXMonths_MatchesRecordWithinNextXMonths() [Test] public async Task NextXYears_MatchesRecordWithinNextXYears() { - var inside = AccountAt(Now.AddYears(1)); - var outside = AccountAt(Now.AddYears(5)); + var inside = AccountAt(s_now.AddYears(1)); + var outside = AccountAt(s_now.AddYears(5)); var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -218,8 +218,8 @@ public async Task NextXYears_MatchesRecordWithinNextXYears() [Test] public async Task OlderThanXMinutes_MatchesRecordOlderThanXMinutes() { - var old = AccountAt(Now.AddMinutes(-30)); - var recent = AccountAt(Now.AddMinutes(-5)); + var old = AccountAt(s_now.AddMinutes(-30)); + var recent = AccountAt(s_now.AddMinutes(-5)); var sut = CreateService(old, recent); var query = new QueryExpression(Account.EntityLogicalName); @@ -233,8 +233,8 @@ public async Task OlderThanXMinutes_MatchesRecordOlderThanXMinutes() [Test] public async Task OlderThanXHours_MatchesRecordOlderThanXHours() { - var old = AccountAt(Now.AddHours(-10)); - var recent = AccountAt(Now.AddHours(-1)); + var old = AccountAt(s_now.AddHours(-10)); + var recent = AccountAt(s_now.AddHours(-1)); var sut = CreateService(old, recent); var query = new QueryExpression(Account.EntityLogicalName); @@ -248,8 +248,8 @@ public async Task OlderThanXHours_MatchesRecordOlderThanXHours() [Test] public async Task OlderThanXDays_MatchesRecordOlderThanXDays() { - var old = AccountAt(Now.AddDays(-20)); - var recent = AccountAt(Now.AddDays(-2)); + var old = AccountAt(s_now.AddDays(-20)); + var recent = AccountAt(s_now.AddDays(-2)); var sut = CreateService(old, recent); var query = new QueryExpression(Account.EntityLogicalName); @@ -263,8 +263,8 @@ public async Task OlderThanXDays_MatchesRecordOlderThanXDays() [Test] public async Task OlderThanXWeeks_MatchesRecordOlderThanXWeeks() { - var old = AccountAt(Now.AddDays(-30)); - var recent = AccountAt(Now.AddDays(-5)); + var old = AccountAt(s_now.AddDays(-30)); + var recent = AccountAt(s_now.AddDays(-5)); var sut = CreateService(old, recent); var query = new QueryExpression(Account.EntityLogicalName); @@ -278,8 +278,8 @@ public async Task OlderThanXWeeks_MatchesRecordOlderThanXWeeks() [Test] public async Task OlderThanXMonths_MatchesRecordOlderThanXMonths() { - var old = AccountAt(Now.AddMonths(-6)); - var recent = AccountAt(Now.AddMonths(-1)); + var old = AccountAt(s_now.AddMonths(-6)); + var recent = AccountAt(s_now.AddMonths(-1)); var sut = CreateService(old, recent); var query = new QueryExpression(Account.EntityLogicalName); @@ -293,8 +293,8 @@ public async Task OlderThanXMonths_MatchesRecordOlderThanXMonths() [Test] public async Task OlderThanXYears_MatchesRecordOlderThanXYears() { - var old = AccountAt(Now.AddYears(-5)); - var recent = AccountAt(Now.AddYears(-1)); + var old = AccountAt(s_now.AddYears(-5)); + var recent = AccountAt(s_now.AddYears(-1)); var sut = CreateService(old, recent); var query = new QueryExpression(Account.EntityLogicalName); @@ -419,8 +419,8 @@ public async Task ThisWeek_MatchesRecordInCurrentWeek() public async Task LastWeek_MatchesRecordInPreviousWeek() { // Now = 2024-06-12 (Wednesday). 7 days before (Jun 5) should be "last week". - var inside = AccountAt(Now.AddDays(-7).Date); - var outside = AccountAt(Now.Date); // this week + var inside = AccountAt(s_now.AddDays(-7).Date); + var outside = AccountAt(s_now.Date); // this week var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); @@ -435,8 +435,8 @@ public async Task LastWeek_MatchesRecordInPreviousWeek() public async Task NextWeek_MatchesRecordInNextWeek() { // Now = 2024-06-12 (Wednesday). 7 days after (Jun 19) should be "next week". - var inside = AccountAt(Now.AddDays(7).Date); - var outside = AccountAt(Now.Date); // this week + var inside = AccountAt(s_now.AddDays(7).Date); + var outside = AccountAt(s_now.Date); // this week var sut = CreateService(inside, outside); var query = new QueryExpression(Account.EntityLogicalName); diff --git a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AnyAllFilterLinkEntityTests.cs b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AnyAllFilterLinkEntityTests.cs index 570d7b5..8ce00e2 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AnyAllFilterLinkEntityTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AnyAllFilterLinkEntityTests.cs @@ -12,8 +12,8 @@ namespace Digitall.Dataverse.Testing.Tests.OrganizationRequests; /// public class AnyAllFilterLinkEntityTests { - private static readonly Guid CorpAId = Guid.Parse("00000000-0000-0000-0001-000000000001"); - private static readonly Guid CorpBId = Guid.Parse("00000000-0000-0000-0001-000000000002"); + private static readonly Guid s_corpAId = Guid.Parse("00000000-0000-0000-0001-000000000001"); + private static readonly Guid s_corpBId = Guid.Parse("00000000-0000-0000-0001-000000000002"); #region JoinOperator.Any via AnyAllFilterLinkEntity @@ -52,7 +52,7 @@ public async Task AnyAllFilter_Any_ReturnsParentWithMatchingLinkedRecord() }); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -139,7 +139,7 @@ public async Task AnyAllFilter_NotAny_ReturnsParentWithoutMatchingLinkedRecord() // corpA has no contacts pointing to it at all → NOT EXISTS is true → included // corpB has "John B" → EXISTS is true → NOT EXISTS is false → excluded await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpAId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpAId); } #endregion @@ -177,7 +177,7 @@ public async Task AnyAllFilter_NotAll_BehavesLikeAny() // NotAll = Any: corpB has "John B" contact → included await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } #endregion @@ -257,7 +257,7 @@ public async Task AnyAllFilter_All_IncludesWhenNoLinkedRecordMatchesCriteria() // corpA: no contacts → excluded. corpB: contacts exist but none match → included. await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } #endregion @@ -289,7 +289,7 @@ public async Task AnyAllFilter_Any_NoCriteria_ReturnsParentsWithAnyLinkedRecord( // corpA: no contacts → excluded. corpB: has contacts → included. await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } #endregion @@ -332,7 +332,7 @@ public async Task AnyAllFilter_Any_NoDuplicateParentRows() // corpB should appear exactly once even though 2 contacts match await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } #endregion diff --git a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AssociateFakeTests.cs b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AssociateFakeTests.cs index ab9cf61..33fd15c 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AssociateFakeTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/AssociateFakeTests.cs @@ -46,10 +46,7 @@ public async Task Execute_ManyToMany_CreatesIntersectRecord() { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", contactId) - } + RelatedEntities = [new("contact", contactId)] }); var intersect = _sut.CreateQuery("account_contact").ToList(); @@ -78,10 +75,7 @@ public async Task Execute_OneToMany_SetsLookupOnRelatedEntity() { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contacts"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", contactId) - } + RelatedEntities = [new("contact", contactId)] }); var contact = _sut.Retrieve("contact", contactId, new ColumnSet(true)); @@ -115,11 +109,7 @@ public async Task Execute_ManyToMany_MultipleRelatedEntities_CreatesMultipleInte { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", contactId1), - new("contact", contactId2) - } + RelatedEntities = [new("contact", contactId1), new("contact", contactId2)] }); var intersects = _sut.CreateQuery("account_contact").ToList(); @@ -136,10 +126,7 @@ void Action() => _sut.Execute(new AssociateRequest { Target = new EntityReference("account", accountId), Relationship = new Relationship("nonexistent_relationship"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", Guid.NewGuid()) - } + RelatedEntities = [new("contact", Guid.NewGuid())] }); var ex = Assert.Throws>(Action); @@ -165,10 +152,7 @@ void Action() => _sut.Execute(new AssociateRequest { Target = new EntityReference("account", Guid.NewGuid()), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", Guid.NewGuid()) - } + RelatedEntities = [new("contact", Guid.NewGuid())] }); var ex = Assert.Throws>(Action); @@ -195,10 +179,7 @@ void Action() => _sut.Execute(new AssociateRequest { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", Guid.NewGuid()) - } + RelatedEntities = [new("contact", Guid.NewGuid())] }); var ex = Assert.Throws>(Action); diff --git a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/CreateFakeDeepInsertTests.cs b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/CreateFakeDeepInsertTests.cs index 3534d16..058431b 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/CreateFakeDeepInsertTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/CreateFakeDeepInsertTests.cs @@ -115,9 +115,9 @@ public async Task Create_WithUnregisteredRelationship_ThrowsFault() account.RelatedEntities[new Relationship("unknown_relationship")] = new EntityCollection([task]); - var act = () => _sut.Execute(new CreateRequest { Target = account }); + void Act() => _sut.Execute(new CreateRequest { Target = account }); - await Assert.That(act).Throws(); + await Assert.That(Act).Throws(); } [Test] @@ -198,9 +198,9 @@ public async Task Create_WithMismatchedOneToManyRelationship_ThrowsFault() account.RelatedEntities[new Relationship("mismatched_onetomany")] = new EntityCollection([task]); - var act = () => _sut.Execute(new CreateRequest { Target = account }); + void Act() => _sut.Execute(new CreateRequest { Target = account }); - await Assert.That(act).Throws(); + await Assert.That(Act).Throws(); } [Test] @@ -213,9 +213,9 @@ public async Task Create_WithMismatchedManyToManyRelationship_ThrowsFault() account.RelatedEntities[new Relationship("systemuserroles_association")] = new EntityCollection([role]); - var act = () => _sut.Execute(new CreateRequest { Target = account }); + void Act() => _sut.Execute(new CreateRequest { Target = account }); - await Assert.That(act).Throws(); + await Assert.That(Act).Throws(); } [Test] @@ -244,11 +244,16 @@ public async Task Create_WithReversedOneToMany_SetsForeignKeyOnParent() var calendar = new Entity("calendar") { Id = Guid.NewGuid(), ["name"] = "Business Hours" }; var innerCalendar = new Entity("calendar") { ["name"] = "Inner Schedule" }; - var calendarRule = new Entity("calendarrule") { ["description"] = "Rule 1" }; + var calendarRule = new Entity("calendarrule") + { + ["description"] = "Rule 1", + RelatedEntities = + { + [new Relationship("calendarrule_innercalendar")] = new EntityCollection([innerCalendar]) + } + }; // Nested deep insert: calendar → calendarrule → innercalendar (reverse direction) - calendarRule.RelatedEntities[new Relationship("calendarrule_innercalendar")] = - new EntityCollection([innerCalendar]); calendar.RelatedEntities[new Relationship("calendar_calendar_rules")] = new EntityCollection([calendarRule]); @@ -293,9 +298,9 @@ public async Task Create_WithReversedOneToMany_MultipleChildren_ThrowsFault() rule.RelatedEntities[new Relationship("calendarrule_innercalendar")] = new EntityCollection([cal1, cal2]); // Two children on N:1 → invalid - var act = () => _sut.Execute(new CreateRequest { Target = rule }); + void Act() => _sut.Execute(new CreateRequest { Target = rule }); - await Assert.That(act).Throws(); + await Assert.That(Act).Throws(); } [Test] @@ -316,8 +321,8 @@ public async Task Create_WithReversedOneToMany_WrongChildLogicalName_ThrowsFault rule.RelatedEntities[new Relationship("calendarrule_innercalendar")] = new EntityCollection([wrongEntity]); - var act = () => _sut.Execute(new CreateRequest { Target = rule }); + void Act() => _sut.Execute(new CreateRequest { Target = rule }); - await Assert.That(act).Throws(); + await Assert.That(Act).Throws(); } } diff --git a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/DisassociateFakeTests.cs b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/DisassociateFakeTests.cs index 3fab401..be49140 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/DisassociateFakeTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/DisassociateFakeTests.cs @@ -53,7 +53,7 @@ public async Task Execute_ManyToMany_RemovesIntersectRecord() { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection { new("contact", contactId) } + RelatedEntities = [new("contact", contactId)] }); // Verify associated @@ -65,7 +65,7 @@ public async Task Execute_ManyToMany_RemovesIntersectRecord() { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection { new("contact", contactId) } + RelatedEntities = [new("contact", contactId)] }); var after = _sut.CreateQuery("account_contact").ToList(); @@ -88,11 +88,7 @@ public async Task Execute_ManyToMany_OnlyRemovesSpecificRelation() { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection - { - new("contact", contactId1), - new("contact", contactId2) - } + RelatedEntities = [new("contact", contactId1), new("contact", contactId2)] }); // Disassociate only one @@ -118,7 +114,7 @@ void Action() => _sut.Execute(new DisassociateRequest { Target = new EntityReference("account", accountId), Relationship = new Relationship("nonexistent"), - RelatedEntities = new EntityReferenceCollection { new("contact", Guid.NewGuid()) } + RelatedEntities = [new("contact", Guid.NewGuid())] }); var ex = Assert.Throws>(Action); @@ -143,7 +139,7 @@ void Action() => _sut.Execute(new DisassociateRequest { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contacts_1n"), - RelatedEntities = new EntityReferenceCollection { new("contact", Guid.NewGuid()) } + RelatedEntities = [new("contact", Guid.NewGuid())] }); var ex = Assert.Throws>(Action); @@ -163,14 +159,14 @@ public async Task Execute_ReturnsDisassociateResponse() { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection { new("contact", contactId) } + RelatedEntities = [new("contact", contactId)] }); var response = _sut.Execute(new DisassociateRequest { Target = new EntityReference("account", accountId), Relationship = new Relationship("account_contact_mm"), - RelatedEntities = new EntityReferenceCollection { new("contact", contactId) } + RelatedEntities = [new("contact", contactId)] }); await Assert.That(response).IsTypeOf(); diff --git a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExecuteTransactionFakeTests.cs b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExecuteTransactionFakeTests.cs index 75f1c08..f879302 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExecuteTransactionFakeTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExecuteTransactionFakeTests.cs @@ -29,12 +29,7 @@ public async Task Execute_MultipleCreates_AllEntitiesCreated() { var request = new ExecuteTransactionRequest { - Requests = new OrganizationRequestCollection - { - new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid(), ["name"] = "A" } }, - new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid(), ["name"] = "B" } }, - new CreateRequest { Target = new Entity("contact") { Id = Guid.NewGuid(), ["name"] = "C" } } - } + Requests = [new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid(), ["name"] = "A" } }, new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid(), ["name"] = "B" } }, new CreateRequest { Target = new Entity("contact") { Id = Guid.NewGuid(), ["name"] = "C" } }] }; _sut.Execute(request); @@ -50,11 +45,7 @@ public async Task Execute_ReturnResponsesTrue_ResponsesCollectionPopulated() { var request = new ExecuteTransactionRequest { - Requests = new OrganizationRequestCollection - { - new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid() } }, - new CreateRequest { Target = new Entity("contact") { Id = Guid.NewGuid() } } - }, + Requests = [new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid() } }, new CreateRequest { Target = new Entity("contact") { Id = Guid.NewGuid() } }], ReturnResponses = true }; @@ -70,10 +61,7 @@ public async Task Execute_ReturnResponsesFalse_ResponsesCollectionEmpty() { var request = new ExecuteTransactionRequest { - Requests = new OrganizationRequestCollection - { - new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid() } } - }, + Requests = [new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid() } }], ReturnResponses = false }; @@ -92,11 +80,7 @@ public async Task Execute_MixedOperations_AllExecutedInOrder() var request = new ExecuteTransactionRequest { - Requests = new OrganizationRequestCollection - { - new UpdateRequest { Target = new Entity("account") { Id = accountId, ["name"] = "Updated" } }, - new CreateRequest { Target = new Entity("contact") { Id = contactId, ["fullname"] = "New Contact" } } - } + Requests = [new UpdateRequest { Target = new Entity("account") { Id = accountId, ["name"] = "Updated" } }, new CreateRequest { Target = new Entity("contact") { Id = contactId, ["fullname"] = "New Contact" } }] }; _sut.Execute(request); @@ -113,11 +97,7 @@ public async Task Execute_FailingRequest_ThrowsException() { var request = new ExecuteTransactionRequest { - Requests = new OrganizationRequestCollection - { - new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid() } }, - new DeleteRequest { Target = new EntityReference("account", Guid.NewGuid()) } // Non-existing - } + Requests = [new CreateRequest { Target = new Entity("account") { Id = Guid.NewGuid() } }, new DeleteRequest { Target = new EntityReference("account", Guid.NewGuid()) }] // Non-existing }; void Action() => _sut.Execute(request); diff --git a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExistsJoinTests.cs b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExistsJoinTests.cs index 36d5820..866c20c 100644 --- a/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExistsJoinTests.cs +++ b/tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/ExistsJoinTests.cs @@ -21,8 +21,8 @@ public class ExistsJoinTests // corpB (id: ...0002): conB ("John B") + conC ("John C") // conA : no parentcustomerid - private static readonly Guid CorpAId = Guid.Parse("00000000-0000-0000-0001-000000000001"); - private static readonly Guid CorpBId = Guid.Parse("00000000-0000-0000-0001-000000000002"); + private static readonly Guid s_corpAId = Guid.Parse("00000000-0000-0000-0001-000000000001"); + private static readonly Guid s_corpBId = Guid.Parse("00000000-0000-0000-0001-000000000002"); #region JoinOperator.Any (QueryExpression) @@ -49,7 +49,7 @@ public async Task QueryExpression_AnyJoin_ReturnsOnlyAccountsWithAtLeastOneRelat }); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -75,7 +75,7 @@ public async Task QueryExpression_AnyJoin_DoesNotDuplicateOuterRowsForMultipleMa }); // corpB matches conB AND conC, but must only appear once - var corpBRows = result.Entities.Where(e => e.Id == CorpBId).ToList(); + var corpBRows = result.Entities.Where(e => e.Id == s_corpBId).ToList(); await Assert.That(corpBRows).Count().IsEqualTo(1); } @@ -142,7 +142,7 @@ public async Task QueryExpression_AnyJoin_WithLinkCriteria_AppliesSubqueryFilter // Only corpB has a contact named "John B" await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -205,7 +205,7 @@ public async Task QueryExpression_NotAnyJoin_ReturnsOnlyAccountsWithNoRelatedEnt }); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpAId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpAId); } /// @@ -263,7 +263,7 @@ public async Task QueryExpression_ExistsJoin_ReturnsOnlyAccountsWithAtLeastOneRe }); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -287,7 +287,7 @@ public async Task QueryExpression_ExistsJoin_DoesNotDuplicateOuterRows() } }); - var corpBRows = result.Entities.Where(e => e.Id == CorpBId).ToList(); + var corpBRows = result.Entities.Where(e => e.Id == s_corpBId).ToList(); await Assert.That(corpBRows).Count().IsEqualTo(1); } @@ -398,7 +398,7 @@ public async Task FetchXml_AnyJoin_ReturnsOnlyAccountsWithAtLeastOneContact() var result = sut.RetrieveMultiple(new FetchExpression(fetchXml)); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -424,7 +424,7 @@ public async Task FetchXml_NotAnyJoin_ReturnsOnlyAccountsWithNoContact() var result = sut.RetrieveMultiple(new FetchExpression(fetchXml)); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpAId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpAId); } #endregion @@ -466,7 +466,7 @@ public async Task QueryExpression_AnyJoin_WithOuterCriteria_AppliesBothFilters() }); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -575,7 +575,7 @@ public async Task QueryExpression_NotAnyJoin_WithLinkCriteria_ExcludesAccountsWi // corpB has "John B" → excluded. corpA has no contacts → included. await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpAId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpAId); } #endregion @@ -635,7 +635,7 @@ public async Task FetchXml_AnyJoin_WithFilter_AppliesSubqueryFilter() var result = sut.RetrieveMultiple(new FetchExpression(fetchXml)); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } #endregion @@ -666,7 +666,7 @@ public async Task QueryExpression_NotAllJoin_BehavesLikeAny() }); await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } /// @@ -739,7 +739,7 @@ public async Task QueryExpression_AllJoin_IncludesWhenNoLinkedRecordMatchesCrite // corpA: no contacts → excluded. corpB: contacts exist but none match "Nobody" → included. await Assert.That(result.Entities).Count().IsEqualTo(1); - await Assert.That(result.Entities[0].Id).IsEqualTo(CorpBId); + await Assert.That(result.Entities[0].Id).IsEqualTo(s_corpBId); } #endregion