public void TestAddEfCoreNotMainOrmTechnology()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddSingleton(CommonTest.GetHostingEnvironment());

            // Act
            services
            .AddEasyNet()
            .AddEfCore <EfCoreContext>(options =>
            {
                options.UseSqlite("TestConnectionString");
            }, false);

            var serviceProvider  = services.BuildServiceProvider();
            var dbContextOptions = serviceProvider.GetService <DbContextOptions <EfCoreContext> >();
            var sqlServerOptions = dbContextOptions.Extensions.SingleOrDefault(p => p.GetType() == typeof(SqliteOptionsExtension));

            // Assert
            AssertSpecifiedServiceTypeAndImplementationType <EfCoreContext, EfCoreContext>(services, ServiceLifetime.Scoped);
            AssertSpecifiedServiceTypeAndImplementationType <IUnitOfWork, EfCoreUnitOfWork>(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IDbContextProvider <EfCoreContext>, UnitOfWorkDbContextProvider <EfCoreContext> >(services, ServiceLifetime.Scoped);
            Assert.NotNull(sqlServerOptions);
            Assert.Equal("TestConnectionString", ((RelationalOptionsExtension)sqlServerOptions).ConnectionString);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <User, long>, EfCoreRepositoryBase <EfCoreContext, User, long> >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <Role, int>, EfCoreRepositoryBase <EfCoreContext, Role, int> >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <Role>, EfCoreRepositoryBase <EfCoreContext, Role> >(services, ServiceLifetime.Transient);
            Assert.Equal(0, services.Count(p => p.ServiceType == typeof(IRepository <User, long>)));
            Assert.Equal(0, services.Count(p => p.ServiceType == typeof(IRepository <Role, int>)));
            Assert.Equal(0, services.Count(p => p.ServiceType == typeof(IRepository <Role>)));
        }
        public void TestAddIdentityCore()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddSingleton(CommonTest.GetHostingEnvironment());

            // Act
            services
            .AddEasyNet()
            .AddEfCore <IdentityContext>(options =>
            {
                options.UseSqlite("TestConnectionString");
            }, true)
            .AddIdentityCore <IdentityContext, User>();

            // Assert
            AssertSpecifiedServiceTypeAndImplementationType <IdentityContext, IdentityContext>(services, ServiceLifetime.Scoped);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <User>, EfCoreRepositoryBase <IdentityContext, User> >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <Role>, EfCoreRepositoryBase <IdentityContext, Role> >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <EasyNetUserClaim <int> >, EfCoreRepositoryBase <IdentityContext, EasyNetUserClaim <int> > >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <EasyNetRoleClaim <int> >, EfCoreRepositoryBase <IdentityContext, EasyNetRoleClaim <int> > >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <EasyNetUserRole <int> >, EfCoreRepositoryBase <IdentityContext, EasyNetUserRole <int> > >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <EasyNetUserLogin <int> >, EfCoreRepositoryBase <IdentityContext, EasyNetUserLogin <int> > >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IEfCoreRepository <EasyNetUserToken <int> >, EfCoreRepositoryBase <IdentityContext, EasyNetUserToken <int> > >(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <SignInManager <User>, EasyNetSignInManager <User> >(services, ServiceLifetime.Scoped);
            AssertSpecifiedServiceTypeAndImplementationType <IEasyNetGeneralSignInManager, EasyNetSignInManager <User> >(services, ServiceLifetime.Scoped);
            AssertSpecifiedServiceTypeAndImplementationType <UserManager <User>, EasyNetUserManager <User> >(services, ServiceLifetime.Scoped, 2);
        }
Пример #3
0
        public void TestConfigureUnitOfWorkDefaultOptions()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddSingleton(CommonTest.GetHostingEnvironment());

            // Act
            services
            .AddEasyNet()
            .ConfigureUnitOfWorkDefaultOptions(options =>
            {
                options.IsTransactional = false;
                options.Scope           = TransactionScopeOption.Suppress;
                options.Timeout         = TimeSpan.Zero;
                options.IsolationLevel  = IsolationLevel.RepeatableRead;
            });

            var serviceProvider = services.BuildServiceProvider();
            var defaultOptions  = serviceProvider.GetRequiredService <IOptions <UnitOfWorkDefaultOptions> >().Value;

            // Assert
            Assert.Equal(false, defaultOptions.IsTransactional);
            Assert.Equal(TransactionScopeOption.Suppress, defaultOptions.Scope);
            Assert.Equal(TimeSpan.Zero, defaultOptions.Timeout);
            Assert.Equal(IsolationLevel.RepeatableRead, defaultOptions.IsolationLevel);
        }
Пример #4
0
        public void TestAddSession()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddSingleton(CommonTest.GetHostingEnvironment());

            // Act
            services
            .AddEasyNet()
            .AddSession <TestSession>();

            var serviceProvider = services.BuildServiceProvider();
            var session         = serviceProvider.GetRequiredService <IEasyNetSession>();

            // Assert
            AssertSpecifiedServiceTypeAndImplementationType <IEasyNetSession, TestSession>(services, ServiceLifetime.Scoped);
            Assert.Equal("1", session.UserId);
            Assert.Equal("Test", session.UserName);
            Assert.Equal("Admin", session.Role);
        }
Пример #5
0
        public void TestAddEasyNet()
        {
            // Arrange
            var services = new ServiceCollection();

            services.AddSingleton(CommonTest.GetHostingEnvironment());

            // Act
            services.AddEasyNet();
            var serviceProvider = services.BuildServiceProvider();

            // Assert
            AssertSpecifiedServiceTypeAndImplementationType <IHttpContextAccessor, HttpContextAccessor>(services, ServiceLifetime.Singleton);
            AssertSpecifiedServiceTypeAndImplementationType <EasyNetUowActionFilter, EasyNetUowActionFilter>(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <ICurrentUnitOfWorkProvider, AsyncLocalCurrentUnitOfWorkProvider>(services, ServiceLifetime.Scoped);
            AssertSpecifiedServiceTypeAndImplementationType <IUnitOfWorkManager, UnitOfWorkManager>(services, ServiceLifetime.Scoped);
            AssertSpecifiedServiceTypeAndImplementationType <IUnitOfWork, NullUnitOfWork>(services, ServiceLifetime.Transient);
            AssertSpecifiedServiceTypeAndImplementationType <IPrincipalAccessor, DefaultPrincipalAccessor>(services, ServiceLifetime.Singleton);
            AssertSpecifiedServiceTypeAndImplementationType <IEasyNetSession, ClaimsEasyNetSession>(services, ServiceLifetime.Scoped);

            var mvcOptions = serviceProvider.GetRequiredService <IOptions <MvcOptions> >().Value;

            Assert.Contains(mvcOptions.Filters, p => ((ServiceFilterAttribute)p).ServiceType == typeof(EasyNetUowActionFilter));
        }