public async Task SmokeTest() { var csvData = "A;B;C" + Environment.NewLine + "a1;b1;c1" + Environment.NewLine + "a2;b2;c2"; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); Assert.NotNull(csvTableReader); await csvTableReader.ReadHeadersAsync(); Assert.Equal(3, csvTableReader.Headers.Count); Assert.Equal("A", csvTableReader.Headers[0]); Assert.Equal("B", csvTableReader.Headers[1]); Assert.Equal("C", csvTableReader.Headers[2]); await csvTableReader.ReadAsync(); Assert.Equal(3, csvTableReader.Headers.Count); Assert.Equal("a1", csvTableReader[0]); Assert.Equal("b1", csvTableReader[1]); Assert.Equal("c1", csvTableReader[2]); await csvTableReader.ReadAsync(); Assert.Equal(3, csvTableReader.Headers.Count); Assert.Equal("a2", csvTableReader["A"]); Assert.Equal("b2", csvTableReader["B"]); Assert.Equal("c2", csvTableReader["C"]); }
public async Task TestUseValue() { var csvData = "A;B;C" + Environment.NewLine + "a1;b1;c1" + Environment.NewLine + "a2;b2;c2"; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); Assert.NotNull(csvTableReader); await csvTableReader.ReadHeadersAsync(); await csvTableReader.ReadAsync(); csvTableReader.UseValue(0, s => Assert.Equal("a1", s)); csvTableReader.UseValue(1, s => Assert.Equal("b1", s)); csvTableReader.UseValue(2, s => Assert.Equal("c1", s)); await csvTableReader.ReadAsync(); csvTableReader.UseValue("A", s => Assert.Equal("a2", s)); csvTableReader.UseValue("B", s => Assert.Equal("b2", s)); csvTableReader.UseValue("C", s => Assert.Equal("c2", s)); }
public async Task TestTypedValues() { var csvData = "A;B;C;D;E" + Environment.NewLine + "22;Text;true;01.01.2010;A" + Environment.NewLine + "-31;A long text;false;20.01.2050;B" + Environment.NewLine + "55;\"A text with ;\";;31.07.1971;C"; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); Assert.NotNull(csvTableReader); csvTableReader.SetFormats <DateTime>("dd.MM.yyyy"); await csvTableReader.ReadHeadersAsync(); Assert.Equal(5, 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]); Assert.Equal("E", csvTableReader.Headers[4]); await csvTableReader.ReadAsync(); 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")); Assert.Equal(SampleEnum.A, csvTableReader.GetValue <SampleEnum>("E")); await csvTableReader.ReadAsync(); 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")); Assert.Equal(SampleEnum.B, csvTableReader.GetValue <SampleEnum?>("E")); await csvTableReader.ReadAsync(); 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 async Task TestEntityReaderWithAlternativeHeaders() { var csvData = "A;B1;C2;D" + Environment.NewLine + "22;Text;true;01.01.2010"; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); csvTableReader.SetFormats <DateTime>("dd.MM.yyyy"); csvTableReader.SetTrueFalseString <bool>("true", "false"); Assert.NotNull(csvTableReader); await csvTableReader.ReadHeadersAsync(); Assert.Equal(4, csvTableReader.Headers.Count); Assert.Equal("A", csvTableReader.Headers[0]); Assert.Equal("B1", csvTableReader.Headers[1]); Assert.Equal("C2", csvTableReader.Headers[2]); Assert.Equal("D", csvTableReader.Headers[3]); await csvTableReader.ReadAsync(); Assert.Equal(new SampleObject() { A = 22, B = "Text", C = true, D = new DateTime(2010, 1, 1) }, csvTableReader.Get <SampleObject>()); }
private async Task <int> ExportStudentSchoolClassAttendances(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); } while (await csvTableReader.ReadAsync() > 0) { var student = new CsvExportStudent(csvTableReader); var schoolClass = new CsvExportSchoolClass(csvTableReader); if (!string.IsNullOrEmpty(schoolClass.Id)) { ecfTableWriter.TrySetValue(EcfHeaders.StudentId, student.Id); ecfTableWriter.TrySetValue(EcfHeaders.SchoolClassId, schoolClass.Id); await ecfTableWriter.WriteAsync(); ecfRecordCounter++; } } return(ecfRecordCounter); }
public async Task TestEntityReader() { var csvData = "A;B;C;D;G" + Environment.NewLine + "22;Text;true;01.01.2010;A" + Environment.NewLine + "-31;A long text;false;20.01.2050;B" + Environment.NewLine + "55;\"A text with ;\";;31.07.1971;C"; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); csvTableReader.SetFormats <DateTime>("dd.MM.yyyy"); csvTableReader.SetTrueFalseString <bool>("true", "false"); Assert.NotNull(csvTableReader); await csvTableReader.ReadHeadersAsync(); Assert.Equal(5, 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]); Assert.Equal("G", csvTableReader.Headers[4]); await csvTableReader.ReadAsync(); Assert.Equal(new SampleObject() { A = 22, B = "Text", C = true, D = new DateTime(2010, 1, 1), G = SampleEnum.A }, csvTableReader.Get <SampleObject>()); await csvTableReader.ReadAsync(); Assert.Equal(new SampleObject() { A = -31, B = "A long text", C = false, D = new DateTime(2050, 1, 20), G = SampleEnum.B }, csvTableReader.Get <SampleObject>()); await csvTableReader.ReadAsync(); Assert.Equal(new SampleObject() { A = 55, B = "A text with ;", C = null, D = new DateTime(1971, 7, 31), G = SampleEnum.C }, csvTableReader.Get <SampleObject>()); }
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 async Task TestTryGetValue() { var csvData = "A;B;C" + Environment.NewLine + "a1;b1;c1" + Environment.NewLine + "a2;b2;c2"; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); Assert.NotNull(csvTableReader); await csvTableReader.ReadHeadersAsync(); await csvTableReader.ReadAsync(); 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); await csvTableReader.ReadAsync(); 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); }
public async Task TestJsonValues() { var csvData = "A;B" + Environment.NewLine + "42;\"{\"\"IntValue\"\":42,\"\"StrValue\"\":\"\"Forty-Two\"\"}\"" + Environment.NewLine + "5;\"{\"\"IntValue\"\":5,\"\"StrValue\"\":\"\"Five\"\"}\""; using var csvReader = new CsvReader(csvData); var csvTableReader = new CsvTableReader(csvReader); Assert.NotNull(csvTableReader); csvTableReader.AddConverter <CustomType>(new CustomTypeConverter()); await csvTableReader.ReadHeadersAsync(); Assert.Equal(2, csvTableReader.Headers.Count); Assert.Equal("A", csvTableReader.Headers[0]); Assert.Equal("B", csvTableReader.Headers[1]); await csvTableReader.ReadAsync(); Assert.Equal(42, csvTableReader.GetValue <int>("A")); var o1 = csvTableReader.GetValue <CustomType>("B"); Assert.Equal(42, o1.IntValue); Assert.Equal("Forty-Two", o1.StrValue); await csvTableReader.ReadAsync(); Assert.Equal(5, csvTableReader.GetValue <int>("A")); var o2 = csvTableReader.GetValue <CustomType>("B"); Assert.Equal(5, o2.IntValue); Assert.Equal("Five", o2.StrValue); }
private async Task <int> ExportStudents(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.LastName, EcfHeaders.FirstName, EcfHeaders.MiddleName, EcfHeaders.NickName, EcfHeaders.Salutation, EcfHeaders.Gender, EcfHeaders.Birthdate); } while (await csvTableReader.ReadAsync() > 0) { var student = new ExportStudent(_config, csvTableReader); if (!ecfCache.Contains(student.Id)) { ecfTableWriter.SetValue(EcfHeaders.Id, student.Id); ecfTableWriter.SetValue(EcfHeaders.LastName, student.LastName); ecfTableWriter.SetValue(EcfHeaders.FirstName, student.FirstName); ecfTableWriter.SetValue(EcfHeaders.MiddleName, student.MiddleName); ecfTableWriter.SetValue(EcfHeaders.NickName, student.NickName); ecfTableWriter.SetValue(EcfHeaders.Salutation, student.Salutation); ecfTableWriter.SetValue(EcfHeaders.Gender, student.Gender); ecfTableWriter.SetValue(EcfHeaders.Birthdate, student.BirthDate); await ecfTableWriter.WriteAsync(); ecfCache.Add(student.Id); ecfRecordCounter++; } } return(ecfRecordCounter); }
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); } while (await csvTableReader.ReadAsync() > 0) { var student = new ExportStudent(_config, csvTableReader); var schoolClass = new ExportSchoolClass(_config, csvTableReader); if (!string.IsNullOrEmpty(schoolClass.Id)) { for (int i = 1; i < 20; i++) { var subject = new ExportSubject(_config, csvTableReader, $"Fach{i}"); if (!string.IsNullOrEmpty(subject.Id)) { ecfTableWriter.SetValue(EcfHeaders.StudentId, student.Id); ecfTableWriter.SetValue(EcfHeaders.SchoolClassId, schoolClass.Id); ecfTableWriter.SetValue(EcfHeaders.SubjectId, subject.Id); await ecfTableWriter.WriteAsync(); ecfRecordCounter++; } } } } return(ecfRecordCounter); }
private async Task <int> ExportSubjects(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) { for (int i = 1; i < 20; i++) { var subject = new ExportSubject(_config, csvTableReader, $"Fach{i}"); if (!string.IsNullOrEmpty(subject.Id) && !ecfCache.Contains(subject.Id)) { ecfTableWriter.SetValue(EcfHeaders.Id, subject.Id); ecfTableWriter.SetValue(EcfHeaders.Code, subject.Code); await ecfTableWriter.WriteAsync(); ecfCache.Add(subject.Id); ecfRecordCounter++; } } } return(ecfRecordCounter); }
private async Task <int> ExportSchoolClasses(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) { var schoolClass = new CsvExportSchoolClass(csvTableReader); if (!string.IsNullOrEmpty(schoolClass.Id) && !ecfCache.Contains(schoolClass.Id)) { ecfTableWriter.TrySetValue(EcfHeaders.Id, schoolClass.Id); ecfTableWriter.TrySetValue(EcfHeaders.Code, schoolClass.Code); await ecfTableWriter.WriteAsync(); ecfCache.Add(schoolClass.Id); ecfRecordCounter++; } } return(ecfRecordCounter); }
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); }