-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttributeCollectionTest.cs
1172 lines (884 loc) · 51.2 KB
/
AttributeCollectionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using System.Collections;
using System.Globalization;
using OnTopic.Collections.Specialized;
using OnTopic.Tests.Entities;
using Xunit;
namespace OnTopic.Tests {
/*============================================================================================================================
| CLASS: ATTRIBUTE COLLECTION TEST
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Provides unit tests for the <see cref="AttributeCollection"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class AttributeCollectionTest {
/*==========================================================================================================================
| TEST: GET VALUE: CORRECT VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Creates a new attribute via an <c>[AttributeSetter]</c> and ensures that the attribute can be returned.
/// </summary>
[Fact]
public void GetValue_CorrectValue_IsReturned() {
var topic = new Topic("Test", "Container") {
View = "Test"
};
Assert.Equal("Test", topic.Attributes.GetValue("View"));
}
/*==========================================================================================================================
| TEST: GET VALUE: INHERITED VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/> and <see
/// cref="Topic.BaseTopic"/>.
/// </summary>
[Fact]
public void GetValue_InheritedValue_IsReturned() {
var baseTopic = new Topic("Base", "Container");
var topic = new Topic("Test", "Container");
var childTopic = new Topic("Child", "Container", topic);
topic.BaseTopic = baseTopic;
baseTopic.View = "Test";
Assert.Equal("Test", topic.Attributes.GetValue("View"));
Assert.Equal("Test", childTopic.Attributes.GetValue("View", "Invalid", true));
Assert.Null(topic.Attributes.GetValue("View", null, inheritFromBase: false));
}
/*==========================================================================================================================
| TEST: GET VALUE: MISSING VALUE: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Creates a new topic and requests an invalid attribute; ensures falls back to the default.
/// </summary>
[Fact]
public void GetValue_MissingValue_ReturnsDefault() {
var topic = new Topic("Test", "Container");
Assert.Null(topic.Attributes.GetValue("InvalidAttribute"));
Assert.Equal("Foo", topic.Attributes.GetValue("InvalidAttribute", "Foo"));
}
/*==========================================================================================================================
| TEST: GET VALUE: EMPTY VALUE: RETURNS NULL
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// If the <see cref="TrackedRecord{T}.Value"/> is <see cref="String.Empty"/>, then <see cref="TrackedRecordCollection{
/// TItem, TValue, TAttribute}.GetValue(String, Boolean)"/> should return <c>null</c>.
/// </summary>
[Fact]
public void GetValue_EmptyValue_ReturnsNull() {
var topic = new Topic("Test", "Container");
topic.Attributes.Add(new("EmptyValue", ""));
Assert.Null(topic.Attributes.GetValue("EmptyValue"));
}
/*==========================================================================================================================
| TEST: GET INTEGER: CORRECT VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that integer values can be set and retrieved as expected.
/// </summary>
[Fact]
public void GetInteger_CorrectValue_IsReturned() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetInteger("Number1", 1);
Assert.Equal(1, topic.Attributes.GetInteger("Number1", 5));
}
/*==========================================================================================================================
| TEST: GET DOUBLE: INHERITED VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that integer values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/>
/// and <see cref="Topic.BaseTopic"/>.
/// </summary>
[Fact]
public void GetInteger_InheritedValue_IsReturned() {
var baseTopic = new Topic("Base", "Container");
var topic = new Topic("Test", "Container");
var childTopic = new Topic("Child", "Container", topic);
topic.BaseTopic = baseTopic;
baseTopic.Attributes.SetInteger("Number1", 1);
Assert.Equal(1, topic.Attributes.GetInteger("Number1", 5));
Assert.Equal(1, childTopic.Attributes.GetInteger("Number1", 5, true));
Assert.Equal(0, topic.Attributes.GetInteger("Number1", inheritFromBase: false));
}
/*==========================================================================================================================
| TEST: GET INTEGER: INCORRECT VALUE: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that invalid values return the default.
/// </summary>
[Fact]
public void GetInteger_IncorrectValue_ReturnsDefault() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Number3", "Invalid");
Assert.Equal(0, topic.Attributes.GetInteger("Number3"));
Assert.Equal(5, topic.Attributes.GetInteger("Number3", 5));
}
/*==========================================================================================================================
| TEST: GET INTEGER: INCORRECT KEY: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that incorrect key names return the default.
/// </summary>
[Fact]
public void GetInteger_IncorrectKey_ReturnsDefault() {
var topic = new Topic("Test", "Container");
Assert.Equal(0, topic.Attributes.GetInteger("InvalidKey"));
Assert.Equal(5, topic.Attributes.GetInteger("InvalidKey", 5));
}
/*==========================================================================================================================
| TEST: GET DOUBLE: CORRECT VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that double values can be set and retrieved as expected.
/// </summary>
[Fact]
public void GetDouble_CorrectValue_IsReturned() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetDouble("Number1", 1);
Assert.Equal(1.0, topic.Attributes.GetDouble("Number1", 5.0));
}
/*==========================================================================================================================
| TEST: GET DOUBLE: INHERITED VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that double values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/>
/// and <see cref="Topic.BaseTopic"/>.
/// </summary>
[Fact]
public void GetDouble_InheritedValue_IsReturned() {
var baseTopic = new Topic("Base", "Container");
var topic = new Topic("Test", "Container");
var childTopic = new Topic("Child", "Container", topic);
topic.BaseTopic = baseTopic;
baseTopic.Attributes.SetDouble("Number1", 1);
Assert.Equal(1.0, topic.Attributes.GetDouble("Number1", 5.0));
Assert.Equal(1.0, childTopic.Attributes.GetDouble("Number1", 5.0, true));
Assert.Equal(0.0, topic.Attributes.GetInteger("Number1", inheritFromBase: false));
}
/*==========================================================================================================================
| TEST: GET DOUBLE: INCORRECT VALUE: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that double values return the default.
/// </summary>
[Fact]
public void GetDouble_IncorrectValue_ReturnsDefault() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Number3", "Invalid");
Assert.Equal(0.0, topic.Attributes.GetDouble("Number3"));
Assert.Equal(5.0, topic.Attributes.GetDouble("Number3", 5.0));
}
/*==========================================================================================================================
| TEST: GET DOUBLE: INCORRECT KEY: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that double key names return the default.
/// </summary>
[Fact]
public void GetDouble_IncorrectKey_ReturnsDefault() {
var topic = new Topic("Test", "Container");
Assert.Equal(0.0, topic.Attributes.GetDouble("InvalidKey"));
Assert.Equal(5.0, topic.Attributes.GetDouble("InvalidKey", 5.0));
}
/*==========================================================================================================================
| TEST: GET DATETIME: CORRECT VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that Date/Time values can be set and retrieved as expected.
/// </summary>
[Fact]
public void GetDateTime_CorrectValue_IsReturned() {
var topic = new Topic("Test", "Container");
var dateTime1 = new DateTime(1976, 10, 15);
topic.Attributes.SetDateTime("DateTime1", dateTime1);
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.MinValue));
}
/*==========================================================================================================================
| TEST: GET DATETIME: INHERITED VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that Date/Time values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/>
/// and <see cref="Topic.BaseTopic"/>.
/// </summary>
[Fact]
public void GetDateTime_InheritedValue_IsReturned() {
var baseTopic = new Topic("Base", "Container");
var topic = new Topic("Test", "Container");
var childTopic = new Topic("Child", "Container", topic);
var dateTime1 = new DateTime(1976, 10, 15);
topic.BaseTopic = baseTopic;
baseTopic.Attributes.SetDateTime("DateTime1", dateTime1);
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime1", DateTime.Now));
Assert.Equal(dateTime1, childTopic.Attributes.GetDateTime("DateTime1", DateTime.Now, true));
Assert.Equal(new DateTime(), topic.Attributes.GetDateTime("DateTime1", inheritFromBase: false));
}
/*==========================================================================================================================
| TEST: GET DATETIME: INCORRECT VALUE: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that invalid values return the default.
/// </summary>
[Fact]
public void GetDateTime_IncorrectValue_ReturnsDefault() {
var topic = new Topic("Test", "Container");
var dateTime1 = new DateTime(1976, 10, 15);
topic.Attributes.SetValue("DateTime2", "IncorrectValue");
Assert.Equal(new DateTime(), topic.Attributes.GetDateTime("DateTime2"));
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime2", dateTime1));
}
/*==========================================================================================================================
| TEST: GET DATETIME: INCORRECT KEY: RETURNS DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that incorrect key names return the default.
/// </summary>
[Fact]
public void GetDateTime_IncorrectKey_ReturnsDefault() {
var topic = new Topic("Test", "Container");
var dateTime1 = new DateTime(1976, 10, 15);
var dateTime2 = new DateTime(1981, 06, 03);
topic.Attributes.SetDateTime("DateTime2", dateTime2);
Assert.Equal(new DateTime(), topic.Attributes.GetDateTime("DateTime3"));
Assert.Equal(dateTime1, topic.Attributes.GetDateTime("DateTime3", dateTime1));
}
/*==========================================================================================================================
| TEST: GET BOOLEAN: CORRECT VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that boolean values can be set and retrieved as expected.
/// </summary>
[Fact]
public void GetBoolean_CorrectValue_IsReturned() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetBoolean("IsValue1", true);
topic.Attributes.SetBoolean("IsValue2", false);
Assert.True(topic.Attributes.GetBoolean("IsValue1"));
Assert.True(topic.Attributes.GetBoolean("IsValue1", false));
Assert.False(topic.Attributes.GetBoolean("IsValue2", true));
}
/*==========================================================================================================================
| TEST: GET BOOLEAN: INHERITED VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that boolean values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/>
/// and <see cref="Topic.BaseTopic"/>.
/// </summary>
[Fact]
public void GetBoolean_InheritedValue_IsReturned() {
var baseTopic = new Topic("Base", "Container");
var topic = new Topic("Test", "Container");
var childTopic = new Topic("Child", "Container", topic);
topic.BaseTopic = baseTopic;
baseTopic.Attributes.SetBoolean("IsValue1", true);
Assert.True(topic.Attributes.GetBoolean("IsValue1"));
Assert.True(childTopic.Attributes.GetBoolean("IsValue1", false, true));
Assert.False(topic.Attributes.GetBoolean("IsValue1", inheritFromBase: false));
}
/*==========================================================================================================================
| TEST: GET BOOLEAN: INCORRECT VALUE: RETURN DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that invalid values return the default.
/// </summary>
[Fact]
public void GetBoolean_IncorrectValue_ReturnDefault() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("IsValue", "Invalid");
Assert.False(topic.Attributes.GetBoolean("IsValue"));
Assert.True(topic.Attributes.GetBoolean("IsValue", true));
Assert.False(topic.Attributes.GetBoolean("IsValue", false));
}
/*==========================================================================================================================
| TEST: GET BOOLEAN: INCORRECT KEY: RETURN DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that incorrect key names return the default.
/// </summary>
[Fact]
public void GetBoolean_IncorrectKey_ReturnDefault() {
var topic = new Topic("Test", "Container");
Assert.False(topic.Attributes.GetBoolean("InvalidKey"));
Assert.True(topic.Attributes.GetBoolean("InvalidKey", true));
Assert.False(topic.Attributes.GetBoolean("InvalidKey", false));
}
/*==========================================================================================================================
| TEST: GET URI: INHERITED VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that URI values can be set and retrieved as expected via inheritance, both via <see cref="Topic.Parent"/>
/// and <see cref="Topic.BaseTopic"/>.
/// </summary>
[Fact]
public void GetUri_InheritedValue_IsReturned() {
var baseTopic = new Topic("Base", "Container");
var topic = new Topic("Test", "Container");
var childTopic = new Topic("Child", "Container", topic);
var url = "https://www.github.com/OnTopicCMS/";
var uri = new Uri(url);
topic.BaseTopic = baseTopic;
baseTopic.Attributes.SetUri("Url", uri);
Assert.Equal(uri, topic.Attributes.GetUri("Url"));
Assert.Equal(uri, childTopic.Attributes.GetUri("Url", inheritFromParent: true));
Assert.Null(topic.Attributes.GetUri("Url", inheritFromBase: false));
}
/*==========================================================================================================================
| TEST: GET URI: INCORRECT VALUE: RETURN DEFAULT
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Ensures that invalid values return the default.
/// </summary>
[Fact]
public void GetUri_IncorrectValue_ReturnDefault() {
var topic = new Topic("Test", "Container");
var url = "https://www.github.com/OnTopicCMS/";
var uri = new Uri(url);
Assert.Null(topic.Attributes.GetUri("InvalidUrl"));
Assert.Equal(uri, topic.Attributes.GetUri("InvalidUrl", uri));
}
/*==========================================================================================================================
| TEST: SET VALUE: CORRECT VALUE: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a custom attribute on a topic and ensures it can be retrieved.
/// </summary>
[Fact]
public void SetValue_CorrectValue_IsReturned() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar");
Assert.Equal("Bar", topic.Attributes.GetValue("Foo"));
}
/*==========================================================================================================================
| TEST: SET VALUE: VALUE CHANGED: IS DIRTY?
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Modifies the value of a custom attribute on a topic and ensures it is marked as IsDirty.
/// </summary>
[Fact]
public void SetValue_ValueChanged_IsDirty() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar", false);
topic.Attributes.SetValue("Foo", "Baz");
Assert.True(topic.Attributes["Foo"].IsDirty);
}
/*==========================================================================================================================
| TEST: CLEAR: NON-NULLABLE VALUE WITH BUSINESS LOGIC: THROWS EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets an attribute on a topic instance with an valid value, and then attempts to clear all attributes. Ensures that the
/// business logic is enforced by throwing an exception on the non-nullable property.
/// </summary>
[Fact]
public void Clear_NonNullableValueWithBusinessLogic_ThrowsException() {
var topic = new CustomTopic("Test", "Page") {
NonNullableAttribute = "Test"
};
Assert.Throws<ArgumentNullException>(() =>
topic.Attributes.Clear()
);
}
/*==========================================================================================================================
| TEST: CLEAR: EXISTING VALUES: IS DIRTY?
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Calls <see cref="IList.Clear()"/> and confirms that the collection is marked as dirty, due to deleted attrbutes.
/// </summary>
[Fact]
public void Clear_ExistingValues_IsDirty() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar", false);
topic.Attributes.Clear();
Assert.True(topic.Attributes.IsDirty());
Assert.Contains("Foo", topic.Attributes.DeletedItems);
}
/*==========================================================================================================================
| TEST: SET VALUE: VALUE UNCHANGED: IS NOT DIRTY?
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets the value of a custom <see cref="AttributeRecord"/> to the existing value and ensures it is <i>not</i> marked as
/// <see cref="TrackedRecord{T}.IsDirty"/>.
/// </summary>
[Fact]
public void SetValue_ValueUnchanged_IsNotDirty() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Fah", "Bar", false);
topic.Attributes.SetValue("Fah", "Bar");
Assert.False(topic.Attributes["Fah"].IsDirty);
}
/*==========================================================================================================================
| TEST: IS DIRTY: DIRTY VALUES: RETURNS TRUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> that is marked as <see cref="
/// TrackedRecord{T}.IsDirty"/>. Confirms that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns <c>true</c>.
/// </summary>
[Fact]
public void IsDirty_DirtyValues_ReturnsTrue() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar");
Assert.True(topic.Attributes.IsDirty());
}
/*==========================================================================================================================
| TEST: IS DIRTY: IS NEW: RETURNS TRUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> that is <i>not</i> marked as <see
/// cref="TrackedRecord{T}.IsDirty"/>. Confirms that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns
/// <c>true</c> if <see cref="Topic.IsNew"/>, even if <see cref="TrackedRecordCollection{TItem, TValue, TAttribute}.
/// MarkClean(String)"/> was called.
/// </summary>
[Fact]
public void IsDirty_IsNew_ReturnsTrue() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar", false);
topic.Attributes.MarkClean("Foo");
topic.Attributes.MarkClean();
Assert.True(topic.Attributes.IsDirty());
Assert.True(topic.Attributes.IsDirty(true));
}
/*==========================================================================================================================
| TEST: IS DIRTY: DELETED VALUES: RETURNS TRUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> and then deletes it. Confirms
/// that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns <c>true</c>.
/// </summary>
[Fact]
public void IsDirty_DeletedValues_ReturnsTrue() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar");
topic.Attributes.Remove("Foo");
Assert.True(topic.Attributes.IsDirty());
Assert.True(topic.Attributes.IsDirty(true));
}
/*==========================================================================================================================
| TEST: IS DIRTY: UNDELETED VALUES: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> and then deletes it. Confirms
/// that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns <c>false</c> after recreating the value.
/// </summary>
[Fact]
public void IsDirty_UndeletedValues_ReturnsFalse() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar");
topic.Attributes.Remove("Foo");
topic.Attributes.SetValue("Foo", "Bar", false);
Assert.False(topic.Attributes.IsDirty());
Assert.False(topic.Attributes.IsDirty(true));
Assert.DoesNotContain("Foo", topic.Attributes.DeletedItems);
}
/*==========================================================================================================================
| TEST: IS DIRTY: NO DIRTY VALUES: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> that is <i>not</i> marked as <see
/// cref="TrackedRecord{T}.IsDirty"/>. Confirms that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns
/// <c>false</c>.
/// </summary>
[Fact]
public void IsDirty_NoDirtyValues_ReturnsFalse() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar", false);
Assert.False(topic.Attributes.IsDirty());
}
/*==========================================================================================================================
| TEST: IS DIRTY: IS NEW: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> that is <i>not</i> marked as <see
/// cref="TrackedRecord{T}.IsDirty"/>. Confirms that <see cref="AttributeCollection.IsDirty(Boolean)"/> still returns
/// <c>true</c> if the associated <see cref="Topic"/> is <see cref="Topic.IsNew"/>. New topics cannot be clean.
/// </summary>
[Fact]
public void IsDirty_IsNew_ReturnsFalse() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar", false);
Assert.True(topic.Attributes.IsDirty());
}
/*==========================================================================================================================
| TEST: IS DIRTY: MISSING KEY: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Confirms that calling <see cref="TrackedRecordCollection{TItem, TValue, TAttribute}.IsDirty(String)"/> with an invalid
/// <c>key</c> returns <c>false</c>.
/// </summary>
[Fact]
public void IsDirty_MissingKey_ReturnsFalse() {
var topic = new Topic("Test", "Container");
Assert.False(topic.Attributes.IsDirty("MissingKey"));
}
/*==========================================================================================================================
| TEST: IS DIRTY: EXCLUDE LAST MODIFIED: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> that is <i>not</i> marked as <see
/// cref="TrackedRecord{T}.IsDirty"/>. Updates the <c>LastModified</c> attributes, thus marking the collection as <see
/// cref="TrackedRecordCollection{TItem, TValue, TAttribute}.IsDirty()"/>. Confirms that <see cref="AttributeCollection.
/// IsDirty(Boolean)"/> returns <c>false</c>.
/// </summary>
[Fact]
public void IsDirty_ExcludeLastModified_ReturnsFalse() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar", false);
topic.Attributes.SetValue("LastModified", DateTime.Now.ToString(CultureInfo.InvariantCulture));
topic.Attributes.SetValue("LastModifiedBy", "System");
Assert.True(topic.Attributes.IsDirty());
Assert.False(topic.Attributes.IsDirty(true));
}
/*==========================================================================================================================
| TEST: IS DIRTY: MARK CLEAN: UPDATES LAST MODIFIED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> and then deletes it. Confirms
/// that the <see cref="TrackedRecord{T}.LastModified"/> returns the new <c>version</c> after calling <see cref="
/// TrackedRecordCollection{TItem, TValue, TAttribute}.MarkClean(DateTime?)"/>.
/// </summary>
[Fact]
public void IsDirty_MarkClean_UpdatesLastModified() {
var topic = new Topic("Test", "Container", null, 1);
var firstVersion = DateTime.Now.AddDays(5);
var secondVersion = DateTime.Now.AddDays(10);
var thirdVersion = DateTime.Now.AddDays(15);
topic.Attributes.SetValue("Foo", "Qux", false, firstVersion);
topic.Attributes.SetValue("Bar", "Quux");
topic.Attributes.SetValue("Baz", "Quuz");
topic.Attributes.MarkClean("Bar", secondVersion);
topic.Attributes.MarkClean(thirdVersion);
topic.Attributes.TryGetValue("Foo", out var cleanedAttribute1);
topic.Attributes.TryGetValue("Bar", out var cleanedAttribute2);
topic.Attributes.TryGetValue("Baz", out var cleanedAttribute3);
Assert.Equal(firstVersion, cleanedAttribute1?.LastModified);
Assert.Equal(secondVersion, cleanedAttribute2?.LastModified);
Assert.Equal(thirdVersion, cleanedAttribute3?.LastModified);
}
/*==========================================================================================================================
| TEST: IS DIRTY: MARK CLEAN: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> and then deletes it. Confirms
/// that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns <c>false</c> after calling <see cref="
/// TrackedRecordCollection{TItem, TValue, TAttribute}.MarkClean(DateTime?)"/>.
/// </summary>
[Fact]
public void IsDirty_MarkClean_ReturnsFalse() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar");
topic.Attributes.SetValue("Baz", "Foo");
topic.Attributes.Remove("Foo");
topic.Attributes.MarkClean();
Assert.False(topic.Attributes.IsDirty());
Assert.Empty(topic.Attributes.DeletedItems);
}
/*==========================================================================================================================
| TEST: IS DIRTY: MARK CLEAN: RETURNS TRUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Adds an <see cref="AttributeRecord"/> to a <see cref="AttributeCollection"/> associated with a <see cref="Topic"/>
/// that is <see cref="Topic.IsNew"/>. Confirms that <see cref="AttributeCollection.IsDirty(Boolean)"/> returns <c>true
/// </c> even after calling <see cref="TrackedRecordCollection{TItem, TValue, TAttribute}.MarkClean(String)"/> since new
/// topics cannot be clean.
/// </summary>
[Fact]
public void IsDirty_MarkClean_ReturnsTrue() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar");
topic.Attributes.MarkClean("Foo");
Assert.True(topic.Attributes.IsDirty("Foo"));
}
/*==========================================================================================================================
| TEST: IS DIRTY: MARK ATTRIBUTE CLEAN: RETURNS FALSE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates the <see cref="AttributeCollection"/> with a <see cref="AttributeRecord"/> and then confirms that <see cref=
/// "TrackedRecordCollection{TItem, TValue, TAttribute}.IsDirty(String)"/> returns <c>false</c> for that attribute after
/// calling <see cref="TrackedRecordCollection{TItem, TValue, TAttribute}.MarkClean(String, DateTime?)"/>.
/// </summary>
[Fact]
public void IsDirty_MarkAttributeClean_ReturnsFalse() {
var topic = new Topic("Test", "Container", null, 1);
topic.Attributes.SetValue("Foo", "Bar");
topic.Attributes.MarkClean("Foo");
Assert.False(topic.Attributes.IsDirty("Foo"));
}
/*==========================================================================================================================
| TEST: IS DIRTY: ADD CLEAN ATTRIBUTE TO NEW TOPIC: RETURNS TRUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates a <see cref="AttributeCollection"/> associated with an <see cref="Topic.IsNew"/> <see cref="Topic"/> with a
/// <see cref="AttributeRecord"/> that is not marked as <see cref="TrackedRecord{T}.IsDirty"/> and then confirms that <see
/// cref="TrackedRecordCollection{TItem, TValue, TAttribute}.IsDirty()"/> returns <c>true</c>.
/// </summary>
[Fact]
public void IsDirty_AddCleanAttributeToNewTopic_ReturnsTrue() {
var topic = new Topic("Test", "Container");
topic.Attributes.Add(
new() {
Key = "Foo",
Value = "Bar",
IsDirty = false
}
);
Assert.True(topic.Attributes.IsDirty());
}
/*==========================================================================================================================
| TEST: IS DIRTY: MARK NEW TOPIC AS CLEAN: RETURNS TRUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Populates a <see cref="AttributeCollection"/> associated with an <see cref="Topic.IsNew"/> <see cref="Topic"/> with a
/// <see cref="AttributeRecord"/> and then confirms that <see cref="TrackedRecordCollection{TItem, TValue, TAttribute}.
/// IsDirty(String)"/> returns <c>true</c> for that attribute after calling <see cref="TrackedRecordCollection{TItem,
/// TValue, TAttribute}.MarkClean(String, DateTime?)"/>.
/// </summary>
[Fact]
public void IsDirty_MarkNewTopicAsClean_ReturnsTrue() {
var topic = new Topic("Test", "Container");
topic.Attributes.SetValue("Foo", "Bar");
topic.Attributes.MarkClean();
Assert.True(topic.Attributes.IsDirty());
}
/*==========================================================================================================================
| TEST: SET VALUE: INVALID VALUE: THROWS EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Attempts to violate the business logic by bypassing the property setter; ensures that business logic is enforced.
/// </summary>
[Fact]
public void SetValue_InvalidValue_ThrowsException() {
var topic = new Topic("Test", "Container");
Assert.Throws<InvalidKeyException>(() =>
topic.Attributes.SetValue("View", "# ?")
);
}
/*==========================================================================================================================
| TEST: SET VALUE: DUPLICATE VALUE: THROWS EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Attempts to insert a <see cref="TrackedRecord{T}"/> with the same <see cref="TrackedRecord{T}.Key"/> as an existing
/// value, and confirms that the expected <see cref="ArgumentException"/> is thrown.
/// </summary>
[Fact]
public void SetValue_DuplicateValue_ThrowsException() {
var topic = new Topic("Test", "Container");
topic.Attributes.Add(new("Test", "Original"));
Assert.Throws<ArgumentException>(() =>
topic.Attributes.Add(new("Test", "New"))
);
}
/*==========================================================================================================================
| TEST: ADD: VALID ATTRIBUTE RECORD: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a custom attribute on a topic by directly adding an <see cref="AttributeRecord"/> instance; ensures it can be
/// retrieved.
/// </summary>
[Fact]
public void Add_ValidAttributeRecord_IsReturned() {
var topic = new Topic("Test", "Container");
topic.Attributes.Add(new("View", "NewKey", false));
Assert.Equal("NewKey", topic.View);
}
/*==========================================================================================================================
| TEST: ADD: NUMERIC VALUE WITH BUSINESS LOGIC: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a numeric attribute on a topic instance; ensures it is routed through the corresponding property and correctly
/// retrieved.
/// </summary>
[Fact]
public void Add_NumericValueWithBusinessLogic_IsReturned() {
var topic = new CustomTopic("Test", "Page");
topic.Attributes.SetInteger("NumericAttribute", 1);
Assert.Equal(1, topic.NumericAttribute);
}
/*==========================================================================================================================
| TEST: ADD: BOOLEAN VALUE WITH BUSINESS LOGIC: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a Boolean attribute on a topic instance; ensures it is routed through the corresponding property and correctly
/// retrieved.
/// </summary>
[Fact]
public void Add_BooleanValueWithBusinessLogic_IsReturned() {
var topic = new CustomTopic("Test", "Page");
topic.Attributes.SetBoolean("BooleanAttribute", true);
Assert.True(topic.BooleanAttribute);
}
/*==========================================================================================================================
| TEST: ADD: NUMERIC VALUE WITH BUSINESS LOGIC: THROWS EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a numeric attribute on a topic instance with an invalid value; ensures an exception is thrown.
/// </summary>
[Fact]
public void Add_NumericValueWithBusinessLogic_ThrowsException() {
var topic = new CustomTopic("Test", "Page");
Assert.Throws<ArgumentOutOfRangeException>(() =>
topic.Attributes.SetInteger("NumericAttribute", -1)
);
}
/*==========================================================================================================================
| TEST: ADD: DATE/TIME VALUE WITH BUSINESS LOGIC: IS RETURNED
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a Date/Time attribute on a topic instance; ensures it is routed through the corresponding property and correctly
/// retrieved.
/// </summary>
[Fact]
public void Add_DateTimeValueWithBusinessLogic_IsReturned() {
var topic = new CustomTopic("Test", "Page");
var dateTime = new DateTime(2021, 1, 5);
topic.Attributes.SetDateTime("DateTimeAttribute", dateTime);
Assert.Equal(dateTime, topic.DateTimeAttribute);
}
/*==========================================================================================================================
| TEST: ADD: DATE/TIME VALUE WITH BUSINESS LOGIC: THROWS EXCEPTION
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Sets a <see cref="DateTime"/> attribute on a topic instance with an invalid value; ensures an exception is thrown.
/// </summary>
[Fact]
public void Add_DateTimeValueWithBusinessLogic_ThrowsException() {
var topic = new CustomTopic("Test", "Page");
Assert.Throws<ArgumentOutOfRangeException>(() =>
topic.Attributes.SetDateTime("DateTimeAttribute", DateTime.MinValue)
);
}
/*==========================================================================================================================
| TEST: ATTRIBUTE RECORD: LAST MODIFIED: DEFAULT VALUE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Creates a new <see cref="AttributeRecord"/> and ensures that the <see cref="TrackedRecord{T}.LastModified"/> is set to
/// <see cref="DateTime.UtcNow"/>.
/// </summary>
[Fact]
public void AttributeRecord_LastModified_DefaultValue() {