Beispiel #1
0
        public void Fire_LowPriorityActivatesTwiceTriggersHighPriority_HighPriorityPreemptsLowPriority()
        {
            //Arrange
            var invokedRules = new List<BaseRule>();

            var mock = new Mock<Action<BaseRule>>();
            mock.Setup(x => x(It.IsAny<BaseRule>())).Callback<BaseRule>(invokedRules.Add);

            GetRuleInstance<PriorityLowRule>().InvocationHandler = mock.Object;
            GetRuleInstance<PriorityHighRule>().InvocationHandler = mock.Object;

            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            var fact2 = new FactType1 {TestProperty = "Valid Value 2"};
            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
                //low priority activates twice
                //it runs once, activates high priority rule, which preempts low priority and fires once
                //low priority fires second time, which activates high priority which also fires second time
            Assert.AreEqual(4, invokedRules.Count);
            Assert.IsInstanceOf<PriorityLowRule>(invokedRules[0]);
            Assert.IsInstanceOf<PriorityHighRule>(invokedRules[1]);
            Assert.IsInstanceOf<PriorityLowRule>(invokedRules[2]);
            Assert.IsInstanceOf<PriorityHighRule>(invokedRules[3]);
        }
Beispiel #2
0
        public void Fire_RuleFires_RaisesRuleFiringEvent()
        {
            //Arrange
            var factory = CreateTarget();
            var session = factory.CreateSession();

            var fact = new FactType1 { TestProperty = "Valid Value" };
            session.Insert(fact);

            object factorySender = null;
            AgendaEventArgs factoryArgs = null;
            object sessionSender = null;
            AgendaEventArgs sessionArgs = null;
            factory.Events.RuleFiringEvent += (sender, args) =>
            {
                factorySender = sender;
                factoryArgs = args;
            };
            session.Events.RuleFiringEvent += (sender, args) =>
            {
                sessionSender = sender;
                sessionArgs = args;
            };

            //Act
            session.Fire();

            //Assert
            Assert.AreSame(session, factorySender);
            Assert.AreSame(session, sessionSender);
            Assert.AreSame(fact, factoryArgs.Facts.Single().Value);
            Assert.AreSame(fact, sessionArgs.Facts.Single().Value);
            Assert.That(factoryArgs.Rule.Name.Contains("OneFactRule"));
            Assert.That(sessionArgs.Rule.Name.Contains("OneFactRule"));
        }
        public void Fire_TwoFactsForOneGroupAndOneForAnotherAndOneInvalidTheInvalidUpdatedToSecondGroup_FiresWithEachFactFromEachGroup()
        {
            //Arrange
            var fact1 = new FactType1 { TestProperty = "Valid Value Group1" };
            var fact2 = new FactType1 { TestProperty = "Valid Value Group1" };
            var fact3 = new FactType1 { TestProperty = "Valid Value Group2" };
            var fact4 = new FactType1 { TestProperty = "Invalid Value" };

            Session.Insert(fact1);
            Session.Insert(fact2);
            Session.Insert(fact3);
            Session.Insert(fact4);

            fact4.TestProperty = "Valid Value Group2";
            Session.Update(fact4);

            //Act
            Session.Fire();

            //Assert
            AssertFiredTimes(4);
            Assert.AreEqual(fact1, GetFiredFact<FactType1>(0));
            Assert.AreEqual(fact2, GetFiredFact<FactType1>(1));
            Assert.AreEqual(fact3, GetFiredFact<FactType1>(2));
            Assert.AreEqual(fact4, GetFiredFact<FactType1>(3));
        }
Beispiel #4
0
        public void Fire_LowPriorityActivatesTwiceTriggersHighPriority_HighPriorityPreemptsLowPriority()
        {
            //Arrange
            var invokedRules = new List<string>();

            Session.Events.RuleFiredEvent += (sender, args) => invokedRules.Add(args.Rule.Name);

            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            var fact2 = new FactType1 {TestProperty = "Valid Value 2"};
            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
                //low priority activates twice
                //it runs once, activates high priority rule, which preempts low priority and fires once
                //low priority fires second time, which activates high priority which also fires second time
            Assert.AreEqual(4, invokedRules.Count);
            Assert.AreEqual("NRules.IntegrationTests.TestRules.PriorityLowRule", invokedRules[0]);
            Assert.AreEqual("NRules.IntegrationTests.TestRules.PriorityHighRule", invokedRules[1]);
            Assert.AreEqual("NRules.IntegrationTests.TestRules.PriorityLowRule", invokedRules[2]);
            Assert.AreEqual("NRules.IntegrationTests.TestRules.PriorityHighRule", invokedRules[3]);
        }
        public void Fire_DefaultResolver_Throws()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact);

            //Act - Assert
            Assert.Throws<InvalidOperationException>(() => Session.Fire());
        }
Beispiel #6
0
        public void Fire_DuplicateInsert_Throws()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Valid Value 1"};

            //Act - Assert
            Session.Insert(fact);
            Assert.Throws<ArgumentException>(() => Session.Insert(fact));
        }
