public void FactHierarchy_CrossLeaf_Test()
        {
            for (short i = 4; i <= 7; i++)
                for (short j = 4; j <= 7; j++)
                {
                    facts.Add(new ConcreteFact() { ProductId = i, GeographyId = j, SalesComponentId = 1, TimeId = 1, Value = 1 });
                    facts.Add(new ConcreteFact() { ProductId = i, GeographyId = j, SalesComponentId = 2, TimeId = 1, Value = 1 });
                }

            var lookup = new ConcreteFactLookup(this.facts, this.prod, this.geog, this.causal, this.period);

            // this is a 4x4 graph, so 16.
            Assert.AreEqual(16, lookup.SumChildren(1, 1, 1, 1));

            // including both causals it will be 32
            Assert.AreEqual(32, lookup.SumChildren(1, 1, null, 1));
            Assert.AreEqual(32, lookup.SumChildren(null, null, null, null));

            // each product will have 4 leaf items (the 4 geogs)
            Assert.AreEqual(4, lookup.SumChildren(4, 1, 1, 1));
            // both causals
            Assert.AreEqual(8, lookup.SumChildren(4, 1, null, 1));
            Assert.AreEqual(8, lookup.SumChildren(4, null, null, 1));

            // each geog will have 4 leaf items (the 4 prods)
            Assert.AreEqual(4, lookup.SumChildren(1, 4, 1, 1));
            // both causals
            Assert.AreEqual(8, lookup.SumChildren(1, 4, null, 1));
            Assert.AreEqual(8, lookup.SumChildren(null, 4, null, 1));

            // the real leaf level
            Assert.AreEqual(1, lookup.SumChildren(6, 6, 1, 1));
            Assert.AreEqual(1, lookup.SumChildren(5, 7, 2, 1));
            Assert.AreEqual(2, lookup.SumChildren(6, 6, null, 1));
        }
        public void ConcreteFactLookup_GlobalFact()
        {
            var facts = new List<ConcreteFact>();
            facts.Add(new ConcreteFact() { ProductId = 2, GeographyId = 2, SalesComponentId = 2, TimeId = 1, Value = 100 });
            facts.Add(new ConcreteFact() { ProductId = 3, GeographyId = 3, SalesComponentId = 3, TimeId = 1, Value = 100 });

            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, IEnumerable<short>>> anyInt = p => p.GetChildren(Moq.It.IsAny<short>());
            var result = new List<short> { 2, 3 };
            products.Setup(anyInt).Returns(result);
            geographies.Setup(anyInt).Returns(result);
            causals.Setup(anyInt).Returns(result);
            periods.Setup(anyInt).Returns(result);

            Expression<Func<IHierarchy, bool>> relation = h => h.RelationExists(Moq.It.IsAny<short>(), Moq.It.IsAny<short>());
            products.Setup(relation).Returns(true);
            geographies.Setup(relation).Returns(true);
            causals.Setup(relation).Returns(true);
            periods.Setup(relation).Returns(true);

            //, Hierarchy products, Hierarchy geographies, Hierarchy causals, Hierarchy periods

            var lookup = new ConcreteFactLookup(facts, products.Object, geographies.Object, causals.Object, periods.Object);

            Assert.AreEqual(200, lookup.SumChildren(1, 1, 1, 1));
        }
        public void TestSimple_Unmatched1()
        {
            var facts = new List<ConcreteFact>();
            facts.Add(new ConcreteFact() { ProductId = 1, GeographyId = 1, SalesComponentId = 1, TimeId = 1, Value = 100 });

            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, IEnumerable<short>>> anyInt = p => p.GetParents(2);
            var result = new List<short> { 1 };
            products.Setup(anyInt).Returns(result);
            geographies.Setup(anyInt).Returns(result);
            causals.Setup(anyInt).Returns(result);
            periods.Setup(anyInt).Returns(result);

            //, Hierarchy products, Hierarchy geographies, Hierarchy causals, Hierarchy periods
            var lookup = new ConcreteFactLookup(facts, products.Object, geographies.Object, causals.Object, periods.Object);

            Assert.AreEqual(100, lookup.SumChildren(3, 2, 2, 2, true));
        }
        public void Test_SingleFact()
        {
            var products = new Moq.Mock<IHierarchy>();
            var geographies = new Moq.Mock<IHierarchy>();
            var causals = new Moq.Mock<IHierarchy>();
            var periods = new Moq.Mock<IHierarchy>();

            Expression<Func<IHierarchy, IEnumerable<short>>> anyInt = p => p.GetChildren(Moq.It.IsAny<short>());
            var result = new List<short> { 2 };
            products.Setup(anyInt).Returns(result);
            geographies.Setup(anyInt).Returns(result);
            causals.Setup(anyInt).Returns(result);
            periods.Setup(anyInt).Returns(result);

            var facts = new List<NullableFact>();

            var factLookup = new ConcreteFactLookup(null, products.Object, geographies.Object, causals.Object, periods.Object);

            factLookup.SumChildren(1, 1, 1, 1, true);
        }
        public void FactHierarchy_CrossLeaf_UnbalancedTest()
        {
            for (short i = 4; i <= 7; i++)
                for (short j = 4; j <= 7; j++)
                {
                    facts.Add(new ConcreteFact() { ProductId = i, GeographyId = j, SalesComponentId = 1, TimeId = 1, Value = 1 });
                    facts.Add(new ConcreteFact() { ProductId = i, GeographyId = j, SalesComponentId = 2, TimeId = 1, Value = 1 });
                }

            // we remove item 7 from the geo hierarchy
            var geo = new List<Relation>(this.hierarchy);
            geo.RemoveAll(r => r.Child == 7);

            var lookup = new ConcreteFactLookup(this.facts, this.prod, new Hierarchy(geo), this.causal, this.period);

            // this is a 4x3 graph, so 12
            Assert.AreEqual(12, lookup.SumChildren(1, 1, 1, 1));
            // including both causals will double
            Assert.AreEqual(24, lookup.SumChildren(1, 1, null, 1));
            // null will still include geog 7, so 4x4x2
            Assert.AreEqual(32, lookup.SumChildren(null, null, null, null));

            // each product will have 3 leaf items (the 3 geogs)
            Assert.AreEqual(3, lookup.SumChildren(4, 1, 1, 1));
            // both causals
            Assert.AreEqual(6, lookup.SumChildren(4, 1, null, 1));
            // null includes geog 7
            Assert.AreEqual(8, lookup.SumChildren(4, null, null, 1));

            // each geog will have 4 leaf items (the 4 prods)
            Assert.AreEqual(4, lookup.SumChildren(1, 4, 1, 1));
            // both causals
            Assert.AreEqual(8, lookup.SumChildren(1, 4, null, 1));
            Assert.AreEqual(8, lookup.SumChildren(null, 4, null, 1));

            // the real leaf level
            Assert.AreEqual(1, lookup.SumChildren(6, 6, 1, 1));
            Assert.AreEqual(2, lookup.SumChildren(6, 6, null, 1));

            // we can still query 7 - it exists, just not part of other hierarchy
            Assert.AreEqual(1, lookup.SumChildren(5, 7, 2, 1));
            Assert.AreEqual(4, lookup.SumChildren(null, 7, 2, 1));
            Assert.AreEqual(8, lookup.SumChildren(null, 7, null, 1));
        }
        public void FactHierarchy_SimpleLeafLevelTest()
        {
            facts.Add(new ConcreteFact() { ProductId = 4, GeographyId = 1, SalesComponentId = 1, TimeId = 1, Value = 100 });
            facts.Add(new ConcreteFact() { ProductId = 5, GeographyId = 1, SalesComponentId = 1, TimeId = 1, Value = 100 });
            facts.Add(new ConcreteFact() { ProductId = 6, GeographyId = 1, SalesComponentId = 1, TimeId = 1, Value = 100 });
            facts.Add(new ConcreteFact() { ProductId = 7, GeographyId = 1, SalesComponentId = 1, TimeId = 1, Value = 100 });

            var lookup = new ConcreteFactLookup(this.facts, this.prod, this.geog, this.causal, this.period);

            Assert.AreEqual(400, lookup.SumChildren(1, 1, 1, 1));
            Assert.AreEqual(200, lookup.SumChildren(2, 1, 1, 1));
            Assert.AreEqual(200, lookup.SumChildren(3, 1, 1, 1));

            // we can also provide null values for any 'all' ids.
            Assert.AreEqual(400, lookup.SumChildren(null, null, null, null));
            Assert.AreEqual(400, lookup.SumChildren(1, null, null, null));
            Assert.AreEqual(200, lookup.SumChildren(2, null, null, null));
            Assert.AreEqual(200, lookup.SumChildren(3, null, null, null));
        }
        public void FactHierarchy_CrossLeaf_WithGapsTest()
        {
            for (short i = 4; i <= 7; i++)
                for (short j = 4; j <= 7; j++)
                {
                    facts.Add(new ConcreteFact() { ProductId = i, GeographyId = j, SalesComponentId = 1, TimeId = 1, Value = 1 });
                    facts.Add(new ConcreteFact() { ProductId = i, GeographyId = j, SalesComponentId = 2, TimeId = 1, Value = 1 });
                }

            // product 5 is not sold in geography 5
            facts.RemoveAll(f => f.ProductId == 5 && f.GeographyId == 5);

            var lookup = new ConcreteFactLookup(this.facts, this.prod, this.geog, this.causal, this.period);

            // this is a 4x4 graph, so 16 less 1 gap
            Assert.AreEqual(15, lookup.SumChildren(1, 1, 1, 1));

            // including both causals it will be 32
            Assert.AreEqual(30, lookup.SumChildren(1, 1, null, 1));
            Assert.AreEqual(30, lookup.SumChildren(null, null, null, null));

            // each product will have 4 leaf items (the 4 geogs)
            Assert.AreEqual(4, lookup.SumChildren(4, 1, 1, 1));
            // both causals
            Assert.AreEqual(8, lookup.SumChildren(4, 1, null, 1));
            Assert.AreEqual(8, lookup.SumChildren(4, null, null, 1));

            // product 5 has 3 leaf items
            Assert.AreEqual(3, lookup.SumChildren(5, 1, 1, 1));
            // both causals
            Assert.AreEqual(6, lookup.SumChildren(5, 1, null, 1));
            Assert.AreEqual(6, lookup.SumChildren(5, null, null, 1));

            // each geog will have 4 leaf items (the 4 prods)
            Assert.AreEqual(4, lookup.SumChildren(1, 4, 1, 1));
            // both causals
            Assert.AreEqual(8, lookup.SumChildren(1, 4, null, 1));
            Assert.AreEqual(8, lookup.SumChildren(null, 4, null, 1));

            // geog 5 has 3 leaf items
            Assert.AreEqual(3, lookup.SumChildren(1, 5, 1, 1));
            // both causals
            Assert.AreEqual(6, lookup.SumChildren(1, 5, null, 1));
            Assert.AreEqual(6, lookup.SumChildren(null, 5, null, 1));

            // the real leaf level
            Assert.AreEqual(1, lookup.SumChildren(6, 6, 1, 1));
            Assert.AreEqual(1, lookup.SumChildren(5, 7, 2, 1));
            Assert.AreEqual(2, lookup.SumChildren(6, 6, null, 1));

            bool exception = false;
            try
            {
                Assert.AreEqual(0, lookup.SumChildren(5, 5, 2, 1, true));
            }
            catch (InvalidOperationException)
            {
                exception = true;
            }
            Assert.IsTrue(exception);
        }