public void LoadTradeData_XmlFile_NoException()
        {
            // arrange
            const string xmlFileAsString =
                @"<?xml version='1.0' encoding='utf-8' ?>
                <values>
                    <value date='2014-5-20' open='30.16' high='30.39' low='30.02' close='30.17' volume='1478200' />
                    <value date='2014-5-17' open='29.77' high='30.26' low='29.77' close='30.26' volume='2481400' />
                    <value date='2014-5-16' open='29.78' high='29.94' low='29.55' close='29.67' volume='1077000' />
                </values>";
            var fileInMemory = Helpers.GenerateStreamFromString(xmlFileAsString);

            #region prepare expected values
            var expectedPackage = new TradeDataPackage();
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 20),
                    new decimal(30.16),
                    new decimal(30.39),
                    new decimal(30.02),
                    new decimal(30.17),
                    1478200
                    ));
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 17),
                    new decimal(29.77),
                    new decimal(30.26),
                    new decimal(29.77),
                    new decimal(30.26),
                    2481400
                    ));
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 16),
                    new decimal(29.78),
                    new decimal(29.94),
                    new decimal(29.55),
                    new decimal(29.67),
                    1077000
                    ));
            #endregion

            // act
            var package = _loader.LoadTradeData(fileInMemory);

            // assert
            Assert.IsTrue(package.TradeDataList.Count == 3); // check the count

            for (int i = 0; i < 3; i++)                      // check all the values
            {
                var actual   = package.TradeDataList[i];
                var expected = expectedPackage.TradeDataList[i];
                Assert.AreEqual(expected, actual, "One of the values has been read incorrect");
            }
        }
Exemplo n.º 2
0
        public void Constuctor_NoParams_NoException()
        {
            // arrange

            // act
            var package = new TradeDataPackage();

            // assert
            Assert.IsTrue(package.TradeDataList.Count == 0);
        }
Exemplo n.º 3
0
        /// <summary>
        /// On any update of trade data
        /// </summary>
        /// <param name="sender">ITradeDataMonitor</param>
        /// <param name="tradeDataPackage">update trade data package</param>
        private void TradeDataMonitorOnTradeDataUpdate(object sender, TradeDataPackage tradeDataPackage)
        {
            if (tradeDataPackage == null)
            {
                return;
            }

            // adding to TradeDataList to let UI display new data
            _dispatcher.Invoke(() => tradeDataPackage.TradeDataList.ForEach(data => _tradeDataList.Add(data)));
        }
        public void LoadTradeData_TxtFile_NoException()
        {
            // arrange
            const string txtFileAsString =
                @"Date;Open;High;Low;Close;Volume
                2014-5-20;30.16;30.39;30.02;30.17;1478200
                2014-5-17;29.77;30.26;29.77;30.26;2481400
                2014-5-16;29.78;29.94;29.55;29.67;1077000";
            var fileInMemory = Helpers.GenerateStreamFromString(txtFileAsString);

            #region prepare expected values
            var expectedPackage = new TradeDataPackage();
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 20),
                    new decimal(30.16),
                    new decimal(30.39),
                    new decimal(30.02),
                    new decimal(30.17),
                    1478200
                    ));
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 17),
                    new decimal(29.77),
                    new decimal(30.26),
                    new decimal(29.77),
                    new decimal(30.26),
                    2481400
                    ));
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 16),
                    new decimal(29.78),
                    new decimal(29.94),
                    new decimal(29.55),
                    new decimal(29.67),
                    1077000
                    ));
            #endregion

            // act
            var package = _loader.LoadTradeData(fileInMemory);

            // assert
            Assert.IsTrue(package.TradeDataList.Count == 3); // check the count

            for (int i = 0; i < 3; i++)                      // check all the values
            {
                var actual   = package.TradeDataList[i];
                var expected = expectedPackage.TradeDataList[i];
                Assert.AreEqual(expected, actual, "One of the values has been read incorrect");
            }
        }
        public void LoadTradeData_CsvFile_NoException()
        {
            // arrange
            const string csvFileAsString =
                @"2014-5-20,30.16,30.39,30.02,30.17,1478200
                2014-5-17,29.77,30.26,29.77,30.26,2481400
                2014-5-16,29.78,29.94,29.55,29.67,1077000";
            var csvFileInMemory = Helpers.GenerateStreamFromString(csvFileAsString);

            #region prepare expected values from csv format
            var expectedPackage = new TradeDataPackage();
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 20),
                    new decimal(30.16),
                    new decimal(30.39),
                    new decimal(30.02),
                    new decimal(30.17),
                    1478200
                    ));
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 17),
                    new decimal(29.77),
                    new decimal(30.26),
                    new decimal(29.77),
                    new decimal(30.26),
                    2481400
                    ));
            expectedPackage.TradeDataList.Add(
                new TradeData(
                    new DateTime(2014, 5, 16),
                    new decimal(29.78),
                    new decimal(29.94),
                    new decimal(29.55),
                    new decimal(29.67),
                    1077000
                    ));
            #endregion

            // act
            var package = _loader.LoadTradeData(csvFileInMemory);

            // assert
            Assert.IsTrue(package.TradeDataList.Count == 3); // check the count

            for (int i = 0; i < 3; i++)                      // check all the values
            {
                var actual   = package.TradeDataList[i];
                var expected = expectedPackage.TradeDataList[i];
                Assert.AreEqual(expected, actual, "One of the values has been read incorrect");
            }
        }
