Exemplo n.º 1
0
        public void CombinedClassAndCollectionFiltersEnabled()
        {
            TestData testData = new TestData(this);

            testData.Prepare();

            ISession session = OpenSession();

            session.EnableFilter("regionlist").SetParameterList("regions", new string[] { "LA", "APAC" });
            session.EnableFilter("fulfilledOrders").SetParameter("asOfDate", testData.lastMonth);

            // test retreival through hql with the collection as non-eager
            IList salespersons = session.CreateQuery("select s from Salesperson as s").List();

            Assert.AreEqual(1, salespersons.Count, "Incorrect salesperson count");
            Salesperson sp = (Salesperson)salespersons[0];

            Assert.AreEqual(1, sp.Orders.Count, "Incorrect order count");

            session.Clear();

            // test retreival through hql with the collection join fetched
            salespersons = session.CreateQuery("select s from Salesperson as s left join fetch s.Orders").List();
            Assert.AreEqual(1, salespersons.Count, "Incorrect salesperson count");
            sp = (Salesperson)salespersons[0];
            Assert.AreEqual(sp.Orders.Count, 1, "Incorrect order count");

            session.Close();
            testData.Release();
        }
Exemplo n.º 2
0
        public void HqlFilters()
        {
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // HQL test
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            log.Info("Starting HQL filter tests");
            TestData testData = new TestData(this);

            testData.Prepare();

            ISession session = OpenSession();

            session.EnableFilter("region").SetParameter("region", "APAC");

            session.EnableFilter("effectiveDate")
            .SetParameter("asOfDate", testData.lastMonth);

            log.Info("HQL against Salesperson...");
            IList results = session.CreateQuery("select s from Salesperson as s left join fetch s.Orders").List();

            Assert.IsTrue(results.Count == 1, "Incorrect filtered HQL result count [" + results.Count + "]");
            Salesperson result = (Salesperson)results[0];

            Assert.IsTrue(result.Orders.Count == 1, "Incorrect collectionfilter count");

            log.Info("HQL against Product...");
            results = session.CreateQuery("from Product as p where p.StockNumber = ?").SetInt32(0, 124).List();
            Assert.IsTrue(results.Count == 1);

            session.Close();
            testData.Release();
        }
Exemplo n.º 3
0
        public void SecondLevelCachedCollectionsFiltering()
        {
            TestData testData = new TestData(this);

            testData.Prepare();

            ISession session = OpenSession();

            // Force a collection into the second level cache, with its non-filtered elements
            Salesperson sp = (Salesperson)session.Load(typeof(Salesperson), testData.steveId);

            NHibernateUtil.Initialize(sp.Orders);
            ICollectionPersister persister = ((ISessionFactoryImplementor)sessions)
                                             .GetCollectionPersister(typeof(Salesperson).FullName + ".Orders");

            Assert.IsTrue(persister.HasCache, "No cache for collection");
            CacheKey cacheKey =
                new CacheKey(testData.steveId, persister.KeyType, persister.Role, EntityMode.Poco, (ISessionFactoryImplementor)sessions);
            CollectionCacheEntry cachedData = (CollectionCacheEntry)persister.Cache.Cache.Get(cacheKey);

            Assert.IsNotNull(cachedData, "collection was not in cache");

            session.Close();

            session = OpenSession();
            session.EnableFilter("fulfilledOrders").SetParameter("asOfDate", testData.lastMonth);
            sp = (Salesperson)session.CreateQuery("from Salesperson as s where s.id = :id")
                 .SetInt64("id", testData.steveId)
                 .UniqueResult();
            Assert.AreEqual(1, sp.Orders.Count, "Filtered-collection not bypassing 2L-cache");

            CollectionCacheEntry cachedData2 = (CollectionCacheEntry)persister.Cache.Cache.Get(cacheKey);

            Assert.IsNotNull(cachedData2, "collection no longer in cache!");
            Assert.AreSame(cachedData, cachedData2, "Different cache values!");

            session.Close();

            session = OpenSession();
            session.EnableFilter("fulfilledOrders").SetParameter("asOfDate", testData.lastMonth);
            sp = (Salesperson)session.Load(typeof(Salesperson), testData.steveId);
            Assert.AreEqual(1, sp.Orders.Count, "Filtered-collection not bypassing 2L-cache");

            session.Close();

            // Finally, make sure that the original cached version did not get over-written
            session = OpenSession();
            sp      = (Salesperson)session.Load(typeof(Salesperson), testData.steveId);
            Assert.AreEqual(2, sp.Orders.Count, "Actual cached version got over-written");

            session.Close();
            testData.Release();
        }
