Skip to content

Commit

Permalink
Introduced specialized AttributeDictionary view model for unit tests
Browse files Browse the repository at this point in the history
The `AttributeDictionaryConstructorTopicViewModel` provides a more specialized model for evaluating the `AttributeDictionary` mapping capabilities added to the `TopicMappingService` (see #99). This builds off of the existing `PageTopicViewModel` by including both a `MappedProperty` (which will get mapped by the constructor) and an `UnmappedProperty` (which will not get mapped by the constructor). This allows us to confirm that the `AttributeDictionary` constructor did, in fact, get called, since otherwise the `UnmappedProperty` would get picked up by `SetPropertyAsync()`.

As part of this, I also extended the attributes in the test to ensure that it isn't falling under the recently introduced threshold for honoring the `AttributeDictionary` constructor (2e157e5).
  • Loading branch information
JeremyCaney committed Dec 29, 2021
1 parent fabeb82 commit a68d459
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
26 changes: 21 additions & 5 deletions OnTopic.Tests/TopicMappingServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,29 @@ public class TopicMappingServiceTest {
var topic = new Topic("Test", "Page");
var lastModified = new DateTime(2021, 12, 22);

topic.Attributes.SetValue("Subtitle", "Value");
topic.Attributes.SetValue("Title", "Value");
topic.Attributes.SetValue("ShortTitle", "Short Title");
topic.Attributes.SetValue("Subtitle", "Subtitle");
topic.Attributes.SetValue("MetaTitle", "Meta Title");
topic.Attributes.SetValue("MetaDescription", "Meta Description");
topic.Attributes.SetValue("MetaKeywords", "Load;Test;Keywords");
topic.Attributes.SetValue("NoIndex", "0");
topic.Attributes.SetValue("Body", "Body of test topic");
topic.Attributes.SetValue("MappedProperty", "Mapped Value");
topic.Attributes.SetValue("UnmappedProperty", "Unmapped Value");
topic.VersionHistory.Add(lastModified);

var target = await _mappingService.MapAsync<PageTopicViewModel>(topic).ConfigureAwait(false);

Assert.Equal("Test", target?.Title);
Assert.Equal("Value", target?.Subtitle);
var target = await _mappingService.MapAsync<AttributeDictionaryConstructorTopicViewModel>(topic).ConfigureAwait(false);

Assert.Equal("Value", target?.Title);
Assert.Equal("Short Title", target?.ShortTitle);
Assert.Equal("Subtitle", target?.Subtitle);
Assert.Equal("Meta Title", target?.MetaTitle);
Assert.Equal("Meta Description", target?.MetaDescription);
Assert.Equal(false, target?.NoIndex);
Assert.Equal("Load;Test;Keywords", target?.MetaKeywords);
Assert.Equal("Mapped Value", target?.MappedProperty);
Assert.Null(target?.UnmappedProperty);
Assert.Equal(lastModified, target?.LastModified);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/

using OnTopic.Attributes;

namespace OnTopic.Tests.ViewModels {

/*============================================================================================================================
| VIEW MODEL: ATTRIBUTE DICTIONARY CONSTRUCTOR
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides a strongly-typed data transfer object for testing a constructor with a <see cref="AttributeDictionary"/>.
/// </summary>
/// <remarks>
/// This is a sample class intended for test purposes only; it is not designed for use in a production environment.
/// </remarks>
public record AttributeDictionaryConstructorTopicViewModel: PageTopicViewModel {

/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Initializes a new <see cref="AttributeDictionaryConstructorTopicViewModel"/> with an <paramref name="attributes"/>
/// dictionary.
/// </summary>
/// <param name="attributes">An <see cref="AttributeDictionaryConstructorTopicViewModel"/> of attribute values.</param>
public AttributeDictionaryConstructorTopicViewModel(AttributeDictionary attributes) : base(attributes) {
Contract.Requires(attributes, nameof(attributes));
MappedProperty = attributes.GetValue(nameof(MappedProperty));
}

/// <summary>
/// Initializes a new <see cref="AttributeDictionaryConstructorTopicViewModel"/> with no parameters.
/// </summary>
public AttributeDictionaryConstructorTopicViewModel() { }

/*==========================================================================================================================
| PROPERTIES
\-------------------------------------------------------------------------------------------------------------------------*/
public string? MappedProperty { get; init; }
public string? UnmappedProperty { get; init; }


} //Class
} //Namespace

0 comments on commit a68d459

Please sign in to comment.