Exemplo n.º 6
0
        public void TradeDataList_OnTradeDataUpdate_NoException()
        {
            // arrange
            #region prepare trade data updates (TradeDataPackage)
            var date           = new DateTime(2013, 5, 20);
            var open           = new decimal(30.16);
            var high           = new decimal(30.39);
            var low            = new decimal(30.02);
            var close          = new decimal(30.17);
            var volume         = 1478200;
            var data1          = new TradeData(date, open, high, low, close, volume);
            var data2          = new TradeData(date.AddDays(1), open, high, low, close, volume);
            var data3          = new TradeData(date.AddDays(2), open, high, low, close, volume);
            var updatePackage1 = new TradeDataPackage(new List <TradeData> {
                data1, data2, data3
            });
            var data4          = new TradeData(date.AddDays(3), open, high, low, close, volume);
            var data5          = new TradeData(date.AddDays(4), open, high, low, close, volume);
            var data6          = new TradeData(date.AddDays(5), open, high, low, close, volume);
            var updatePackage2 = new TradeDataPackage(new List <TradeData> {
                data4, data5, data6
            });
            var expectedTradeDataList = new List <TradeData> {
                data1, data2, data3, data4, data5, data6
            };
            #endregion

            var mockedTradeDataMonitor = A.Fake <ITradeDataMonitor>(); // setup mocked model ITradeDataMonitor

            var mockedAppDispatcher = A.Fake <IDispatcher>();          // setup mocked Application.Current.Dispatcher.Invoke()
            mockedAppDispatcher.CallsTo(d => d.Invoke(null)).WithAnyArguments().Invokes((conf) => (conf.Arguments.First() as Action).Invoke());

            var viewModel = new TradeDataMonitorViewModel(mockedTradeDataMonitor, mockedAppDispatcher); // inject

            // act
            mockedTradeDataMonitor.TradeDataUpdate += Raise.With(updatePackage1); // send first update to viewModel
            mockedTradeDataMonitor.TradeDataUpdate += Raise.With(updatePackage2); // send second update to viewModel

            // assert
            Assert.AreEqual(expectedTradeDataList.Count, viewModel.TradeDataList.Count);
            for (int i = 0; i < expectedTradeDataList.Count; i++)
            {
                Assert.AreEqual(expectedTradeDataList[i], viewModel.TradeDataList[i]);
            }
        }
