public void ParseHousePriceCsvRowMatchesExpectedHousePriceDao() { var housePriceFields = "E06000001,Hartlepool,E05008943,De Bruce,38000,38000,37000,30500,30000,30500,29500,31500,37000,39950,40000,42000,32000,32000,34750,35750,35750,35000,32450,34975,36650,43500,53372.5,52875,54995,57500,50497.5,48997.5,49475,48750,58187,62995,62995,60000,58475,57500,57500,59500,62575,72000,79000,79000,83000,86500,86000,90000,92500,97500,100000,112000,119500,108000,96500,92500,90000,85000,98000,98000,99997.5,99997.5,90000,98942.5,90000,81250,79975,76000,72412.5,68000,80000,85000,100000,90000,83000,75000,72500,82000,81000,82250,81000,78000,80000,80000,87000,100975,107225,115000,118250,124950,130000,126500,127975,124950,121375,128000,135000,131475,,,,," .Split(","); var expectedHousePrice = new HousePrice { AverageHousePrice = 131475, LocalAuthorityDistrict = new LocalAuthorityDistrict { Code = "E06000001", Name = "Hartlepool" }, Ward = new Ward { Code = "E05008943", Name = "De Bruce" } }; var expectedList = new List <HousePrice> { expectedHousePrice }; _textFieldParser.ReadFields().Returns(housePriceFields) .AndDoes((action) => _textFieldParser.EndOfData.Returns(true)); var actualList = _housePriceParser.Parse(); Assert.AreEqual(expectedList, actualList); }
public void ParseLowerLayerSuperOutputAreaCsvRowMatchesExpectedLowerLayerSuperOutputAreaDao() { var lowerLayerSuperOutputAreaRowFields = "E01000001,City of London 001A,E09000001,City of London,29199,9".Split(','); var expectedLowerLayerSuperOutputArea = new LowerLayerSuperOutputArea { Code = "E01000001", Name = "City of London 001A", IndexOfMultipleDeprivation = new IndexOfMultipleDeprivation { Decile = 9, Rank = 29199, }, LocalAuthorityDistrict = new LocalAuthorityDistrict { Code = "E09000001", Name = "City of London", } }; var expectedList = new List <LowerLayerSuperOutputArea> { expectedLowerLayerSuperOutputArea }; _textFieldParser.ReadFields().Returns(lowerLayerSuperOutputAreaRowFields) .AndDoes((action) => _textFieldParser.EndOfData.Returns(true)); var actualList = _lowerLayerSuperOutputAreaParser.Parse(); Assert.AreEqual(expectedList, actualList); }
public void ParseLsoaToWardCodeCsvRowMatchesLsoaToWardCodeKvPair() { var lsoaToWardCodeRowFields = "157,E01011954,Hartlepool 001A,E05008943,De Bruce,E06000001,Hartlepool".Split(","); var expectedDictionary = new Dictionary <string, string> { { "E01011954", "E05008943" }, }; _textFieldParser.ReadFields().Returns(lsoaToWardCodeRowFields) .AndDoes((action) => _textFieldParser.EndOfData.Returns(true)); var actualDictionary = _lowerLayerSuperOutputAreaCodeToWardCodeParser.KeyValueParse(); Assert.AreEqual(expectedDictionary, actualDictionary); }
private void ReadCsvPath(IList <K> list, IDictionary <K, V> dictionary) { _textFieldParser.CommentTokens = new[] { "#" }; _textFieldParser.SetDelimiters(","); _textFieldParser.HasFieldsEnclosedInQuotes = true; _textFieldParser.ReadLine(); while (!_textFieldParser.EndOfData) { var fields = _textFieldParser.ReadFields(); if (list == null && dictionary != null) { OnReadFields(dictionary, fields); } else if (dictionary == null && list != null) { OnReadFields(list, fields); } } }
/// <summary> /// Loads and parses a csv file. /// </summary> /// <returns>Task.</returns> public async Task ParseCSVFile() { try { EnableControls = false; OnStatusUpdated("Reading CSV file..."); Scrobbles.Clear(); using (ITextFieldParser parser = _parserFactory.CreateParser(CSVFilePath)) { parser.SetDelimiters(Settings.Default.CSVDelimiters.Select(x => new string(x, 1)).ToArray()); string[] fields = null; List <string> errors = new List <string>(); ObservableCollection <ParsedCSVScrobbleViewModel> parsedScrobbles = new ObservableCollection <ParsedCSVScrobbleViewModel>(); await Task.Run(() => { while (!parser.EndOfData) { try { fields = parser.ReadFields(); string dateString = fields[Settings.Default.TimestampFieldIndex]; // check for 'now playing' if (dateString == "" && ScrobbleMode == CSVScrobbleMode.Normal) { continue; } DateTime date = DateTime.Now; if (!DateTime.TryParse(dateString, out date)) { bool parsed = false; // try different formats until succeeded foreach (string format in _formats) { parsed = DateTime.TryParseExact(dateString, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out date); if (parsed) { break; } } if (!parsed && ScrobbleMode == CSVScrobbleMode.Normal) { throw new Exception("Timestamp could not be parsed!"); } } // try to get optional parameters first string album = fields.ElementAtOrDefault(Settings.Default.AlbumFieldIndex); string albumArtist = fields.ElementAtOrDefault(Settings.Default.AlbumArtistFieldIndex); string duration = fields.ElementAtOrDefault(Settings.Default.DurationFieldIndex); TimeSpan time = TimeSpan.FromSeconds(Duration); TimeSpan.TryParse(duration, out time); DatedScrobble parsedScrobble = new DatedScrobble(date.AddSeconds(1), fields[Settings.Default.TrackFieldIndex], fields[Settings.Default.ArtistFieldIndex], album, albumArtist, time); ParsedCSVScrobbleViewModel vm = new ParsedCSVScrobbleViewModel(parsedScrobble, ScrobbleMode); vm.ToScrobbleChanged += ToScrobbleChanged; parsedScrobbles.Add(vm); } catch (Exception ex) { string errorString = "CSV line number: " + parser.LineNumber + ","; foreach (string s in fields) { errorString += s + ","; } errorString += ex.Message; errors.Add(errorString); } } }); if (errors.Count == 0) { OnStatusUpdated("Successfully parsed CSV file. Parsed " + parsedScrobbles.Count + " rows"); } else { OnStatusUpdated("Partially parsed CSV file. " + errors.Count + " rows could not be parsed"); if (MessageBox.Show("Some rows could not be parsed. Do you want to save a text file with the rows that could not be parsed?", "Error parsing rows", MessageBoxButtons.YesNo) == DialogResult.Yes) { SaveFileDialog sfd = new SaveFileDialog() { Filter = "Text Files|*.txt" }; if (sfd.ShowDialog() == DialogResult.OK) { File.WriteAllLines(sfd.FileName, errors.ToArray()); } } } Scrobbles = parsedScrobbles; } } catch (Exception ex) { Scrobbles.Clear(); OnStatusUpdated("Error parsing CSV file: " + ex.Message); } finally { EnableControls = true; } }