Skip to content

Commit 532f03e

Browse files
committed
Introduce GetChildTopic() helper functions
Quite a few of the tests rely on checking a collection to determine if a view model with a particular key exists. The code for this is long, repetitive, and clunky. In recent refactorings (see e.g. commits references from #9225f23), quite a few of these were replaced with local helper functions (e.g., `getChildTopic()`, `getRelation()`, &c.). That said, nearly all instances of these can be handled by two overloads of a function (one for view models deriving from `KeyOnlyTopicViewModel`, and another for those deriving from `TopicViewModel`). As such, this refactoring replaces nearly every call that depends on verifying that a child topic exists with a new `GetChildTopic()` helper function, and removes any redundant local functions that served the same purpose.
1 parent 5746ace commit 532f03e

1 file changed

Lines changed: 41 additions & 47 deletions

File tree

OnTopic.Tests/TopicMappingServiceTest.cs

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,9 @@ public async Task MapRelationships() {
268268
var target = (RelationTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
269269

270270
Assert.AreEqual<int>(2, target.Cousins.Count);
271-
Assert.IsNotNull(getCousin(target, "Cousin1"));
272-
Assert.IsNotNull(getCousin(target, "Cousin2"));
273-
Assert.IsNull(getCousin(target, "Sibling"));
274-
275-
RelationTopicViewModel getCousin(RelationTopicViewModel topic, string key)
276-
=> topic.Cousins.FirstOrDefault((t) => t.Key.StartsWith(key, StringComparison.InvariantCulture));
271+
Assert.IsNotNull(GetChildTopic(target.Cousins, "Cousin1"));
272+
Assert.IsNotNull(GetChildTopic(target.Cousins, "Cousin2"));
273+
Assert.IsNull(GetChildTopic(target.Cousins, "Sibling"));
277274

278275
}
279276

@@ -312,10 +309,8 @@ public async Task AlternateRelationship() {
312309
var target = (AmbiguousRelationTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
313310

314311
Assert.AreEqual<int>(1, target.RelationshipAlias.Count);
315-
Assert.IsNotNull(getRelation(target, "IncomingRelation"));
312+
Assert.IsNotNull(GetChildTopic(target.RelationshipAlias, "IncomingRelation"));
316313

317-
KeyOnlyTopicViewModel getRelation(AmbiguousRelationTopicViewModel topic, string key)
318-
=> topic.RelationshipAlias.FirstOrDefault((t) => t.Key.StartsWith(key, StringComparison.InvariantCulture));
319314
}
320315

321316
/*==========================================================================================================================
@@ -360,13 +355,10 @@ public async Task MapNestedTopics() {
360355

361356
Assert.AreEqual<int>(2, target.Categories.Count);
362357

363-
Assert.IsNotNull(getCategory(target, "NestedTopic1"));
364-
Assert.IsNotNull(getCategory(target, "NestedTopic2"));
365-
Assert.IsNull(getCategory(target, "Categories"));
366-
Assert.IsNull(getCategory(target, "ChildTopic"));
367-
368-
KeyOnlyTopicViewModel getCategory(NestedTopicViewModel topic, string key)
369-
=> topic.Categories.FirstOrDefault((t) => t.Key.StartsWith(key, StringComparison.InvariantCulture));
358+
Assert.IsNotNull(GetChildTopic(target.Categories, "NestedTopic1"));
359+
Assert.IsNotNull(GetChildTopic(target.Categories, "NestedTopic2"));
360+
Assert.IsNull(GetChildTopic(target.Categories, "Categories"));
361+
Assert.IsNull(GetChildTopic(target.Categories, "ChildTopic"));
370362

371363
}
372364

@@ -392,18 +384,17 @@ public async Task MapChildren() {
392384
var target = (DescendentTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
393385

394386
Assert.AreEqual<int>(4, target.Children.Count);
395-
Assert.IsNotNull(getTopic(target, "ChildTopic1"));
396-
Assert.IsNotNull(getTopic(target, "ChildTopic2"));
397-
Assert.IsNotNull(getTopic(target, "ChildTopic3"));
398-
Assert.IsNotNull(getTopic(target, "ChildTopic4"));
399-
Assert.IsTrue(((DescendentSpecializedTopicViewModel)getTopic(target, "ChildTopic4")).IsLeaf);
400-
Assert.IsNull(getTopic(target, "invalidChildTopic"));
401-
Assert.IsNull(getTopic(target, "GrandchildTopic"));
402-
Assert.IsNotNull(getTopic(getTopic(target, "ChildTopic3"), "GrandchildTopic"));
403-
404-
DescendentTopicViewModel getTopic(DescendentTopicViewModel parent, string key) =>
405-
parent.Children.FirstOrDefault((t) => t.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
406-
387+
Assert.IsNotNull(GetChildTopic(target.Children, "ChildTopic1"));
388+
Assert.IsNotNull(GetChildTopic(target.Children, "ChildTopic2"));
389+
Assert.IsNotNull(GetChildTopic(target.Children, "ChildTopic3"));
390+
Assert.IsNotNull(GetChildTopic(target.Children, "ChildTopic4"));
391+
Assert.IsTrue(((DescendentSpecializedTopicViewModel)GetChildTopic(target.Children, "ChildTopic4")).IsLeaf);
392+
Assert.IsNull(GetChildTopic(target.Children, "invalidChildTopic"));
393+
Assert.IsNull(GetChildTopic(target.Children, "GrandchildTopic"));
394+
Assert.IsNotNull(GetChildTopic(
395+
((DescendentTopicViewModel)GetChildTopic(target.Children, "ChildTopic3")).Children,
396+
"GrandchildTopic"
397+
));
407398
}
408399

409400
/*==========================================================================================================================
@@ -466,8 +457,8 @@ public async Task RecursiveRelationships() {
466457

467458
var target = (RelationTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
468459

469-
var cousinTarget = getCousin(target, "CousinTopic3") as RelationWithChildrenTopicViewModel;
470-
var distantCousinTarget = getChild(cousinTarget, "ChildTopic3");
460+
var cousinTarget = GetChildTopic(target.Cousins, "CousinTopic3") as RelationWithChildrenTopicViewModel;
461+
var distantCousinTarget = GetChildTopic(cousinTarget.Children, "ChildTopic3") as RelationWithChildrenTopicViewModel;
471462

472463
//Because Cousins is set to recurse over Children, its children should be set
473464
Assert.AreEqual<int>(3, cousinTarget.Children.Count);
@@ -478,12 +469,6 @@ public async Task RecursiveRelationships() {
478469
//Because Children is not set to recurse over Children, the grandchildren of a cousin should NOT be set
479470
Assert.AreEqual<int>(0, distantCousinTarget.Children.Count);
480471

481-
RelationTopicViewModel getCousin(RelationTopicViewModel topic, string key) =>
482-
topic.Cousins.FirstOrDefault((t) => t.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
483-
484-
RelationWithChildrenTopicViewModel getChild(RelationWithChildrenTopicViewModel topic, string key) =>
485-
topic.Children.FirstOrDefault((t) => t.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
486-
487472
}
488473

489474
/*==========================================================================================================================
@@ -507,10 +492,10 @@ public async Task MapSlideshow() {
507492
var target = (SlideshowTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
508493

509494
Assert.AreEqual<int>(4, target.ContentItems.Count);
510-
Assert.IsNotNull(target.ContentItems.FirstOrDefault((t) => t.Key.StartsWith("ChildTopic1", StringComparison.InvariantCulture)));
511-
Assert.IsNotNull(target.ContentItems.FirstOrDefault((t) => t.Key.StartsWith("ChildTopic2", StringComparison.InvariantCulture)));
512-
Assert.IsNotNull(target.ContentItems.FirstOrDefault((t) => t.Key.StartsWith("ChildTopic3", StringComparison.InvariantCulture)));
513-
Assert.IsNotNull(target.ContentItems.FirstOrDefault((t) => t.Key.StartsWith("ChildTopic4", StringComparison.InvariantCulture)));
495+
Assert.IsNotNull(GetChildTopic(target.ContentItems, "ChildTopic1"));
496+
Assert.IsNotNull(GetChildTopic(target.ContentItems, "ChildTopic2"));
497+
Assert.IsNotNull(GetChildTopic(target.ContentItems, "ChildTopic3"));
498+
Assert.IsNotNull(GetChildTopic(target.ContentItems, "ChildTopic4"));
514499

515500
}
516501

@@ -534,7 +519,7 @@ public async Task MapTopics() {
534519
topic.Relationships.SetTopic("RelatedTopics", relatedTopic3);
535520

536521
var target = (RelatedEntityTopicViewModel?)await _mappingService.MapAsync(topic).ConfigureAwait(false);
537-
var relatedTopic3copy = ((Topic)target.RelatedTopics.FirstOrDefault((t) => t.Key.StartsWith("RelatedTopic3", StringComparison.InvariantCulture)));
522+
var relatedTopic3copy = (getRelatedTopic(target, "RelatedTopic3"));
538523

539524
Assert.AreEqual<int>(3, target.RelatedTopics.Count);
540525

@@ -608,12 +593,9 @@ public async Task FilterByContentType() {
608593
var specialized = target.Children.GetByContentType("DescendentSpecialized");
609594

610595
Assert.AreEqual<int>(2, specialized.Count);
611-
Assert.IsNotNull(getTopic(specialized, "ChildTopic2"));
612-
Assert.IsNotNull(getTopic(specialized, "ChildTopic3"));
613-
Assert.IsNull(getTopic(specialized, "ChildTopic4"));
614-
615-
DescendentTopicViewModel getTopic(TopicViewModelCollection<DescendentTopicViewModel> collection, string key)
616-
=> collection.FirstOrDefault((t) => t.Key.StartsWith(key, StringComparison.InvariantCulture));
596+
Assert.IsNotNull(GetChildTopic(specialized, "ChildTopic2"));
597+
Assert.IsNotNull(GetChildTopic(specialized, "ChildTopic3"));
598+
Assert.IsNull(GetChildTopic(specialized, "ChildTopic4"));
617599

618600
}
619601

@@ -820,5 +802,17 @@ public async Task Caching() {
820802

821803
}
822804

805+
/*==========================================================================================================================
806+
| METHOD: GET CHILD TOPIC
807+
\-------------------------------------------------------------------------------------------------------------------------*/
808+
/// <summary>
809+
/// A helper function which retrieves a child topic based on the key.
810+
/// </summary>
811+
public static KeyOnlyTopicViewModel GetChildTopic(IEnumerable<KeyOnlyTopicViewModel> topicCollection, string key)
812+
=> topicCollection.FirstOrDefault((t) => t.Key.StartsWith(key, StringComparison.InvariantCulture));
813+
814+
public static TopicViewModel GetChildTopic(IEnumerable<TopicViewModel> topicCollection, string key)
815+
=> topicCollection.FirstOrDefault((t) => t.Key.StartsWith(key, StringComparison.InvariantCulture));
816+
823817
} //Class
824818
} //Namespace

0 commit comments

Comments
 (0)