Beispiel #7
0
        public void Fire_ConditionDoesNotMatch_DoesNotFire()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Invalid Value 1"};
            Session.Insert(fact);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
        public void Fire_MatchingFact_FiresOnce()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Valid value"};
            Session.Insert(fact);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
        public void Fire_MatchingNotPatternFact_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
        public void Fire_MatchingMainFactAndNoneOfOrGroup_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
        public void Fire_MatchingFactOfTypeOneNothigOfTypeTwo_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 { TestProperty = "Valid Value 1" };

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
Beispiel #12
0
        public void Fire_OneMatchingFactAssertedAndRetracted_DoesNotFire()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact);
            Session.Retract(fact);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
        public void Fire_FactTwoDoesNotExist_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
        public void Fire_ErrorInActionErrorHandler_DoesNotThrow()
        {
            //Arrange
            Session.Events.ActionFailedEvent += (sender, args) => args.IsHandled = true;

            var fact = new FactType1 { TestProperty = "Valid value" };
            Session.Insert(fact);

            GetRuleInstance<OneFactRule>().Action = null;

            //Act - Assert
            Assert.DoesNotThrow(() => Session.Fire());
        }
        public void Fire_FactMatchingFirstPartOfOrGroup_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
        public void Fire_OneMatchingFact_FiresOnceWithOneFactInCollection()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.AreEqual(1, GetFiredFact<IEnumerable<FactType1>>().Count());
        }
        public void Fire_MatchingFactAndNoFactsMatchingNotPattern_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
        public void Fire_MatchingFactOfFirstKindNoFactsOfOtherKind_FiresCollect()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};

            Session.Insert(fact1);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce<TwoFactOneCollectionRule>();
            AssertDidNotFire<TwoFactOneExistsCheckRule>();
        }
        public void Fire_MatchingFactInsertedAndUpdatedToInvalid_DoesNotFire()
        {
            //Arrange
            var fact = new FactType1 { TestProperty = "Valid value" };
            Session.Insert(fact);
            fact.TestProperty = "Invalid value";
            Session.Update(fact);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
Beispiel #20
0
        public void Query_RuleInsertsSecondFact_TwoFactsInMemory()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact1);
            Session.Fire();

            //Act
            var query = Session.Query<object>();
            var facts = query.ToList();

            //Assert
            Assert.AreEqual(2, facts.Count);
        }
Beispiel #21
0
        public void Query_OneFact_RetrievesFactFromQuery()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact1);

            //Act
            var query = Session.Query<object>();
            var facts = query.ToList();

            //Assert
            Assert.AreEqual(1, facts.Count);
            Assert.AreSame(fact1, facts[0]);
        }
        public void Fire_TwoMatchingFacts_FiresTwice()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid value"};
            Session.Insert(fact1);
            var fact2 = new FactType1 {TestProperty = "Valid value"};
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
            AssertFiredTwice();
        }
Beispiel #23
0
        public void Fire_TwoMatchingFacts_FiresOnceAndHalts()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            var fact2 = new FactType1 {TestProperty = "Valid Value 2"};
            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
        public void Fire_FactsMatchingSecondPartOfOrGroup_FiresOnce()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Invalid Value 1"};
            var fact2 = new FactType2 { TestProperty = "Valid Value 2", JoinProperty = fact1.TestProperty };

            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
Beispiel #25
0
        public void Fire_OneFactUpdatedFromInvalidToMatching_FiresOnce()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Invalid Value 1"};
            Session.Insert(fact);

            fact.TestProperty = "Valid Value 1";
            Session.Update(fact);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
        }
Beispiel #26
0
        public void Query_QueryFactsByType_OnlyReturnsFactsOfThatType()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact1);
            Session.Fire();

            //Act
            var query = Session.Query<FactType2>();
            var facts = query.ToList();

            //Assert
            Assert.AreEqual(1, facts.Count);
            Assert.AreEqual(fact1.TestProperty, facts[0].JoinProperty);
        }
Beispiel #27
0
        public void Fire_TwoMatchingFactsFireCalledTwice_FiresOnceThenHaltsThenResumesAndFiresAgain()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            var fact2 = new FactType1 {TestProperty = "Valid Value 2"};
            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();
            Session.Fire();

            //Assert
            AssertFiredTwice();
        }
        public void Fire_OneMatchingFactTwoFactsToAggregate_FiresOnceWithTwoFactsInCollection()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            var fact2 = new FactType1 {TestProperty = "Invalid Value 2"};
            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.AreEqual(2, GetFiredFact<IEnumerable<FactType1>>().Count());
        }
Beispiel #29
0
        public void Fire_OneMatchingFactAssertedAndRetractedAndAssertedAgain_FiresOnce()
        {
            //Arrange
            var fact = new FactType1 {TestProperty = "Valid Value 1"};
            Session.Insert(fact);
            Session.Retract(fact);
            Session.Insert(fact);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.AreEqual(fact.TestProperty, GetFiredFact<FactType1Projection>().Value);
        }
        public void Fire_TwoMatchingFacts_DoesNotFire()
        {
            //Arrange
            var fact1 = new FactType1 {TestProperty = "Valid Value 1"};
            var fact2 = new FactType1 {TestProperty = "Valid Value 2"};

            Session.Insert(fact1);
            Session.Insert(fact2);

            //Act
            Session.Fire();

            //Assert
            AssertDidNotFire();
        }
Beispiel #31
0
 public FactType1Projection(FactType1 fact)
 {
     Value = fact.TestProperty;
 }