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"); }
public async Task ShouldRaiseQueryEvents() { var interceptor = new TestEntityManagerDelegate(); CompositionContext contextWithEntityManagerDelegate = CompositionContext.Fake .WithGenerator(typeof(EntityManagerDelegate), () => interceptor) .WithName("ShouldRaiseQueryEvents"); CompositionContextResolver.Add(contextWithEntityManagerDelegate); IEntityManagerProvider <NorthwindIBEntities> emp = EntityManagerProviderFactory.CreateTestEntityManagerProvider("ShouldRaiseQueryEvents"); Assert.IsTrue(interceptor.QueriedRaised == 0); Assert.IsTrue(interceptor.QueryingRaised == 0); Assert.IsTrue(interceptor.FetchingRaised == 0); await InitFakeBackingStoreAsync(emp.ConnectionOptions.CompositionContext.Name); await emp.Manager.Customers.ExecuteAsync(); Assert.IsTrue(interceptor.QueriedRaised > 0); Assert.IsTrue(interceptor.QueryingRaised > 0); Assert.IsTrue(interceptor.FetchingRaised > 0); Assert.IsTrue(interceptor.QueryingRaised < interceptor.FetchingRaised); Assert.IsTrue(interceptor.FetchingRaised < interceptor.QueriedRaised); }
public void ShouldRaiseClearedEvent() { var interceptor = new TestEntityManagerDelegate(); CompositionContext contextWithEntityManagerDelegate = CompositionContext.Fake .WithGenerator(typeof(EntityManagerDelegate), () => interceptor) .WithName("ShouldRaiseClearedEvent"); CompositionContextResolver.Add(contextWithEntityManagerDelegate); IEntityManagerProvider <NorthwindIBEntities> emp = EntityManagerProviderFactory.CreateTestEntityManagerProvider("ShouldRaiseClearedEvent"); Assert.IsTrue(interceptor.ClearedRaised == 0); emp.Manager.Clear(); Assert.IsTrue(interceptor.ClearedRaised > 0); }
public async Task ShouldSynchronizeDeletesBetweenEntityManagers() { CompositionContext compositionContextWithSyncInterceptor = CompositionContext.Fake .WithGenerator(typeof(IEntityManagerSyncInterceptor), () => new SyncInterceptor()) .WithName("ShouldSynchronizeDeletesBetweenEntityManagers"); CompositionContextResolver.Add(compositionContextWithSyncInterceptor); var rep1 = new CustomerRepository( EntityManagerProviderFactory.CreateTestEntityManagerProvider( "ShouldSynchronizeDeletesBetweenEntityManagers")); var rep2 = new CustomerRepository( EntityManagerProviderFactory.CreateTestEntityManagerProvider( "ShouldSynchronizeDeletesBetweenEntityManagers")); await InitFakeBackingStoreAsync("ShouldSynchronizeDeletesBetweenEntityManagers"); var customers = await rep1.GetCustomersAsync(null); var customers2 = await rep2.GetCustomersAsync(null); Customer customer = customers.First(); Assert.IsNotNull( rep1.EntityManagerProvider.Manager.FindEntity(customer.EntityAspect.EntityKey), "Customer should be in EM1's cache"); Assert.IsNotNull( rep2.EntityManagerProvider.Manager.FindEntity(customer.EntityAspect.EntityKey), "Customer should be in EM2's cache"); rep1.DeleteCustomer(customer); Assert.IsNull( rep1.EntityManagerProvider.Manager.FindEntity(customer.EntityAspect.EntityKey), "Customer should have been removed from first cache"); Assert.IsNotNull( rep2.EntityManagerProvider.Manager.FindEntity(customer.EntityAspect.EntityKey), "Customer should still be in second EntityManager"); await rep1.SaveAsync(); Assert.IsNull(rep2.EntityManagerProvider.Manager.FindEntity(customer.EntityAspect.EntityKey), "Customer should have been removed from second EntityManager"); }
public async Task ShouldRaiseSaveEvents() { var interceptor = new TestEntityManagerDelegate(); CompositionContext contextWithEntityManagerDelegate = CompositionContext.Fake .WithGenerator(typeof(EntityManagerDelegate), () => interceptor) .WithName("ShouldRaiseSaveEvents"); CompositionContextResolver.Add(contextWithEntityManagerDelegate); IEntityManagerProvider <NorthwindIBEntities> emp = EntityManagerProviderFactory.CreateTestEntityManagerProvider("ShouldRaiseSaveEvents"); int entityChangedRaised = 0; emp.EntityChanged += (sender, args) => entityChangedRaised++; Assert.IsTrue(interceptor.SavingRaised == 0); Assert.IsTrue(interceptor.SavedRaised == 0); Assert.IsTrue(interceptor.EntityChangingRaised == 0); Assert.IsTrue(interceptor.EntityChangedRaised == 0); var customer = emp.Manager.CreateEntity <Customer>(); emp.Manager.AddEntity(customer); customer.CompanyName = "Foo"; await InitFakeBackingStoreAsync(emp.Manager.CompositionContext.Name); await emp.Manager.SaveChangesAsync(); Assert.IsTrue(entityChangedRaised == 3); Assert.IsTrue(interceptor.SavingRaised > 0); Assert.IsTrue(interceptor.SavedRaised > 0); Assert.IsTrue(interceptor.EntityChangingRaised > 0); Assert.IsTrue(interceptor.EntityChangedRaised > 0); Assert.IsTrue(interceptor.EntityChangingRaised < interceptor.EntityChangedRaised); Assert.IsTrue(interceptor.SavingRaised < interceptor.SavedRaised); Assert.IsTrue(interceptor.SavingRaised < interceptor.EntityChangingRaised); Assert.IsTrue(interceptor.EntityChangedRaised < interceptor.SavedRaised); }
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"); }