public void TasksAreExecutedAutomaticallyWhenCountLimitIsReachedTest()
        {
            const int entityCount = 120;
            var       keys        = new List <Key>(entityCount);

            using (var session = Domain.OpenSession())
                using (var tx = session.OpenTransaction()) {
                    for (int i = 0; i < entityCount; i++)
                    {
                        keys.Add(new Book().Key);
                    }
                    tx.Complete();
                }

            using (var session = Domain.OpenSession())
                using (var tx = session.OpenTransaction()) {
                    var prefetchManager = (PrefetchManager)PrefetchProcessorField.GetValue(session.Handler);
                    var graphContainers = (SetSlim <GraphContainer>)GraphContainersField.GetValue(prefetchManager);
                    var idField         = BookType.Fields["Id"];
                    for (var i = 1; i < keys.Count; i++)
                    {
                        prefetchManager.InvokePrefetch(keys[i - 1], null, new PrefetchFieldDescriptor(idField));
                        Assert.AreEqual(i % entityCount, graphContainers.Count);
                    }
                    prefetchManager.InvokePrefetch(keys[entityCount - 1], null, new PrefetchFieldDescriptor(idField));
                    Assert.AreEqual(0, graphContainers.Count);
                    for (var i = 0; i < entityCount; i++)
                    {
                        PrefetchTestHelper.AssertOnlySpecifiedColumnsAreLoaded(keys[i], BookType, session,
                                                                               IsFieldKeyOrSystem);
                    }
                }
        }
        public void PrefetchEmptyEntitySetTest()
        {
            Key orderKey;

            using (var session = Domain.OpenSession())
                using (var tx = session.OpenTransaction()) {
                    var order    = session.Query.All <Order>().OrderBy(o => o.Id).First();
                    var newOrder = new Order {
                        Employee = null, Customer = order.Customer
                    };
                    orderKey = newOrder.Key;
                    tx.Complete();
                }

            using (var session = Domain.OpenSession())
                using (var tx = session.OpenTransaction()) {
                    var prefetchManager = (PrefetchManager)PrefetchProcessorField.GetValue(session.Handler);
                    session.Handler.FetchEntityState(orderKey);
                    prefetchManager.InvokePrefetch(orderKey, null, new PrefetchFieldDescriptor(DetailsField, null));
                    var graphContainers = (SetSlim <GraphContainer>)GraphContainersField.GetValue(prefetchManager);
                    Assert.AreEqual(1, graphContainers.Count);
                    prefetchManager.ExecuteTasks(true);
                    EntitySetState actualState;
                    session.Handler.LookupState(orderKey, DetailsField, out actualState);
                    Assert.AreEqual(0, actualState.TotalItemCount);
                    Assert.IsTrue(actualState.IsFullyLoaded);
                }
        }
        public void ReferencedEntityHasBeenFullyLoadedBeforeTaskActivationTest()
        {
            Require.AnyFeatureSupported(ProviderFeatures.RowNumber | ProviderFeatures.NativePaging);
            Key order0Key;
            Key employee0Key;
            Key order1Key;
            Key employee1Key;

            using (var session = Domain.OpenSession())
                using (var tx = session.OpenTransaction()) {
                    var order0 = session.Query.All <Order>().OrderBy(o => o.Id).First();
                    var order1 = session.Query.All <Order>().OrderBy(o => o.Id).Skip(1).First();
                    order0Key    = order0.Key;
                    employee0Key = order0.Employee.Key;
                    order1Key    = order1.Key;
                    employee1Key = order1.Employee.Key;
                }

            using (var session = Domain.OpenSession())
                using (session.OpenTransaction()) {
                    var prefetchManager   = (PrefetchManager)PrefetchProcessorField.GetValue(session.Handler);
                    var employeeNameField = Domain.Model.Types[typeof(Person)].Fields["Name"];
                    var employeeAgeField  = employee1Key.TypeInfo.Fields["Age"];
                    prefetchManager.InvokePrefetch(employee0Key, null,
                                                   new PrefetchFieldDescriptor(employeeNameField), new PrefetchFieldDescriptor(AgeField));
                    prefetchManager.InvokePrefetch(employee1Key, null, new PrefetchFieldDescriptor(AgeField));
                    prefetchManager.InvokePrefetch(order0Key, null, new PrefetchFieldDescriptor(OrderIdField));
                    prefetchManager.InvokePrefetch(order1Key, null, new PrefetchFieldDescriptor(OrderIdField));
                    prefetchManager.ExecuteTasks(true);

                    prefetchManager.InvokePrefetch(order0Key, null, new PrefetchFieldDescriptor(EmployeeField, true, true));
                    prefetchManager.InvokePrefetch(order1Key, null, new PrefetchFieldDescriptor(EmployeeField, true, true));
                    var graphContainers = (SetSlim <GraphContainer>)GraphContainersField.GetValue(prefetchManager);
                    Assert.AreEqual(2, graphContainers.Count);
                    Func <Key, ReferencedEntityContainer> taskSelector = containerKey => graphContainers
                                                                         .Where(container => container.Key == containerKey)
                                                                         .SelectMany(container => container.ReferencedEntityContainers).Single();
                    var entityContainer0 = taskSelector.Invoke(order0Key);
                    var entityContainer1 = taskSelector.Invoke(order1Key);
                    prefetchManager.ExecuteTasks(true);
                    Assert.IsNull(entityContainer0.Task);
                    Assert.IsNotNull(entityContainer1.Task);
                    PrefetchTestHelper.AssertOnlySpecifiedColumnsAreLoaded(employee0Key, employee0Key.TypeInfo, session,
                                                                           PrefetchTestHelper.IsFieldToBeLoadedByDefault);
                    PrefetchTestHelper.AssertOnlySpecifiedColumnsAreLoaded(employee1Key, employee1Key.TypeInfo, session,
                                                                           PrefetchTestHelper.IsFieldToBeLoadedByDefault);
                }
        }
        public void PrefetchReferencedEntityWhenTypeSpecifiedForOwnerIsInvalidTest()
        {
            var productKey = GetFirstKey <Product>();

            using (var session = Domain.OpenSession())
                using (var tx = session.OpenTransaction()) {
                    var keyWithoutType  = Key.Create(Domain, typeof(PersonalProduct), productKey.Value);
                    var prefetchManager = (PrefetchManager)PrefetchProcessorField.GetValue(session.Handler);
                    prefetchManager.InvokePrefetch(keyWithoutType, Domain.Model.Types[typeof(PersonalProduct)],
                                                   new PrefetchFieldDescriptor(Domain.Model.Types[typeof(PersonalProduct)].Fields["Employee"],
                                                                               true, true));
                    var graphContainers           = (SetSlim <GraphContainer>)GraphContainersField.GetValue(prefetchManager);
                    var referencedEntityContainer = graphContainers
                                                    .Where(container => container.ReferencedEntityContainers != null).Single()
                                                    .ReferencedEntityContainers.Single();
                    prefetchManager.ExecuteTasks(true);
                    Assert.IsNull(referencedEntityContainer.Task);
                }
        }
        public void DeletingOfTasksAtTransactionCommitOrRollbackTest()
        {
            Key orderKey = GetFirstKey <Order>();

            using (var session = Domain.OpenSession()) {
                var prefetchManager = (PrefetchManager)PrefetchProcessorField.GetValue(session.Handler);
                SetSlim <GraphContainer> graphContainers;
                using (var tx = session.OpenTransaction()) {
                    prefetchManager.InvokePrefetch(orderKey, null, new PrefetchFieldDescriptor(CustomerField));
                    graphContainers = (SetSlim <GraphContainer>)GraphContainersField.GetValue(prefetchManager);
                    Assert.AreEqual(1, graphContainers.Count);
                    tx.Complete();
                }
                Assert.AreEqual(0, graphContainers.Count);

                using (var tx = session.OpenTransaction()) {
                    prefetchManager.InvokePrefetch(orderKey, null, new PrefetchFieldDescriptor(EmployeeField));
                    Assert.AreEqual(1, graphContainers.Count);
                    // tx.Complete();
                }
                Assert.AreEqual(0, graphContainers.Count);
            }
        }