Exemplo n.º 7
0
        public void Constuctor_TradeDataList_NoException()
        {
            // arrange
            var date     = new DateTime(2013, 5, 20);
            var open     = new decimal(30.16);
            var high     = new decimal(30.39);
            var low      = new decimal(30.02);
            var close    = new decimal(30.17);
            var volume   = 1478200;
            var data     = new TradeData(date, open, high, low, close, volume);
            var dataList = new List <TradeData> {
                data, data, data
            };

            // act
            var package = new TradeDataPackage(dataList);

            // assert
            Assert.IsTrue(package.TradeDataList.Count == dataList.Count);
            Assert.AreSame(package.TradeDataList, dataList);
        }
        public void TradeDataUpdateEventFired_NewCorrectFilesInDirectory_NoException()
        {
            // arrange

            // fake new file to be returned from IFileSystemManager.GetNewFilesFromDirectory :
            var someNewFile = new FileInfo("someNewFile.csv");

            // fake TradeDataPackage as allegedly read from a fake new file ITradeDataLoader.LoadTradeData :
            #region prepare trade data updates (TradeDataPackage)
            var date   = new DateTime(2013, 5, 20);
            var open   = new decimal(30.16);
            var high   = new decimal(30.39);
            var low    = new decimal(30.02);
            var close  = new decimal(30.17);
            var volume = 1478200;
            var data1  = new TradeData(date, open, high, low, close, volume);
            var data2  = new TradeData(date.AddDays(1), open, high, low, close, volume);
            var data3  = new TradeData(date.AddDays(2), open, high, low, close, volume);
            #endregion
            var tradeDataPackageFromSomeNewFile = new TradeDataPackage(new List <TradeData> {
                data1, data2, data3
            });

            // setup mocked IFileSystemManager to return list of new files
            var mockedFileSystem = A.Fake <IFileSystemManager>();
            mockedFileSystem.CallsTo(m => m.GetNewFilesFromDirectory(DateTime.MinValue, null))
            .WithAnyArguments()
            .Returns(new List <FileInfo> {
                someNewFile
            });

            // setup mocked ITradeDataLoader to read TradeData from a fake new file
            var loader = A.Fake <ITradeDataLoader>();
            loader.CallsTo(l => l.CouldLoad(someNewFile)).Returns(true);
            loader.CallsTo(l => l.LoadTradeData(someNewFile)).Returns(tradeDataPackageFromSomeNewFile);

            #region setup mocked timer to immediate callback call (Timer_Tick) on ITimer.Change regardless of periods
            var mockedTimer = A.Fake <ITimer>();

            // preserve callback on Timer.Init call:
            TimerCallback callbackTimerTick = null;
            mockedTimer.CallsTo(t => t.Init(null, null, 0, 0)).WithAnyArguments().
            Invokes(conf => callbackTimerTick = (conf.Arguments[0] as TimerCallback));

            // call callback regardless of dueTimes periods, but only once to avoid infinite loop
            bool wasCallbackCalledYet = false;
            mockedTimer.CallsTo(t => t.Change(0, 0)).WithAnyArguments().
            Invokes(conf =>
            {
                if (!wasCallbackCalledYet)
                {
                    wasCallbackCalledYet = true;     // only once
                    callbackTimerTick.Invoke(null);
                }
            });
            #endregion

            const int    periodSeconds       = 5;
            const string monitoringDirectory = "X:\\folder1\\folder2\\";
            var          monitor             = new TradeDataMonitor(mockedFileSystem, mockedTimer, loader, periodSeconds, monitoringDirectory); // inject

            // setup mocked eventHandler for TradeDataUpdate event
            var mockedTradeDataUpdateEventHandler = A.Fake <EventHandler <TradeDataPackage> >();
            monitor.TradeDataUpdate += mockedTradeDataUpdateEventHandler; // subscribe mockedHandler

            // act
            monitor.StartMonitoring(); // call StartMonitoring produce a chain of calls of precooked mocked methods, all the way down to TradeDataUpdate event fire!

            // assert TradeDataUpdateEvent has been fired with apropriate params
            A.CallTo(() => mockedTradeDataUpdateEventHandler.Invoke(monitor, tradeDataPackageFromSomeNewFile)).MustHaveHappened();
        }