Skip to content

Commit ac2b534

Browse files
committed
Merge branch 'improvement/AttributeDictionary-unit-tests' into develop
Introduced unit tests for the `AttributeDictionary` support (f137ca1), thus completing the requirements for Feature #99. This includes unit tests for the `AttributeDictionary` class (11894b7), the `AsAttributeDictionary()` method (d4b77c1), and the constructor support for `AttributeDictionary` within `TopicMappingService` (6b8ed99).
2 parents d5530ed + 6b8ed99 commit ac2b534

3 files changed

Lines changed: 297 additions & 0 deletions

File tree

OnTopic.Tests/AttributeCollectionTest.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,50 @@ public void GetBoolean_IncorrectKey_ReturnDefault() {
405405

406406
}
407407

408+
/*==========================================================================================================================
409+
| TEST: GET URI: INHERITED VALUE: IS RETURNED
410+
\-------------------------------------------------------------------------------------------------------------------------*/
411+
/// <summary>
412+
/// Ensures that URI values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/>
413+
/// and <see cref="Topic.BaseTopic"/>.
414+
/// </summary>
415+
[Fact]
416+
public void GetUri_InheritedValue_IsReturned() {
417+
418+
var baseTopic = new Topic("Base", "Container");
419+
var topic = new Topic("Test", "Container");
420+
var childTopic = new Topic("Child", "Container", topic);
421+
var url = "https://www.github.com/OnTopicCMS/";
422+
var uri = new Uri(url);
423+
424+
topic.BaseTopic = baseTopic;
425+
426+
baseTopic.Attributes.SetUri("Url", uri);
427+
428+
Assert.Equal(uri, topic.Attributes.GetUri("Url"));
429+
Assert.Equal(uri, childTopic.Attributes.GetUri("Url", inheritFromParent: true));
430+
Assert.Null(topic.Attributes.GetUri("Url", inheritFromBase: false));
431+
432+
}
433+
434+
/*==========================================================================================================================
435+
| TEST: GET URI: INCORRECT VALUE: RETURN DEFAULT
436+
\-------------------------------------------------------------------------------------------------------------------------*/
437+
/// <summary>
438+
/// Ensures that invalid values return the default.
439+
/// </summary>
440+
[Fact]
441+
public void GetUri_IncorrectValue_ReturnDefault() {
442+
443+
var topic = new Topic("Test", "Container");
444+
var url = "https://www.github.com/OnTopicCMS/";
445+
var uri = new Uri(url);
446+
447+
Assert.Null(topic.Attributes.GetUri("InvalidUrl"));
448+
Assert.Equal(uri, topic.Attributes.GetUri("InvalidUrl", uri));
449+
450+
}
451+
408452
/*==========================================================================================================================
409453
| TEST: SET VALUE: CORRECT VALUE: IS RETURNED
410454
\-------------------------------------------------------------------------------------------------------------------------*/
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
using OnTopic.Attributes;
7+
using Xunit;
8+
9+
namespace OnTopic.Tests {
10+
11+
/*============================================================================================================================
12+
| CLASS: ATTRIBUTE DICTIONARY TEST
13+
\---------------------------------------------------------------------------------------------------------------------------*/
14+
/// <summary>
15+
/// Provides unit tests for the <see cref="AttributeDictionary"/> class.
16+
/// </summary>
17+
[ExcludeFromCodeCoverage]
18+
public class AttributeDictionaryTest {
19+
20+
/*==========================================================================================================================
21+
| TEST: GET VALUE: RETURNS EXPECTED VALUE
22+
\-------------------------------------------------------------------------------------------------------------------------*/
23+
/// <summary>
24+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
25+
/// "AttributeDictionary.GetValue(String)"/> returns the <paramref name="expected"/> value.
26+
/// </summary>
27+
/// <param name="input">The value to add to the dictionary.</param>
28+
/// <param name="expected">The value expected to be returned.</param>
29+
[Theory]
30+
[InlineData(null, null)]
31+
[InlineData("", null)]
32+
[InlineData(" ", null)]
33+
[InlineData("0", "0")]
34+
[InlineData("True", "True")]
35+
[InlineData("Hello", "Hello")]
36+
public void GetValue_ReturnsExpectedValue(string? input, string? expected) {
37+
38+
var attributes = new AttributeDictionary() {{"Key", input}};
39+
40+
Assert.Equal(expected, attributes.GetValue("Key"));
41+
42+
}
43+
44+
/*==========================================================================================================================
45+
| TEST: GET BOOLEAN: RETURNS EXPECTED VALUE
46+
\-------------------------------------------------------------------------------------------------------------------------*/
47+
/// <summary>
48+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
49+
/// "AttributeDictionary.GetBoolean(String)"/> returns the <paramref name="expected"/> value.
50+
/// </summary>
51+
/// <param name="input">The value to add to the dictionary.</param>
52+
/// <param name="expected">The value expected to be returned.</param>
53+
[Theory]
54+
[InlineData("0", false)]
55+
[InlineData("1", true)]
56+
[InlineData("False", false)]
57+
[InlineData("True", true)]
58+
[InlineData("", null)]
59+
[InlineData("Hello", null)]
60+
public void GetBoolean_ReturnsExpectedValue(string input, bool? expected) {
61+
62+
var attributes = new AttributeDictionary() {{"Key", input}};
63+
64+
Assert.Equal(expected, attributes.GetBoolean("Key"));
65+
66+
}
67+
68+
/*==========================================================================================================================
69+
| TEST: GET INTEGER: RETURNS EXPECTED VALUE
70+
\-------------------------------------------------------------------------------------------------------------------------*/
71+
/// <summary>
72+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
73+
/// "AttributeDictionary.GetInteger(String)"/> returns the <paramref name="expected"/> value.
74+
/// </summary>
75+
/// <param name="input">The value to add to the dictionary.</param>
76+
/// <param name="expected">The value expected to be returned.</param>
77+
[Theory]
78+
[InlineData("0", 0)]
79+
[InlineData("1", 1)]
80+
[InlineData("2.4", null)]
81+
[InlineData("", null)]
82+
[InlineData("Hello", null)]
83+
public void GetInteger_ReturnsExpectedValue(string input, int? expected) {
84+
85+
var attributes = new AttributeDictionary() {{"Key", input}};
86+
87+
Assert.Equal(expected, attributes.GetInteger("Key"));
88+
89+
}
90+
91+
/*==========================================================================================================================
92+
| TEST: GET DOUBLE: RETURNS EXPECTED VALUE
93+
\-------------------------------------------------------------------------------------------------------------------------*/
94+
/// <summary>
95+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
96+
/// "AttributeDictionary.GetDouble(String)"/> returns the <paramref name="expected"/> value.
97+
/// </summary>
98+
/// <param name="input">The value to add to the dictionary.</param>
99+
/// <param name="expected">The value expected to be returned.</param>
100+
[Theory]
101+
[InlineData("0.0", 0.0)]
102+
[InlineData("1.0", 1.0)]
103+
[InlineData("1", 1.0)]
104+
[InlineData("1.4", 1.4)]
105+
[InlineData("", null)]
106+
[InlineData("Hello", null)]
107+
public void GetDouble_ReturnsExpectedValue(string input, double? expected) {
108+
109+
var attributes = new AttributeDictionary() {{"Key", input}};
110+
111+
Assert.Equal(expected, attributes.GetDouble("Key"));
112+
113+
}
114+
115+
/*==========================================================================================================================
116+
| TEST: GET DATE/TIME: RETURNS EXPECTED VALUE
117+
\-------------------------------------------------------------------------------------------------------------------------*/
118+
/// <summary>
119+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
120+
/// "AttributeDictionary.GetDateTime(String)"/> returns the expected value.
121+
/// </summary>
122+
/// <param name="input">The value to add to the dictionary.</param>
123+
/// <param name="isSet">Determines whether a valid <see cref="DateTime"/> is expected in response.</param>
124+
[Theory]
125+
[InlineData("1976-10-15 01:02:03", true)]
126+
[InlineData("October 15, 1976 01:02:03 AM", true)]
127+
[InlineData("15 Oct 1976 01:02:03", true)]
128+
[InlineData("10/15/1976 01:02:03 AM", true)]
129+
[InlineData("", false)]
130+
[InlineData("Hello", false)]
131+
public void GetDate_ReturnsExpectedValue(string input, bool isSet) {
132+
133+
var attributes = new AttributeDictionary() {{"Key", input}};
134+
135+
Assert.Equal(isSet? new DateTime(1976, 10, 15, 1, 2, 3) : null, attributes.GetDateTime("Key"));
136+
137+
}
138+
139+
/*==========================================================================================================================
140+
| TEST: GET URI: RETURNS EXPECTED VALUE
141+
\-------------------------------------------------------------------------------------------------------------------------*/
142+
/// <summary>
143+
/// Constructs a <see cref="AttributeDictionary"/> based on the <paramref name="input"/> data and confirms that <see cref=
144+
/// "AttributeDictionary.GetUri(String)"/> returns the expected value.
145+
/// </summary>
146+
/// <param name="input">The value to add to the dictionary.</param>
147+
[Theory]
148+
[InlineData("https://www.github.com/OnTopicCMS")]
149+
[InlineData("Some:\\\\URL")]
150+
public void GetUri_ReturnsExpectedValue(string input) {
151+
152+
var attributes = new AttributeDictionary() {{"Key", input}};
153+
154+
Assert.Equal(new Uri(input), attributes.GetUri("Key"));
155+
156+
}
157+
158+
/*==========================================================================================================================
159+
| TEST: GET {TYPE}: INVALID KEY: RETURNS NULL
160+
\-------------------------------------------------------------------------------------------------------------------------*/
161+
/// <summary>
162+
/// Constructs a <see cref="AttributeDictionary"/> and confirms that each of the <see cref="AttributeDictionary.GetValue(
163+
/// String)"/> methods return <c>null</c> if an invalid key is passed.
164+
/// </summary>
165+
[Fact]
166+
public void GetType_InvalidKey_ReturnsNull() {
167+
168+
var attributes = new AttributeDictionary();
169+
170+
Assert.Null(attributes.GetValue("MissingKey"));
171+
Assert.Null(attributes.GetBoolean("MissingKey"));
172+
Assert.Null(attributes.GetInteger("MissingKey"));
173+
Assert.Null(attributes.GetDouble("MissingKey"));
174+
Assert.Null(attributes.GetDateTime("MissingKey"));
175+
Assert.Null(attributes.GetUri("MissingKey"));
176+
177+
}
178+
179+
/*==========================================================================================================================
180+
| TEST: AS ATTRIBUTE DICTIONARY: EXCLUDED KEYS: EXCLUDED
181+
\-------------------------------------------------------------------------------------------------------------------------*/
182+
/// <summary>
183+
/// Constructs a <see cref="AttributeDictionary"/> using <see cref="AttributeCollection.AsAttributeDictionary(Boolean)"/>
184+
/// and confirms that <see cref="AttributeDictionary.GetValue(String)"/> doesn't include the excluded values.
185+
/// </summary>
186+
[Fact]
187+
public void AsAttributeDictionary_ExcludedKeys_Excluded() {
188+
189+
var topic = new Topic("Test", "Page");
190+
191+
topic.Attributes.SetValue("Title", "Page Title");
192+
topic.Attributes.SetValue("LastModified", "October 15, 1976");
193+
topic.Attributes.SetValue("Subtitle", "Subtitle");
194+
195+
var attributes = topic.Attributes.AsAttributeDictionary();
196+
197+
Assert.Single(attributes.Keys);
198+
Assert.Null(attributes.GetValue("Title"));
199+
Assert.Null(attributes.GetValue("LastModified"));
200+
Assert.Equal("Subtitle", attributes.GetValue("Subtitle"));
201+
202+
}
203+
204+
/*==========================================================================================================================
205+
| TEST: AS ATTRIBUTE DICTIONARY: INHERIT FROM BASE: INHERITS VALUES
206+
\-------------------------------------------------------------------------------------------------------------------------*/
207+
/// <summary>
208+
/// Constructs a <see cref="AttributeDictionary"/> using <see cref="AttributeCollection.AsAttributeDictionary(Boolean)"/>
209+
/// and confirms that <see cref="AttributeDictionary.GetValue(String)"/> correctly inherits values.
210+
/// </summary>
211+
[Fact]
212+
public void AsAttributeDictionary_InheritFromBase_InheritsValues() {
213+
214+
var baseTopic = new Topic("BaseTopic", "Page");
215+
var topic = new Topic("Test", "Page");
216+
217+
topic.BaseTopic = baseTopic;
218+
219+
baseTopic.Attributes.SetValue("Subtitle", "Subtitle");
220+
221+
var attributes = topic.Attributes.AsAttributeDictionary(true);
222+
223+
Assert.Single(attributes.Keys);
224+
Assert.Equal("Subtitle", attributes.GetValue("Subtitle"));
225+
226+
}
227+
228+
} //Class
229+
} //Namespace

