public void SmokeTest() { var csvLine1 = "A;B;C"; var csvLine2 = "a1;b1;c1"; var csvLine3 = "a2;b2;c2"; var csvLineReader = new CsvLineParser(); var csvTableReader = new CsvLineTableReader(csvLineReader); Assert.NotNull(csvTableReader); csvTableReader.ReadHeaders(csvLine1); Assert.Equal(3, csvTableReader.Headers.Count); Assert.Equal("A", csvTableReader.Headers[0]); Assert.Equal("B", csvTableReader.Headers[1]); Assert.Equal("C", csvTableReader.Headers[2]); csvTableReader.Read(csvLine2); Assert.Equal(3, csvTableReader.Headers.Count); Assert.Equal("a1", csvTableReader[0]); Assert.Equal("b1", csvTableReader[1]); Assert.Equal("c1", csvTableReader[2]); csvTableReader.Read(csvLine3); Assert.Equal(3, csvTableReader.Headers.Count); Assert.Equal("a2", csvTableReader["A"]); Assert.Equal("b2", csvTableReader["B"]); Assert.Equal("c2", csvTableReader["C"]); }
public void TestJsonValues() { var csvLine2 = "42;\"{\"\"IntValue\"\":42,\"\"StrValue\"\":\"\"Forty-Two\"\"}\""; var csvLine3 = "5;\"{\"\"IntValue\"\":5,\"\"StrValue\"\":\"\"Five\"\"}\""; var csvLineReader = new CsvLineParser(); var csvTableReader = new CsvLineTableReader(csvLineReader, "A", "B"); Assert.NotNull(csvTableReader); csvTableReader.AddConverter <CustomType>(new CustomTypeConverter()); Assert.Equal(2, csvTableReader.Headers.Count); Assert.Equal("A", csvTableReader.Headers[0]); Assert.Equal("B", csvTableReader.Headers[1]); csvTableReader.Read(csvLine2); Assert.Equal(42, csvTableReader.GetValue <int>("A")); var o1 = csvTableReader.GetValue <CustomType>("B"); Assert.Equal(42, o1.IntValue); Assert.Equal("Forty-Two", o1.StrValue); csvTableReader.Read(csvLine3); Assert.Equal(5, csvTableReader.GetValue <int>("A")); var o2 = csvTableReader.GetValue <CustomType>("B"); Assert.Equal(5, o2.IntValue); Assert.Equal("Five", o2.StrValue); }
public void Ensures_GivenValidLine_ResultsInValidIShare() { var lineParser = new CsvLineParser(); IShare share = lineParser.ParseLine("04.03.2013;7.654,67;7.708,88;7.637,32;7.691,68;2.148.781.568"); Assert.That(share.Datum, Is.EqualTo(new DateTime(2013, 3, 4))); }
public ExportStudent(Configuration config, XlsReader xlsReader) { xlsReader.TryGetValue(config.GetXlsColumnName("Vorname"), out FirstName); xlsReader.TryGetValue(config.GetXlsColumnName("Mittelname"), out MiddleName); xlsReader.TryGetValue(config.GetXlsColumnName("Nachname"), out LastName); xlsReader.TryGetValue(config.GetXlsColumnName("Geburtstag"), out BirthDate); xlsReader.TryGetValue(config.GetXlsColumnName("Rufname"), out NickName); xlsReader.TryGetValue(config.GetXlsColumnName("Geschlecht"), out Gender); xlsReader.TryGetValue(config.GetXlsColumnName("Anrede"), out Salutation); if (xlsReader.TryGetValue(config.GetXlsColumnName("Name"), out string name)) { var csvLineParser = new CsvLineParser(' '); var parts = csvLineParser.Read(name); if (parts.Length == 2) { FirstName = parts[0]; LastName = parts[1]; } else if (parts.Length == 3) { FirstName = parts[0]; MiddleName = parts[1]; LastName = parts[2]; } } if (!xlsReader.TryGetValue(config.GetXlsColumnName("Id"), out Id)) { Id = GenerateId(); } }
public void ParseLine_InvalidDateTimeFormat_Exception() { CsvLineParser lineParser = new CsvLineParser(TestInitializationFactory.GenerateFakeColumnTypes()); string lineToParse = "2018ax01-03 12:00:00, 1,2,3,4,5"; Assert.Catch(() => lineParser.ParseLine(lineToParse)); }
private async Task <int> ExportTeachers(CsvTableReader csvTableReader, EcfTableWriter ecfTableWriter, string[] ecfHeaders) { var ecfCache = new HashSet <string>(); var ecfRecordCounter = 0; await csvTableReader.ReadHeadersAsync(); if (ecfHeaders != null && ecfHeaders.Length > 0) { await ecfTableWriter.WriteHeadersAsync(ecfHeaders); } else { await ecfTableWriter.WriteHeadersAsync( EcfHeaders.Id, EcfHeaders.Code); } while (await csvTableReader.ReadAsync() > 0) { if (csvTableReader.TryGetValue("Alle Lehrkräfte (Kürzel) mit Fach", out var value)) { var csvLineParser = new CsvLineParser(','); var subValues = csvLineParser.Read(value); csvLineParser.Configuration.Separator = ' '; foreach (var subValue in subValues) { if (!string.IsNullOrEmpty(subValue)) { var subValueParts = csvLineParser.Read(subValue.Trim()); if (subValueParts.Length == 2) { var teacherCode = subValueParts[0]; if (!string.IsNullOrEmpty(teacherCode) && !ecfCache.Contains(teacherCode)) { ecfTableWriter.TrySetValue(EcfHeaders.Id, teacherCode); ecfTableWriter.TrySetValue(EcfHeaders.Code, teacherCode); await ecfTableWriter.WriteAsync(); ecfCache.Add(teacherCode); ecfRecordCounter++; } } } } } } return(ecfRecordCounter); }
public void CreateSystem(XbimModel model, string classificationName) { //set model in which the systems are to be created _model = model; string data = null; //try to get data from resources //get list of classifications available var dir = Directory.EnumerateFiles("Classifications"); string clasPath = null; foreach (var f in dir) { if (Path.GetFileNameWithoutExtension(f).ToLower() == classificationName.ToLower()) { clasPath = f; break; } } if (clasPath == null) throw new ArgumentException("Specified classification doesn't exist"); var clasName = Path.GetFileNameWithoutExtension(clasPath); //get data data = File.ReadAllText(clasPath); CsvLineParser parser = null; if (clasName.Contains("NRM")) { parser = new CsvLineParser(';'); } else parser = new CsvLineParser(); //create classification source var action = new Action<XbimModel>(m => { var source = model.Instances.New<IfcClassification>(c => { c.Source = clasName; c.Edition = "Default edition"; c.Name = clasName; }); ParseCSV(data, parser, source); }); //process file if (_model.IsTransacting) action(_model); else using (var txn = model.BeginTransaction("Systems creation")) { action(_model); txn.Commit(); } }
public async Task TestThat_GetCoffeeShopLocation_When_ValidationDoesNotFail_Returns_NotNullLocation() { // Arrange var csvLineParser = new CsvLineParser(); // Act var location = await csvLineParser.GetCoffeeShopLocation(MockObjects.ValidCsvLine1); // Assert Assert.NotNull(location); }
public async Task TestThat_GetCoffeeShopLocation_When_ValidatorDoesNotFail_Returns_LocationWithExpectedValues() { var csvLineParser = new CsvLineParser(); // Act var location = await csvLineParser.GetCoffeeShopLocation(MockObjects.ValidCsvLine99); // Assert Assert.Equal(MockObjects.ShopLocation99.X, location.X); Assert.Equal(MockObjects.ShopLocation99.Y, location.Y); Assert.Equal(MockObjects.ShopLocation99.Tag, location.Tag); }
public async Task TestThat_GetCoffeeShopLocation_When_CsvLineHasErrorOnPositionTwo_Throws_DataValidationExceptionWithExpectedMessage() { // Arrange var csvLineParser = new CsvLineParser(); // Act async Task Act() => await csvLineParser.GetCoffeeShopLocation(MockObjects.Token2ErrorCsvLine); // Assert var exception = await Assert.ThrowsAsync <DataValidationException>(Act); Assert.Equal(MockValues.IncorrectValuePosition2CsvLineExceptionMessage, exception.Message); }
public async Task TestThat_GetCoffeeShopLocation_When_CsvLineHasMoreThanThreeTokens_Throws_DataValidationExceptionWithExpectedMessage() { // Arrange var csvLineParser = new CsvLineParser(); // Act async Task Act() => await csvLineParser.GetCoffeeShopLocation(MockObjects.MoreThanThreeTokensCsvLine); // Assert var exception = await Assert.ThrowsAsync <DataValidationException>(Act); Assert.Equal(MockValues.IncorrectNumberOfTokensCsvLineExceptionMessage, exception.Message); }
public async Task TestThat_GetCoffeeShopLocation_When_CsvLineIsEmpty_Throws_DataValidationExceptionWithExpectedMessage() { // Arrange var csvLineParser = new CsvLineParser(); // Act async Task Act() => await csvLineParser.GetCoffeeShopLocation(MockObjects.StringNullCsvLine); // Assert var exception = await Assert.ThrowsAsync <DataValidationException>(Act); Assert.Equal(MockValues.NullOrEmptyCsvLineExceptionMessage, exception.Message); }
public void ParseLine_ValidLineParse_Success() { CsvLineParser lineParser = new CsvLineParser(TestInitializationFactory.GenerateFakeColumnTypes()); string lineToParse = "2018-01-03 12:00:00, 1,2,3,4,5"; DataObject result = lineParser.ParseLine(lineToParse); Assert.AreEqual(result.Columns.Length, 6); Assert.AreEqual(result.Columns[0].GetType(), typeof(DateTime)); for (int i = 1; i < 6; i++) { Assert.AreEqual(result.Columns[i].GetType(), typeof(int)); Assert.AreEqual(result.Columns[i], i); } }
public void TestTypedValues() { var csvLine1 = "A;B;C;D"; var csvLine2 = "22;Text;true;01.01.2010"; var csvLine3 = "-31;A long text;false;20.01.2050"; var csvLine4 = "55;\"A text with ;\";;31.07.1971"; var csvLineReader = new CsvLineParser(); var csvTableReader = new CsvLineTableReader(csvLineReader); Assert.NotNull(csvTableReader); csvTableReader.SetFormats <DateTime>("dd.MM.yyyy"); csvTableReader.ReadHeaders(csvLine1); Assert.Equal(4, csvTableReader.Headers.Count); Assert.Equal("A", csvTableReader.Headers[0]); Assert.Equal("B", csvTableReader.Headers[1]); Assert.Equal("C", csvTableReader.Headers[2]); Assert.Equal("D", csvTableReader.Headers[3]); csvTableReader.Read(csvLine2); Assert.Equal(22, csvTableReader.GetValue <int>("A")); Assert.Equal("Text", csvTableReader.GetValue <string>("B")); Assert.True(csvTableReader.GetValue <bool>("C")); Assert.Equal(new DateTime(2010, 1, 1), csvTableReader.GetValue <DateTime>("D")); csvTableReader.Read(csvLine3); Assert.Equal(-31, csvTableReader.GetValue <int>("A")); Assert.Equal("A long text", csvTableReader.GetValue <string>("B")); Assert.False(csvTableReader.GetValue <bool>("C")); Assert.Equal(new DateTime(2050, 1, 20), csvTableReader.GetValue <DateTime>("D")); csvTableReader.Read(csvLine4); Assert.True(csvTableReader.TryGetValue <int>("A", out var a)); Assert.Equal(55, a); Assert.True(csvTableReader.TryGetValue <string>("B", out var b)); Assert.Equal("A text with ;", b); Assert.True(csvTableReader.TryGetValue <bool?>("C", out var c)); Assert.Null(c); Assert.True(csvTableReader.TryGetValue <DateTime>("D", out var d)); Assert.Equal(new DateTime(1971, 7, 31), d); }
public void SupportConvertingEmptyLine() { var textLine = ""; var csvParser = new CsvLineParser(); { Assert.NotNull(csvParser); var fieldsArray = csvParser.Read(textLine); Assert.Single(fieldsArray); var fieldsList = new List <string>(); csvParser.Read(textLine, fieldsList); Assert.Single(fieldsList); } }
public void SupportConvertingToArray() { var textLine = "aaa1;bbb1;ccc1"; var csvParser = new CsvLineParser(); { Assert.NotNull(csvParser); var fields = csvParser.Read(textLine); Assert.Equal(3, fields.Count()); Assert.Equal("aaa1", fields[0]); Assert.Equal("bbb1", fields[1]); Assert.Equal("ccc1", fields[2]); } }
public void SupportConvertingToCollection() { var textLine = "aaa1;bbb1;ccc1"; var csvParser = new CsvLineParser(); { Assert.NotNull(csvParser); var fields = new List <string>(); csvParser.Read(textLine, fields); Assert.Equal(3, fields.Count); Assert.Equal("aaa1", fields[0]); Assert.Equal("bbb1", fields[1]); Assert.Equal("ccc1", fields[2]); } }
public void TestTryGetValue() { var csvLine1 = "A;B;C"; var csvLine2 = "a1;b1;c1"; var csvLine3 = "a2;b2;c2"; var csvLineReader = new CsvLineParser(); var csvTableReader = new CsvLineTableReader(csvLineReader); Assert.NotNull(csvTableReader); csvTableReader.ReadHeaders(csvLine1); csvTableReader.Read(csvLine2); Assert.Equal(3, csvTableReader.Headers.Count); Assert.True(csvTableReader.TryGetValue(0, out string v1)); Assert.Equal("a1", v1); Assert.True(csvTableReader.TryGetValue(1, out string v2)); Assert.Equal("b1", v2); Assert.True(csvTableReader.TryGetValue(2, out string v3)); Assert.Equal("c1", v3); Assert.False(csvTableReader.TryGetValue(3, out string v4)); Assert.Null(v4); csvTableReader.Read(csvLine3); Assert.Equal(3, csvTableReader.Headers.Count); Assert.True(csvTableReader.TryGetValue("A", out string v5)); Assert.Equal("a2", v5); Assert.True(csvTableReader.TryGetValue("B", out string v6)); Assert.Equal("b2", v6); Assert.True(csvTableReader.TryGetValue("C", out string v7)); Assert.Equal("c2", v7); Assert.False(csvTableReader.TryGetValue("D", out string v8)); Assert.Null(v8); }
private void ParseCSV(string data, CsvLineParser parser, IfcClassification source) { TextReader csvReader = new StringReader(data); //header line string line = csvReader.ReadLine(); CsvLineParser lineParser = parser; lineParser.ParseHeader(line); //get first line of data line = csvReader.ReadLine(); while (line != null) { //parse line Line parsedLine = lineParser.ParseLine(line); //create IFC object IfcSystem system = GetOrCreateSystem(parsedLine.Code, parsedLine.Description); var classification = GetOrCreateClassificationReference(parsedLine.Code, parsedLine.Description, source); //set up hierarchy if (system == null) { continue; } IfcSystem parentSystem = GetOrCreateSystem(parsedLine.ParentCode); if (parentSystem != null) { parentSystem.AddObjectToGroup(system); } //read new line to be processed in the next step line = csvReader.ReadLine(); } }
private async Task <int> ExportStudentSubjects(CsvTableReader csvTableReader, EcfTableWriter ecfTableWriter, string[] ecfHeaders) { var ecfRecordCounter = 0; await csvTableReader.ReadHeadersAsync(); if (ecfHeaders != null && ecfHeaders.Length > 0) { await ecfTableWriter.WriteHeadersAsync(ecfHeaders); } else { await ecfTableWriter.WriteHeadersAsync( EcfHeaders.StudentId, EcfHeaders.SchoolClassId, EcfHeaders.SubjectId, EcfHeaders.TeacherId); } while (await csvTableReader.ReadAsync() > 0) { var student = new CsvExportStudent(csvTableReader); var schoolClass = new CsvExportSchoolClass(csvTableReader); if (csvTableReader.TryGetValue("Alle Lehrkräfte (Kürzel) mit Fach", out var value)) { var csvLineParser = new CsvLineParser(','); var subValues = csvLineParser.Read(value); csvLineParser.Configuration.Separator = ' '; foreach (var subValue in subValues) { if (!string.IsNullOrEmpty(subValue)) { var subValueParts = csvLineParser.Read(subValue.Trim()); if (subValueParts.Length == 2) { var teacherCode = subValueParts[0]; var subjectCode = subValueParts[1]; if (!string.IsNullOrEmpty(subjectCode)) { ecfTableWriter.TrySetValue(EcfHeaders.StudentId, student.Id); ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, schoolClass.Id); ecfTableWriter.TrySetValue(EcfHeaders.SubjectId, subjectCode); ecfTableWriter.TrySetValue(EcfHeaders.TeacherId, teacherCode); await ecfTableWriter.WriteAsync(); ecfRecordCounter++; } } } } } } return(ecfRecordCounter); }
public void CreateSystem(XbimModel model, string classificationName) { //set model in which the systems are to be created _model = model; string data = null; //try to get data from resources //get list of classifications available var dir = Directory.EnumerateFiles("Classifications"); string clasPath = null; foreach (var f in dir) { if (Path.GetFileNameWithoutExtension(f).ToLower() == classificationName.ToLower()) { clasPath = f; break; } } if (clasPath == null) { throw new ArgumentException("Specified classification doesn't exist"); } var clasName = Path.GetFileNameWithoutExtension(clasPath); //get data data = File.ReadAllText(clasPath); CsvLineParser parser = null; if (clasName.Contains("NRM")) { parser = new CsvLineParser(';'); } else { parser = new CsvLineParser(); } //create classification source var action = new Action <XbimModel>(m => { var source = model.Instances.New <IfcClassification>(c => { c.Source = clasName; c.Edition = "Default edition"; c.Name = clasName; }); ParseCSV(data, parser, source); }); //process file if (_model.IsTransacting) { action(_model); } else { using (var txn = model.BeginTransaction("Systems creation")) { action(_model); txn.Commit(); } } }
private void ParseCSV(string data, CsvLineParser parser, IfcClassification source) { TextReader csvReader = new StringReader(data); //header line string line = csvReader.ReadLine(); CsvLineParser lineParser = parser; lineParser.ParseHeader(line); //get first line of data line = csvReader.ReadLine(); while (line != null) { //parse line Line parsedLine = lineParser.ParseLine(line); //create IFC object IfcSystem system = GetOrCreateSystem(parsedLine.Code, parsedLine.Description); var classification = GetOrCreateClassificationReference(parsedLine.Code, parsedLine.Description, source); //set up hierarchy if (system == null) continue; IfcSystem parentSystem = GetOrCreateSystem(parsedLine.ParentCode); if (parentSystem != null) parentSystem.AddObjectToGroup(system); //read new line to be processed in the next step line = csvReader.ReadLine(); } }
public void Ensures_GivenNullValueToParse_ExepectArgumentNullException() { var lineParser = new CsvLineParser(); Assert.Throws <ArgumentNullException>(() => lineParser.ParseLine(null)); }