File: SetDiscoveryTests.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 (549 lines) | stat: -rw-r--r-- 19,897 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
// 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.Core.Objects.DataClasses;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Internal;
    using System.Data.Entity.ModelConfiguration.Configuration.Types;
    using System.Data.Entity.Resources;
    using System.Linq;
    using System.Reflection;
    using Moq;
    using Xunit;

    public class FakeEntity1
    {
    }

    public class FakeEntity2
    {
    }

    public class FakeEntity3
    {
    }

    public class FakeEntity4
    {
    }

    public class FakeEntity5
    {
    }

    public class FakeEntity6
    {
    }

    public class FakeEntity7
    {
    }

    public class FakeEntity8
    {
    }

    public class FakeEntity9
    {
    }

    public class FakeEntity10
    {
    }

    public class FakeEntity11
    {
    }

    public class FakeEntity12
    {
    }

    public class FakeEntity13
    {
    }

    public class FakeEntity14
    {
    }

    public class FakeEntity15
    {
    }

    public class FakeEntity16
    {
    }

    public class FakeEntity17
    {
    }

    public class FakeEntity18
    {
    }

    /// <summary>
    ///     Unit tests for the DbSet/ObjectSet discovery service.
    /// </summary>
    public class SetDiscoveryTests : TestBase
    {
        #region Positive DbContext discovery and initialization tests

        private class FakeDbContextWithDbSets : DbContext
        {
            public FakeDbContextWithDbSets()
                : base("this=is=not=valid")
            {
            }

            // Should be detected: DbSets with no modifiers
            public DbSet<FakeEntity1> PublicGetSet { get; set; }
            protected IDbSet<FakeEntity2> ProtectedGetSet { get; set; }
            internal DbSet<FakeEntity3> InternalGetSet { get; set; }
            protected internal IDbSet<FakeEntity4> InternalProtectedGetSet { get; set; }
            private DbSet<FakeEntity5> PrivateGetSet { get; set; }

            // Should be detected: Public DbSets setter modifiers
            public IDbSet<FakeEntity6> PrivateSet { get; private set; }
            public DbSet<FakeEntity7> ProtectedSet { get; protected set; }
            public IDbSet<FakeEntity8> InternalSet { get; internal set; }
            public DbSet<FakeEntity9> InternalProtectedSet { get; protected internal set; }

            // Should be detected: Public DbSets getter modifiers
            public DbSet<FakeEntity10> PrivateGet { private get; set; }
            public IDbSet<FakeEntity11> ProtectedGet { protected get; set; }
            public DbSet<FakeEntity12> InternalGet { internal get; set; }
            public IDbSet<FakeEntity13> InternalProtectedGet { protected internal get; set; }

            // Should be detected: DbSets with no setters
            public DbSet<FakeEntity14> PublicGetNoSet
            {
                get { return null; }
            }

            protected IDbSet<FakeEntity15> ProtectedGetNoSet
            {
                get { return null; }
            }

            internal IDbSet<FakeEntity16> InternalGetNoSet
            {
                get { return null; }
            }

            protected internal DbSet<FakeEntity17> InternalProtectedGetNoSet
            {
                get { return null; }
            }

            private IDbSet<FakeEntity18> PrivateGetNoSet
            {
                get { return null; }
            }
        }

        [Fact]
        public void DbSet_and_IDbSet_properties_on_derived_DbContext_are_discovered()
        {
            var expected = new[]
                               {
                                   "PublicGetSet", "ProtectedGetSet", "InternalGetSet", "InternalProtectedGetSet", "PrivateGetSet",
                                   "PrivateSet", "ProtectedSet", "InternalSet", "InternalProtectedSet",
                                   "PrivateGet", "ProtectedGet", "InternalGet", "InternalProtectedGet",
                                   "PublicGetNoSet", "ProtectedGetNoSet", "InternalGetNoSet", "InternalProtectedGetNoSet", "PrivateGetNoSet"
                                   ,
                               };

            AssertExpectedSetsDiscovered(new FakeDbContextWithDbSets(), expected);
        }

        private class FakeDbContextWithNonSets : DbContext
        {
            public FakeDbContextWithNonSets()
                : base("this=is=not=valid")
            {
            }

            public DbSet<FakeEntity> Control { get; set; }

            public IQueryable<FakeEntity> QueryableT { get; set; }
            public IQueryable Queryable { get; set; }
            public IEnumerable<FakeEntity> EnumerableT { get; set; }
            public IEnumerable Enumerable { get; set; }
            public DbQuery<FakeEntity> DbQuery { get; set; }
        }

        [Fact]
        public void IQueryable_and_DbQuery_properties_on_derived_DbContext_are_ignored()
        {
            AssertExpectedSetsDiscovered(new FakeDbContextWithNonSets(), new[] { "Control" });
        }

        [Fact]
        public void DbSet_and_IDbSet_properties_with_public_setters_are_initialized()
        {
            using (var context = new FakeDbContextWithDbSets())
            {
                var contextType = typeof(FakeDbContextWithDbSets);
                const BindingFlags binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

                Assert.NotNull(contextType.GetProperty("PublicGetSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("ProtectedGetSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("InternalGetSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("InternalProtectedGetSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("PrivateGetSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("PrivateSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("ProtectedSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("InternalSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("InternalProtectedSet", binding).GetValue(context, null));
                Assert.NotNull(contextType.GetProperty("PrivateGet", binding).GetValue(context, null));
                Assert.NotNull(contextType.GetProperty("ProtectedGet", binding).GetValue(context, null));
                Assert.NotNull(contextType.GetProperty("InternalGet", binding).GetValue(context, null));
                Assert.NotNull(contextType.GetProperty("InternalProtectedGet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("PublicGetNoSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("ProtectedGetNoSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("InternalGetNoSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("InternalProtectedGetNoSet", binding).GetValue(context, null));
                Assert.Null(contextType.GetProperty("PrivateGetNoSet", binding).GetValue(context, null));
            }
        }

        [Fact]
        public void IQueryable_and_DbQuery_properties_with_public_setters_are_not_initialized()
        {
            using (var context = new FakeDbContextWithNonSets())
            {
                Assert.NotNull(context.Control);
                Assert.Null(context.DbQuery);
                Assert.Null(context.Enumerable);
                Assert.Null(context.EnumerableT);
                Assert.Null(context.Queryable);
                Assert.Null(context.QueryableT);
            }
        }

        #endregion

        #region Negative entity types

        private class FakeDbContextWithInterfaceDbSet : DbContext
        {
            public DbSet<IList> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_interface_type_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(IList)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithInterfaceDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithGenericDbSet : DbContext
        {
            public DbSet<List<FakeEntity>> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_generic_type_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(List<FakeEntity>)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithGenericDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithNestedDbSet : DbContext
        {
            public DbSet<FakeDbContextWithDbSets> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_nested_type_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(FakeDbContextWithDbSets)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithNestedDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithObjectDbSet : DbContext
        {
            public DbSet<object> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_object_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(Object)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithObjectDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithEntityObjectDbSet : DbContext
        {
            public DbSet<EntityObject> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_EntityObject_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(EntityObject)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithEntityObjectDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithStructuralObjectDbSet : DbContext
        {
            public DbSet<StructuralObject> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_StructuralObject_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(StructuralObject)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithStructuralObjectDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithComplexObjectDbSet : DbContext
        {
            public DbSet<ComplexObject> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_ComplexObject_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(ComplexObject)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithComplexObjectDbSet()).InitializeSets()).Message);
        }

        private class FakeDbContextWithStringDbSet : DbContext
        {
            public DbSet<String> SetProp { get; set; }
        }

        [Fact]
        public void Discovery_of_DbSet_of_string_throws()
        {
            Assert.Equal(
                Strings.InvalidEntityType(typeof(String)),
                Assert.Throws<InvalidOperationException>(
                    () => new DbSetDiscoveryService(new FakeDbContextWithStringDbSet()).InitializeSets()).Message);
        }

        #endregion

        #region Positive tests for discovery attributes at the class level

        [SuppressDbSetInitialization]
        private class DerivedDbContextWithClassLevelSetDiscoverOnly : DbContext
        {
            public DbSet<DBNull> FakeSet1 { get; set; }
            public DbSet<Random> FakeSet2 { get; set; }
        }

        [Fact]
        public void SuppressDbSetInitializationAttribute_on_context_class_results_in_all_sets_being_found()
        {
            var context = new DerivedDbContextWithClassLevelSetDiscoverOnly();
            DiscoverAndInitializeSets(context, 2);

            Assert.Null(context.FakeSet1);
            Assert.Null(context.FakeSet2);
        }

        #endregion

        #region Positive tests for discovery attributes at the property level

        private class DerivedDbContextWithPropertyLevelSetDiscoverOnly : DbContext
        {
            public DbSet<DBNull> FakeSet1 { get; set; }

            [SuppressDbSetInitialization]
            public DbSet<Random> FakeSet2 { get; set; }
        }

        [Fact]
        public void SuppressDbSetInitializationAttribute_on_property_results_in_property_still_being_discovered()
        {
            var context = new DerivedDbContextWithPropertyLevelSetDiscoverOnly();
            DiscoverAndInitializeSets(context, 2);

            Assert.NotNull(context.FakeSet1);
            Assert.Null(context.FakeSet2);
        }

        #endregion

        #region Positive tests for discovery attributes on contexts with inheritance

        [SuppressDbSetInitialization]
        private class DerivedDbContextWithInheritanceLevel1 : DbContext
        {
            public DbSet<DBNull> FakeSet1 { get; set; }
        }

        private class DerivedDbContextWithInheritanceLevel2 : DerivedDbContextWithInheritanceLevel1
        {
            public DbSet<Random> FakeSet2 { get; set; }
        }

        [SuppressDbSetInitialization]
        private class DerivedDbContextWithInheritanceLevel3 : DerivedDbContextWithInheritanceLevel2
        {
            public DbSet<GopherStyleUriParser> FakeSet3 { get; set; }
        }

        [Fact]
        public void DbSetDiscoveryAttribute_on_class_applies_only_to_properties_in_that_class()
        {
            var context = new DerivedDbContextWithInheritanceLevel3();
            DiscoverAndInitializeSets(context, 3);

            Assert.Null(context.FakeSet1);
            Assert.NotNull(context.FakeSet2);
            Assert.Null(context.FakeSet3);
        }

        private class DerivedDbContextForPropertyOverrideLevel1 : DbContext
        {
            public virtual DbSet<Random> FakeSet1 { get; set; }
        }

        private class DerivedDbContextForPropertyOverrideLevel2 : DerivedDbContextForPropertyOverrideLevel1
        {
            [SuppressDbSetInitialization]
            public override DbSet<Random> FakeSet1 { get; set; }
        }

        [Fact]
        public void DbSetDiscoveryAttribute_on_overriden_property_is_used()
        {
            var context = new DerivedDbContextForPropertyOverrideLevel2();
            DiscoverAndInitializeSets(context, 1);

            Assert.Null(context.FakeSet1);
        }

        private class DerivedDbContextForPropertyHideLevel1 : DbContext
        {
            public DbSet<Random> FakeSet1 { get; set; }
        }

        private class DerivedDbContextForPropertyHideLevel2 : DerivedDbContextForPropertyHideLevel1
        {
            [SuppressDbSetInitialization]
            public new DbSet<Random> FakeSet1 { get; set; }
        }

        [Fact]
        public void DbSetDiscoveryAttribute_on_hiding_property_is_used()
        {
            var context = new DerivedDbContextForPropertyHideLevel2();
            DiscoverAndInitializeSets(context, 1);

            Assert.Null(context.FakeSet1);
        }

        private class DerivedDbContextWithInheritanceBLevel1 : DbContext
        {
            public virtual DbSet<Random> FakeSet1 { get; set; }
        }

        [SuppressDbSetInitialization]
        private class DerivedDbContextWithInheritanceBLevel2 : DerivedDbContextWithInheritanceBLevel1
        {
            public override DbSet<Random> FakeSet1 { get; set; }
        }

        [Fact]
        public void DbSetDiscoveryAttribute_on_class_applies_to_properties_overriden_in_that_class()
        {
            var context = new DerivedDbContextWithInheritanceBLevel2();
            DiscoverAndInitializeSets(context, 1);

            Assert.Null(context.FakeSet1);
        }

        #endregion

        #region Static tests for SuppressDbSetInitializationAttribute usage

        [Fact]
        public void DbSetDiscoveryAttribute_does_not_allow_multiple_uses()
        {
            Assert.False(GetDbSetDiscoveryAttributeUsage().AllowMultiple);
        }

        [Fact]
        public void DbSetDiscoveryAttribute_can_be_applied_to_classes_or_properties_only()
        {
            Assert.Equal(AttributeTargets.Class | AttributeTargets.Property, GetDbSetDiscoveryAttributeUsage().ValidOn);
        }

        private static AttributeUsageAttribute GetDbSetDiscoveryAttributeUsage()
        {
            return
                (AttributeUsageAttribute)typeof(SuppressDbSetInitializationAttribute).GetCustomAttributes(
                    typeof(AttributeUsageAttribute),
                    inherit: false).Single();
        }

        #endregion

        #region Helpers

        internal class EntityTypeConfigurationForMock : EntityTypeConfiguration
        {
            public EntityTypeConfigurationForMock()
                : base(typeof(FakeEntity))
            {
            }
        }

        private void AssertExpectedSetsDiscovered(DbContext context, IEnumerable<string> expected)
        {
            var mockBuilder = new Mock<DbModelBuilder>();
            var mockConfig = new Mock<EntityTypeConfigurationForMock>();
            mockBuilder.Setup(b => b.Entity(It.IsAny<Type>())).Returns(mockConfig.Object);

            var discoveryService = new DbSetDiscoveryService(context);
            discoveryService.RegisterSets(mockBuilder.Object);

            foreach (var setName in expected)
            {
                var name = setName;
                mockConfig.VerifySet(c => c.EntitySetName = name, Times.Once());
            }
        }

        private static void DiscoverAndInitializeSets(DbContext context, int setCount)
        {
            var mockBuilder = new Mock<DbModelBuilder>();
            var mockConfig = new Mock<EntityTypeConfigurationForMock>();
            mockBuilder.Setup(b => b.Entity(It.IsAny<Type>())).Returns(mockConfig.Object);

            var discoveryService = new DbSetDiscoveryService(context);
            discoveryService.RegisterSets(mockBuilder.Object);

            mockBuilder.Verify(b => b.Entity(It.IsAny<Type>()), Times.Exactly(setCount));
            mockConfig.VerifySet(c => c.EntitySetName = It.IsAny<string>(), Times.Exactly(setCount));
        }

        #endregion
    }
}