Exemple #1
0
        public void Initializes_all_entity_set_properties_with_setters()
        {
            var setFinderMock = new Mock <DbSetFinder>();

            setFinderMock.Setup(m => m.FindSets(It.IsAny <DbContext>())).Returns(
                new[]
            {
                new DbSetFinder.DbSetProperty(typeof(JustAContext), "One", typeof(string), hasSetter: true),
                new DbSetFinder.DbSetProperty(typeof(JustAContext), "Two", typeof(object), hasSetter: true),
                new DbSetFinder.DbSetProperty(typeof(JustAContext), "Three", typeof(string), hasSetter: true),
                new DbSetFinder.DbSetProperty(typeof(JustAContext), "Four", typeof(string), hasSetter: false)
            });

            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddEntityFramework()
            .UseDbSetInitializer(new DbSetInitializer(setFinderMock.Object, new ClrPropertySetterSource()));
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var configuration = new DbContextOptions().BuildConfiguration();

            using (var context = new JustAContext(serviceProvider, configuration))
            {
                Assert.NotNull(context.One);
                Assert.NotNull(context.GetTwo());
                Assert.NotNull(context.Three);
                Assert.Null(context.Four);
            }
        }
        public void Initializes_all_entity_set_properties_with_setters()
        {
            var setFinderMock = new Mock <IDbSetFinder>();

            setFinderMock.Setup(m => m.FindSets(It.IsAny <DbContext>())).Returns(
                new[]
            {
                new DbSetProperty(typeof(JustAContext), "One", typeof(string), hasSetter: true),
                new DbSetProperty(typeof(JustAContext), "Two", typeof(object), hasSetter: true),
                new DbSetProperty(typeof(JustAContext), "Three", typeof(string), hasSetter: true),
                new DbSetProperty(typeof(JustAContext), "Four", typeof(string), hasSetter: false)
            });

            var customServices = new ServiceCollection()
                                 .AddInstance <IDbSetInitializer>(new DbSetInitializer(setFinderMock.Object, new ClrPropertySetterSource(), new DbSetSource()));

            var serviceProvider = TestHelpers.Instance.CreateServiceProvider(customServices);

            using (var context = new JustAContext(serviceProvider, new EntityOptionsBuilder().Options))
            {
                Assert.NotNull(context.One);
                Assert.NotNull(context.GetTwo());
                Assert.NotNull(context.Three);
                Assert.Null(context.Four);
            }
        }
        public void Initializes_all_entity_set_properties_with_setters()
        {
            var setterFactory = new ClrPropertySetterFactory();

            var setFinderMock = new Mock <IDbSetFinder>();

            setFinderMock.Setup(m => m.FindSets(It.IsAny <DbContext>())).Returns(
                new[]
            {
                new DbSetProperty("One", typeof(string), setterFactory.Create(typeof(JustAContext).GetAnyProperty("One"))),
                new DbSetProperty("Two", typeof(object), setterFactory.Create(typeof(JustAContext).GetAnyProperty("Two"))),
                new DbSetProperty("Three", typeof(string), setterFactory.Create(typeof(JustAContext).GetAnyProperty("Three"))),
                new DbSetProperty("Four", typeof(string), null)
            });

            var customServices = new ServiceCollection()
                                 .AddSingleton <IDbSetInitializer>(new DbSetInitializer(setFinderMock.Object, new DbSetSource()));

            var serviceProvider = InMemoryTestHelpers.Instance.CreateServiceProvider(customServices);

            using (var context = new JustAContext(
                       new DbContextOptionsBuilder().UseInternalServiceProvider(serviceProvider).Options))
            {
                Assert.NotNull(context.One);
                Assert.NotNull(context.GetTwo());
                Assert.NotNull(context.Three);
                Assert.Null(context.Four);
            }
        }
        public void Initializes_all_entity_set_properties_with_setters()
        {
            var setterFactory = new ClrPropertySetterFactory();

            var setFinderMock = new Mock<IDbSetFinder>();
            setFinderMock.Setup(m => m.FindSets(It.IsAny<DbContext>())).Returns(
                new[]
                {
                    new DbSetProperty("One", typeof(string), setterFactory.Create(typeof(JustAContext).GetAnyProperty("One"))),
                    new DbSetProperty("Two", typeof(object), setterFactory.Create(typeof(JustAContext).GetAnyProperty("Two"))),
                    new DbSetProperty("Three", typeof(string), setterFactory.Create(typeof(JustAContext).GetAnyProperty("Three"))),
                    new DbSetProperty("Four", typeof(string), null)
                });

            var customServices = new ServiceCollection()
                .AddInstance<IDbSetInitializer>(new DbSetInitializer(setFinderMock.Object, new DbSetSource()));

            var serviceProvider = TestHelpers.Instance.CreateServiceProvider(customServices);

            using (var context = new JustAContext(serviceProvider, new DbContextOptionsBuilder().Options))
            {
                Assert.NotNull(context.One);
                Assert.NotNull(context.GetTwo());
                Assert.NotNull(context.Three);
                Assert.Null(context.Four);
            }
        }
        public void Initializes_all_entity_set_properties_with_setters()
        {
            var setFinder = new FakeSetFinder();
            var setSource = new DbSetSource();

            var customServices = new ServiceCollection()
                                 .AddSingleton <IDbSetInitializer>(
                new DbSetInitializer(setFinder, setSource));

            var serviceProvider = InMemoryTestHelpers.Instance.CreateServiceProvider(customServices);

            using var context = new JustAContext(
                      new DbContextOptionsBuilder().UseInternalServiceProvider(serviceProvider).Options);
            Assert.NotNull(context.One);
            Assert.NotNull(context.GetTwo());
            Assert.NotNull(context.Three);
            Assert.Null(context.Four);
        }