Exemplo n.º 4
0
        public async Task GetFiltersAsync()
        {
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // Get() test
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            log.Info("Starting get() filter tests (eager assoc. fetching).");
            using (var session = OpenSession())
            {
                session.EnableFilter("region").SetParameter("region", "APAC");

                log.Info("Performing get()...");
                Salesperson salesperson = (Salesperson)await(session.GetAsync(typeof(Salesperson), testData.steveId));
                Assert.IsNotNull(salesperson);
                Assert.AreEqual(1, salesperson.Orders.Count, "Incorrect order count");
            }
        }
Exemplo n.º 5
0
        public async Task CombinedClassAndCollectionFiltersEnabledAsync()
        {
            using (var session = OpenSession())
            {
                session.EnableFilter("regionlist").SetParameterList("regions", new[] { "LA", "APAC" });
                session.EnableFilter("fulfilledOrders").SetParameter("asOfDate", testData.lastMonth);

                // test retreival through hql with the collection as non-eager
                IList salespersons = await(session.CreateQuery("select s from Salesperson as s").ListAsync());
                Assert.AreEqual(1, salespersons.Count, "Incorrect salesperson count");
                Salesperson sp = (Salesperson)salespersons[0];
                Assert.AreEqual(1, sp.Orders.Count, "Incorrect order count");

                session.Clear();

                // test retreival through hql with the collection join fetched
                salespersons = await(session.CreateQuery("select s from Salesperson as s left join fetch s.Orders").ListAsync());
                Assert.AreEqual(1, salespersons.Count, "Incorrect salesperson count");
                sp = (Salesperson)salespersons[0];
                Assert.AreEqual(sp.Orders.Count, 1, "Incorrect order count");
            }
        }
Exemplo n.º 6
0
        public void GetFilters()
        {
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // Get() test
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            log.Info("Starting get() filter tests (eager assoc. fetching).");
            TestData testData = new TestData(this);

            testData.Prepare();

            ISession session = OpenSession();

            session.EnableFilter("region").SetParameter("region", "APAC");

            log.Info("Performing get()...");
            Salesperson salesperson = (Salesperson)session.Get(typeof(Salesperson), testData.steveId);

            Assert.IsNotNull(salesperson);
            Assert.AreEqual(1, salesperson.Orders.Count, "Incorrect order count");

            session.Close();
            testData.Release();
        }
Exemplo n.º 7
0
        public async Task HqlFiltersAsync()
        {
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            // HQL test
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            log.Info("Starting HQL filter tests");
            using (var session = OpenSession())
            {
                session.EnableFilter("region").SetParameter("region", "APAC");

                session.EnableFilter("effectiveDate")
                .SetParameter("asOfDate", testData.lastMonth);

                log.Info("HQL against Salesperson...");
                IList results = await(session.CreateQuery("select s from Salesperson as s left join fetch s.Orders").ListAsync());
                Assert.IsTrue(results.Count == 1, "Incorrect filtered HQL result count [" + results.Count + "]");
                Salesperson result = (Salesperson)results[0];
                Assert.IsTrue(result.Orders.Count == 1, "Incorrect collectionfilter count");

                log.Info("HQL against Product...");
                results = await(session.CreateQuery("from Product as p where p.StockNumber = ?").SetInt32(0, 124).ListAsync());
                Assert.IsTrue(results.Count == 1);
            }
        }
