using System.Text; using LehrerApp.Core.Importing; using LehrerApp.Core.Models; using Xunit; namespace LehrerApp.Tests; public sealed class StudentMasterDataCsvImportHandlerTests { private readonly StudentMasterDataCsvImportHandler _handler = new(); [Fact] public async Task Detect_ErkenntDieCharakteristischenSpaltenUnabhaengigVomDateinamen() { var detection = await _handler.DetectAsync(File(ValidFile(), "export.data")); Assert.True(detection.IsMatch); Assert.Equal(95, detection.Confidence); } [Fact] public async Task Detect_LehntEineBeliebigeCsvDateiAb() { var detection = await _handler.DetectAsync(File("Vorname;Nachname\nMax;Beispiel")); Assert.False(detection.IsMatch); } [Fact] public async Task Parse_UebernimmtDieUnterstuetztenStammdaten() { var result = await _handler.ParseAsync(File(ValidFile())); Assert.True(result.IsValid); Assert.Equal(2, result.Items.Count); var first = result.Items[0]; Assert.Equal("Max", first.FirstName); Assert.Equal("Beispiel", first.LastName); Assert.Equal(Gender.M, first.Gender); Assert.Equal(new DateOnly(2010, 1, 2), first.DateOfBirth); Assert.Equal("4711", first.ExternalId); Assert.Equal("10c", first.GroupName); Assert.Equal("Zeile 2", first.SourceReference); Assert.Equal("max@example.invalid", first.Contact?.Email); Assert.Equal("01234", first.Contact?.Phone); Assert.Equal("Musterweg 1", first.Contact?.Street); Assert.Equal(Gender.W, result.Items[1].Gender); Assert.Empty(result.Messages); } [Fact] public async Task Parse_UngueltigesGeburtsdatumErzeugtEinenFehlerMitZeilenangabe() { var text = ValidFile().Replace("02.01.2010", "2010-01-02"); var result = await _handler.ParseAsync(File(text)); Assert.False(result.IsValid); var error = Assert.Single(result.Messages, message => message.Code == "master-data.birth-date.invalid"); Assert.Equal("Zeile 2", error.SourceReference); } [Fact] public async Task Parse_BehandeltTabulatorenUndAnfuehrungszeichenInTextfeldern() { var text = ValidFile().Replace("Musterweg 1", "\"Musterweg\t\"\"Nord\"\" 1\""); var result = await _handler.ParseAsync(File(text)); Assert.True(result.IsValid); Assert.Equal(2, result.Items.Count); } private static ImportFile File(string text, string name = "students.csv") => new(name, Encoding.UTF8.GetBytes(text)); private static string ValidFile() { var header = string.Join('\t', [ "name", "longName", "foreName", "gender", "birthDate", "klasse.name", "entryDate", "exitDate", "text", "id", "externKey", "medicalReportDuty", "schulpflicht", "majority", "address.email", "address.mobile", "address.phone", "address.city", "address.postCode", "address.street", "attribute.iL", ]); var first = string.Join('\t', [ "max.b", "Beispiel", "Max", "m", "02.01.2010", "10c", "01.08.2021", "", "", "100", "4711", "false", "false", "false", "max@example.invalid", "", "01234", "Beispielstadt", "12345", "Musterweg 1", "max.b", ]); var second = string.Join('\t', [ "erika.m", "Mustermann", "Erika", "w", "03.04.2010", "10c", "01.08.2021", "", "", "101", "4712", "false", "false", "false", "", "", "", "", "", "", "erika.m", ]); return string.Join('\n', header, first, second) + "\n"; } }