public async Task ShouldRetrieveCustomerWithIdFromCache() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var id = SampleDataProvider.CreateGuid(1); Assert.IsFalse(unitOfWork.Entities.ExistsInCache(id)); Customer customer = null; try { customer = unitOfWork.Entities.WithIdFromCache(id); } catch (EntityNotFoundException) { // Expected exception } Assert.IsNull(customer); // Fetch from data source await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); await unitOfWork.Entities.WithIdFromDataSourceAsync(id); Assert.IsTrue(unitOfWork.Entities.ExistsInCache(id)); customer = unitOfWork.Entities.WithIdFromCache(id); Assert.IsNotNull(customer); Assert.IsTrue(customer.CustomerID == id); }
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 ShouldCreateEntityWithInternalCtor() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <EntityWithInternalCtor>(provider); var task = unitOfWork.Factory.CreateAsync(); Assert.IsNotNull(task.Result); Assert.IsTrue(task.Result.EntityAspect.EntityState.IsAdded()); }
public async Task ShouldRetrieveAllCustomersAndOrders() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); unitOfWork.Clear(); var customers = await unitOfWork.Entities.AllAsync(); Assert.IsTrue(customers.Any()); Assert.IsFalse(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllAsync(fetchOptions : options => options.Include(x => x.Orders)); Assert.IsTrue(customers.Any()); Assert.IsTrue(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllAsync(CancellationToken.None); Assert.IsTrue(customers.Any()); Assert.IsFalse(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllAsync(CancellationToken.None, fetchOptions : options => options.Include("Orders")); Assert.IsTrue(customers.Any()); Assert.IsTrue(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllInDataSourceAsync(); Assert.IsTrue(customers.Any()); Assert.IsFalse(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllInDataSourceAsync(fetchOptions : options => options.Include("Orders")); Assert.IsTrue(customers.Any()); Assert.IsTrue(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllInDataSourceAsync(CancellationToken.None); Assert.IsTrue(customers.Any()); Assert.IsFalse(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); unitOfWork.Clear(); customers = await unitOfWork.Entities.AllInDataSourceAsync(CancellationToken.None, fetchOptions : options => options.Include("Orders")); Assert.IsTrue(customers.Any()); Assert.IsTrue(provider.Manager.FindEntities <Order>(EntityState.AllButDetached).Any()); }
public async Task ShouldRetrieveTopCustomers() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var customers = await unitOfWork.Entities.AllAsync(fetchOptions : options => options.Take(2)); Assert.IsTrue(customers.Count() == 2); }
public async Task ShouldRetrieveCustomersWithSelector() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var result = await unitOfWork.Entities.FindAsync(q => q.Select(x => x.CompanyName), x => x.City == "SomeCity", q => q.OrderBy(x => x)); Assert.IsTrue(result.Any()); }
public async Task ShouldCancelQueryTask() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var em = provider.Manager; em.Querying += (sender, args) => args.Cancel = true; await unitOfWork.Entities.AllAsync() .ContinueWith(task => Assert.IsTrue(task.IsCanceled, "Should be cancelled")); }
public async Task ShouldGetCustomers() { var emp = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var repository = new CustomerRepository(emp); await InitFakeBackingStoreAsync(emp.Manager.CompositionContext.Name); var customers = await repository.GetCustomersAsync(null); Assert.IsTrue(customers.Any(), "Should have some customers"); }
public async Task ShouldRetrieveCustomersIfSortedWithSortFunction() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var customers = await unitOfWork.Entities.AllAsync(q => q.OrderBy(c => c.City)); Assert.IsTrue(customers.Any()); }
public async Task ShouldRetrieveCustomersWithPredicateExpression() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var customers = await unitOfWork.Entities.FindAsync(c => c.City == "SomeCity"); Assert.IsTrue(customers.Any()); Assert.IsTrue(customers.All(c => c.City == "SomeCity")); }
public async Task ShouldRetrieveCustomersWithPredicateDescription() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var pd = PredicateBuilder.Make("City", FilterOperator.IsEqualTo, "SomeCity"); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var customers = await unitOfWork.Entities.FindAsync(pd.ToPredicate <Customer>()); Assert.IsTrue(customers.Any()); Assert.IsTrue(customers.All(c => c.City == "SomeCity")); }
public async Task ShouldRetrieveDistinctCitiesDynamic() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var cities = await unitOfWork.Entities.FindAsync( q => q.Select(new ProjectionSelector("City")), fetchOptions : options => options.Distinct()); Assert.IsTrue(cities.Cast <string>().Any()); Assert.IsTrue(cities.Cast <string>().GroupBy(x => x).All(x => x.Count() == 1)); }
public async Task ShouldCancelProjectionQueryTask() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var cts = new CancellationTokenSource(); cts.Cancel(); await unitOfWork.Entities.FindAsync(q => q.Select(x => x.CompanyName), cts.Token) .ContinueWith(task => Assert.IsTrue(task.IsCanceled, "Should be cancelled")); await unitOfWork.Entities.FindInDataSourceAsync(q => q.Select(x => x.CompanyName), cts.Token) .ContinueWith(task => Assert.IsTrue(task.IsCanceled, "Should be cancelled")); }
public async Task ShouldRetrieveCustomerWithId() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var id = SampleDataProvider.CreateGuid(1); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var customer = await unitOfWork.Entities.WithIdAsync(id); Assert.IsNotNull(customer); Assert.IsTrue(customer.CustomerID == id); }
public async Task ShouldRetrieveAllCustomers() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var expectedCount = await unitOfWork.Entities.CountAsync(); var customers = await unitOfWork.Entities.AllAsync(q => q.OrderBy(x => x.CompanyName)); Assert.IsTrue(customers.Count() == expectedCount); Assert.IsTrue(unitOfWork.Entities.CountInCache() == expectedCount); }
public async Task ShouldCancelQueryTaskWithToken() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var cts = new CancellationTokenSource(); cts.Cancel(); await unitOfWork.Entities.AllAsync(cts.Token) .ContinueWith(task => Assert.IsTrue(task.IsCanceled, "Should be cancelled")); await unitOfWork.Entities.AllInDataSourceAsync(cts.Token) .ContinueWith(task => Assert.IsTrue(task.IsCanceled, "Should be cancelled")); }
public async Task ShouldRetrieveOrderProjectionAndCustomer() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var orders = await unitOfWork.Entities.FindAsync( q => q.SelectMany(x => x.Orders), fetchOptions : options => options.Include(x => x.Customer)); Assert.IsTrue(orders.Any()); var customers = provider.Manager.FindEntities <Customer>(EntityState.AllButDetached); Assert.IsTrue(customers.Any()); }
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 ShouldCancelSaveTask() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var em = provider.Manager; var customer = new Customer { CustomerID = Guid.NewGuid(), CompanyName = "Foo" }; em.AddEntity(customer); em.Saving += (sender, args) => args.Cancel = true; Assert.IsTrue(em.HasChanges()); await unitOfWork.CommitAsync() .ContinueWith(task => Assert.IsTrue(task.IsCanceled, "Should be cancelled")); }
public async Task ShouldPageCustomersWithPredicate() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var repository = new PagerRepository <Customer>(provider); var sortSelector = new SortSelector("CompanyName"); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var pager = repository.Pager(sortSelector, 2, x => x.City == "SomeCity"); var page = await pager.LastPageAsync(); Assert.IsTrue(page.PageWasFound); Assert.IsTrue(page.Results.Count() == 1); Assert.IsTrue(pager.TotalItemCount == 3); Assert.IsTrue(pager.TotalNumberOfPages == 2); Assert.IsTrue(page.PageIndex == 1); }
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 ShouldRetrieveCustomersWithSelectorFromCache() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var customers = await unitOfWork.Entities.FindInDataSourceAsync(x => x.City == "SomeCity", q => q.OrderBy(x => x.CompanyName)); Assert.IsTrue(customers.Any()); var names = unitOfWork.Entities.FindInCache( q => q.Select(x => x.CompanyName), x => x.City == "SomeCity", q => q.OrderBy(x => x)); Assert.IsTrue(names.Count() == customers.Count()); Assert.IsTrue( names.All((value, index) => customers.ElementAt(index).CompanyName == value)); }
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 ShouldRetrieveCustomersWithPredicateExpressionFromCache() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); Expression <Func <Customer, bool> > expression = c => c.City == "SomeCity"; var entities = unitOfWork.Entities.FindInCache(expression); Assert.IsFalse(entities.Any()); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var result = await unitOfWork.Entities.FindInDataSourceAsync(expression); Assert.IsTrue(result.Any()); Assert.IsTrue(result.All(c => c.City == "SomeCity")); entities = unitOfWork.Entities.FindInCache(expression); Assert.IsTrue(entities.Count() == result.Count()); Assert.IsTrue(entities.All(c => c.City == "SomeCity")); }
public async Task ShouldRetrieveCustomersWithProjectionSelector() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var unitOfWork = new UnitOfWork <Customer>(provider); var selector = ProjectionSelector.Combine(new[] { new ProjectionSelector("CustomerID"), new ProjectionSelector("CompanyName") }); var pd = PredicateBuilder.Make("City", FilterOperator.IsEqualTo, "SomeCity"); var sortSelector = new SortSelector("CompanyName"); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var result = await unitOfWork.Entities.FindAsync( q => q.Select(selector), pd.ToPredicate <Customer>(), q => q.OrderBySelector(sortSelector)); Assert.IsTrue(result.Cast <object>().Any()); }
public async Task ShouldPageProjection() { var provider = EntityManagerProviderFactory.CreateTestEntityManagerProvider(); var repository = new PagerRepository <Customer>(provider); var sortSelector = new SortSelector("CompanyName"); await InitFakeBackingStoreAsync(CompositionContext.Fake.Name); var pager = repository.Pager( q => q.Select(x => new PageProjection() { CompanyName = x.CompanyName, City = x.City }), 2, sortSelector, x => x.City == "SomeCity"); var page = await pager.FirstPageAsync(); Assert.IsTrue(page.PageWasFound); Assert.IsTrue(page.Results.Count() == 2); Assert.IsTrue(page.PageIndex == 0); }