public void WhenUpdateArrives(Table table)
        {
            var market = new Market {Id = "TestId"};
            market.AddOrUpdateTagValue("name", "TestMarket");
            market.Selections.Clear();
            market.Selections.AddRange(table.Rows.Select(r=>  Helper.GetObjectFromTableRow<Selection>(r)));

            _fixture.Markets.Clear();
            _fixture.Markets.Add(market);            
        }
        private static Market CreateSuspendedMarket(IMarketState MarketState)
        {

            var market = new Market(MarketState.Id) {IsSuspended = true};

            if (MarketState.HasTag("line"))
            {
                market.AddOrUpdateTagValue("line", MarketState.GetTagValue("line"));
            }

            foreach (var stateSelection in MarketState.Selections)
                market.Selections.Add(new Selection { Id = stateSelection.Id, Status = SelectionStatus.Pending, Tradable = false, Price = 0 });

            return market;
        }
        public void GivenAFixtureWithTheFollowingMarkets(Table table)
        {
            ScenarioContext.Current.Clear();

            Fixture fixture = new Fixture { Id = "Test", MatchStatus = "40" };
            ScenarioContext.Current.Add("FIXTURE", fixture);

            foreach (var row in table.Rows)
            {
                Market mkt = new Market {Id = row["Market"]};
                mkt.AddOrUpdateTagValue("name", row["Name"]);

                fixture.Markets.Add(mkt);

                if (table.ContainsColumn("Selections"))
                {
                    int seln_count = Convert.ToInt32(row["Selections"]);
                    for (int i = 0; i < seln_count; i++)
                    {
                        Selection seln = new Selection { Name = row["Name"] + (i + 1) };
                        seln.Id = seln.Name;
                        mkt.Selections.Add(seln);
                    }
                }
            }
        }
        public void SerializeModelTest()
        {

            Fixture fixture = new Fixture
            {
                FixtureName = "TEST-NAME",
                Epoch = 10,
                LastEpochChangeReason = new[] { 10, 20 },
                Id = "TEST-ID",
                StartTime = new DateTime(2000, 01, 01),
                Sequence = 12,
                MatchStatus = "40"
            };
            fixture.Tags.Add("TEST-TAG-1", "1");
            fixture.Tags.Add("TEST-TAG-2", 2);
            fixture.GameState.Add("TEST-STATE-1", 1);
            fixture.GameState.Add("TEST-STATE-2", "2");

            Participant p1 = new Participant { Id = 1, Name = "P1" };
            p1.Tags.Add("TEST-TAG-1", "1");
            p1.Tags.Add("TEST-TAG-2", 2);

            Participant p2 = new Participant { Id = 2, Name = "P2" };

            fixture.Participants.Add(p1);
            fixture.Participants.Add(p2);

            Market mkt1 = new Market { Id = "MKT1" };
            mkt1.AddOrUpdateTagValue("name", "MKT1");

            Selection seln1 = new Selection
            {
                Id = "SELN1",
                Price = 2.0,
                Status = SelectionStatus.Active,
                Tradable = false
            };
            seln1.AddOrUpdateTagValue("name", "seln1");
            seln1.AddOrUpdateTagValue("displayed", "true");

            Selection seln2 = new Selection
            {
                Id = "SELN2",
                Price = 3.0,
                Status = SelectionStatus.Pending,
                Tradable = true
            };
            seln2.AddOrUpdateTagValue("name", "seln2");
            seln2.AddOrUpdateTagValue("displayed", "false");

            mkt1.Selections.Add(seln1);
            mkt1.Selections.Add(seln2);

            RollingMarket mkt2 = new RollingMarket { Id = "RMKT2", Line = 2.0 };
            mkt2.AddOrUpdateTagValue("name", "RMKT2");

            RollingSelection seln3 = new RollingSelection
            {
                Id = "SELN3",
                Price = 7.0,
                Status = SelectionStatus.Pending,
                Tradable = true,
                Line = 21
            };

            ((Market)mkt2).Selections.Add(seln3);
            Market test2 = mkt2;
            test2.Selections.Count.Should().Be(1);
            (test2.Selections.FirstOrDefault() is RollingSelection).Should().BeTrue();

            Market mkt3 = new RollingMarket();
            mkt3.Id = "RMKT3";
            mkt3.AddOrUpdateTagValue("name", "RMKT3");

            fixture.Markets.Add(mkt1);
            fixture.Markets.Add(mkt2);
            fixture.Markets.Add(mkt3);

            fixture.RollingMarkets.FirstOrDefault(x => x.Id == "MKT1").Should().BeNull();
            fixture.RollingMarkets.FirstOrDefault(x => x.Id == "RMKT2").Should().NotBeNull();
            fixture.RollingMarkets.FirstOrDefault(x => x.Id == "RMKT3").Should().NotBeNull();

            fixture.StandardMarkets.FirstOrDefault(x => x.Id == "MKT1").Should().NotBeNull();
            fixture.StandardMarkets.FirstOrDefault(x => x.Id == "RMKT2").Should().BeNull();
            fixture.StandardMarkets.FirstOrDefault(x => x.Id == "RMKT3").Should().BeNull();

            string value = FixtureJsonHelper.ToJson(fixture);
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(value)))
            {

                stream.Position = 0;
                string value2 = Encoding.UTF8.GetString(stream.ToArray());

                value.Should().BeEquivalentTo(value2);

                fixture = FixtureJsonHelper.GetFromJson(value2);
            }

            fixture.RollingMarkets.Count().Should().Be(2);
            fixture.StandardMarkets.Count().Should().Be(1);
            fixture.Markets.Count().Should().Be(3);
            fixture.Tags.Count.Should().Be(2);
            fixture.GameState.Count().Should().Be(2);
            fixture.Participants.Count().Should().Be(2);

            fixture.Markets.FirstOrDefault(x => x.Id == "MKT1").Selections.Count.Should().Be(2);
            fixture.Markets.FirstOrDefault(x => x.Id == "RMKT2").Selections.Count.Should().Be(1);
            fixture.Markets.FirstOrDefault(x => x.Id == "RMKT3").Selections.Count.Should().Be(0);

            BinaryStoreProvider<Fixture> provider = new BinaryStoreProvider<Fixture>(".", "test-{0}.dat");

            provider.SetObject("TEST", fixture);

            fixture = provider.GetObject("TEST");

            fixture.RollingMarkets.Count().Should().Be(2);
            fixture.StandardMarkets.Count().Should().Be(1);
            fixture.Markets.Count().Should().Be(3);
            fixture.Tags.Count.Should().Be(2);
            fixture.GameState.Count().Should().Be(2);
            fixture.Participants.Count().Should().Be(2);

            fixture.Markets.FirstOrDefault(x => x.Id == "MKT1").Selections.Count.Should().Be(2);
            fixture.Markets.FirstOrDefault(x => x.Id == "RMKT2").Selections.Count.Should().Be(1);
            fixture.Markets.FirstOrDefault(x => x.Id == "RMKT3").Selections.Count.Should().Be(0);

            fixture = value.FromJson<Fixture>();

            fixture.RollingMarkets.Count().Should().Be(2);
            fixture.StandardMarkets.Count().Should().Be(1);
            fixture.Markets.Count().Should().Be(3);
            fixture.Tags.Count.Should().Be(2);
            fixture.GameState.Count().Should().Be(2);
            fixture.Participants.Count().Should().Be(2);

            fixture.Markets.FirstOrDefault(x => x.Id == "MKT1").Selections.Count.Should().Be(2);
            fixture.Markets.FirstOrDefault(x => x.Id == "RMKT2").Selections.Count.Should().Be(1);
            fixture.Markets.FirstOrDefault(x => x.Id == "RMKT3").Selections.Count.Should().Be(0);
        }
        private static Market CreateSettledMarket(IMarketState MarketState)
        {
            var market = new Market (MarketState.Id);

            if (MarketState.HasTag("line"))
            {
                market.AddOrUpdateTagValue("line", MarketState.GetTagValue("line"));
            }

            foreach (var seln in MarketState.Selections)
                market.Selections.Add(new Selection { Id = seln.Id, Status = SelectionStatus.Void, Price = 0 });

            return market;
        }
        public void UnsuspendFixtureAndMarketsTest()
        {
            // STEP 1: prepare stub data
            Mock<IResourceFacade> resource = new Mock<IResourceFacade>();
            Mock<IAdapterPlugin> connector = new Mock<IAdapterPlugin>();
            Mock<IEventState> state = new Mock<IEventState>();
            Mock<ISettings> settings = new Mock<ISettings>();
            var provider = new StateManager(settings.Object, connector.Object);

            provider.SuspensionManager.RegisterAction(provider.SuspensionManager.SuspendInPlayMarketsStrategy, SuspensionReason.DISCONNECT_EVENT);

            // STEP 2: prepare the fixture data
            Fixture fixture = new Fixture { Id = "ABC", Sequence = 1, MatchStatus = ((int)MatchStatus.InRunning).ToString() };
            fixture.Tags.Add("Sport", "Football"); // add at least one tags, so the MarketsRulesManager recognize it as a full-snapshot

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

            var mkt = new Market("MKT-2");
            mkt.AddOrUpdateTagValue("traded_in_play", "false");
            fixture.Markets.Add(mkt);
            
            resource.Setup(x => x.Id).Returns("ABC");
            resource.Setup(x => x.Content).Returns(new Summary() { Sequence = 1 } );
            resource.Setup(x => x.MatchStatus).Returns(MatchStatus.InRunning);
            resource.Setup(x => x.StartStreaming()).Raises(x => x.StreamConnected += null, EventArgs.Empty);
            resource.Setup(x => x.StopStreaming()).Raises(x => x.StreamDisconnected += null, EventArgs.Empty);
            resource.Setup(x => x.GetSnapshot()).Returns(FixtureJsonHelper.ToJson(fixture));

            // STEP 4: start the listener
            StreamListener listener = new StreamListener(resource.Object, connector.Object, state.Object, provider, settings.Object);

            listener.Start();

            listener.IsStreaming.Should().BeTrue();
            
            connector.Verify(x => x.ProcessStreamUpdate(It.IsAny<Fixture>(), It.IsAny<bool>()), Times.Never);

            listener.Dispose();

            
            //Should suspend at the fixture level only
            connector.Verify(x=> x.Suspend(It.Is<string>(fId => fId == "ABC")),Times.Once);
            

            //recreate listener
            //must have state setup otherwise it will process snapshot not unsuspend
            state.Setup(x => x.GetFixtureState(It.Is<string>(fId => fId == fixture.Id)))
                .Returns(new FixtureState()
                {
                    Id = fixture.Id,
                    MatchStatus = MatchStatus.InRunning,
                    Sequence = fixture.Sequence
                });

            listener = new StreamListener(resource.Object, connector.Object, state.Object, provider, settings.Object);
            listener.Start();

            // Unsuspend should be called on reconnect 
            connector.Verify(x => x.UnSuspend(It.IsAny<Fixture>()),Times.Once);

        }
        public void SuspensionManagerStrategiesTest()
        {
            var stateProvider = new Mock<IStateProvider>();
            var plugin = new Mock<IAdapterPlugin>();
            var suspensionManager = new SuspensionManager(stateProvider.Object, plugin.Object);

            var state = new MarketStateCollection("FXT-ID");

            // STEP 1: prepare the fixture 
            // 1) fixture is in running
            // 2) with 2 in play markets
            // 3) 1 not in-play market
            // 4) 3 markets with an unknown state
            Fixture fixture = new Fixture
            {
                Id = "FXT-ID",
                MatchStatus = MatchStatus.InRunning.ToString(),
                Sequence = 2
            };

            var mkt1 = new Market { Id = "MKT-1" };
            mkt1.Selections.Add(new Selection { Id = "SELN", Status = SelectionStatus.Active });

            var mkt2 = new Market { Id = "MKT-2" };
            mkt2.Selections.Add(new Selection { Id = "SELN", Status = SelectionStatus.Active });

            var mkt3 = new Market { Id = "MKT-3" };
            mkt3.Selections.Add(new Selection { Id = "SELN", Status = SelectionStatus.Pending });

            var mkt4 = new Market { Id = "MKT-4" };
            mkt4.Selections.Add(new Selection { Id = "SELN", Status = SelectionStatus.Active });
            mkt4.AddOrUpdateTagValue("traded_in_play", "false");

            var mkt5 = new Market { Id = "MKT-5" };
            mkt5.Selections.Add(new Selection { Id = "SELN", Status = SelectionStatus.Active });
            mkt5.AddOrUpdateTagValue("traded_in_play", "true");

            var mkt6 = new Market { Id = "MKT-6" };
            mkt6.AddOrUpdateTagValue("traded_in_play", "true");

            fixture.Markets.Add(mkt1);
            fixture.Markets.Add(mkt2);
            fixture.Markets.Add(mkt3);
            fixture.Markets.Add(mkt4);
            fixture.Markets.Add(mkt5);
            fixture.Markets.Add(mkt6);
            state.Update(fixture, true);
            
            state.CommitChanges();
            
            // STEP 2: test the suspension strategies
            suspensionManager.SuspendAllMarketsStrategy(state);

            plugin.Verify(x => x.ProcessStreamUpdate(It.Is<Fixture>
                (
                    y => y.Markets.Count == 6 &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-1") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-2") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-3") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-4") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-5") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-6") != null
                ), It.IsAny<bool>()));


            suspensionManager.SuspendFixtureIfInPlayStrategy(state);

            plugin.Verify(x => x.Suspend(It.Is<string>(y => y == "FXT-ID")));

            suspensionManager.SuspendFixtureStrategy(state);

            plugin.Verify(x => x.Suspend(It.Is<string>(y => y == "FXT-ID")));

            suspensionManager.SuspendInPlayMarketsStrategy(state);

            // The SuspensionManager takes a conservative approach.
            // If the traded_in_play tag is not present, it assumes
            // that the market is a in-play market
            plugin.Verify(x => x.ProcessStreamUpdate(It.Is<Fixture>
                (
                    y => y.Markets.Count == 3 &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-1") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-2") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-3") == null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-4") == null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-5") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-6") == null
                ), It.IsAny<bool>()));


            // STEP 3: change the fixture
            // 1) change the fixture's match status
            // 2) remove a mkt
            // 3) add a new mkt

            fixture.MatchStatus = MatchStatus.MatchOver.ToString();

            fixture.Markets.Remove(fixture.Markets.FirstOrDefault(x => x.Id == "MKT-5"));

            var mkt7 = new Market { Id = "MKT-7" };
            mkt7.Selections.Add(new Selection { Id = "SELN", Status = SelectionStatus.Active });
            mkt7.AddOrUpdateTagValue("traded_in_play", "true");

            fixture.Markets.Add(mkt7);

            state.Update(fixture, true);
            state.CommitChanges();
            
            // STEP 4: test the suspension strategies again
            suspensionManager.SuspendAllMarketsStrategy(state);

            // note that we must have 7 markets now because the 
            // SuspensionManager looks at the MarketState, not
            // at the fixture
            plugin.Verify(x => x.ProcessStreamUpdate(It.Is<Fixture>
                (
                    y => y.Markets.Count == 7 &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-1") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-2") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-3") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-4") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-5") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-6") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-7") != null 
                ), It.IsAny<bool>()));


            suspensionManager.SuspendFixtureIfInPlayStrategy(state);

            // The fixture is "MatchOver", so the Suspend() method should not have been called
            // Times.Exactly(2) because it has been called in the previous step...unfortunately
            // there seems to be no way to reset the verifications
            plugin.Verify(x => x.Suspend(It.Is<string>(y => y == "FXT-ID")), Times.Exactly(2));

            suspensionManager.SuspendFixtureStrategy(state);

            plugin.Verify(x => x.Suspend(It.Is<string>(y => y == "FXT-ID")), Times.Exactly(3));

            suspensionManager.SuspendInPlayMarketsStrategy(state);

            plugin.Verify(x => x.ProcessStreamUpdate(It.Is<Fixture>
                (
                    y => y.Markets.Count == 4 &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-1") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-2") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-3") == null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-4") == null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-5") != null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-6") == null &&
                         y.Markets.FirstOrDefault(z => z.Id == "MKT-7") != null
                ), It.IsAny<bool>()));

        }
        public void SuspendFixtureOnStreamListenerDisposeTest()
        {
            // STEP 1: prepare stub data
            Mock<IResourceFacade> resource = new Mock<IResourceFacade>();
            Mock<IAdapterPlugin> connector = new Mock<IAdapterPlugin>();
            Mock<IEventState> state = new Mock<IEventState>();
            Mock<ISettings> settings = new Mock<ISettings>();
            var provider = new StateManager(settings.Object, connector.Object);

            provider.SuspensionManager.RegisterAction(provider.SuspensionManager.SuspendInPlayMarketsStrategy, SuspensionReason.DISCONNECT_EVENT);

            // STEP 3: prepare the fixture data
            Fixture fixture = new Fixture { Id = "ABC", Sequence = 1, MatchStatus = ((int)MatchStatus.InRunning).ToString() };
            fixture.Tags.Add("Sport", "Football"); // add at least one tags, so the MarketsRulesManager recognize it as a full-snapshot

            fixture.Markets.Add(new Market("MKT-1"));
            var mkt = new Market("MKT-2");
            mkt.AddOrUpdateTagValue("traded_in_play", "false");
            fixture.Markets.Add(mkt);


            resource.Setup(x => x.Id).Returns("ABC");
            resource.Setup(x => x.Content).Returns(new Summary());
            resource.Setup(x => x.MatchStatus).Returns(MatchStatus.InRunning);
            resource.Setup(x => x.StartStreaming()).Raises(x => x.StreamConnected += null, EventArgs.Empty);
            resource.Setup(x => x.StopStreaming()).Raises(x => x.StreamDisconnected += null, EventArgs.Empty);
            resource.Setup(x => x.GetSnapshot()).Returns(FixtureJsonHelper.ToJson(fixture));

            // STEP 4: start the listener
            StreamListener listener = new StreamListener(resource.Object, connector.Object, state.Object, provider,settings.Object);

            listener.Start();

            listener.IsStreaming.Should().BeTrue();

            connector.Verify(x => x.ProcessStreamUpdate(It.IsAny<Fixture>(), It.IsAny<bool>()), Times.Never);

            listener.Dispose();

            connector.Verify(x=> x.Suspend(It.IsAny<string>()),Times.Once);

            //// STEP 5: check the result, note tha MKT-2 is not in-play
            //connector.Verify(x => x.ProcessStreamUpdate(It.Is<Fixture>(
            //    y => y.Id == "ABC" &&
            //         y.Markets.Count == 1 &&
            //         y.Markets.FirstOrDefault(z => z.Id == "MKT-1") != null),
            //    It.IsAny<bool>()), Times.Once);

        }
        public void DoNotSuspendFixtureOnDisconeectIfDeletedTest()
        {
            // STEP 1: prepare stub data
            Mock<IResourceFacade> resource = new Mock<IResourceFacade>();
            Mock<IAdapterPlugin> connector = new Mock<IAdapterPlugin>();
            Mock<IEventState> state = new Mock<IEventState>();
            Mock<ISettings> settings = new Mock<ISettings>();
            var provider = new StateManager(settings.Object, connector.Object);


            // STEP 3: prepare the fixture data
            Fixture fixture = new Fixture { Id = "ABC", Sequence = 1, MatchStatus = ((int)MatchStatus.InRunning).ToString() };
            fixture.Tags.Add("Sport", "Football"); // add at least one tags, so the MarketsRulesManager recognize it as a full-snapshot

            fixture.Markets.Add(new Market("MKT-1"));
            var mkt = new Market("MKT-2");
            mkt.AddOrUpdateTagValue("traded_in_play", "false");
            fixture.Markets.Add(mkt);

            // ...and MatchStatus=MatchOver
            Fixture update = new Fixture
            {
                Id = "ABC",
                Sequence = 2,
                MatchStatus = ((int)MatchStatus.InRunning).ToString(),
                Epoch = 2,
                LastEpochChangeReason = new[] {(int)EpochChangeReason.Deleted}
            };

            StreamMessage message = new StreamMessage { Content = update };


            resource.Setup(x => x.Id).Returns("ABC");
            resource.Setup(x => x.Content).Returns(new Summary());
            resource.Setup(x => x.MatchStatus).Returns(MatchStatus.InRunning);
            resource.Setup(x => x.StartStreaming()).Raises(x => x.StreamConnected += null, EventArgs.Empty);
            resource.Setup(x => x.StopStreaming()).Raises(x => x.StreamDisconnected += null, EventArgs.Empty);
            resource.Setup(x => x.GetSnapshot()).Returns(FixtureJsonHelper.ToJson(fixture));

            // STEP 4: start the listener
            StreamListener listener = new StreamListener(resource.Object, connector.Object, state.Object, provider, settings.Object);

            listener.Start();

            listener.IsStreaming.Should().BeTrue();

            // STEP 5: send the update
            listener.ResourceOnStreamEvent(this, new StreamEventArgs(JsonConvert.SerializeObject(message)));


            // STEP 6: check the result, note tha MKT-2 is not in-play
            connector.Verify(x => x.ProcessStreamUpdate(It.Is<Fixture>(y => y.Id == "ABC" && y.Markets.Count == 0), It.IsAny<bool>()));

        }
        public void DeltaRuleWithMarketSeverity()
        {
            // STEP 1: prepare stub data
            var settings = new Mock<ISettings>();
            var plugin = new Mock<IAdapterPlugin>();
            var stateprovider = new StateManager(settings.Object, plugin.Object);

            DeltaRule.Instance.Severity = DeltaRule.DeltaRuleSeverity.REMOVE_MARKETS;

            List<IMarketRule> rules = new List<IMarketRule> { DeltaRule.Instance };

            // STEP 2: prepare fixture data
            // Fixture
            //  - MKT1
            //  -- SELN_1_1
            //  -- SELN_1_2
            //  - MKT2
            //  -- SELN_2_1
            //  -- SELN_2_2

            Fixture fixture = new Fixture { Id = "TestId", MatchStatus = "40" };
            fixture.Tags.Add("Sport", "Football");

            Market mkt = new Market { Id = "MKT1" };
            mkt.AddOrUpdateTagValue("name", "mkt1");
            mkt.AddOrUpdateTagValue("type", "type1");

            Selection seln = new Selection { Id = "SELN_1_1" };
            seln.AddOrUpdateTagValue("name", "seln_1_1");
            seln.Tradable = true;
            seln.Status = SelectionStatus.Active;
            seln.Price = 0.1;

            mkt.Selections.Add(seln);

            seln = new Selection { Id = "SELN_1_2" };
            seln.AddOrUpdateTagValue("name", "seln_1_2");
            seln.Tradable = true;
            seln.Status = SelectionStatus.Active;
            seln.Price = 0.2;

            mkt.Selections.Add(seln);

            fixture.Markets.Add(mkt);

            mkt = new Market { Id = "MKT2" };
            mkt.AddOrUpdateTagValue("name", "mkt2");
            mkt.AddOrUpdateTagValue("type", "type2");

            seln = new Selection { Id = "SELN_2_1" };
            seln.AddOrUpdateTagValue("name", "seln_2_1");
            seln.Tradable = false;
            seln.Status = SelectionStatus.Pending;
            seln.Price = null;

            mkt.Selections.Add(seln);

            seln = new Selection { Id = "SELN_2_2" };
            seln.AddOrUpdateTagValue("name", "seln_2_2");
            seln.Tradable = false;
            seln.Status = SelectionStatus.Pending;
            seln.Price = null;

            mkt.Selections.Add(seln);

            fixture.Markets.Add(mkt);

            // STEP 3: invoke the delta rule
            MarketRulesManager manager = new MarketRulesManager(fixture.Id, stateprovider, rules);

            manager.ApplyRules(fixture); // this will not have any effect as we don't have any state yet

            manager.CommitChanges();

            manager.ApplyRules(fixture); // here is where the delta rule is invoked (note that we haven't changed anything on the fixture)

            manager.CommitChanges();

            // STEP 4: check the results (delta rule should have removed everything)
            fixture.Markets.Count.Should().Be(0);

            // STEP 5: change a single field
            seln.Price = 1.2;
            fixture.Markets.Add(mkt);
            mkt.Selections.Count().Should().Be(2);

            // STEP 6: apply the delta rule again
            manager.ApplyRules(fixture);

            // STEP 7: the market should not have been filtered out
            fixture.Markets.Count().Should().Be(1);
            fixture.Markets[0].Selections.Count().Should().Be(2);
        }