File: DbPropertyValuesTests.cs

package info (click to toggle)
mono 3.2.8%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 527,964 kB
  • ctags: 623,327
  • sloc: cs: 3,938,236; xml: 1,891,753; ansic: 418,737; java: 59,920; sh: 15,754; makefile: 11,067; sql: 7,956; perl: 2,279; cpp: 1,380; yacc: 1,203; python: 594; asm: 422; sed: 16; php: 1
file content (1296 lines) | stat: -rw-r--r-- 55,389 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace ProductivityApiUnitTests
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Internal;
    using System.Data.Entity.Resources;
    using System.Linq;
    using System.Reflection;
    using Moq;
    using Xunit;

    /// <summary>
    ///     General unit tests for DbPropertyValues and related classes/methods.
    ///     Some specific features, such as concurrency, are tested elsewhere.
    /// </summary>
    public class DbPropertyValuesTests : TestBase
    {
        #region Helper classes

        private const BindingFlags PropertyBindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

        /// <summary>
        ///     A type with a variety of different types of properties used for the tests in this class.
        /// </summary>
        public class FakeTypeWithProps
        {
            public FakeTypeWithProps()
            {
                PublicNonNullStringProp = "NonNullValue";
            }

            public FakeTypeWithProps(
                int id, string publicStringProp, string privateStringProp,
                int publicIntProp, int privateIntProp,
                int publicIntPropWithPrivateSetter, int publicIntPropWithPrivateGetter,
                byte[] publicBinaryProp)
                : this()
            {
                Id = id;
                PublicStringProp = publicStringProp;
                PrivateStringProp = privateStringProp;
                PublicIntProp = publicIntProp;
                PrivateIntProp = privateIntProp;
                PublicIntPropWithPrivateSetter = publicIntPropWithPrivateSetter;
                PublicIntPropWithPrivateGetter = publicIntPropWithPrivateGetter;
                PublicBinaryProp = publicBinaryProp;
            }

            public virtual int Id { get; set; }

            public virtual string PublicStringProp { get; set; }
            public virtual string PublicNonNullStringProp { get; set; }
            private string PrivateStringProp { get; set; }

            public virtual string PublicReadonlyStringProp
            {
                get { return "PublicReadonlyStringProp"; }
            }

            private string PrivateReadonlyStringProp
            {
                get { return "PrivateReadonlyStringProp"; }
            }

            public virtual string PublicWriteOnlyStringProp
            {
                set { }
            }

            private string PrivateWriteOnlyStringProp
            {
                set { }
            }

            public virtual int PublicIntProp { get; set; }
            private int PrivateIntProp { get; set; }

            public virtual int PublicIntPropWithPrivateSetter { get; private set; }
            public virtual int PublicIntPropWithPrivateGetter { private get; set; }

            public virtual byte[] PublicBinaryProp { get; set; }

            public virtual FakeTypeWithProps NestedObject { get; set; }
        }

        public class FakeDerivedTypeWithProps : FakeTypeWithProps
        {
        }

        #endregion

        #region Tests for copying values to an object

        [Fact]
        public void Non_Generic_DbPropertyValues_uses_ToObject_on_InternalPropertyValues()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 }
                                 };
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(properties));

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Equal(1, clone.Id);
        }

        [Fact]
        public void ToObject_for_entity_uses_CreateObject_to_return_instance_of_correct_type()
        {
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(null, isEntityValues: true);
            values.MockInternalContext.Setup(c => c.CreateObject(typeof(FakeTypeWithProps))).Returns(new FakeDerivedTypeWithProps());

            var clone = values.ToObject();

            Assert.IsType<FakeDerivedTypeWithProps>(clone);
        }

        [Fact]
        public void ToObject_for_complex_object_does_not_use_CreateObject_to_create_instance()
        {
            var values = new TestInternalPropertyValues<FakeTypeWithProps>();

            var clone = values.ToObject();

            values.MockInternalContext.Verify(c => c.CreateObject(typeof(FakeTypeWithProps)), Times.Never());
            Assert.IsType<FakeTypeWithProps>(clone);
        }

        [Fact]
        public void ToObject_sets_all_properties_from_the_dictionary_onto_the_object_including_those_with_private_setters()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "PublicStringProp", "PublicStringPropValue" },
                                     { "PrivateStringProp", "PrivateStringPropValue" },
                                     { "PublicIntProp", 2 },
                                     { "PrivateIntProp", 3 },
                                     { "PublicIntPropWithPrivateSetter", 4 },
                                     { "PublicIntPropWithPrivateGetter", 5 },
                                     { "PublicBinaryProp", new byte[] { 3, 1, 4, 1, 5, 9 } },
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Equal(1, clone.Id);
            Assert.Equal("PublicStringPropValue", clone.PublicStringProp);
            Assert.Equal(2, clone.PublicIntProp);
            Assert.Equal(
                "PrivateStringPropValue",
                typeof(FakeTypeWithProps).GetProperty("PrivateStringProp", PropertyBindingFlags).GetValue(clone, null));
            Assert.Equal(3, typeof(FakeTypeWithProps).GetProperty("PrivateIntProp", PropertyBindingFlags).GetValue(clone, null));
            Assert.Equal(4, clone.PublicIntPropWithPrivateSetter);
            Assert.Equal(
                5, typeof(FakeTypeWithProps).GetProperty("PublicIntPropWithPrivateGetter", PropertyBindingFlags).GetValue(clone, null));
            Assert.True(DbHelpers.KeyValuesEqual(new byte[] { 3, 1, 4, 1, 5, 9 }, clone.PublicBinaryProp));
        }

        [Fact]
        public void ToObject_ignores_properties_that_are_conceptually_in_shadow_state()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "MissingProp", "MissingPropValue" }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Equal(1, clone.Id);
        }

        [Fact]
        public void ToObject_ignores_properties_that_are_readonly()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "PublicReadonlyStringProp", "Foo" },
                                     { "PrivateReadonlyStringProp", "Foo" }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Equal(1, clone.Id);
            Assert.Equal("PublicReadonlyStringProp", clone.PublicReadonlyStringProp);
            Assert.Equal(
                "PrivateReadonlyStringProp",
                typeof(FakeTypeWithProps).GetProperty("PrivateReadonlyStringProp", PropertyBindingFlags).GetValue(clone, null));
        }

        [Fact]
        public void ToObject_can_set_reference_propeties_to_null()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "PublicNonNullStringProp", null }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Null(clone.PublicNonNullStringProp);
        }

        [Fact]
        public void ToObject_returns_nested_property_dictionary_as_cloned_object()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 1 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Equal(1, clone.NestedObject.Id);
        }

        [Fact]
        public void ToObject_ignores_null_nested_property_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "NestedObject", null }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var clone = (FakeTypeWithProps)values.ToObject();

            Assert.Equal(1, clone.Id);
            Assert.Null(clone.NestedObject);
        }

        [Fact]
        public void ToObject_throws_when_trying_to_set_null_values_onto_non_nullable_properties()
        {
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(
                new Dictionary<string, object>
                    {
                        { "Id", 1 }
                    });
            values["Id"] = null;

            Assert.Equal(
                Strings.DbPropertyValues_CannotSetNullValue("Id", "Int32", "FakeTypeWithProps"),
                Assert.Throws<InvalidOperationException>(() => values.ToObject()).Message);
        }

        #endregion

        #region Tests for setting values from an object

        [Fact]
        public void Attempt_to_copy_values_from_null_object_throws()
        {
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>());

            Assert.Equal("obj", Assert.Throws<ArgumentNullException>(() => values.SetValues((object)null)).ParamName);
        }

        [Fact]
        public void SetValues_copies_values_from_object_to_property_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "PublicStringProp", null },
                                     { "PrivateStringProp", null },
                                     { "PublicIntProp", 0 },
                                     { "PrivateIntProp", 0 },
                                     { "PublicIntPropWithPrivateSetter", 0 },
                                     { "PublicIntPropWithPrivateGetter", 0 },
                                     { "PublicBinaryProp", null },
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var obj = new FakeTypeWithProps(
                1, "PublicStringPropValue", "PrivateStringPropValue", 2, 3, 4, 5, new byte[] { 3, 1, 4, 1, 5, 9 });

            values.SetValues(obj);

            Assert.Equal(1, values["Id"]);
            Assert.Equal("PublicStringPropValue", values["PublicStringProp"]);
            Assert.Equal(2, values["PublicIntProp"]);
            Assert.Equal("PrivateStringPropValue", values["PrivateStringProp"]);
            Assert.Equal(3, values["PrivateIntProp"]);
            Assert.Equal(4, values["PublicIntPropWithPrivateSetter"]);
            Assert.Equal(5, values["PublicIntPropWithPrivateGetter"]);
            Assert.True(DbHelpers.KeyValuesEqual(new byte[] { 3, 1, 4, 1, 5, 9 }, values["PublicBinaryProp"]));
        }

        [Fact]
        public void Attempt_to_copy_values_from_object_of_differnt_type_copies_no_properties_if_no_properties_match()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "PublicStringProp", null },
                                     { "PrivateStringProp", null },
                                     { "PublicIntProp", 0 },
                                     { "PrivateIntProp", 0 },
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            values.SetValues("Bang!");

            Assert.Null(values["PublicStringProp"]);
            Assert.Equal(0, values["PublicIntProp"]);
            Assert.Null(values["PrivateStringProp"]);
            Assert.Equal(0, values["PrivateIntProp"]);
        }

        public class FakeTypeWithSomeProps
        {
            public FakeTypeWithSomeProps(string publicStringProp, int privateIntProp, int someOtherIntProp)
            {
                PublicStringProp = publicStringProp;
                PrivateIntProp = privateIntProp;
                SomeOtherIntProp = someOtherIntProp;
            }

            public string PublicStringProp { get; set; }
            private int PrivateIntProp { get; set; }
            public int SomeOtherIntProp { get; set; }
        }

        [Fact]
        public void Attempt_to_copy_values_from_object_of_differnt_type_copies_only_properties_that_match()
        {
            Attempt_to_copy_values_from_object_of_differnt_type_copies_only_properties_that_match_implementation(
                new FakeTypeWithSomeProps("PublicStringPropValue", 3, 4));
        }

        [Fact]
        public void Attempt_to_copy_values_from_anonymous_object_copies_only_properties_that_match()
        {
            var obj = new
                          {
                              PublicStringProp = "PublicStringPropValue",
                              PrivateIntProp = 3,
                              SomeDifferentIntProp = 4
                          };
            Attempt_to_copy_values_from_object_of_differnt_type_copies_only_properties_that_match_implementation(obj);
        }

        private void Attempt_to_copy_values_from_object_of_differnt_type_copies_only_properties_that_match_implementation(object obj)
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "PublicStringProp", null },
                                     { "PrivateStringProp", null },
                                     { "PublicIntProp", 0 },
                                     { "PrivateIntProp", 0 },
                                     { "PublicIntPropWithPrivateSetter", 0 },
                                     { "PublicIntPropWithPrivateGetter", 0 },
                                     { "PublicBinaryProp", null },
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            values.SetValues(obj);

            Assert.Equal(0, values["Id"]);
            Assert.Equal("PublicStringPropValue", values["PublicStringProp"]);
            Assert.Equal(0, values["PublicIntProp"]);
            Assert.Null(values["PrivateStringProp"]);
            Assert.Equal(3, values["PrivateIntProp"]);
            Assert.Equal(0, values["PublicIntPropWithPrivateSetter"]);
            Assert.Equal(0, values["PublicIntPropWithPrivateGetter"]);
            Assert.Null(values["PublicBinaryProp"]);
        }

        [Fact]
        public void Non_Generic_DbPropertyValues_SetValues_works_on_the_underlying_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 }
                                 };
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(properties));

            values.SetValues(
                new FakeTypeWithProps
                    {
                        Id = 1
                    });

            Assert.Equal(1, values["Id"]);
        }

        [Fact]
        public void Calling_SetValues_with_instance_of_derived_type_works()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 }
                                 };
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(properties));

            values.SetValues(
                new FakeDerivedTypeWithProps
                    {
                        Id = 1
                    });

            Assert.Equal(1, values["Id"]);
        }

        [Fact]
        public void SetValues_ignores_properties_that_are_conceptually_in_shadow_state()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "MissingProp", "MissingPropValue" }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            values.SetValues(
                new FakeTypeWithProps
                    {
                        Id = 1
                    });

            Assert.Equal(1, values["Id"]);
            Assert.Equal("MissingPropValue", values["MissingProp"]);
        }

        [Fact]
        public void SetValues_ignores_properties_that_are_write_only()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "PublicWriteOnlyStringProp", "Foo" },
                                     { "PrivateWriteOnlyStringProp", "Bar" }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            values.SetValues(
                new FakeTypeWithProps
                    {
                        Id = 1
                    });

            Assert.Equal(1, values["Id"]);
            Assert.Equal("Foo", values["PublicWriteOnlyStringProp"]);
            Assert.Equal("Bar", values["PrivateWriteOnlyStringProp"]);
        }

        [Fact]
        public void SetValues_can_set_reference_propeties_to_null()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "PublicStringProp", "NonNull" }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            values.SetValues(
                new FakeTypeWithProps
                    {
                        Id = 1
                    });

            Assert.Equal(1, values["Id"]);
            Assert.Null(values["PublicStringProp"]);
        }

        [Fact]
        public void SetValues_sets_values_from_complex_object_into_nested_property_dictionary()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 0 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            values.SetValues(
                new FakeTypeWithProps
                    {
                        Id = 1,
                        NestedObject = new FakeTypeWithProps
                                           {
                                               Id = 2
                                           }
                    });

            Assert.Equal(1, values["Id"]);
            Assert.Equal(2, nestedValues["Id"]);
        }

        [Fact]
        public void SetValues_when_complex_object_is_null_throws()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 0 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var obj = new FakeTypeWithProps
                          {
                              Id = 1,
                              NestedObject = null
                          };

            Assert.Equal(
                Strings.DbPropertyValues_ComplexObjectCannotBeNull("NestedObject", typeof(FakeTypeWithProps).Name),
                Assert.Throws<InvalidOperationException>(() => values.SetValues(obj)).Message);
        }

        [Fact]
        public void SetValues_when_nested_property_dictionary_in_source_is_null_throws()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "NestedObject", null }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var obj = new FakeTypeWithProps
                          {
                              Id = 1,
                              NestedObject = new FakeTypeWithProps
                                                 {
                                                     Id = 2
                                                 }
                          };

            Assert.Equal(
                Strings.DbPropertyValues_NestedPropertyValuesNull("NestedObject", typeof(FakeTypeWithProps).Name),
                Assert.Throws<InvalidOperationException>(() => values.SetValues(obj)).Message);
        }

        [Fact]
        public void SetValues_does_not_attempt_to_set_values_that_are_not_different()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "PublicStringProp", "PublicStringPropValue" },
                                     { "PublicIntProp", 2 },
                                     { "PublicBinaryProp", new byte[] { 3, 1, 4, 1, 5, 9 } },
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var obj = new FakeTypeWithProps
                          {
                              PublicStringProp = "PublicStringPropValue",
                              PublicIntProp = 2,
                              PublicBinaryProp = new byte[] { 3, 1, 4, 1, 5, 9 }
                          };

            values.SetValues(obj);

            values.GetMockItem("PublicStringProp").VerifySet(i => i.Value = It.IsAny<object>(), Times.Never());
            values.GetMockItem("PublicIntProp").VerifySet(i => i.Value = It.IsAny<object>(), Times.Never());
            values.GetMockItem("PublicBinaryProp").VerifySet(i => i.Value = It.IsAny<object>(), Times.Never());
        }

        #endregion

        #region Tests for copying values to a new dictionary

        [Fact]
        public void Non_Generic_DbPropertyValues_uses_Clone_on_InternalPropertyValues()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 }
                                 };
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(properties));

            var clone = values.Clone();

            Assert.Equal(1, clone["Id"]);
        }

        [Fact]
        public void Clone_for_an_entity_returns_a_new_dictionary_that_is_also_for_an_entity()
        {
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(null, isEntityValues: true);

            var clone = values.Clone();

            Assert.True(clone.IsEntityValues);
        }

        [Fact]
        public void Clone_for_a_complex_object_returns_a_new_dictionary_that_is_also_for_an_complex_object()
        {
            var values = new TestInternalPropertyValues<FakeTypeWithProps>();

            var clone = values.Clone();

            Assert.False(clone.IsEntityValues);
        }

        [Fact]
        public void Clone_sets_all_properties_from_the_dictionary_into_the_new_values()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "PublicStringProp", "PublicStringPropValue" },
                                     { "PrivateStringProp", "PrivateStringPropValue" },
                                     { "PublicIntProp", 2 },
                                     { "PrivateIntProp", 3 },
                                     { "PublicIntPropWithPrivateSetter", 4 },
                                     { "PublicIntPropWithPrivateGetter", 5 },
                                     { "PublicBinaryProp", new byte[] { 3, 1, 4, 1, 5, 9 } },
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var clone = values.Clone();

            Assert.Equal(1, clone["Id"]);
            Assert.Equal("PublicStringPropValue", clone["PublicStringProp"]);
            Assert.Equal(2, clone["PublicIntProp"]);
            Assert.Equal("PrivateStringPropValue", clone["PrivateStringProp"]);
            Assert.Equal(3, clone["PrivateIntProp"]);
            Assert.Equal(4, clone["PublicIntPropWithPrivateSetter"]);
            Assert.Equal(5, clone["PublicIntPropWithPrivateGetter"]);
            Assert.True(DbHelpers.KeyValuesEqual(new byte[] { 3, 1, 4, 1, 5, 9 }, clone["PublicBinaryProp"]));
        }

        [Fact]
        public void Clone_can_copy_null_properties()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "PublicNonNullStringProp", null }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var clone = values.Clone();

            Assert.Null(clone["PublicNonNullStringProp"]);
        }

        [Fact]
        public void Clone_clones_nested_property_dictionary_into_new_cloned_nested_dictionary()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 2 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var clone = values.Clone();
            var nestedClone = (InternalPropertyValues)clone["NestedObject"];

            Assert.Equal(1, clone["Id"]);
            Assert.Equal(2, nestedClone["Id"]);

            Assert.False(clone.GetItem("Id").IsComplex);
            Assert.True(clone.GetItem("NestedObject").IsComplex);
            Assert.False(nestedClone.GetItem("Id").IsComplex);
        }

        [Fact]
        public void Clone_ignores_null_nested_property_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "NestedObject", null }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var clone = values.Clone();

            Assert.Equal(1, clone["Id"]);
            Assert.Null(clone["NestedObject"]);
        }

        [Fact]
        public void Modifying_properties_on_cloned_dictionary_does_not_change_properties_on_original_dictionary_and_vice_versa()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 2 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 },
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var clone = values.Clone();
            var nestedClone = (InternalPropertyValues)clone["NestedObject"];

            values["Id"] = -1;
            nestedValues["Id"] = -2;
            clone["Id"] = -3;
            nestedClone["Id"] = -4;

            Assert.Equal(-1, values["Id"]);
            Assert.Equal(-2, nestedValues["Id"]);
            Assert.Equal(-3, clone["Id"]);
            Assert.Equal(-4, nestedClone["Id"]);
        }

        #endregion

        #region Tests for setting values from another dictionary

        [Fact]
        public void Attempt_to_copy_values_from_null_dictionary_on_non_generic_DbPropertyValues_throws()
        {
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>());

            Assert.Equal("propertyValues", Assert.Throws<ArgumentNullException>(() => values.SetValues(null)).ParamName);
        }

        [Fact]
        public void Attempt_to_copy_values_from_dictionary_of_wrong_type_throws()
        {
            var values1 = new TestInternalPropertyValues<FakeDerivedTypeWithProps>();
            var values2 = new TestInternalPropertyValues<FakeTypeWithProps>();

            Assert.Equal(
                Strings.DbPropertyValues_AttemptToSetValuesFromWrongType(
                    typeof(FakeTypeWithProps).Name, typeof(FakeDerivedTypeWithProps).Name),
                Assert.Throws<ArgumentException>(() => values1.SetValues(values2)).Message);
        }

        [Fact]
        public void SetValues_copies_values_from_one_dictionary_to_another_dictionary()
        {
            var fromValues = CreateSimpleValues();
            fromValues["Id"] = 1;
            fromValues["PublicStringProp"] = "PublicStringPropValue";
            fromValues["PrivateStringProp"] = "PrivateStringPropValue";
            fromValues["PublicIntProp"] = 2;
            fromValues["PrivateIntProp"] = 3;
            fromValues["PublicIntPropWithPrivateSetter"] = 4;
            fromValues["PublicIntPropWithPrivateGetter"] = 5;
            fromValues["PublicBinaryProp"] = new byte[] { 3, 1, 4, 1, 5, 9 };

            var toValues = CreateSimpleValues();

            toValues.SetValues(fromValues);

            Assert.Equal(1, toValues["Id"]);
            Assert.Equal("PublicStringPropValue", toValues["PublicStringProp"]);
            Assert.Equal(2, toValues["PublicIntProp"]);
            Assert.Equal("PrivateStringPropValue", toValues["PrivateStringProp"]);
            Assert.Equal(3, toValues["PrivateIntProp"]);
            Assert.Equal(4, toValues["PublicIntPropWithPrivateSetter"]);
            Assert.Equal(5, toValues["PublicIntPropWithPrivateGetter"]);
            Assert.True(DbHelpers.KeyValuesEqual(new byte[] { 3, 1, 4, 1, 5, 9 }, toValues["PublicBinaryProp"]));
        }

        private TestInternalPropertyValues<FakeTypeWithProps> CreateSimpleValues()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 0 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 0 },
                                     { "PublicStringProp", null },
                                     { "PrivateStringProp", null },
                                     { "PublicIntProp", 0 },
                                     { "PrivateIntProp", 0 },
                                     { "PublicIntPropWithPrivateSetter", 0 },
                                     { "PublicIntPropWithPrivateGetter", 0 },
                                     { "PublicBinaryProp", null },
                                     { "NestedObject", nestedValues },
                                 };
            return new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });
        }

        [Fact]
        public void Non_Generic_DbPropertyValues_SetValues_with_a_dictionary_works_on_the_underlying_dictionary()
        {
            var fromValues = new DbPropertyValues(CreateSimpleValues());
            fromValues["Id"] = 1;

            var toValues = new DbPropertyValues(CreateSimpleValues());

            toValues.SetValues(fromValues);

            Assert.Equal(1, toValues["Id"]);
        }

        [Fact]
        public void Calling_SetValues_with_dictionary_of_derived_type_works()
        {
            var fromProperties = new Dictionary<string, object>
                                     {
                                         { "Id", 1 }
                                     };
            var fromValues = new DbPropertyValues(new TestInternalPropertyValues<FakeDerivedTypeWithProps>(fromProperties));

            var toProperties = new Dictionary<string, object>
                                   {
                                       { "Id", 0 }
                                   };
            var toValues = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(toProperties));

            toValues.SetValues(fromValues);

            Assert.Equal(1, toValues["Id"]);
        }

        [Fact]
        public void SetValues_with_dictionary_works_with_null_values()
        {
            var fromValues = CreateSimpleValues();
            fromValues["Id"] = 1;
            var toValues = CreateSimpleValues();
            toValues["PublicStringProp"] = "Non-null";

            toValues.SetValues(fromValues);

            Assert.Equal(1, toValues["Id"]);
            Assert.Null(toValues["PublicStringProp"]);
        }

        [Fact]
        public void SetValues_sets_nested_values_from_the_dictionary_into_nested_property_dictionary()
        {
            var fromValues = CreateSimpleValues();
            fromValues["Id"] = 1;
            ((InternalPropertyValues)fromValues["NestedObject"])["Id"] = 2;

            var toValues = CreateSimpleValues();

            toValues.SetValues(fromValues);

            Assert.Equal(1, toValues["Id"]);
            Assert.Equal(2, ((InternalPropertyValues)toValues["NestedObject"])["Id"]);
        }

        [Fact]
        public void SetValues_when_nested_dictionary_is_for_wrong_type_throws()
        {
            var fromProperties = new Dictionary<string, object>
                                     {
                                         { "Id", 1 }
                                     };
            var fromValues = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(fromProperties));

            var toProperties = new Dictionary<string, object>
                                   {
                                       { "Id", 0 }
                                   };
            var toValues = new DbPropertyValues(new TestInternalPropertyValues<FakeDerivedTypeWithProps>(toProperties));

            Assert.Equal(
                Strings.DbPropertyValues_AttemptToSetValuesFromWrongType(
                    typeof(FakeTypeWithProps).Name, typeof(FakeDerivedTypeWithProps).Name),
                Assert.Throws<ArgumentException>(() => toValues.SetValues(fromValues)).Message);
        }

        [Fact]
        public void SetValues_when_nested_dictionary_in_source_is_null_throws()
        {
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>();

            var fromProperties = new Dictionary<string, object>
                                     {
                                         { "NestedObject", null }
                                     };
            var fromValues = new TestInternalPropertyValues<FakeTypeWithProps>(fromProperties, new[] { "NestedObject" });

            var toProperties = new Dictionary<string, object>
                                   {
                                       { "NestedObject", nestedValues }
                                   };
            var toValues = new TestInternalPropertyValues<FakeTypeWithProps>(toProperties, new[] { "NestedObject" });

            Assert.Equal(
                Strings.DbPropertyValues_NestedPropertyValuesNull("NestedObject", typeof(FakeTypeWithProps).Name),
                Assert.Throws<InvalidOperationException>(() => toValues.SetValues(fromValues)).Message);
        }

        [Fact]
        public void SetValues_when_nested_dictionary_in_target_is_null_throws()
        {
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>();

            var fromProperties = new Dictionary<string, object>
                                     {
                                         { "NestedObject", nestedValues }
                                     };
            var fromValues = new TestInternalPropertyValues<FakeTypeWithProps>(fromProperties, new[] { "NestedObject" });

            var toProperties = new Dictionary<string, object>
                                   {
                                       { "NestedObject", null }
                                   };
            var toValues = new TestInternalPropertyValues<FakeTypeWithProps>(toProperties, new[] { "NestedObject" });

            Assert.Equal(
                Strings.DbPropertyValues_NestedPropertyValuesNull("NestedObject", typeof(FakeTypeWithProps).Name),
                Assert.Throws<InvalidOperationException>(() => toValues.SetValues(fromValues)).Message);
        }

        [Fact]
        public void SetValues_from_another_dictionary_does_not_attempt_to_set_values_that_are_not_different()
        {
            var fromValues = CreateSimpleValues();
            var toValues = CreateSimpleValues();

            toValues.SetValues(fromValues);

            toValues.GetMockItem("PublicStringProp").VerifySet(i => i.Value = It.IsAny<object>(), Times.Never());
            toValues.GetMockItem("PublicIntProp").VerifySet(i => i.Value = It.IsAny<object>(), Times.Never());
            toValues.GetMockItem("PublicBinaryProp").VerifySet(i => i.Value = It.IsAny<object>(), Times.Never());
        }

        #endregion

        #region Tests for reading values from dictionarys

        [Fact]
        public void Scalar_values_can_be_read_from_a_property_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            Assert.Equal(1, values["Id"]);
        }

        [Fact]
        public void Complex_values_can_be_read_from_a_property_dictionary()
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 1 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var readValues = (InternalPropertyValues)values["NestedObject"];
            Assert.Equal(1, readValues["Id"]);
        }

        [Fact]
        public void Reading_values_from_non_generic_DbPropertyValues_uses_the_internal_dictionary_and_returns_a_non_generic_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 }
                                 };
            var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(properties));

            Assert.Equal(1, values["Id"]);
        }

        [Fact]
        public void Reading_value_from_non_generic_DbPropertyValues_for_a_null_property_name_throws()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("propertyName"),
                Assert.Throws<ArgumentException>(() => { var _ = values[null]; }).Message);
        }

        [Fact]
        public void Reading_value_from_non_generic_DbPropertyValues_for_an_empty_property_name_throws()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("propertyName"), Assert.Throws<ArgumentException>(() => { var _ = values[""]; }).Message);
        }

        [Fact]
        public void Reading_value_from_non_generic_DbPropertyValues_for_a_whitespace_property_name_throws()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("propertyName"), Assert.Throws<ArgumentException>(() => { var _ = values[" "]; }).Message);
        }

        [Fact]
        public void Reading_value_for_a_missing_property_name_throws()
        {
            var values = CreateSimpleValues();

            Assert.Equal(
                Strings.DbPropertyValues_PropertyDoesNotExist("NoSuchProperty", typeof(FakeTypeWithProps).Name),
                Assert.Throws<ArgumentException>(() => { var _ = values["NoSuchProperty"]; }).Message);
        }

        #endregion

        #region Tests for writing values into property values

        [Fact]
        public void Scalar_values_can_be_set_into_a_property_dictionary()
        {
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(
                new Dictionary<string, object>
                    {
                        { "Id", 0 }
                    });

            values["Id"] = 1;

            Assert.Equal(1, values["Id"]);
        }

        [Fact]
        public void Complex_values_can_be_set_at_the_complex_object_level_into_a_nested_property_dictionary()
        {
            SettingNestedValuesTest((values, nestedValues) => values["NestedObject"] = nestedValues);
        }

        [Fact]
        public void Complex_values_can_be_set_at_the_complex_object_level_into_a_non_generic_dictionary_using_a_generic_dictionary()
        {
            SettingNestedValuesTest(
                (values, nestedValues) => new DbPropertyValues(values)["NestedObject"] = new DbPropertyValues(nestedValues));
        }

        [Fact]
        public void Complex_values_can_be_set_at_the_complex_object_level_into_a_non_generic_dictionary_using_a_non_generic_dictionary()
        {
            SettingNestedValuesTest(
                (values, nestedValues) => new DbPropertyValues(values)["NestedObject"] = new DbPropertyValues(nestedValues));
        }

        private void SettingNestedValuesTest(Action<InternalPropertyValues, InternalPropertyValues> bagger)
        {
            var nestedProperties = new Dictionary<string, object>
                                       {
                                           { "Id", 0 }
                                       };
            var nestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(nestedProperties);

            var properties = new Dictionary<string, object>
                                 {
                                     { "NestedObject", nestedValues }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties, new[] { "NestedObject" });

            var newNestedProperties = new Dictionary<string, object>
                                          {
                                              { "Id", 1 }
                                          };
            var newNestedValues = new TestInternalPropertyValues<FakeTypeWithProps>(newNestedProperties);

            bagger(values, newNestedValues);

            var readValues = (InternalPropertyValues)values["NestedObject"];

            Assert.Same(nestedValues, readValues); // The nested dictionary itself has not changed
            Assert.Equal(1, readValues["Id"]); // But the values in the dictionary have been copied.
        }

        [Fact]
        public void Complex_values_cannot_be_set_to_null_in_a_property_dictionary()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.DbPropertyValues_AttemptToSetNonValuesOnComplexProperty,
                Assert.Throws<ArgumentException>(() => values["NestedObject"] = null).Message);
        }

        [Fact]
        public void Complex_values_cannot_be_set_to_actual_complex_object_instance_in_a_property_dictionary()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.DbPropertyValues_AttemptToSetNonValuesOnComplexProperty,
                Assert.Throws<ArgumentException>(() => values["NestedObject"] = new FakeTypeWithProps()).Message);
        }

        [Fact]
        public void Writing_values_to_non_generic_DbPropertyValues_uses_the_internal_dictionary()
        {
            var values = new DbPropertyValues(
                new TestInternalPropertyValues<FakeTypeWithProps>(
                    new Dictionary<string, object>
                        {
                            { "Id", 0 }
                        }));

            values["Id"] = 1;

            Assert.Equal(1, values["Id"]);
        }

        [Fact]
        public void Writing_values_to_non_generic_DbPropertyValues_for_a_null_property_name_throws()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("propertyName"), Assert.Throws<ArgumentException>(() => values[null] = 0).Message);
        }

        [Fact]
        public void Writing_values_to_non_generic_DbPropertyValues_for_an_empty_property_name_throws()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(Strings.ArgumentIsNullOrWhitespace("propertyName"), Assert.Throws<ArgumentException>(() => values[""] = 0).Message);
        }

        [Fact]
        public void Writing_values_to_non_generic_DbPropertyValues_for_a_whitespace_property_name_throws()
        {
            var values = new DbPropertyValues(CreateSimpleValues());

            Assert.Equal(
                Strings.ArgumentIsNullOrWhitespace("propertyName"), Assert.Throws<ArgumentException>(() => values[" "] = 0).Message);
        }

        [Fact]
        public void Writing_values_to_a_missing_property_name_throws()
        {
            var values = CreateSimpleValues();

            Assert.Equal(
                Strings.DbPropertyValues_PropertyDoesNotExist("NoSuchProperty", typeof(FakeTypeWithProps).Name),
                Assert.Throws<ArgumentException>(() => values["NoSuchProperty"] = 0).Message);
        }

        #endregion

        #region Tests for reading property names from a dictionary

        [Fact]
        public void Property_names_can_be_read_from_a_property_dictionary()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "One", 1 },
                                     { "Two", 2 },
                                     { "Three", 3 }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var names = values.PropertyNames;

            Assert.Equal(3, names.Count);
            Assert.True(names.Contains("One"));
            Assert.True(names.Contains("Two"));
            Assert.True(names.Contains("Three"));
        }

        [Fact]
        public void Property_names_are_returned_as_a_readonly_set()
        {
            var properties = new Dictionary<string, object>
                                 {
                                     { "Id", 1 }
                                 };
            var values = new TestInternalPropertyValues<FakeTypeWithProps>(properties);

            var names = values.PropertyNames;

            Assert.True(names.IsReadOnly);
        }

        [Fact]
        public void ReadOnlyHashSet_is_readonly_and_throws_for_mutating_methods()
        {
            var set = new ReadOnlySet<string>(new HashSet<string>());

            Assert.True(set.IsReadOnly);

            Assert.Equal(
                Strings.DbPropertyValues_PropertyValueNamesAreReadonly,
                Assert.Throws<NotSupportedException>(() => ((ICollection<string>)set).Add("")).Message);
            Assert.Equal(
                Strings.DbPropertyValues_PropertyValueNamesAreReadonly, Assert.Throws<NotSupportedException>(() => set.Add("")).Message);
            Assert.Equal(
                Strings.DbPropertyValues_PropertyValueNamesAreReadonly, Assert.Throws<NotSupportedException>(() => set.Clear()).Message);
            Assert.Equal(
                Strings.DbPropertyValues_PropertyValueNamesAreReadonly, Assert.Throws<NotSupportedException>(() => set.Remove("")).Message);
        }

        [Fact]
        public void ReadOnlyHashSet_calls_underlying_Set_methods()
        {
            var mockSet = new Mock<ISet<string>>();
            var mockIEnumerable = mockSet.As<IEnumerable>();
            var set = new ReadOnlySet<string>(mockSet.Object);
            var other = new HashSet<string>();

            var array = new string[0];
            set.CopyTo(array, 0);
            mockSet.Verify(s => s.CopyTo(array, 0), Times.Once());

            set.ExceptWith(other);
            mockSet.Verify(s => s.ExceptWith(other), Times.Once());

            set.IntersectWith(other);
            mockSet.Verify(s => s.IntersectWith(other), Times.Once());

            set.IsProperSubsetOf(other);
            mockSet.Verify(s => s.IsProperSubsetOf(other), Times.Once());

            set.IsProperSupersetOf(other);
            mockSet.Verify(s => s.IsProperSupersetOf(other), Times.Once());

            set.IsSubsetOf(other);
            mockSet.Verify(s => s.IsSubsetOf(other), Times.Once());

            set.IsSupersetOf(other);
            mockSet.Verify(s => s.IsSupersetOf(other), Times.Once());

            set.Overlaps(other);
            mockSet.Verify(s => s.Overlaps(other), Times.Once());

            set.SetEquals(other);
            mockSet.Verify(s => s.SetEquals(other), Times.Once());

            set.SymmetricExceptWith(other);
            mockSet.Verify(s => s.SymmetricExceptWith(other), Times.Once());

            set.UnionWith(other);
            mockSet.Verify(s => s.UnionWith(other), Times.Once());

            set.Contains("Foo");
            mockSet.Verify(s => s.Contains("Foo"), Times.Once());

            var _ = set.Count;
            mockSet.Verify(s => s.Count, Times.Exactly(1));

            mockSet.Setup(s => s.GetEnumerator()).Returns(Enumerable.Empty<string>().GetEnumerator());
            set.GetEnumerator();
            mockSet.Verify(s => s.GetEnumerator(), Times.Once());

            mockIEnumerable.Setup(s => s.GetEnumerator()).Returns(Enumerable.Empty<string>().GetEnumerator());
            ((IEnumerable)set).GetEnumerator();
            mockIEnumerable.Verify(s => s.GetEnumerator(), Times.Once());
        }

        #endregion
    }
}