OnTopic.Tests/TopicMappingServiceTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,30 @@ public async Task Map_Generic_ReturnsConvertedProperty() {
148148

149149
}
150150

151+
/*==========================================================================================================================
152+
| TEST: MAP: ATTRIBUTE DICTIONARY: RETURNS NEW MODEL
153+
\-------------------------------------------------------------------------------------------------------------------------*/
154+
/// <summary>
155+
/// Establishes a <see cref="TopicMappingService"/> and attempts to map a view model with a constructor containing a <see
156+
/// cref="AttributeDictionary"/>. Confirms that the expected model is returned.
157+
/// </summary>
158+
[Fact]
159+
public async Task Map_AttributeDictionary_ReturnsNewModel() {
160+
161+
var topic = new Topic("Test", "Page");
162+
var lastModified = new DateTime(2021, 12, 22);
163+
164+
topic.Attributes.SetValue("Subtitle", "Value");
165+
topic.VersionHistory.Add(lastModified);
166+
167+
var target = await _mappingService.MapAsync<PageTopicViewModel>(topic).ConfigureAwait(false);
168+
169+
Assert.Equal("Test", target?.Title);
170+
Assert.Equal("Value", target?.Subtitle);
171+
Assert.Equal(lastModified, target?.LastModified);
172+
173+
}
174+
151175
/*==========================================================================================================================
152176
| TEST: MAP: CONSTRUCTOR: RETURNS NEW MODEL
153177
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)