File: DbCollectionEntryTests.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 (325 lines) | stat: -rw-r--r-- 15,843 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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.Infrastructure
{
    using System.Collections.Generic;
    using System.Data.Entity.Internal;
    using System.Data.Entity.ModelConfiguration.Internal.UnitTests;
    using System.Data.Entity.Resources;
    using System.Threading;
    using Moq;
    using Xunit;

    public class DbCollectionEntryTests
    {
        [Fact]
        public void NonGeneric_DbCollectionEntry_delegates_to_InternalReferenceEntry_correctly()
        {
            var v = new DbCollectionEntryVerifier();
            v.VerifyGetter(e => e.CurrentValue, m => m.CurrentValue);
            var currentValue = new object[0];
            v.VerifySetter(e => e.CurrentValue = currentValue, m => m.CurrentValue = currentValue);
            v.VerifyGetter(e => e.EntityEntry, m => m.InternalEntityEntry);
            v.VerifyGetter(e => e.IsLoaded, m => m.IsLoaded);
            v.VerifyGetter(e => e.Name, m => m.Name);
            v.VerifyMethod(e => e.GetValidationErrors(), m => m.GetValidationErrors());
            v.VerifyMethod(e => e.Load(), m => m.Load());
            v.VerifyMethod(e => e.Query(), m => m.Query());

#if !NET40
            v.VerifyMethod(e => e.LoadAsync(), m => m.LoadAsync(CancellationToken.None));
            v.VerifyMethod(e => e.LoadAsync(CancellationToken.None), m => m.LoadAsync(CancellationToken.None));
#endif
        }

        [Fact]
        public void Generic_DbCollectionEntry_delegates_to_InternalReferenceEntry_correctly()
        {
            var v = new DbCollectionEntryVerifier<object, object>();
            v.VerifyGetter(e => e.CurrentValue, m => m.CurrentValue);
            var currentValue = new object[0];
            v.VerifySetter(e => e.CurrentValue = currentValue, m => m.CurrentValue = currentValue);
            v.VerifyGetter(e => e.EntityEntry, m => m.InternalEntityEntry);
            v.VerifyGetter(e => e.IsLoaded, m => m.IsLoaded);
            v.VerifyGetter(e => e.Name, m => m.Name);
            v.VerifyMethod(e => e.GetValidationErrors(), m => m.GetValidationErrors());
            v.VerifyMethod(e => e.Load(), m => m.Load());
            v.VerifyMethod(e => e.Query(), m => m.Query());

#if !NET40
            v.VerifyMethod(e => e.LoadAsync(), m => m.LoadAsync(CancellationToken.None));
            v.VerifyMethod(e => e.LoadAsync(CancellationToken.None), m => m.LoadAsync(CancellationToken.None));
#endif
        }

        public class EntityEntry
        {
            [Fact]
            public void EntityEntity_can_be_obtained_from_generic_DbCollectionEntry_back_reference()
            {
                var entityEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object);

                var backEntry = entityEntry.Collection(e => e.Collection).EntityEntry;

                Assert.Same(entityEntry.Entity, backEntry.Entity);
            }

            [Fact]
            public void EntityEntity_can_be_obtained_from_non_generic_DbCollectionEntry_back_reference()
            {
                var entityEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object);

                var backEntry = entityEntry.Collection("Collection").EntityEntry;

                Assert.Same(entityEntry.Entity, backEntry.Entity);
            }
        }

        public class Cast
        {
            [Fact]
            public void Non_generic_DbMemberEntry_for_collection_can_be_converted_to_generic_version()
            {
                var memberEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Member(
                    "Collection");

                var generic = memberEntry.Cast<FakeWithProps, ICollection<FakeEntity>>();

                Assert.IsType<DbCollectionEntry<FakeWithProps, FakeEntity>>(generic);
                Assert.Same(memberEntry.InternalMemberEntry, generic.InternalMemberEntry);
            }

            [Fact]
            public void Non_generic_DbCollectionEntry_for_collection_can_be_converted_to_generic_version()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Collection("Collection");

                var generic = memberEntry.Cast<FakeWithProps, FakeEntity>();

                Assert.IsType<DbCollectionEntry<FakeWithProps, FakeEntity>>(generic);
                Assert.Same(memberEntry.InternalMemberEntry, generic.InternalMemberEntry);
            }

            [Fact]
            public void Non_generic_DbMemberEntry_for_collection_can_be_converted_to_generic_version_of_base_entity_type()
            {
                var memberEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Member(
                    "Collection");

                var generic = memberEntry.Cast<object, ICollection<FakeEntity>>();

                Assert.IsType<DbCollectionEntry<object, FakeEntity>>(generic);
                Assert.Same(memberEntry.InternalMemberEntry, generic.InternalMemberEntry);
            }

            [Fact]
            public void Non_generic_DbCollectionEntry_for_collection_can_be_converted_to_generic_version_of_base_entity_type()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Collection("Collection");

                var generic = memberEntry.Cast<object, FakeEntity>();

                Assert.IsType<DbCollectionEntry<object, FakeEntity>>(generic);
                Assert.Same(memberEntry.InternalMemberEntry, generic.InternalMemberEntry);
            }

            [Fact]
            public void Non_generic_DbMemberEntry_for_collection_cannot_be_converted_to_generic_version_of_base_property_type()
            {
                var memberEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Member(
                    "Collection");

                // This cast fails because an ICollection<FakeEntity> is not an IColletion<object>.
                Assert.Equal(
                    Strings.DbMember_BadTypeForCast(
                        typeof(DbMemberEntry).Name, typeof(FakeWithProps).Name, typeof(ICollection<object>).Name, typeof(FakeWithProps).Name,
                        typeof(ICollection<FakeEntity>).Name),
                    Assert.Throws<InvalidCastException>(() => memberEntry.Cast<FakeWithProps, ICollection<object>>()).Message);
            }

            [Fact]
            public void Non_generic_DbCollectionEntry_for_collection_can_be_converted_to_generic_version_of_base_property_type()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Collection("Collection");

                var generic = memberEntry.Cast<FakeWithProps, object>();

                Assert.IsType<DbCollectionEntry<FakeWithProps, object>>(generic);
                Assert.Same(memberEntry.InternalMemberEntry, generic.InternalMemberEntry);
            }

            [Fact]
            public void Non_generic_DbMemberEntry_for_collection_cannot_be_converted_to_generic_version_of_derived_entity_type()
            {
                var memberEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Member(
                    "Collection");

                Assert.Equal(
                    Strings.DbMember_BadTypeForCast(
                        typeof(DbMemberEntry).Name, typeof(DerivedFakeWithProps).Name, typeof(ICollection<FakeEntity>).Name,
                        typeof(FakeWithProps).Name, typeof(ICollection<FakeEntity>).Name),
                    Assert.Throws<InvalidCastException>(() => memberEntry.Cast<DerivedFakeWithProps, ICollection<FakeEntity>>()).Message);
            }

            [Fact]
            public void Non_generic_DbCollectionEntry_for_collection_cannot_be_converted_to_generic_version_of_derived_entity_type()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Collection("Collection");

                Assert.Equal(
                    Strings.DbMember_BadTypeForCast(
                        typeof(DbCollectionEntry).Name, typeof(DerivedFakeWithProps).Name, typeof(FakeEntity).Name,
                        typeof(FakeWithProps).Name, typeof(FakeEntity).Name),
                    Assert.Throws<InvalidCastException>(() => memberEntry.Cast<DerivedFakeWithProps, FakeEntity>()).Message);
            }

            [Fact]
            public void Non_generic_DbMemberEntry_for_collection_cannot_be_converted_to_generic_version_of_derived_property_type()
            {
                var memberEntry = new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Member(
                    "Collection");

                Assert.Equal(
                    Strings.DbMember_BadTypeForCast(
                        typeof(DbMemberEntry).Name, typeof(FakeWithProps).Name, typeof(ICollection<FakeDerivedEntity>).Name,
                        typeof(FakeWithProps).Name, typeof(ICollection<FakeEntity>).Name),
                    Assert.Throws<InvalidCastException>(() => memberEntry.Cast<FakeWithProps, ICollection<FakeDerivedEntity>>()).Message);
            }

            [Fact]
            public void Non_generic_DbCollectionEntry_for_collection_cannot_be_converted_to_generic_version_of_derived_property_type()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Collection("Collection");

                Assert.Equal(
                    Strings.DbMember_BadTypeForCast(
                        typeof(DbCollectionEntry).Name, typeof(FakeWithProps).Name, typeof(FakeDerivedEntity).Name,
                        typeof(FakeWithProps).Name, typeof(FakeEntity).Name),
                    Assert.Throws<InvalidCastException>(() => memberEntry.Cast<FakeWithProps, FakeDerivedEntity>()).Message);
            }
        }

        public class ImplicitDbCollectionEntry
        {
            [Fact]
            public void Generic_DbCollectionEntry_can_be_implicitly_converted_to_non_generic_version()
            {
                var propEntry =
                    new DbCollectionEntry<FakeWithProps, FakeEntity>(
                        new InternalCollectionEntry(
                            new Mock<InternalEntityEntryForMock<FakeEntity>>().Object, FakeWithProps.CollectionMetadata));

                NonGenericTestMethod(propEntry, "Collection");
            }

            private void NonGenericTestMethod(DbCollectionEntry nonGenericEntry, string name)
            {
                Assert.Same(name, nonGenericEntry.Name);
            }

            [Fact]
            public void Generic_DbCollectionEntry_typed_as_DbMemberEntry_can_be_implicitly_converted_to_non_generic_version()
            {
                DbMemberEntry<FakeWithProps, ICollection<FakeEntity>> propEntry =
                    new DbCollectionEntry<FakeWithProps, FakeEntity>(
                        new InternalCollectionEntry(
                            new Mock<InternalEntityEntryForMock<FakeEntity>>().Object, FakeWithProps.CollectionMetadata));

                NonGenericTestMethodCollectionAsMember(propEntry, "Collection");
            }

            private void NonGenericTestMethodCollectionAsMember(DbMemberEntry nonGenericEntry, string name)
            {
                Assert.Same(name, nonGenericEntry.Name);
                Assert.IsType<DbCollectionEntry>(nonGenericEntry);
            }

            [Fact]
            public void Generic_DbCollectionEntry_typed_as_DbMemberEntry_can_be_implicitly_converted_to_non_generic_DbCollectionEntry()
            {
                DbMemberEntry<FakeWithProps, ICollection<FakeEntity>> propEntry =
                    new DbCollectionEntry<FakeWithProps, FakeEntity>(
                        new InternalCollectionEntry(
                            new Mock<InternalEntityEntryForMock<FakeEntity>>().Object, FakeWithProps.CollectionMetadata));

                NonGenericTestMethod((DbCollectionEntry)propEntry, "Collection");
            }

            [Fact]
            public void Generic_DbMemberEntry_for_collection_can_be_converted_to_non_generic_version()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Member<ICollection<FakeEntity>>(
                        "Collection");

                var nonGeneric = ImplicitConvert(memberEntry);

                Assert.IsType<DbCollectionEntry>(nonGeneric);
                Assert.Same(memberEntry.InternalMemberEntry, nonGeneric.InternalMemberEntry);
            }

            private static DbMemberEntry ImplicitConvert(DbMemberEntry nonGeneric)
            {
                return nonGeneric;
            }

            [Fact]
            public void Generic_DbCollectionEntry_for_collection_can_be_converted_to_non_generic_version()
            {
                var memberEntry =
                    new DbEntityEntry<FakeWithProps>(FakeWithProps.CreateMockInternalEntityEntry().Object).Collection(e => e.Collection);

                var nonGeneric = ImplicitConvert(memberEntry);

                Assert.IsType<DbCollectionEntry>(nonGeneric);
                Assert.Same(memberEntry.InternalMemberEntry, nonGeneric.InternalMemberEntry);
            }

            private static DbCollectionEntry ImplicitConvert(DbCollectionEntry nonGeneric)
            {
                return nonGeneric;
            }
        }

        #region Helpers

        internal class DbCollectionEntryVerifier : DbMemberEntryVerifier<DbCollectionEntry, InternalCollectionEntry>
        {
            protected override DbCollectionEntry CreateEntry(InternalCollectionEntry internalEntry)
            {
                return new DbCollectionEntry(internalEntry);
            }

            protected override Mock<InternalCollectionEntry> CreateInternalEntryMock()
            {
                return new Mock<InternalCollectionEntry>(
                    new Mock<InternalEntityEntryForMock<object>>().Object,
                    new NavigationEntryMetadata(typeof(object), typeof(object), "fake collection", isCollection: true));
            }
        }

        internal class DbCollectionEntryVerifier<TEntity, TElement> :
            DbMemberEntryVerifier<DbCollectionEntry<TEntity, TElement>, InternalCollectionEntry>
            where TEntity : class
        {
            protected override DbCollectionEntry<TEntity, TElement> CreateEntry(InternalCollectionEntry internalEntry)
            {
                return new DbCollectionEntry<TEntity, TElement>(internalEntry);
            }

            protected override Mock<InternalCollectionEntry> CreateInternalEntryMock()
            {
                return new Mock<InternalCollectionEntry>(
                    new Mock<InternalEntityEntryForMock<object>>().Object,
                    new NavigationEntryMetadata(typeof(object), typeof(object), "fake collection", isCollection: true));
            }
        }

        #endregion
    }
}