Skip to content

Commit b86b4dc

Browse files
committed
Fixed error in custom collection mapping test
After assigning `Id`s to all topics in the `StubTopicRepository` (45aa7be), the unit test that validates that associations of associations don't get mapped started failing. The reason this passed previously is because `IsNew` topics aren't cached by the `TopicMappingService`. As such, while the code does not, correctly, crawl associations of associations, it appears to do so when the topics are saved because the first association points back to the object being mapped—and, thus, when the cached instance of that mapped object is returns, its associations are mapped. This isn't because the mapping service crawled them. This is resolved by first rearranging the assignment of the `PermittedContentTypes` (the `ContentTypes` relationship) in the `StubTopicRepository` so that both `PermittedContentTypes` themselves have `PermittedContentTypes`, and then setting it to evaluate the second of those relationships, which will not have been cached. To help further validate this, the test now confirms that the source `Topic` is not set to the mapped `Topic`, and that it does have `PermittedContentTypes` set; this will help avoid false positives in the future. This resolves #82.
1 parent 45aa7be commit b86b4dc

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

OnTopic.TestDoubles/StubTopicRepository.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,16 @@ private Topic CreateFakeData() {
277277
addAttribute(pageContentType, "IsHidden", "TextAttributeDescriptor", false);
278278
addAttribute(pageContentType, "TopicReference", "TopicReferenceAttributeDescriptor", false);
279279

280-
pageContentType.Relationships.SetValue("ContentTypes", pageContentType);
281-
pageContentType.Relationships.SetValue("ContentTypes", contentTypeDescriptor);
282-
283280
var contactContentType = new ContentTypeDescriptor("Contact", "ContentTypeDescriptor", contentTypes, currentAttributeId++);
284281

285282
addAttribute(contactContentType, "Name", isExtended: false);
286283
addAttribute(contactContentType, "AlternateEmail", isExtended: false);
287284
addAttribute(contactContentType, "BillingContactEmail", isExtended: false);
288285

286+
pageContentType.Relationships.SetValue("ContentTypes", pageContentType);
287+
pageContentType.Relationships.SetValue("ContentTypes", contactContentType);
288+
contactContentType.Relationships.SetValue("ContentTypes", pageContentType);
289+
289290
/*------------------------------------------------------------------------------------------------------------------------
290291
| Local addAttribute() helper function
291292
\-----------------------------------------------------------------------------------------------------------------------*/

OnTopic.Tests/TopicMappingServiceTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,16 @@ public async Task Map_AlternateRelationship_ReturnsCorrectRelationship() {
680680
[Fact]
681681
public async Task Map_CustomCollection_ReturnsCollection() {
682682

683-
var topic = _topicRepository.Load("Root:Configuration:ContentTypes:Page");
683+
var topic = (ContentTypeDescriptor)_topicRepository.Load("Root:Configuration:ContentTypes:Page");
684684
var target = await _mappingService.MapAsync<ContentTypeDescriptorTopicViewModel>(topic).ConfigureAwait(false);
685685

686686
Assert.Equal<int?>(8, target?.AttributeDescriptors.Count);
687687
Assert.Equal<int?>(2, target?.PermittedContentTypes.Count);
688688

689689
//Ensure custom collections are not recursively followed without instruction
690-
Assert.Empty(target?.PermittedContentTypes.FirstOrDefault()?.PermittedContentTypes);
690+
Assert.NotEqual<Topic?>(topic, topic?.PermittedContentTypes.LastOrDefault());
691+
Assert.NotEmpty(topic?.PermittedContentTypes.LastOrDefault()?.PermittedContentTypes);
692+
Assert.Empty(target?.PermittedContentTypes.LastOrDefault()?.PermittedContentTypes);
691693

692694
}
693695

0 commit comments

Comments
 (0)