public void ExampleC1() { var file = GetTestFilePath.FromDirectory(CsvFiles) .AndFileName("ExampleC.csv"); var csv = Read.Csv.FromFile(file) .With.ColumnsDelimiter("\t") .ThatReturns.ArrayOf <(int Id, string PhoneNumber, string CustomEnum, string Address)>() .Put.Column(0).As <int>().Into(a => a.Id) .Put.Column(1).MakingSureThat(PhoneNumberIsValid).Into(a => a.PhoneNumber) .Put.Column(2).MakingSureThat(EnumIsValid).InThisWay(a => a.Replace("|", " and ")).Into(a => a.CustomEnum) .Put.Column(3).Into(a => a.Address) .GetAll(); Debug.WriteLine("CSV DATA"); csv.ResultSet.ForEach(a => Debug.WriteLine($"id = {a.Id}, Phone = {a.PhoneNumber}, Enum = {a.CustomEnum}, Address = {a.Address}")); Debug.WriteLine("ERRORS"); csv.Errors.ForEach(e => Debug.WriteLine($"Error at line {e.LineNumber} column index {e.ColumnZeroBasedIndex} : {e.ErrorMessage}")); Data PhoneNumberIsValid(string phone) => Regex.IsMatch(phone, "[0-9]{10}") ? Data.Valid : Data.Invalid("Phone number is invalid"); Data EnumIsValid(string @enum) => @enum.Split('|').ToList().TrueForAll(a => a == "A" || a == "B" || a == "F") ? Data.Valid : Data.Invalid("Invalid enum character"); }
public void LoadAnnuaireDebitTabacWithTuple() { var sourceFile = GetTestFilePath .FromDirectory("CsvFiles") .AndFileName("annuaire-des-debits-de-tabac.csv"); var result = Read.Csv.FromFile(sourceFile) .ThatReturns.ArrayOf <(int Id, string Enseigne, string Cp)>() .Put.Column("ID").As <int>().Into(a => a.Id) .Put.Column("ENSEIGNE").MakingSureThat(IsTabac).Into(a => a.Enseigne) .Put.Column("CODE POSTAL").Into(a => a.Cp) .GetAll(); result.Errors.Should().HaveCount(14510); result.ResultSet.Should().HaveCount(11263); var testLine = result.ResultSet.First(a => a.Id == 565); testLine.Id.Should().Be(565); testLine.Enseigne.Should().Be("Tabac"); testLine.Cp.Should().Be("03800"); Data IsTabac(string arg) => arg == "Tabac" ? Data.Valid : Data.Invalid("is not a tabac"); }
public void LoadAnnuaireDebitTabacWithError() { var sourceFile = GetTestFilePath .FromDirectory("CsvFiles") .AndFileName("annuaire-des-debits-de-tabac.csv"); var result = Read.Csv.FromFile(sourceFile) .ThatReturns.ArrayOf <AnnuaireDebitTabacResult>() .Put.Column("ID").As <int>().Into(a => a.Id) .Put.Column("ENSEIGNE").InThisWay(CheckIfTabac).Into(a => a.Enseigne) .Put.Column("NUMERO ET LIBELLE DE VOIE").Into(a => a.NumeroEtLibelle) .Put.Column("COMPLEMENT").Into(a => a.Complement) .Put.Column("CODE POSTAL").Into(a => a.CodePostal) .Put.Column("COMMUNE").Into(a => a.Commune) .GetAll(); result.Errors.Should().HaveCount(14510); string CheckIfTabac(string enseigne) { if (enseigne != "Tabac") { throw new Exception("Not a tabac"); } return(enseigne); } }
public void Sample1() { var file = GetTestFilePath.FromDirectory(CsvFiles) .AndFileName("Sample1.csv"); var csv = Read.Csv.FromFile(file) .ThatReturns.ArrayOf <(string Name, int Age)>() .Put.Column("name").Into(a => a.Name) .Put.Column("age").As <int>().Into(a => a.Age) .GetAll(); Debug.WriteLine("CSV DATA"); csv.ResultSet.ForEach(r => Debug.WriteLine($"Name : {r.Name} - Age : {r.Age}")); Debug.WriteLine("ERRORS"); csv.Errors.ForEach(e => Debug.WriteLine($"Error at line {e.LineNumber} column index {e.ColumnZeroBasedIndex} : {e.ErrorMessage}")); }
public void LoadAnnuaireEcolesDoctorales() { var sourceFile = GetTestFilePath .FromDirectory("CsvFiles") .AndFileName("fr-esr-ecoles_doctorales_annuaire.csv"); var result = Read.Csv.EncodedIn(Encoding.UTF8).FromFile(sourceFile) .ThatReturns.ArrayOf <AnnuaireEcolesDoctoralesResult>() .Put.Column("adresse_postale").Into(a => a.AddressePostale) .Put.Column("code_etablissement_support").Into(a => a.CodeEtablissementSupport) .GetAll(); result.Errors.Should().BeEmpty(); result.ResultSet.Should().HaveCount(267); var firstRow = result.ResultSet.First(); firstRow.AddressePostale.Should().Be("Aix- Marseille Université\r\nFaculté des Sciences de Luminy\r\nCase 901\r\n163, avenue de Luminy"); }
public void ExampleA() { var file = GetTestFilePath.FromDirectory(CsvFiles) .AndFileName("ExampleA.csv"); var csv = Read.Csv.FromFile(file) .With.CultureInfo("en-US") .ThatReturns.ArrayOf <CsvInfos>() .Put.Column("FirstName").Into(p => p.Contact.Firstname) .Put.Column("LastName").Into(p => p.Contact.Lastname) .Put.Column("BirthDate").As <DateTime>().Into(p => p.Contact.BirthDate) .Put.Column("Street").Into(p => p.Address.Street) .Put.Column("City").Into(p => p.Address.City) .Put.Column("ZipCode").Into(p => p.Address.ZipCode) .GetAll(); Console.WriteLine("CSV DATA"); csv.ResultSet.ForEach(Console.WriteLine); }
public void ExampleC() { var file = GetTestFilePath.FromDirectory(CsvFiles) .AndFileName("ExampleC.csv"); var csv = Read.Csv.FromFile(file) .With.ColumnsDelimiter("\t") .ThatReturns.ArrayOf <LineExampleC>() .Put.Column(0).As <int>().Into(a => a.Id) .Put.Column(1).As <PhoneNumber>().InThisWay(s => new PhoneNumber(s)).Into(a => a.PhoneNumber) .Put.Column(2).As <CustomEnum>().InThisWay(s => new CustomEnum(s)).Into(a => a.CustomEnum) .Put.Column(3).Into(a => a.Address) .GetAll(); Console.WriteLine("CSV DATA"); csv.ResultSet.ForEach(Console.WriteLine); Console.WriteLine("ERRORS"); csv.Errors.ForEach(e => Console.WriteLine($"Error at line {e.LineNumber} column index {e.ColumnZeroBasedIndex} : {e.ErrorMessage}")); }
public void BeFastOnLargeFile() { var sourceFile = GetTestFilePath .FromDirectory("CsvFiles") .AndFileName("annuaire-des-debits-de-tabac.csv"); FluentActions.Invoking(() => { var result = Read.Csv.FromFile(sourceFile) .ThatReturns.ArrayOf <AnnuaireDebitTabacResult>() .Put.Column("ID").As <int>().Into(a => a.Id) .Put.Column("ENSEIGNE").MakingSureThat(IsTabac).Into(a => a.Enseigne) .Put.Column("NUMERO ET LIBELLE DE VOIE").Into(a => a.NumeroEtLibelle) .Put.Column("COMPLEMENT").Into(a => a.Complement) .Put.Column("CODE POSTAL").Into(a => a.CodePostal) .Put.Column("COMMUNE").Into(a => a.Commune) .GetAll(); Data IsTabac(string source) => source == "Tabac" ? Data.Valid : Data.Invalid("N'est pas un tabac"); result.Errors.Count().Should().Be(14510); }).ExecutionTime().Should().BeLessOrEqualTo(TimeSpan.FromSeconds(3)); }
public void LoadAnnuaireDebitTabac() { var sourceFile = GetTestFilePath .FromDirectory("CsvFiles") .AndFileName("annuaire-des-debits-de-tabac.csv"); var result = Read.Csv.FromFile(sourceFile) .ThatReturns.ArrayOf <AnnuaireDebitTabacResult>() .Put.Column("ID").As <int>().Into(a => a.Id) .Put.Column("ENSEIGNE").Into(a => a.Enseigne) .Put.Column("NUMERO ET LIBELLE DE VOIE").Into(a => a.NumeroEtLibelle) .Put.Column("COMPLEMENT").Into(a => a.Complement) .Put.Column("CODE POSTAL").Into(a => a.CodePostal) .Put.Column("COMMUNE").Into(a => a.Commune) .GetAll(); result.ResultSet.Should().HaveCount(25773); var testLine = result.ResultSet.First(a => a.Id == 566); testLine.Id.Should().Be(566); testLine.Enseigne.Should().Be("\"chez Clement\""); testLine.NumeroEtLibelle.Should().Be("29 cours de la republique"); result.Errors.Should().BeEmpty(); }