public void KeepPendingMarkets()
        {
            // here I want to test that all the markets
            // in a pending state (that they haven't been actived before)
            // will be removed from the snapshot through the PendingMarketFilteringRule

            var pendingRule = new PendingMarketFilteringRule();
            pendingRule.AddSportToRule("TestFootball");

            List<IMarketRule> rules = new List<IMarketRule> { 
                VoidUnSettledMarket.Instance, 
                InactiveMarketsFilteringRule.Instance,
                pendingRule
            };

  
            _market1.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Pending, false));
            _market2.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Pending, false));
            _market3.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, true));

            var filteredMarkets = new MarketRulesManager(_snapshot.Id, _objectProvider, rules);


            filteredMarkets.ApplyRules(_snapshot);
            filteredMarkets.CommitChanges();

            _snapshot.Markets.Exists(m => m.Id == _market1.Object.Id).Should().BeFalse();   // market1 should have been removed
            _snapshot.Markets.Exists(m => m.Id == _market2.Object.Id).Should().BeFalse();   // market2 should have been removed
            _snapshot.Markets.Exists(m => m.Id == _market3.Object.Id).Should().BeTrue();   // market3 should be there

            _market1.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, false));
            _market2.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, false));

            _snapshot.Markets.Add(_market1.Object);
            _snapshot.Markets.Add(_market2.Object);

            filteredMarkets.ApplyRules(_snapshot);
            filteredMarkets.CommitChanges();

            _snapshot.Markets.Exists(m => m.Id == _market1.Object.Id).Should().BeTrue();   // market1 should have been removed
            _snapshot.Markets.Exists(m => m.Id == _market2.Object.Id).Should().BeTrue();   // market2 should have been removed
            _snapshot.Markets.Exists(m => m.Id == _market3.Object.Id).Should().BeTrue();   // market3 should be there
        }
        public void RemovePendingMarketsWithExcludeListTest()
        {
            // here I want to test that all the markets
            // in a pending state (that never haven't been active before)
            // will be removed from the snapshot through the PendingMarketFilteringRule
            // I also add a list of market type that the rule should not touch

            // market1 and market2 are in pending state, while market3 is active
            _market1.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Pending, false));
            _market2.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Pending, false));
            _market3.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, true));

            _market1.Object.AddOrUpdateTagValue("type", "do_not_touch");

            var rule = new PendingMarketFilteringRule();
            rule.AddSportToRule("TestFootball");
            rule.ExcludeMarketType("TestFootball.do_not_touch");

            List<IMarketRule> rules = new List<IMarketRule> { 
                rule,
                InactiveMarketsFilteringRule.Instance,
                VoidUnSettledMarket.Instance, 
            };


            var filteredMarkets = new MarketRulesManager(_snapshot.Id, _objectProvider, rules);

            filteredMarkets.ApplyRules(_snapshot);


            _snapshot.Markets.Exists(m => m.Id == _market1.Object.Id).Should().BeTrue();   // market1 has not been touched by InactiveMarketsFilteringRule
            _snapshot.Markets.Exists(m => m.Id == _market2.Object.Id).Should().BeFalse();  // market2 should have been removed
            _snapshot.Markets.Exists(m => m.Id == _market3.Object.Id).Should().BeTrue();   // market3 should be there
        }
        public void RemovePendingMarketsWithExcludeListAndInactiveFilterTest()
        {
            // here I want to test that all the markets
            // in a pending state (that never haven't been active before)
            // will be removed from the snapshot through the PendingMarketFilteringRule
            // I also add a list of market type that the rule should not touch

            // market1 and market2 are in pending state, while market3 is active
            _market1.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Pending, false));
            _market2.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Pending, false));
            _market3.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, true));

            _market1.Object.AddOrUpdateTagValue("type", "do_not_touch");
            _market1.Object.AddOrUpdateTagValue("extra_tag", "just_to_check_that_tags_are_correctly_passed");

            var rule = new PendingMarketFilteringRule();
            rule.AddSportToRule("TestFootball");
            rule.ExcludeMarketType("TestFootball.do_not_touch");

            List<IMarketRule> rules = new List<IMarketRule> { 
                rule,
                InactiveMarketsFilteringRule.Instance,
                VoidUnSettledMarket.Instance, 
            };


            var filteredMarkets = new MarketRulesManager(_snapshot.Id, _objectProvider, rules);

            filteredMarkets.ApplyRules(_snapshot);
            filteredMarkets.CommitChanges();

            // market1 should not be touched by InactiveMarketsFilteringRule as PendingMarketFilterRule
            // should add it to the list of the "un-removable" markets
            _snapshot.Markets.Exists(m => m.Id == _market1.Object.Id).Should().BeTrue();   
            _snapshot.Markets.Exists(m => m.Id == _market2.Object.Id).Should().BeFalse();  // market2 should have been removed
            _snapshot.Markets.Exists(m => m.Id == _market3.Object.Id).Should().BeTrue();   // market3 should be there

            // market 1 and market 2 are now active. As market2 was removed, PendingMarketFiltering rule
            // should add all the tags back to the market
            _market1.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, true));
            _market2.Setup(x => x.Selections).Returns(GetSelections(SelectionStatus.Active, true));
            _market3.Setup(x => x.Selections).Returns(GetSettledSelections());
            _snapshot.Markets.Add(_market2.Object);

            filteredMarkets.ApplyRules(_snapshot);
            filteredMarkets.CommitChanges();

            _snapshot.Markets.Exists(m => m.Id == _market1.Object.Id).Should().BeTrue();   
            _snapshot.Markets.Exists(m => m.Id == _market2.Object.Id).Should().BeTrue();  
            _snapshot.Markets.Exists(m => m.Id == _market3.Object.Id).Should().BeTrue(); 

            _market1.Object.HasTag("extra_tag").Should().BeTrue();
            _market1.Object.GetTagValue("extra_tag").Should().BeEquivalentTo("just_to_check_that_tags_are_correctly_passed");
        }
        public void PostRuleProcessingWithDefaultRulesTest()
        {
            var settings = new Mock<ISettings>();
            var plugin = new Mock<IAdapterPlugin>();
            var stateprovider = new StateManager(settings.Object, plugin.Object);

            var pendingRule = new PendingMarketFilteringRule();
            pendingRule.AddSportToRule("Football");

            List<IMarketRule> rules = new List<IMarketRule>
            {
                VoidUnSettledMarket.Instance,
                pendingRule
            };


            Fixture fixture = new Fixture { Id = "ABCD", MatchStatus = "40" };
            fixture.Tags["Sport"] = "Football";

            Market testMkt = new Market { Id = "1" };
            testMkt.Selections.Add(new Selection { Id= "1-1", Status = SelectionStatus.Pending, Tradable = false});
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "2" };
            testMkt.Selections.Add(new Selection { Id = "2-1", Status = SelectionStatus.Pending, Tradable = false });
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "3" };
            testMkt.Selections.Add(new Selection { Id = "3-1", Status = SelectionStatus.Pending, Tradable = false });
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "4" };
            testMkt.Selections.Add(new Selection { Id = "4-1", Status = SelectionStatus.Pending, Tradable = false });
            fixture.Markets.Add(testMkt);

            MarketRulesManager manager = new MarketRulesManager(fixture.Id, stateprovider, rules);

            manager.ApplyRules(fixture);

            // all the markets should have been removed by the pendinRule
            fixture.Markets.Count().Should().Be(0);

            manager.CommitChanges();

            fixture.Markets.Clear();

            // STEP 2: enable markets "1" and "2"
            testMkt = new Market { Id = "1" };
            testMkt.Selections.Add(new Selection { Id = "1-1", Status = SelectionStatus.Active, Tradable = true });
            fixture.Markets.Add(testMkt);

            testMkt = new Market { Id = "2" };
            testMkt.Selections.Add(new Selection { Id = "2-1", Status = SelectionStatus.Active, Tradable = true });
            fixture.Markets.Add(testMkt);

            manager.ApplyRules(fixture);

            fixture.Markets.Count().Should().Be(2);

            manager.CommitChanges();

            // STEP 3: set fixture match status to "MatchOver" so the VoidUnSettledMarket can kick in

            fixture.Markets.Clear();
            fixture.MatchStatus = "50";
            testMkt = new Market { Id = "1" };
            testMkt.Selections.Add(new Selection { Id = "1-1", Status = SelectionStatus.Active, Tradable = true });

            // as the fixture is matchover, the VoidUnSettledMarket should add the un-settled markets
            // BUT, it should only add the markets that have been processed or NOT been active
            manager.ApplyRules(fixture);

            fixture.Markets.Count().Should().Be(0);
            fixture.Markets.FirstOrDefault(x => x.Id == "1").Should().BeNull(); // because the market has been active
            fixture.Markets.FirstOrDefault(x => x.Id == "2").Should().BeNull(); // because the market has been active
            fixture.Markets.FirstOrDefault(x => x.Id == "3").Should().BeNull(); // because the market has NOT been processed
            fixture.Markets.FirstOrDefault(x => x.Id == "4").Should().BeNull(); // because the market has NOT been processed
        }