Exemplo n.º 8
0
            public void Prepare()
            {
                using (var session = outer.OpenSession())
                    using (var transaction = session.BeginTransaction())
                    {
                        lastMonth = DateTime.Today.AddMonths(-1);
                        nextMonth = DateTime.Today.AddMonths(1);

                        sixMonthsAgo  = DateTime.Today.AddMonths(-6);
                        fourMonthsAgo = DateTime.Today.AddMonths(-4);

                        Department dept = new Department();
                        dept.Name = ("Sales");

                        session.Save(dept);
                        deptId = dept.Id;
                        entitiesToCleanUp.Add(dept);

                        Salesperson steve = new Salesperson();
                        steve.Name     = ("steve");
                        steve.Region   = ("APAC");
                        steve.HireDate = (sixMonthsAgo);

                        steve.Department = (dept);
                        dept.Salespersons.Add(steve);

                        Salesperson max = new Salesperson();
                        max.Name     = ("max");
                        max.Region   = ("EMEA");
                        max.HireDate = (nextMonth);

                        max.Department = (dept);
                        dept.Salespersons.Add(max);

                        session.Save(steve);
                        session.Save(max);
                        entitiesToCleanUp.Add(steve);
                        entitiesToCleanUp.Add(max);

                        steveId = steve.Id;

                        Category cat1 = new Category("test cat 1", lastMonth, nextMonth);
                        Category cat2 = new Category("test cat 2", sixMonthsAgo, fourMonthsAgo);

                        Product product1 = new Product();
                        product1.Name               = ("Acme Hair Gel");
                        product1.StockNumber        = (123);
                        product1.EffectiveStartDate = (lastMonth);
                        product1.EffectiveEndDate   = (nextMonth);
                        product1.ProductGuid        = Guid.NewGuid();
                        Product1Guid = product1.ProductGuid;

                        product1.AddCategory(cat1);
                        product1.AddCategory(cat2);

                        session.Save(product1);
                        entitiesToCleanUp.Add(product1);
                        prod1Id = product1.Id;

                        Order order1 = new Order();
                        order1.Buyer           = "gavin";
                        order1.Region          = ("APAC");
                        order1.PlacementDate   = sixMonthsAgo;
                        order1.FulfillmentDate = fourMonthsAgo;
                        order1.Salesperson     = steve;
                        order1.AddLineItem(product1, 500);

                        session.Save(order1);
                        entitiesToCleanUp.Add(order1);

                        Product product2 = new Product();
                        product2.Name               = ("Acme Super-Duper DTO Factory");
                        product2.StockNumber        = (124);
                        product2.EffectiveStartDate = (sixMonthsAgo);
                        product2.EffectiveEndDate   = (DateTime.Today);
                        product2.ProductGuid        = Guid.NewGuid();
                        Product2Guid = product2.ProductGuid;

                        Category cat3 = new Category("test cat 2", sixMonthsAgo, DateTime.Today);
                        product2.AddCategory(cat3);

                        session.Save(product2);
                        entitiesToCleanUp.Add(product2);

                        // An uncategorized product
                        Product product3 = new Product();
                        product3.Name = ("Uncategorized product");
                        session.Save(product3);
                        entitiesToCleanUp.Add(product3);

                        Order order2 = new Order();
                        order2.Buyer         = "christian";
                        order2.Region        = ("EMEA");
                        order2.PlacementDate = lastMonth;
                        order2.Salesperson   = steve;
                        order2.AddLineItem(product2, -1);

                        session.Save(order2);
                        entitiesToCleanUp.Add(order2);

                        transaction.Commit();
                    }
            }
			public void Prepare()
			{
				ISession session = outer.OpenSession();
				ITransaction transaction = session.BeginTransaction();

				lastMonth = DateTime.Today.AddMonths(-1);
				nextMonth = DateTime.Today.AddMonths(1);

				sixMonthsAgo = DateTime.Today.AddMonths(-6);
				fourMonthsAgo = DateTime.Today.AddMonths(-4);

				Department dept = new Department();
				dept.Name = ("Sales");

				session.Save(dept);
				deptId = dept.Id;
				entitiesToCleanUp.Add(dept);

				Salesperson steve = new Salesperson();
				steve.Name = ("steve");
				steve.Region = ("APAC");
				steve.HireDate = (sixMonthsAgo);

				steve.Department = (dept);
				dept.Salespersons.Add(steve);

				Salesperson max = new Salesperson();
				max.Name = ("max");
				max.Region = ("EMEA");
				max.HireDate = (nextMonth);

				max.Department = (dept);
				dept.Salespersons.Add(max);

				session.Save(steve);
				session.Save(max);
				entitiesToCleanUp.Add(steve);
				entitiesToCleanUp.Add(max);

				steveId = steve.Id;

				Category cat1 = new Category("test cat 1", lastMonth, nextMonth);
				Category cat2 = new Category("test cat 2", sixMonthsAgo, fourMonthsAgo);

				Product product1 = new Product();
				product1.Name = ("Acme Hair Gel");
				product1.StockNumber = (123);
				product1.EffectiveStartDate = (lastMonth);
				product1.EffectiveEndDate = (nextMonth);

				product1.AddCategory(cat1);
				product1.AddCategory(cat2);

				session.Save(product1);
				entitiesToCleanUp.Add(product1);
				prod1Id = product1.Id;

				Order order1 = new Order();
				order1.Buyer = "gavin";
				order1.Region = ("APAC");
				order1.PlacementDate = sixMonthsAgo;
				order1.FulfillmentDate = fourMonthsAgo;
				order1.Salesperson = steve;
				order1.AddLineItem(product1, 500);

				session.Save(order1);
				entitiesToCleanUp.Add(order1);

				Product product2 = new Product();
				product2.Name = ("Acme Super-Duper DTO Factory");
				product2.StockNumber = (124);
				product2.EffectiveStartDate = (sixMonthsAgo);
				product2.EffectiveEndDate = (DateTime.Today);

				Category cat3 = new Category("test cat 2", sixMonthsAgo, DateTime.Today);
				product2.AddCategory(cat3);

				session.Save(product2);
				entitiesToCleanUp.Add(product2);

				// An uncategorized product
				Product product3 = new Product();
				product3.Name = ("Uncategorized product");
				session.Save(product3);
				entitiesToCleanUp.Add(product3);

				Order order2 = new Order();
				order2.Buyer = "christian";
				order2.Region = ("EMEA");
				order2.PlacementDate = lastMonth;
				order2.Salesperson = steve;
				order2.AddLineItem(product2, -1);

				session.Save(order2);
				entitiesToCleanUp.Add(order2);

				transaction.Commit();
				session.Close();
			}
