Skip to content

Commit a68d459

Browse files
committed
Introduced specialized AttributeDictionary view model for unit tests
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).
1 parent fabeb82 commit a68d459

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

OnTopic.Tests/TopicMappingServiceTest.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,29 @@ public async Task Map_AttributeDictionary_ReturnsNewModel() {
295295
var topic = new Topic("Test", "Page");
296296
var lastModified = new DateTime(2021, 12, 22);
297297

298-
topic.Attributes.SetValue("Subtitle", "Value");
298+
topic.Attributes.SetValue("Title", "Value");
299+
topic.Attributes.SetValue("ShortTitle", "Short Title");
300+
topic.Attributes.SetValue("Subtitle", "Subtitle");
301+
topic.Attributes.SetValue("MetaTitle", "Meta Title");
302+
topic.Attributes.SetValue("MetaDescription", "Meta Description");
303+
topic.Attributes.SetValue("MetaKeywords", "Load;Test;Keywords");
304+
topic.Attributes.SetValue("NoIndex", "0");
305+
topic.Attributes.SetValue("Body", "Body of test topic");
306+
topic.Attributes.SetValue("MappedProperty", "Mapped Value");
307+
topic.Attributes.SetValue("UnmappedProperty", "Unmapped Value");
299308
topic.VersionHistory.Add(lastModified);
300309

301-
var target = await _mappingService.MapAsync<PageTopicViewModel>(topic).ConfigureAwait(false);
302-
303-
Assert.Equal("Test", target?.Title);
304-
Assert.Equal("Value", target?.Subtitle);
310+
var target = await _mappingService.MapAsync<AttributeDictionaryConstructorTopicViewModel>(topic).ConfigureAwait(false);
311+
312+
Assert.Equal("Value", target?.Title);
313+
Assert.Equal("Short Title", target?.ShortTitle);
314+
Assert.Equal("Subtitle", target?.Subtitle);
315+
Assert.Equal("Meta Title", target?.MetaTitle);
316+
Assert.Equal("Meta Description", target?.MetaDescription);
317+
Assert.Equal(false, target?.NoIndex);
318+
Assert.Equal("Load;Test;Keywords", target?.MetaKeywords);
319+
Assert.Equal("Mapped Value", target?.MappedProperty);
320+
Assert.Null(target?.UnmappedProperty);
305321
Assert.Equal(lastModified, target?.LastModified);
306322

307323
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
7+
using OnTopic.Attributes;
8+
9+
namespace OnTopic.Tests.ViewModels {
10+
11+
/*============================================================================================================================
12+
| VIEW MODEL: ATTRIBUTE DICTIONARY CONSTRUCTOR
13+
\---------------------------------------------------------------------------------------------------------------------------*/
14+
/// <summary>
15+
/// Provides a strongly-typed data transfer object for testing a constructor with a <see cref="AttributeDictionary"/>.
16+
/// </summary>
17+
/// <remarks>
18+
/// This is a sample class intended for test purposes only; it is not designed for use in a production environment.
19+
/// </remarks>
20+
public record AttributeDictionaryConstructorTopicViewModel: PageTopicViewModel {
21+
22+
/*==========================================================================================================================
23+
| CONSTRUCTOR
24+
\-------------------------------------------------------------------------------------------------------------------------*/
25+
/// <summary>
26+
/// Initializes a new <see cref="AttributeDictionaryConstructorTopicViewModel"/> with an <paramref name="attributes"/>
27+
/// dictionary.
28+
/// </summary>
29+
/// <param name="attributes">An <see cref="AttributeDictionaryConstructorTopicViewModel"/> of attribute values.</param>
30+
public AttributeDictionaryConstructorTopicViewModel(AttributeDictionary attributes) : base(attributes) {
31+
Contract.Requires(attributes, nameof(attributes));
32+
MappedProperty = attributes.GetValue(nameof(MappedProperty));
33+
}
34+
35+
/// <summary>
36+
/// Initializes a new <see cref="AttributeDictionaryConstructorTopicViewModel"/> with no parameters.
37+
/// </summary>
38+
public AttributeDictionaryConstructorTopicViewModel() { }
39+
40+
/*==========================================================================================================================
41+
| PROPERTIES
42+
\-------------------------------------------------------------------------------------------------------------------------*/
43+
public string? MappedProperty { get; init; }
44+
public string? UnmappedProperty { get; init; }
45+
46+
47+
} //Class
48+
} //Namespace

0 commit comments

Comments
 (0)