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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
        public void Equal_GetHashCode_ToString(int x1, int y1, int x2, int y2, bool expectedResult)
        {
            // Arrange
            var p1 = new Position(x1, y1);
            var p2 = new Position(x2, y2);

            CommonTest.TestOverridedObjectMethods(expectedResult, p1, p2);
        }
Exemplo n.º 5
0
        public void Equal_GetHashCode_ToString_NoCost(int v1, int v2, int v3, int v4, bool expectedResult)
        {
            // Arrange
            var p1 = GetEdge(v1, v2);
            var p2 = GetEdge(v3, v4);

            CommonTest.TestOverridedObjectMethods(expectedResult, p1, p2);
        }
Exemplo n.º 6
0
        public async Task TestEasyNetUowActionFilter(bool isControllerAction, bool unitOfWorkAutoStart)
        {
            // Arrange
            var completeHandleMock = new Mock <IUnitOfWorkCompleteHandle>();

            var uowMock = new Mock <IUnitOfWorkManager>();

            uowMock
            .Setup(f => f.Begin())
            .Returns(() => completeHandleMock.Object);

            var optionsMock = new Mock <IOptions <EasyNetOptions> >();

            optionsMock
            .Setup(f => f.Value)
            .Returns(() => new EasyNetOptions {
                UnitOfWorkAutoStart = unitOfWorkAutoStart
            });

            var uowActionFilterMock = new Mock <EasyNetUowActionFilter>(uowMock.Object, optionsMock.Object);

            uowActionFilterMock.As <IAsyncActionFilter>()
            .Setup(f => f.OnActionExecutionAsync(
                       It.IsAny <ActionExecutingContext>(),
                       It.IsAny <ActionExecutionDelegate>()))
            .CallBase();

            var context = isControllerAction
                ? CommonTest.CreateControllerActionExecutingContext(uowActionFilterMock.As <IFilterMetadata>().Object)
                : CommonTest.CreateActionExecutingContext(uowActionFilterMock.As <IFilterMetadata>().Object);
            var next = new ActionExecutionDelegate(() =>
                                                   Task.FromResult(CommonTest.CreateActionExecutedContext(context)));

            // Act
            await uowActionFilterMock.As <IAsyncActionFilter>().Object.OnActionExecutionAsync(context, next);

            // Assert
            var times = !isControllerAction || !unitOfWorkAutoStart?Times.Never() : Times.Once();

            uowMock.Verify(f => f.Begin(), times);
            completeHandleMock.Verify(f => f.CompleteAsync(), times);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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));
        }
 public static Task InitializeClass(TestContext context) => CommonTest.Initialize(context);