Exemplo n.º 10
0
            public async Task PrepareAsync(CancellationToken cancellationToken = default(CancellationToken))
            {
                ISession     session     = outer.OpenSession();
                ITransaction transaction = session.BeginTransaction();

                lastMonth = DateTime.Today.AddMonths(-1);
                nextMonth = DateTime.Today.AddMonths(1);

                sixMonthsAgo  = DateTime.Today.AddMonths(-6);
                fourMonthsAgo = DateTime.Today.AddMonths(-4);

                Department dept = new Department();

                dept.Name = ("Sales");

                await(session.SaveAsync(dept, cancellationToken));
                deptId = dept.Id;
                entitiesToCleanUp.Add(dept);

                Salesperson steve = new Salesperson();

                steve.Name     = ("steve");
                steve.Region   = ("APAC");
                steve.HireDate = (sixMonthsAgo);

                steve.Department = (dept);
                dept.Salespersons.Add(steve);

                Salesperson max = new Salesperson();

                max.Name     = ("max");
                max.Region   = ("EMEA");
                max.HireDate = (nextMonth);

                max.Department = (dept);
                dept.Salespersons.Add(max);

                await(session.SaveAsync(steve, cancellationToken));
                await(session.SaveAsync(max, cancellationToken));
                entitiesToCleanUp.Add(steve);
                entitiesToCleanUp.Add(max);

                steveId = steve.Id;

                Category cat1 = new Category("test cat 1", lastMonth, nextMonth);
                Category cat2 = new Category("test cat 2", sixMonthsAgo, fourMonthsAgo);

                Product product1 = new Product();

                product1.Name               = ("Acme Hair Gel");
                product1.StockNumber        = (123);
                product1.EffectiveStartDate = (lastMonth);
                product1.EffectiveEndDate   = (nextMonth);

                product1.AddCategory(cat1);
                product1.AddCategory(cat2);

                await(session.SaveAsync(product1, cancellationToken));
                entitiesToCleanUp.Add(product1);
                prod1Id = product1.Id;

                Order order1 = new Order();

                order1.Buyer           = "gavin";
                order1.Region          = ("APAC");
                order1.PlacementDate   = sixMonthsAgo;
                order1.FulfillmentDate = fourMonthsAgo;
                order1.Salesperson     = steve;
                order1.AddLineItem(product1, 500);

                await(session.SaveAsync(order1, cancellationToken));
                entitiesToCleanUp.Add(order1);

                Product product2 = new Product();

                product2.Name               = ("Acme Super-Duper DTO Factory");
                product2.StockNumber        = (124);
                product2.EffectiveStartDate = (sixMonthsAgo);
                product2.EffectiveEndDate   = (DateTime.Today);

                Category cat3 = new Category("test cat 2", sixMonthsAgo, DateTime.Today);

                product2.AddCategory(cat3);

                await(session.SaveAsync(product2, cancellationToken));
                entitiesToCleanUp.Add(product2);

                // An uncategorized product
                Product product3 = new Product();

                product3.Name = ("Uncategorized product");
                await(session.SaveAsync(product3, cancellationToken));
                entitiesToCleanUp.Add(product3);

                Order order2 = new Order();

                order2.Buyer         = "christian";
                order2.Region        = ("EMEA");
                order2.PlacementDate = lastMonth;
                order2.Salesperson   = steve;
                order2.AddLineItem(product2, -1);

                await(session.SaveAsync(order2, cancellationToken));
                entitiesToCleanUp.Add(order2);

                await(transaction.CommitAsync(cancellationToken));
                session.Close();
            }