Example #1
0
        public void TestSetStock()
        {
            var oldStocks = new HashSet<OldStock>(new[]
            {
                new OldStock
                {
                    Code = "ABC",
                    Name = "ABC Limited",
                    Price = 100,
                    Turnover = 1000,
                },
                new OldStock
                {
                    Code = "DEF",
                    Name = "DEF Limited",
                    Price = 20,
                    Turnover = 100,
                },
            });

            var oOldStocks = oldStocks.ToLiveSet();
            var iOldStocks = oOldStocks.PublishInner;
            var totalPrices = oOldStocks.Select(s => s.LiveProperty<decimal>("Price")).Sum();

            var stocks = new HashSet<Stock>
            {
                new Stock()
                {
                    Code = {PublishValue = "ABC" },
                    Name = {PublishValue = "ABC Limited"},
                    Price = {PublishValue = 100},
                    Turnover = {PublishValue = 1000},
                    Bid = {PublishValue = 10},
                    Ask = {PublishValue = 12},
                },
                new Stock()
                {
                    Code = {PublishValue = "DEF"},
                    Name = {PublishValue = "DEF Limited"},
                    Price = {PublishValue = 20},
                    Turnover = {PublishValue = 100},
                    Bid = {PublishValue = 8},
                    Ask = {PublishValue = 11},
                },
            }.ToLiveSet();

            var turnovers = stocks.Select(s => s.Turnover);
            var totalTurnover = turnovers.Sum();
            totalTurnover.Trace("totalTurnover");
            using (Publish.Transaction(true))
            {
            }

            Assert.AreEqual(StateStatus.Connecting, totalTurnover.Snapshot.Status);
            Assert.AreEqual(totalTurnover.Value, 1100);

            var abc = stocks.PublishInner.Single(s => s.Code.PublishValue == "ABC");
            var def = stocks.PublishInner.Single(s => s.Code.PublishValue == "DEF");

            using (Publish.Transaction(true))
            {
                abc.Turnover.PublishValue = 999;
                Assert.AreEqual(1100, totalTurnover.Snapshot.NewValue);
            }

            using (Publish.Transaction(true))
            {
                Assert.AreEqual(1099, totalTurnover.Snapshot.NewValue);
                abc.Turnover.PublishValue = 997;
                abc.Turnover.PublishValue = 998;
                Assert.AreEqual(1099, totalTurnover.Snapshot.NewValue);
            }

            using (Publish.Transaction(true))
            {
                Assert.AreEqual(1098, totalTurnover.Snapshot.NewValue);

                var overlap = stocks.CrossJoin(stocks).Where(ab => ab.Item1.Bid.GreaterThan(ab.Item2.Ask));
                overlap.Trace("overlap");
            }

            using (Publish.Transaction(true))
            {
                abc.Bid.PublishValue = 11.5M;
            }
            using (Publish.Transaction(true))
            {
                def.Ask.PublishValue = 12;
            }
        }