private static TSList <TradeData> DeserializeTrades(string[] lines) { if (lines == null) { return(null); } TSList <TradeData> tempTrades = new TSList <TradeData>(); int cnt = int.Parse(lines[0]); for (int i = 0; i < cnt; i++) { string[] vars = new string[7]; for (int j = 0; j < 7; j++) { vars[j] = lines[i * 7 + 1 + j]; } TradeData td = TradeData.Parse(vars); tempTrades.Add(td); } return(tempTrades); }
/// <summary> /// Reading a stream by lines /// trying to parse TradeData /// <remarks>method allocated to decouple from filesteams and to provide testability on memorystreams</remarks> /// </summary> /// <param name="stream">Stream to read</param> /// <returns>Single package of trade data that has been loaded</returns> public override TradeDataPackage LoadTradeData(Stream stream) { var dataList = new List <TradeData>(); using (var sr = new StreamReader(stream)) { string line; sr.ReadLine(); // skip the first line with columns names while ((line = sr.ReadLine()) != null) { try { var arr = line.Split(';'); // split by value separator symbol var data = TradeData.Parse(arr); // parse values into TradeData object dataList.Add(data); } catch { // suppress any exceptions when trying to read corrupted data from file // disputable decision // TODO: possibly revise exception suppressing later } } } return(new TradeDataPackage(dataList)); }
/// <summary> /// Reading a stream by lines /// trying to parse TradeData /// <remarks>method allocated to decouple from filesteams and to provide testability on memorystreams</remarks> /// </summary> /// <param name="stream">Stream to read</param> /// <returns>Single package of trade data that has been loaded</returns> public override TradeDataPackage LoadTradeData(Stream stream) { var dataList = new List <TradeData>(); using (XmlReader reader = XmlReader.Create(stream)) { try { while (reader.ReadToFollowing("value")) { var valuesArray = new string[6]; reader.MoveToFirstAttribute(); valuesArray[0] = reader.Value; int i = 1; while (reader.MoveToNextAttribute()) { valuesArray[i++] = reader.Value; } var data = TradeData.Parse(valuesArray); dataList.Add(data); } } catch { // suppress any exceptions when trying to read corrupted data from file or different xml schema // disputable decision // TODO: possibly revise exception suppressing later } } return(new TradeDataPackage(dataList)); }
public void Parse_NullValuesArray_ArgumentNullException() { // arrange string[] nullValuesArray = null; // act var data = TradeData.Parse(nullValuesArray); }
public void Parse_IncorrectVolumeValue_FormatException() { // arrange const string csvStringWithIncorrectDateFormat = "2013-5-20,30.16,30.39,30.02,30.17,##123#"; var incorrectValues = csvStringWithIncorrectDateFormat.Split(','); // act var data = TradeData.Parse(incorrectValues); }
public void Parse_AllValuesPlusExtras_NoException() { // arrange const string csvString = "2013-5-20,30.16,30.39,30.02,30.17,1478200,some-extra-data1, some-extra-data2"; var valuesWithExtras = csvString.Split(','); // act var data = TradeData.Parse(valuesWithExtras); }
public void Parse_NotAllValues_FormatException() { // arrange const string csvStringWithNoVolume = "2013-5-20,30.16,30.39,30.02,30.17"; var valuesWithoutVolume = csvStringWithNoVolume.Split(','); // act var data = TradeData.Parse(valuesWithoutVolume); }
public void Parse_IncorrectNullDateValue_FormatException() { // arrange const string csvStringWithIncorrectDateFormat = "2013####,30.16,30.39,30.02,30.17,1478200"; var incorrectValues = csvStringWithIncorrectDateFormat.Split(','); incorrectValues[0] = null; // act var data = TradeData.Parse(incorrectValues); }
public void Parse_AllCorrectValues_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; const string correctCsvString = "2013-5-20,30.16,30.39,30.02,30.17,1478200"; var corectValues = correctCsvString.Split(','); // act var data = TradeData.Parse(corectValues); // assert Assert.AreEqual(date, data.Date); Assert.AreEqual(open, data.Open); Assert.AreEqual(high, data.High); Assert.AreEqual(low, data.Low); Assert.AreEqual(close, data.Close); Assert.AreEqual(volume, data.Volume); }