Example #1
0
        public async Task ShouldSyncBetweenDifferentEMTypes()
        {
            var context = CompositionContext
                          .Fake
                          .WithGenerator(typeof(IEntityManagerSyncInterceptor), () => new SyncInterceptor())
                          .WithName("ShouldSyncBetweenDifferentEMTypes");

            CompositionContextResolver.Add(context);

            var connectionOptions =
                ConnectionOptions.Default.WithCompositionContext(context.Name)
                .WithName("ShouldSyncBetweenDifferentEMTypes");

            ConnectionOptionsResolver.Add(connectionOptions);

            var emp1 = new EntityManagerProvider <EntityManager>()
                       .Configure(x => x.WithConnectionOptions(connectionOptions.Name));
            var emp2 = new EntityManagerProvider <NorthwindIBEntities>()
                       .Configure(x => x.WithConnectionOptions(connectionOptions.Name));

            await InitFakeBackingStoreAsync(context.Name);

            var customer = emp1.Manager.FindEntities <Customer>(EntityState.AllButDetached).FirstOrDefault();

            Assert.IsNull(customer);

            customer = await emp2.Manager.Customers.AsScalarAsync().FirstOrDefault();

            customer.CompanyName = "Foo";
            await emp2.Manager.SaveChangesAsync();

            customer = emp1.Manager.FindEntities <Customer>(EntityState.AllButDetached).FirstOrDefault();
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.CompanyName == "Foo");
        }
Example #2
0
        public void ShouldReturnDesignTimeConnectionOptions()
        {
            var emp = new EntityManagerProvider <EntityManager>()
                      .Configure(provider => provider.WithConnectionOptions(ConnectionOptions.DesignTime.Name));

            Assert.IsTrue(emp.ConnectionOptions.IsDesignTime);
            Assert.IsFalse(emp.ConnectionOptions.IsFake);
        }
Example #3
0
        public async Task ShouldLoginLogout()
        {
            bool principalChangedFired = false;
            bool loggedInFired = false;
            bool loggedOutFired = false;
            bool managerCreatedFired = false;

            var auth = new AuthenticationService();
            CompositionContext contextWithAuthService = CompositionContext.Fake
                .WithGenerator(typeof(IAuthenticationService), () => auth)
                .WithName("ShouldLoginLogout");
            CompositionContextResolver.Add(contextWithAuthService);
            var connectionOptions =
                ConnectionOptions.Default.WithCompositionContext("ShouldLoginLogout").WithName("ShouldLoginLogout");
            ConnectionOptionsResolver.Add(connectionOptions);
            var emp = new EntityManagerProvider<NorthwindIBEntities>()
                .Configure(provider => provider.WithConnectionOptions("ShouldLoginLogout"));

            auth.PrincipalChanged += (s, e) => principalChangedFired = true;
            auth.LoggedIn += (s, e) => loggedInFired = true;
            auth.LoggedOut += (s, e) => loggedOutFired = true;

            emp.ManagerCreated += (s, e) => managerCreatedFired = true;
            NorthwindIBEntities manager = null;

            await InitFakeBackingStoreAsync("ShouldLoginLogout");
            await auth.LoginAsync(new LoginCredential("test", "test", null));

            Assert.IsTrue(principalChangedFired, "PrincipalChanged should have been fired");
            Assert.IsTrue(loggedInFired, "LoggedIn should have been fired");
            Assert.IsFalse(loggedOutFired, "LoggedOut shouldn't have been fired");
            Assert.IsTrue(auth.IsLoggedIn, "Should be logged in");
            Assert.IsTrue(auth.Principal.Identity.Name == "test", "Username should be test");

            manager = emp.Manager;

            Assert.IsTrue(managerCreatedFired, "ManagerCreated should have been fired");
            Assert.IsNotNull(manager.AuthenticationContext.Principal,
                            "The principal should not be null on the EntitiyManager");
            Assert.IsTrue(manager.AuthenticationContext.Principal.Identity.Name == "test",
                            "Principal should have the same username");

            principalChangedFired = false;
            loggedInFired = false;
            managerCreatedFired = false;

            await auth.LogoutAsync();

            Assert.IsTrue(principalChangedFired,
                          "PrincipalChanged should have been fired");
            Assert.IsFalse(loggedInFired,
                           "LoggedIn shouldn't have been fired");
            Assert.IsTrue(loggedOutFired, "LoggedOut should have been fired");
            Assert.IsFalse(auth.IsLoggedIn, "Should be logged out");
            Assert.IsNull(auth.Principal, "Principal should be null");

            Assert.IsFalse(manager.IsConnected,
                           "Old EntityManager should be disconnected");

            bool exceptionThrown = false;
            try
            {
                await manager.Customers.ExecuteAsync();
            }
            catch (InvalidOperationException)
            {
                exceptionThrown = true;
            }
            Assert.IsTrue(exceptionThrown, "Should have thrown an error");

            manager = emp.Manager;
            Assert.IsTrue(managerCreatedFired,
                          "ManagerCreated should have been fired");
            Assert.IsNull(manager.AuthenticationContext.Principal,
                          "The principal should be null on the EntitiyManager");

        }
