public void TestCollectionQuery_DetachParent()
        {
            CatalogEntityContainer ec = new CatalogEntityContainer();

            NumNotifications = 0;

            ec.LoadEntities(new PurchaseOrder[] { TestOrder });

            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged += new NotifyCollectionChangedEventHandler(EntityCollectionChanged);

            Assert.AreEqual(0, TestOrder.PurchaseOrderDetails.Count);

            // load a detail and verify we are notified
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail {
                                                            PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID()
                                                        } });
            Assert.AreEqual(1, NumNotifications);

            // detach the parent entity and verify we no longer receive notifications
            NumNotifications    = 0;
            TestOrder.EntitySet = null;
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail {
                                                            PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID()
                                                        } });
            Assert.AreEqual(0, NumNotifications);
        }
        public void TestEntityCaching_NewEntities()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            // add two orders and a detail
            PurchaseOrder       order1 = new PurchaseOrder();
            PurchaseOrder       order2 = new PurchaseOrder();
            PurchaseOrderDetail detail = new PurchaseOrderDetail();

            container.GetEntitySet <PurchaseOrder>().Add(order1);
            container.GetEntitySet <PurchaseOrder>().Add(order2);
            container.GetEntitySet <PurchaseOrderDetail>().Add(detail);

            // examine the order ref of the detail - ensure that
            // no result is returned, since a FK query would match
            // BOTH orders
            Assert.IsNull(detail.PurchaseOrder);

            // now that we've cached a null, make sure that if more
            // new entities are added, the ref doesn't change
            container.GetEntitySet <PurchaseOrder>().Add(new PurchaseOrder());
            Assert.IsNull(detail.PurchaseOrder);

            // now assign order1, and remove order2 - make sure that our
            // ref to order1 remains
            detail.PurchaseOrder = order1;
            Assert.AreSame(order1, detail.PurchaseOrder);
            container.GetEntitySet <PurchaseOrder>().Remove(order2);
            Assert.AreSame(order1, detail.PurchaseOrder);

            container.GetEntitySet <PurchaseOrder>().Remove(order1);
            Assert.IsNull(detail.PurchaseOrder);
        }
        public void TestCollectionQuery_DetachedEntity()
        {
            CatalogEntityContainer ec = new CatalogEntityContainer();

            // with the order not part of any EntityContainer/Set,
            // its collection returns empty
            Assert.IsNull(TestOrder.EntitySet);
            Assert.AreEqual(0, TestOrder.PurchaseOrderDetails.Count());

            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged -= EntityCollectionChanged;
        }
        public void TestCollectionQuery_SubscribeAfterAttach()
        {
            CatalogEntityContainer ec = new CatalogEntityContainer();

            NumNotifications = 0;

            ec.LoadEntities(new PurchaseOrder[] { TestOrder });

            // here we subscribe to the event AFTER the entity is added
            // to the container
            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged += new NotifyCollectionChangedEventHandler(EntityCollectionChanged);

            TestNotifications(ec);
        }
        public void TestCollectionQuery_SubscribeBeforeAttach()
        {
            CatalogEntityContainer ec = new CatalogEntityContainer();

            NumNotifications = 0;

            // here we subscribe to the event BEFORE the entity is added
            // to the container
            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e) {
                NumNotifications++;
            };

            ec.LoadEntities(new PurchaseOrder[] { TestOrder });

            TestNotifications(ec);
        }
        public void TestEntityRefCaching()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            PurchaseOrderDetail detail = new PurchaseOrderDetail {
                PurchaseOrderDetailID = 1,
                PurchaseOrderID       = 1
            };
            PurchaseOrder order = new PurchaseOrder {
                PurchaseOrderID = 1
            };
            PurchaseOrder order2 = new PurchaseOrder {
                PurchaseOrderID = 2
            };

            container.LoadEntities(new Entity[] { order, order2 });
            container.LoadEntities(new Entity[] { detail });

            // force the EntityRef to cache
            Assert.AreSame(order, detail.PurchaseOrder);

            // clear the entity set to verify that the cached
            // entity is cleared
            EntitySet purchaseOrderSet = container.GetEntitySet <PurchaseOrder>();

            purchaseOrderSet.Clear();
            Assert.AreEqual(0, purchaseOrderSet.Count);

            // after the set has been cleared, we expect null
            Assert.IsNull(detail.PurchaseOrder);

            // change the FK and verify that we requery again, getting no match
            // since all orders have been cleared from the set
            detail.PurchaseOrderID = 2;
            Assert.AreSame(null, detail.PurchaseOrder);

            // Reload the order entities and verify we get the
            // correct order
            container.LoadEntities(new Entity[] { order, order2 });
            Assert.AreSame(order2, detail.PurchaseOrder);

            // reset the FK and verify that we requery to get the
            // right entity
            detail.PurchaseOrderID = 1;
            Assert.AreSame(order, detail.PurchaseOrder);
        }
        public void TestEntityCollectionCaching()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            PurchaseOrder order = new PurchaseOrder
            {
                PurchaseOrderID = 1
            };
            PurchaseOrderDetail detail1 = new PurchaseOrderDetail
            {
                PurchaseOrderID = 1, PurchaseOrderDetailID = 1
            };
            PurchaseOrderDetail detail2 = new PurchaseOrderDetail
            {
                PurchaseOrderID = 1, PurchaseOrderDetailID = 2
            };

            container.LoadEntities(new Entity[] { order, detail1, detail2 });

            Assert.AreEqual(2, order.PurchaseOrderDetails.Count);

            // now add a couple new details
            PurchaseOrderDetail detail3 = new PurchaseOrderDetail();
            PurchaseOrderDetail detail4 = new PurchaseOrderDetail();

            order.PurchaseOrderDetails.Add(detail3);
            order.PurchaseOrderDetails.Add(detail4);

            Assert.AreEqual(4, order.PurchaseOrderDetails.Count);

            // now modify the parent FK, which will cause the cached
            // results to be reset, but we expect the explicitly added
            // entities to be retained
            // Here we're using ApplyState to allow us to set a PK member w/o validation failure
            // since PK members cannot be changed. This test should really be based on an association
            // not involving PK, but the test is still valid this way.
            order.ApplyState(new Dictionary <string, object> {
                { "PurchaseOrderID", 2 }
            });
            Assert.AreEqual(2, order.PurchaseOrderDetails.Count);
            Assert.IsTrue(order.PurchaseOrderDetails.Contains(detail3));
            Assert.IsTrue(order.PurchaseOrderDetails.Contains(detail4));
        }
        private void TestNotifications(CatalogEntityContainer ec)
        {
            // with only the order in the container
            // its collection returns empty
            Assert.IsNotNull(TestOrder.EntitySet);
            Assert.AreEqual(0, TestOrder.PurchaseOrderDetails.Count());
            Assert.AreEqual(0, NumNotifications);

            // after we load some entities, we expect a change notification
            ec.LoadEntities(TestDetails);
            Assert.AreEqual(TestDetails.Count, NumNotifications);
            Assert.IsTrue(TestDetails.SequenceEqual(TestOrder.PurchaseOrderDetails));

            // now add an entity that doesn't match the predicate and
            // verify we don't get notified
            NumNotifications = 0;
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail {
                                                            PurchaseOrderID = 9, PurchaseOrderDetailID = GetUniquePurchaseOrderID()
                                                        } });
            Assert.AreEqual(0, NumNotifications);

            // now load one matching and verify we get notified
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail {
                                                            PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID()
                                                        } });
            Assert.AreEqual(1, NumNotifications);

            // verify we get notified if the set is cleared
            NumNotifications = 0;
            EntitySet <PurchaseOrderDetail> entitySet = ec.GetEntitySet <PurchaseOrderDetail>();

            entitySet.Clear();
            Assert.AreEqual(1, NumNotifications);

            // verify that we can reuse the set and continue getting notifications
            NumNotifications = 0;
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail {
                                                            PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID()
                                                        } });
            Assert.AreEqual(1, NumNotifications);
        }
        public void TestEntityRefCaching_Detach()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            PurchaseOrderDetail detail = new PurchaseOrderDetail
            {
                PurchaseOrderDetailID = 1,
                PurchaseOrderID       = 1
            };
            PurchaseOrder order = new PurchaseOrder
            {
                PurchaseOrderID = 1
            };

            container.LoadEntities(new Entity[] { order, detail });

            Assert.AreSame(order, detail.PurchaseOrder);

            // now detach the detail and verify that the
            // cached entity is still returned
            container.GetEntitySet <PurchaseOrderDetail>().Detach(detail);
            Assert.AreSame(order, detail.PurchaseOrder);
        }
        public void TestEntityCollectionCaching()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            PurchaseOrder order = new PurchaseOrder
            {
                PurchaseOrderID = 1
            };
            PurchaseOrderDetail detail1 = new PurchaseOrderDetail 
            { 
                PurchaseOrderID = 1, PurchaseOrderDetailID = 1 
            };
            PurchaseOrderDetail detail2 = new PurchaseOrderDetail
            {
                PurchaseOrderID = 1, PurchaseOrderDetailID = 2
            };
            container.LoadEntities(new Entity[] { order, detail1, detail2 });

            Assert.AreEqual(2, order.PurchaseOrderDetails.Count);

            // now add a couple new details
            PurchaseOrderDetail detail3 = new PurchaseOrderDetail();
            PurchaseOrderDetail detail4 = new PurchaseOrderDetail();
            order.PurchaseOrderDetails.Add(detail3);
            order.PurchaseOrderDetails.Add(detail4);

            Assert.AreEqual(4, order.PurchaseOrderDetails.Count);
            
            // now modify the parent FK, which will cause the cached
            // results to be reset, but we expect the explicitly added
            // entities to be retained
            // Here we're using ApplyState to allow us to set a PK member w/o validation failure
            // since PK members cannot be changed. This test should really be based on an association
            // not involving PK, but the test is still valid this way.
            order.ApplyState(new Dictionary<string, object> { { "PurchaseOrderID", 2 } });
            Assert.AreEqual(2, order.PurchaseOrderDetails.Count);
            Assert.IsTrue(order.PurchaseOrderDetails.Contains(detail3));
            Assert.IsTrue(order.PurchaseOrderDetails.Contains(detail4));
        }
        public void TestEntityCaching_NewEntities()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            // add two orders and a detail
            PurchaseOrder order1 = new PurchaseOrder();
            PurchaseOrder order2 = new PurchaseOrder();
            PurchaseOrderDetail detail = new PurchaseOrderDetail();
            container.GetEntitySet<PurchaseOrder>().Add(order1);
            container.GetEntitySet<PurchaseOrder>().Add(order2);
            container.GetEntitySet<PurchaseOrderDetail>().Add(detail);

            // examine the order ref of the detail - ensure that
            // no result is returned, since a FK query would match
            // BOTH orders
            Assert.IsNull(detail.PurchaseOrder);

            // now that we've cached a null, make sure that if more
            // new entities are added, the ref doesn't change
            container.GetEntitySet<PurchaseOrder>().Add(new PurchaseOrder());
            Assert.IsNull(detail.PurchaseOrder);

            // now assign order1, and remove order2 - make sure that our
            // ref to order1 remains
            detail.PurchaseOrder = order1;
            Assert.AreSame(order1, detail.PurchaseOrder);
            container.GetEntitySet<PurchaseOrder>().Remove(order2);
            Assert.AreSame(order1, detail.PurchaseOrder);

            container.GetEntitySet<PurchaseOrder>().Remove(order1);
            Assert.IsNull(detail.PurchaseOrder);
        }
        public void TestEntityRefCaching_Detach()
        {
            CatalogEntityContainer container = new CatalogEntityContainer();

            PurchaseOrderDetail detail = new PurchaseOrderDetail
            {
                PurchaseOrderDetailID = 1,
                PurchaseOrderID = 1
            };
            PurchaseOrder order = new PurchaseOrder
            {
                PurchaseOrderID = 1
            };

            container.LoadEntities(new Entity[] { order, detail });

            Assert.AreSame(order, detail.PurchaseOrder);

            // now detach the detail and verify that the
            // cached entity is still returned
            container.GetEntitySet<PurchaseOrderDetail>().Detach(detail);
            Assert.AreSame(order, detail.PurchaseOrder);
        }
        public void TestCollectionQuery_DetachedEntity() {
            CatalogEntityContainer ec = new CatalogEntityContainer();

            // with the order not part of any EntityContainer/Set,
            // its collection returns empty
            Assert.IsNull(TestOrder.EntitySet);
            Assert.AreEqual(0, TestOrder.PurchaseOrderDetails.Count());

            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged -= EntityCollectionChanged;
        }
        public void TestCollectionQuery_SubscribeBeforeAttach() {
            CatalogEntityContainer ec = new CatalogEntityContainer();
            NumNotifications = 0;

            // here we subscribe to the event BEFORE the entity is added
            // to the container
            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e) {
                NumNotifications++;
            };

            ec.LoadEntities(new PurchaseOrder[] { TestOrder });

            TestNotifications(ec);
        }
        public void TestCollectionQuery_SubscribeAfterAttach() {
            CatalogEntityContainer ec = new CatalogEntityContainer();
            NumNotifications = 0;

            ec.LoadEntities(new PurchaseOrder[] { TestOrder });

            // here we subscribe to the event AFTER the entity is added
            // to the container
            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged += new NotifyCollectionChangedEventHandler(EntityCollectionChanged);

            TestNotifications(ec);
        }
        public void TestCollectionQuery_DetachParent() {
            CatalogEntityContainer ec = new CatalogEntityContainer();
            NumNotifications = 0;

            ec.LoadEntities(new PurchaseOrder[] { TestOrder });

            ((INotifyCollectionChanged)TestOrder.PurchaseOrderDetails).CollectionChanged += new NotifyCollectionChangedEventHandler(EntityCollectionChanged);

            Assert.AreEqual(0, TestOrder.PurchaseOrderDetails.Count);

            // load a detail and verify we are notified
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail { PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID() } });
            Assert.AreEqual(1, NumNotifications);
            
            // detach the parent entity and verify we no longer receive notifications
            NumNotifications = 0;
            TestOrder.EntitySet = null;
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail { PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID() } });
            Assert.AreEqual(0, NumNotifications);
        }
        public void TestEntityRefCaching() {
            CatalogEntityContainer container = new CatalogEntityContainer();

            PurchaseOrderDetail detail = new PurchaseOrderDetail {
                PurchaseOrderDetailID = 1,
                PurchaseOrderID = 1
            };
            PurchaseOrder order = new PurchaseOrder {
                PurchaseOrderID = 1
            };
            PurchaseOrder order2 = new PurchaseOrder {
                PurchaseOrderID = 2
            };

            container.LoadEntities(new Entity[] { order, order2});
            container.LoadEntities(new Entity[] { detail });

            // force the EntityRef to cache
            Assert.AreSame(order, detail.PurchaseOrder);

            // clear the entity set to verify that the cached
            // entity is cleared
            EntitySet purchaseOrderSet = container.GetEntitySet<PurchaseOrder>();
            purchaseOrderSet.Clear();
            Assert.AreEqual(0, purchaseOrderSet.Count);

            // after the set has been cleared, we expect null
            Assert.IsNull(detail.PurchaseOrder);

            // change the FK and verify that we requery again, getting no match
            // since all orders have been cleared from the set
            detail.PurchaseOrderID = 2;
            Assert.AreSame(null, detail.PurchaseOrder);

            // Reload the order entities and verify we get the
            // correct order
            container.LoadEntities(new Entity[] { order, order2 });
            Assert.AreSame(order2, detail.PurchaseOrder);

            // reset the FK and verify that we requery to get the
            // right entity
            detail.PurchaseOrderID = 1;
            Assert.AreSame(order, detail.PurchaseOrder);
        }
        private void TestNotifications(CatalogEntityContainer ec) {
            // with only the order in the container
            // its collection returns empty
            Assert.IsNotNull(TestOrder.EntitySet);
            Assert.AreEqual(0, TestOrder.PurchaseOrderDetails.Count());
            Assert.AreEqual(0, NumNotifications);

            // after we load some entities, we expect a change notification
            ec.LoadEntities(TestDetails);
            Assert.AreEqual(TestDetails.Count, NumNotifications);
            Assert.IsTrue(TestDetails.SequenceEqual(TestOrder.PurchaseOrderDetails));

            // now add an entity that doesn't match the predicate and
            // verify we don't get notified
            NumNotifications = 0;
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail { PurchaseOrderID = 9, PurchaseOrderDetailID = GetUniquePurchaseOrderID() } });
            Assert.AreEqual(0, NumNotifications);

            // now load one matching and verify we get notified
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail { PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID() } });
            Assert.AreEqual(1, NumNotifications);

            // verify we get notified if the set is cleared
            NumNotifications = 0;
            EntitySet<PurchaseOrderDetail> entitySet = ec.GetEntitySet<PurchaseOrderDetail>();
            entitySet.Clear();
            Assert.AreEqual(1, NumNotifications);

            // verify that we can reuse the set and continue getting notifications
            NumNotifications = 0;
            ec.LoadEntities(new PurchaseOrderDetail[] { new PurchaseOrderDetail { PurchaseOrderID = 1, PurchaseOrderDetailID = GetUniquePurchaseOrderID() } });
            Assert.AreEqual(1, NumNotifications);
        }