public void GetService_caches_the_fact_that_no_connection_factory_is_set()
        {
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.TryGetDefaultConnectionFactory()).Returns((IDbConnectionFactory)null);
            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService <IDbConnectionFactory>());
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
            Assert.Null(resolver.GetService <IDbConnectionFactory>());
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
        }
        public void GetService_caches_connection_factory()
        {
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.TryGetDefaultConnectionFactory()).Returns(new FakeConnectionFactory());
            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            var factoryInstance = resolver.GetService <IDbConnectionFactory>();

            Assert.NotNull(factoryInstance);
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
            Assert.Same(factoryInstance, resolver.GetService <IDbConnectionFactory>());
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
        }
示例#3
0
        public void GetService_caches_the_fact_that_no_provider_is_registered()
        {
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.DbProviderServices).Returns(new NamedDbProviderService[0]);

            var resolver = new AppConfigDependencyResolver(
                mockConfig.Object, new Mock <InternalConfiguration>(null, null, null, null).Object);

            Assert.Null(resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert"));
            mockConfig.Verify(m => m.DbProviderServices, Times.Once());
            Assert.Null(resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert"));
            mockConfig.Verify(m => m.DbProviderServices, Times.Once());
        }
        public void GetService_caches_the_fact_that_no_provider_is_registered()
        {
            var mockProviders = new Mock <ProviderConfig>();

            mockProviders.Setup(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert")).Returns((DbProviderServices)null);
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
            Assert.Null(resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
        }
        public void GetService_caches_provider()
        {
            var mockProviders = new Mock<ProviderConfig>();
            mockProviders.Setup(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert")).Returns(new Mock<DbProviderServices>().Object);
            var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection());
            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            var factoryInstance = resolver.GetService<DbProviderServices>("Ask.Rhod.Gilbert");

            Assert.NotNull(factoryInstance);
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
            Assert.Same(factoryInstance, resolver.GetService<DbProviderServices>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
        }
        public void GetService_can_be_accessed_from_multiple_threads_concurrently()
        {
            var appConfig = new AppConfig(
                CreateEmptyConfig().AddDefaultConnectionFactory(typeof(FakeConnectionFactory).AssemblyQualifiedName));

            for (var i = 0; i < 30; i++)
            {
                var bag      = new ConcurrentBag <IDbConnectionFactory>();
                var resolver = new AppConfigDependencyResolver(appConfig);

                ExecuteInParallel(() => bag.Add(resolver.GetService <IDbConnectionFactory>()));

                Assert.Equal(20, bag.Count);
                Assert.True(bag.All(c => resolver.GetService <IDbConnectionFactory>() == c));
            }
        }
        public void GetService_caches_the_fact_that_no_database_initializer_is_registered()
        {
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.Initializers).Returns(
                new InitializerConfig(
                    CreateEfSection(initializerDisabled: false),
                    new KeyValueConfigurationCollection()));

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService <IDatabaseInitializer <DbContext> >());
            mockConfig.Verify(m => m.Initializers, Times.Once());
            Assert.Null(resolver.GetService <IDatabaseInitializer <DbContext> >());
            mockConfig.Verify(m => m.Initializers, Times.Once());
        }
示例#8
0
        public void GetService_registers_all_providers_as_secondary_resolvers_only_once()
        {
            var mockSection       = CreateMockSectionWithProviders();
            var mockFactory       = CreateMockFactory(mockSection.Object);
            var appConfig         = new AppConfig(new ConnectionStringSettingsCollection(), null, mockSection.Object, mockFactory.Object);
            var mockConfiguration = new Mock <InternalConfiguration>(null, null, null, null);

            var resolver = new AppConfigDependencyResolver(appConfig, mockConfiguration.Object, mockFactory.Object);

            resolver.GetService <IPilkington>();

            mockConfiguration.Verify(m => m.AddSecondaryResolver(It.IsAny <DbProviderServices>()), Times.Exactly(3));

            resolver.GetService <IPilkington>();

            mockConfiguration.Verify(m => m.AddSecondaryResolver(It.IsAny <DbProviderServices>()), Times.Exactly(3));
        }
        public void GetService_caches_provider()
        {
            var mockProviders = new Mock <ProviderConfig>();

            mockProviders.Setup(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert")).Returns(new Mock <DbProviderServices>().Object);
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            var factoryInstance = resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert");

            Assert.NotNull(factoryInstance);
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
            Assert.Same(factoryInstance, resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
        }
        public void GetService_caches_the_fact_that_no_Migrations_SQL_generator_is_registered()
        {
            var mockProviders = new Mock <ProviderConfig>();

            mockProviders.Setup(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"))
            .Returns((Func <MigrationSqlGenerator>)(() => null));

            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService <MigrationSqlGenerator>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());
            Assert.Null(resolver.GetService <MigrationSqlGenerator>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());
        }
示例#11
0
        public void GetService_caches_database_initializer()
        {
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.DbProviderServices).Returns(new NamedDbProviderService[0]);
            mockConfig.Setup(m => m.Initializers).Returns(
                new InitializerConfig(
                    CreateEfSection(initializerDisabled: false),
                    new KeyValueConfigurationCollection()));

            var resolver = new AppConfigDependencyResolver(
                mockConfig.Object, new Mock <InternalConfiguration>(null, null, null, null).Object);
            var initializer = resolver.GetService <IDatabaseInitializer <FakeContext> >();

            Assert.NotNull(initializer);
            mockConfig.Verify(m => m.Initializers, Times.Once());
            Assert.Same(initializer, resolver.GetService <IDatabaseInitializer <FakeContext> >());
            mockConfig.Verify(m => m.Initializers, Times.Once());
        }
示例#12
0
        public void GetService_caches_provider()
        {
            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.DbProviderServices)
            .Returns(new[] { new NamedDbProviderService("Ask.Rhod.Gilbert", new Mock <DbProviderServices>().Object) });

            var resolver = new AppConfigDependencyResolver(
                mockConfig.Object,
                new Mock <InternalConfiguration>(null, null, null, null).Object,
                new Mock <ProviderServicesFactory>().Object);

            var factoryInstance = resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert");

            Assert.NotNull(factoryInstance);
            mockConfig.Verify(m => m.DbProviderServices, Times.Once());
            Assert.Same(factoryInstance, resolver.GetService <DbProviderServices>("Ask.Rhod.Gilbert"));
            mockConfig.Verify(m => m.DbProviderServices, Times.Once());
        }
示例#13
0
        public void GetService_caches_connection_factory()
        {
            try
            {
                var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());
                mockConfig.Setup(m => m.DbProviderServices).Returns(new NamedDbProviderService[0]);
                mockConfig.Setup(m => m.TryGetDefaultConnectionFactory()).Returns(new FakeConnectionFactory());
                var resolver = new AppConfigDependencyResolver(
                    mockConfig.Object, new Mock <InternalConfiguration>(null, null, null, null).Object);

                var factoryInstance = resolver.GetService <IDbConnectionFactory>();

                Assert.NotNull(factoryInstance);
                mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
                Assert.Same(factoryInstance, resolver.GetService <IDbConnectionFactory>());
                mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
            }
            finally
            {
                Database.ResetDefaultConnectionFactory();
            }
        }
        public void GetService_caches_Migrations_SQL_generator_and_uses_it_to_return_a_new_instance_every_call()
        {
            var mockProviders = new Mock <ProviderConfig>();

            mockProviders.Setup(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"))
            .Returns(() => new Mock <MigrationSqlGenerator>().Object);

            var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection());

            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            var migrationSqlGenerator1 = resolver.GetService <MigrationSqlGenerator>("Ask.Rhod.Gilbert");

            Assert.NotNull(migrationSqlGenerator1);
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());

            var migrationSqlGenerator2 = resolver.GetService <MigrationSqlGenerator>("Ask.Rhod.Gilbert");

            Assert.NotNull(migrationSqlGenerator2);
            Assert.NotSame(migrationSqlGenerator1, migrationSqlGenerator2);
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());
        }
        public void GetService_caches_the_fact_that_no_provider_is_registered()
        {
            var mockProviders = new Mock<ProviderConfig>();
            mockProviders.Setup(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert")).Returns((DbProviderServices)null);
            var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection());
            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService<DbProviderServices>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
            Assert.Null(resolver.GetService<DbProviderServices>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetDbProviderServices("Ask.Rhod.Gilbert"), Times.Once());
        }
        public void GetService_can_be_accessed_from_multiple_threads_concurrently()
        {
            var appConfig = new AppConfig(
                CreateEmptyConfig().AddDefaultConnectionFactory(typeof(FakeConnectionFactory).AssemblyQualifiedName));

            for (var i = 0; i < 30; i++)
            {
                var bag = new ConcurrentBag<IDbConnectionFactory>();
                var resolver = new AppConfigDependencyResolver(appConfig);

                ExecuteInParallel(() => bag.Add(resolver.GetService<IDbConnectionFactory>()));
                
                Assert.Equal(20, bag.Count);
                Assert.True(bag.All(c => resolver.GetService<IDbConnectionFactory>() == c));
            }
        }
        public void GetService_caches_the_fact_that_no_connection_factory_is_set()
        {
            var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection());
            mockConfig.Setup(m => m.TryGetDefaultConnectionFactory()).Returns((IDbConnectionFactory)null);
            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService<IDbConnectionFactory>());
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
            Assert.Null(resolver.GetService<IDbConnectionFactory>());
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
        }
        public void GetService_caches_connection_factory()
        {
            var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection());
            mockConfig.Setup(m => m.TryGetDefaultConnectionFactory()).Returns(new FakeConnectionFactory());
            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            var factoryInstance = resolver.GetService<IDbConnectionFactory>();

            Assert.NotNull(factoryInstance);
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
            Assert.Same(factoryInstance, resolver.GetService<IDbConnectionFactory>());
            mockConfig.Verify(m => m.TryGetDefaultConnectionFactory(), Times.Once());
        }
        public void GetService_caches_the_fact_that_no_Migrations_SQL_generator_is_registered()
        {
            var mockProviders = new Mock<ProviderConfig>();
            mockProviders.Setup(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"))
                .Returns((Func<MigrationSqlGenerator>)(() => null));
            
            var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection());
            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            Assert.Null(resolver.GetService<MigrationSqlGenerator>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());
            Assert.Null(resolver.GetService<MigrationSqlGenerator>("Ask.Rhod.Gilbert"));
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());
        }
        public void GetService_caches_Migrations_SQL_generator_and_uses_it_to_return_a_new_instance_every_call()
        {
            var mockProviders = new Mock<ProviderConfig>();
            mockProviders.Setup(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"))
                .Returns(() => new Mock<MigrationSqlGenerator>().Object);
            
            var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection());
            mockConfig.Setup(m => m.Providers).Returns(mockProviders.Object);

            var resolver = new AppConfigDependencyResolver(mockConfig.Object);

            var migrationSqlGenerator1 = resolver.GetService<MigrationSqlGenerator>("Ask.Rhod.Gilbert");
            Assert.NotNull(migrationSqlGenerator1);
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());

            var migrationSqlGenerator2 = resolver.GetService<MigrationSqlGenerator>("Ask.Rhod.Gilbert");
            Assert.NotNull(migrationSqlGenerator2);
            Assert.NotSame(migrationSqlGenerator1, migrationSqlGenerator2);
            mockProviders.Verify(m => m.TryGetMigrationSqlGeneratorFactory("Ask.Rhod.Gilbert"), Times.Once());
        }