Example #4
0
        public async Task ShouldSyncBetweenDifferentEMTypes()
        {
            var context = CompositionContext
                .Fake
                .WithGenerator(typeof(IEntityManagerSyncInterceptor), () => new SyncInterceptor())
                .WithName("ShouldSyncBetweenDifferentEMTypes");
            CompositionContextResolver.Add(context);

            var connectionOptions =
                ConnectionOptions.Default.WithCompositionContext(context.Name)
                                 .WithName("ShouldSyncBetweenDifferentEMTypes");
            ConnectionOptionsResolver.Add(connectionOptions);

            var emp1 = new EntityManagerProvider<EntityManager>()
                .Configure(x => x.WithConnectionOptions(connectionOptions.Name));
            var emp2 = new EntityManagerProvider<NorthwindIBEntities>()
                .Configure(x => x.WithConnectionOptions(connectionOptions.Name));

            await InitFakeBackingStoreAsync(context.Name);

            var customer = emp1.Manager.FindEntities<Customer>(EntityState.AllButDetached).FirstOrDefault();
            Assert.IsNull(customer);

            customer = await emp2.Manager.Customers.AsScalarAsync().FirstOrDefault();
            customer.CompanyName = "Foo";
            await emp2.Manager.SaveChangesAsync();

            customer = emp1.Manager.FindEntities<Customer>(EntityState.AllButDetached).FirstOrDefault();
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.CompanyName == "Foo");
        }
Example #5
0
 public void ShouldReturnDesignTimeConnectionOptions()
 {
     var emp = new EntityManagerProvider<EntityManager>()
         .Configure(provider => provider.WithConnectionOptions(ConnectionOptions.DesignTime.Name));
     Assert.IsTrue(emp.ConnectionOptions.IsDesignTime);
     Assert.IsFalse(emp.ConnectionOptions.IsFake);
 }
Example #6
0
        public async Task ShouldLoginLogout()
        {
            bool principalChangedFired = false;
            bool loggedInFired         = false;
            bool loggedOutFired        = false;
            bool managerCreatedFired   = false;

            var auth = new AuthenticationService();
            CompositionContext contextWithAuthService = CompositionContext.Fake
                                                        .WithGenerator(typeof(IAuthenticationService), () => auth)
                                                        .WithName("ShouldLoginLogout");

            CompositionContextResolver.Add(contextWithAuthService);
            var connectionOptions =
                ConnectionOptions.Default.WithCompositionContext("ShouldLoginLogout").WithName("ShouldLoginLogout");

            ConnectionOptionsResolver.Add(connectionOptions);
            var emp = new EntityManagerProvider <NorthwindIBEntities>()
                      .Configure(provider => provider.WithConnectionOptions("ShouldLoginLogout"));

            auth.PrincipalChanged += (s, e) => principalChangedFired = true;
            auth.LoggedIn         += (s, e) => loggedInFired = true;
            auth.LoggedOut        += (s, e) => loggedOutFired = true;

            emp.ManagerCreated += (s, e) => managerCreatedFired = true;
            NorthwindIBEntities manager = null;

            await InitFakeBackingStoreAsync("ShouldLoginLogout");

            await auth.LoginAsync(new LoginCredential("test", "test", null));

            Assert.IsTrue(principalChangedFired, "PrincipalChanged should have been fired");
            Assert.IsTrue(loggedInFired, "LoggedIn should have been fired");
            Assert.IsFalse(loggedOutFired, "LoggedOut shouldn't have been fired");
            Assert.IsTrue(auth.IsLoggedIn, "Should be logged in");
            Assert.IsTrue(auth.Principal.Identity.Name == "test", "Username should be test");

            manager = emp.Manager;

            Assert.IsTrue(managerCreatedFired, "ManagerCreated should have been fired");
            Assert.IsNotNull(manager.AuthenticationContext.Principal,
                             "The principal should not be null on the EntitiyManager");
            Assert.IsTrue(manager.AuthenticationContext.Principal.Identity.Name == "test",
                          "Principal should have the same username");

            principalChangedFired = false;
            loggedInFired         = false;
            managerCreatedFired   = false;

            await auth.LogoutAsync();

            Assert.IsTrue(principalChangedFired,
                          "PrincipalChanged should have been fired");
            Assert.IsFalse(loggedInFired,
                           "LoggedIn shouldn't have been fired");
            Assert.IsTrue(loggedOutFired, "LoggedOut should have been fired");
            Assert.IsFalse(auth.IsLoggedIn, "Should be logged out");
            Assert.IsNull(auth.Principal, "Principal should be null");

            Assert.IsFalse(manager.IsConnected,
                           "Old EntityManager should be disconnected");

            bool exceptionThrown = false;

            try
            {
                await manager.Customers.ExecuteAsync();
            }
            catch (InvalidOperationException)
            {
                exceptionThrown = true;
            }
            Assert.IsTrue(exceptionThrown, "Should have thrown an error");

            manager = emp.Manager;
            Assert.IsTrue(managerCreatedFired,
                          "ManagerCreated should have been fired");
            Assert.IsNull(manager.AuthenticationContext.Principal,
                          "The principal should be null on the EntitiyManager");
        }