public void TestTypeMapper_BadRecordColumn_SkipError() { const string data = @"1,2017-06-11,John Smith 2,2017-12-32,Tom Stallon 3,2017-08-13,Walter Kay"; var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(x => x.Id); mapper.Property(x => x.Created); mapper.Property(x => x.Name); StringReader stringReader = new StringReader(data); List <int> errorRecords = new List <int>(); var options = new SeparatedValueOptions() { ErrorHandler = (sender, e) => { errorRecords.Add(e.RecordNumber); e.IsHandled = true; } }; var people = mapper.Read(stringReader, options).ToArray(); Assert.Equal(2, people.Count()); Assert.Equal(1, errorRecords.Count); Assert.Equal(2, errorRecords[0]); }
private static ISeparatedValueTypeMapper <RealtyProperty> GetNestedTypeMapper() { var mapper = SeparatedValueTypeMapper.Define(() => new RealtyProperty() { Address = new Address(), Coordinates = new Geolocation() }); mapper.CustomMapping(new Int32Column("Id")).WithReader(x => x.Id).WithWriter(x => x.Id); mapper.CustomMapping(new DecimalColumn("Longitude")) .WithReader((x, v) => x.Coordinates.Longitude = (decimal)v) .WithWriter(x => x.Coordinates.Longitude); mapper.CustomMapping(new DecimalColumn("Latitude")) .WithReader(x => x.Coordinates.Latitude) .WithWriter(x => x.Coordinates.Latitude); mapper.CustomMapping(new StringColumn("Street1")) .WithReader((x, v) => x.Address.Street = (string)v) .WithWriter(x => x.Address.Street); mapper.CustomMapping(new StringColumn("City")) .WithReader((x, v) => x.Address.City = (string)v) .WithWriter(x => x.Address.City); mapper.CustomMapping(new StringColumn("State")) .WithReader((x, v) => x.Address.State = (string)v) .WithWriter(x => x.Address.State); mapper.CustomMapping(new StringColumn("Zip")) .WithReader((x, v) => x.Address.Zip = (string)v) .WithWriter(x => x.Address.Zip); return(mapper); }
public IList <CommunityEmailRecipient> GetCommunityEmailRecipients(string filename) { try { var mapper = SeparatedValueTypeMapper.Define <CommunityEmailRecipient>(); mapper.Property(c => c.Community).ColumnName("community"); mapper.Property(c => c.To).ColumnName("to"); mapper.Property(c => c.Cc).ColumnName("cc"); mapper.Property(c => c.Bcc).ColumnName("bcc"); if (File.Exists(filename)) { using (var reader = new StreamReader(File.OpenRead(filename))) { var options = new SeparatedValueOptions { IsFirstRecordSchema = true }; List <CommunityEmailRecipient> recipients = mapper.Read(reader, options).ToList(); _logger.LogDebug("Found {Count} community email recipient from {Filename}", recipients.Count, filename); return(recipients); } } _logger.LogWarning("Mapping {Filename} not found, returning empty result", filename); return(Array.Empty <CommunityEmailRecipient>()); } catch (Exception e) { _logger.LogError(e, "Failed to read file {Filename}, returning empty result", filename); return(Array.Empty <CommunityEmailRecipient>()); } }
public void TestTypeMapper_BadRecordColumn_SkipError() { const string data = @"1,2017-06-11,John Smith 2,2017-12-32,Tom Stallon 3,2017-08-13,Walter Kay"; var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(x => x.Id); mapper.Property(x => x.Created); mapper.Property(x => x.Name); StringReader stringReader = new StringReader(data); List <int> errorRecords = new List <int>(); var reader = mapper.GetReader(stringReader); reader.RecordError += (sender, e) => { errorRecords.Add(e.RecordContext.PhysicalRecordNumber); e.IsHandled = true; }; var people = reader.ReadAll().ToArray(); Assert.AreEqual(2, people.Length); Assert.AreEqual(1, errorRecords.Count); Assert.AreEqual(2, errorRecords[0]); }
public void RunFieldTest() { var mapper = SeparatedValueTypeMapper.Define <FieldPerson>(() => new FieldPerson()); mapper.Property(x => x.FirstName); mapper.Property(x => x.LastName); mapper.Property(x => x.Age); mapper.Property(x => x.Street1); mapper.Property(x => x.Street2); mapper.Property(x => x.City); mapper.Property(x => x.State); mapper.Property(x => x.Zip); mapper.Property(x => x.FavoriteColor); mapper.Property(x => x.FavoriteFood); mapper.Property(x => x.FavoriteSport); mapper.Property(x => x.CreatedOn); mapper.Property(x => x.IsActive); StringWriter writer = new StringWriter(); mapper.Write(writer, fieldPeople); string serialized = writer.ToString(); StringReader reader = new StringReader(serialized); var deserialized = mapper.Read(reader).ToArray(); }
public async Task RunFlatFiles() { var mapper = SeparatedValueTypeMapper.Define <Person>(() => new Person()); mapper.Property(x => x.FirstName); mapper.Property(x => x.LastName); mapper.Property(x => x.Age); mapper.Property(x => x.Street1); mapper.Property(x => x.Street2); mapper.Property(x => x.City); mapper.Property(x => x.State); mapper.Property(x => x.Zip); mapper.Property(x => x.FavoriteColor); mapper.Property(x => x.FavoriteFood); mapper.Property(x => x.FavoriteSport); mapper.Property(x => x.CreatedOn); mapper.Property(x => x.IsActive); StringReader textReader = new StringReader(data); var people = new List <Person>(); var reader = mapper.GetReader(textReader, new SeparatedValueOptions() { IsFirstRecordSchema = true }); while (await reader.ReadAsync()) { people.Add(reader.Current); } }
public string SyncTest() { var mapper = SeparatedValueTypeMapper.Define <SampleData>(); mapper.Property(x => x.YearStart).ColumnName("YearStart"); mapper.Property(x => x.YearEnd).ColumnName("YearEnd"); mapper.Property(x => x.LocationAbbreviation).ColumnName("LocationAbbr"); mapper.Property(x => x.LocationDescription).ColumnName("LocationDesc"); mapper.Property(x => x.DataSource).ColumnName("DataSource"); mapper.Property(x => x.Topic).ColumnName("Topic"); mapper.Property(x => x.Question).ColumnName("Question"); mapper.Property(x => x.Response).ColumnName("Response"); mapper.Property(x => x.DataValueUnit).ColumnName("DataValueUnit"); mapper.Property(x => x.DataValueType).ColumnName("DataValueType"); mapper.Property(x => x.DataValue).ColumnName("DataValue"); mapper.Property(x => x.AlternativeDataValue).ColumnName("DataValueAlt"); mapper.Property(x => x.DataValueFootnoteSymbol).ColumnName("DataValueFootnoteSymbol"); mapper.Property(x => x.DataValueFootnote).ColumnName("DatavalueFootnote"); mapper.Property(x => x.LowConfidenceLimit).ColumnName("LowConfidenceLimit"); mapper.Property(x => x.HighConfidenceLimit).ColumnName("HighConfidenceLimit"); mapper.Property(x => x.StratificationCategory1).ColumnName("StratificationCategory1"); mapper.Property(x => x.Stratification1).ColumnName("Stratification1"); mapper.Property(x => x.StratificationCategory2).ColumnName("StratificationCategory2"); mapper.Property(x => x.Stratification2).ColumnName("Stratification2"); mapper.Property(x => x.StratificationCategory3).ColumnName("StratificationCategory3"); mapper.Property(x => x.Stratification3).ColumnName("Stratification3"); mapper.CustomProperty(x => x.GeoLocation, new GeoLocationColumn("GeoLocation")); mapper.Property(x => x.ResponseId).ColumnName("ResponseID"); mapper.Property(x => x.LocationId).ColumnName("LocationID"); mapper.Property(x => x.TopicId).ColumnName("TopicID"); mapper.Property(x => x.QuestionId).ColumnName("QuestionID"); mapper.Property(x => x.DataValueTypeId).ColumnName("DataValueTypeID"); mapper.Property(x => x.StratificationCategoryId1).ColumnName("StratificationCategoryID1"); mapper.Property(x => x.StratificationId1).ColumnName("StratificationID1"); mapper.Property(x => x.StratificationCategoryId2).ColumnName("StratificationCategoryID2"); mapper.Property(x => x.StratificationId2).ColumnName("StratificationID2"); mapper.Property(x => x.StratificationCategoryId3).ColumnName("StratificationCategoryID3"); mapper.Property(x => x.StratificationId3).ColumnName("StratificationID3"); StringWriter textWriter = new StringWriter(); var http = WebRequest.CreateHttp("https://raw.githubusercontent.com/jehugaleahsa/FlatFiles/master/FlatFiles.Benchmark/TestFiles/SampleData.csv"); using (var response = http.GetResponse()) using (var textReader = new StreamReader(response.GetResponseStream())) { var reader = mapper.GetReader(textReader, new SeparatedValueOptions() { IsFirstRecordSchema = true }); var writer = mapper.GetWriter(textWriter, new SeparatedValueOptions() { IsFirstRecordSchema = true }); while (reader.Read()) { writer.Write(reader.Current); } } return(textWriter.ToString()); }
public void ShouldDeduceSchemaForType_ColumnNameCustomization() { var stringWriter = new StringWriter(); var nameResolver = AutoMapResolver.For(m => $"Prefix_{m.Name}_Postfix"); var writer = SeparatedValueTypeMapper.GetAutoMappedWriter <Person>(stringWriter, null, nameResolver); var expected = new[] { new Person() { Id = 1, Name = "Bob", CreatedOn = new DateTime(2018, 07, 01), IsActive = true, VisitCount = 1 }, new Person() { Id = 2, Name = "John", CreatedOn = new DateTime(2018, 07, 02), IsActive = false, VisitCount = null }, new Person() { Id = 3, Name = "Susan", CreatedOn = new DateTime(2018, 07, 03), IsActive = false, VisitCount = 10 } }; writer.WriteAll(expected); string output = stringWriter.ToString(); var stringReader = new StringReader(output); var reader = SeparatedValueTypeMapper.GetAutoMappedReader <Person>(stringReader, null, AutoMapMatcher.For(nameResolver)); var results = reader.ReadAll().ToArray(); Assert.AreEqual(3, results.Length, "The wrong number of records were read."); AssertEqual(expected, results, 0); AssertEqual(expected, results, 1); AssertEqual(expected, results, 2); }
public void ShouldTreatConstantAsNull_TypeMapper() { var nullHandler = ConstantNullHandler.For("----"); var mapper = SeparatedValueTypeMapper.Define <Product>(); mapper.Property(p => p.Name).ColumnName("name").NullHandler(nullHandler); mapper.Property(p => p.Cost).ColumnName("cost").NullHandler(nullHandler); mapper.Property(p => p.Available).ColumnName("available").NullHandler(nullHandler); mapper.Property(p => p.Vendor).ColumnName("vendor").NullHandler(nullHandler); string content = "----,5.12,----,apple" + Environment.NewLine; byte[] encoded = Encoding.Default.GetBytes(content); MemoryStream inputStream = new MemoryStream(encoded); var products = mapper.Read(inputStream); Assert.AreEqual(1, products.Count(), "The wrong number of products were found."); Product product = products.Single(); Assert.IsNull(product.Name, "The name was not interpreted as null."); Assert.AreEqual(5.12m, product.Cost, "The cost was not read correctly."); Assert.IsNull(product.Available, "The available was not interpreted as null."); Assert.AreEqual("apple", product.Vendor, "The vendor was not read correctly."); MemoryStream outputStream = new MemoryStream(); mapper.Write(outputStream, products); outputStream.Position = 0; string output = Encoding.Default.GetString(outputStream.ToArray()); Assert.AreEqual(content, output, "The null handler was not respected when writing null."); }
public void TestTypeMapper_IgnoredColumns_RoundTrips() { var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(p => p.Id).ColumnName("id"); mapper.Ignored(); mapper.Ignored(); mapper.Property(p => p.Name).ColumnName("name"); mapper.Ignored(); mapper.Property(p => p.Created).ColumnName("created").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); var bob = new Person() { Id = 123, Name = "Bob Smith", Created = new DateTime(2013, 1, 19) }; StringWriter stringWriter = new StringWriter(); mapper.Write(stringWriter, new Person[] { bob }); StringReader stringReader = new StringReader(stringWriter.ToString()); var people = mapper.Read(stringReader).ToArray(); Assert.Single(people); var person = people.SingleOrDefault(); Assert.Equal(bob.Id, person.Id); Assert.Equal(bob.Name, person.Name); Assert.Equal(bob.Created, person.Created); }
public void RunFlatFiles_TypeMapper_CustomMapping_Unoptimized() { var mapper = SeparatedValueTypeMapper.Define(() => new Person()); mapper.OptimizeMapping(false); mapper.CustomMapping(new StringColumn("FirstName")).WithReader(p => p.FirstName); mapper.CustomMapping(new StringColumn("LastName")).WithReader(p => p.LastName); mapper.CustomMapping(new Int32Column("Age")).WithReader(p => p.Age); mapper.CustomMapping(new StringColumn("Street1")).WithReader(p => p.Street1); mapper.CustomMapping(new StringColumn("Street2")).WithReader(p => p.Street2); mapper.CustomMapping(new StringColumn("City")).WithReader(p => p.City); mapper.CustomMapping(new StringColumn("State")).WithReader(p => p.State); mapper.CustomMapping(new StringColumn("Zip")).WithReader(p => p.Zip); mapper.CustomMapping(new StringColumn("FavoriteColor")).WithReader(p => p.FavoriteColor); mapper.CustomMapping(new StringColumn("FavoriteFood)")).WithReader(p => p.FavoriteFood); mapper.CustomMapping(new StringColumn("FavoriteSport")).WithReader(p => p.FavoriteSport); mapper.CustomMapping(new DateTimeColumn("CreatedOn")).WithReader(p => p.CreatedOn); mapper.CustomMapping(new BooleanColumn("IsActive")).WithReader(p => p.IsActive); StringReader reader = new StringReader(data); var people = mapper.Read(reader, new SeparatedValueOptions() { IsFirstRecordSchema = true }).ToArray(); }
public void RunFlatFiles_TypeMapper_Unoptimized() { var mapper = SeparatedValueTypeMapper.Define(() => new Person()); mapper.OptimizeMapping(false); mapper.Property(x => x.FirstName); mapper.Property(x => x.LastName); mapper.Property(x => x.Age); mapper.Property(x => x.Street1); mapper.Property(x => x.Street2); mapper.Property(x => x.City); mapper.Property(x => x.State); mapper.Property(x => x.Zip); mapper.Property(x => x.FavoriteColor); mapper.Property(x => x.FavoriteFood); mapper.Property(x => x.FavoriteSport); mapper.Property(x => x.CreatedOn); mapper.Property(x => x.IsActive); StringReader reader = new StringReader(data); var people = mapper.Read(reader, new SeparatedValueOptions() { IsFirstRecordSchema = true }).ToArray(); }
public void TestWriter_WithSchema_SchemaCounted() { var outputMapper = SeparatedValueTypeMapper.Define(() => new Person()); outputMapper.Property(x => x.Name); outputMapper.CustomMapping(new RecordNumberColumn("RecordNumber") { IncludeSchema = true }) .WithReader((p, v) => p.RecordNumber = (int)v) .WithWriter(p => p.RecordNumber); outputMapper.Property(x => x.CreatedOn).OutputFormat("MM/dd/yyyy"); var people = new[] { new Person() { Name = "Bob", CreatedOn = new DateTime(2018, 04, 25) }, new Person() { Name = "Tom", CreatedOn = new DateTime(2018, 04, 26) }, new Person() { Name = "Jane", CreatedOn = new DateTime(2018, 04, 27) } }; StringWriter writer = new StringWriter(); outputMapper.Write(writer, people, new SeparatedValueOptions() { IsFirstRecordSchema = true }); string output = writer.ToString(); var inputMapper = SeparatedValueTypeMapper.Define(() => new Person()); inputMapper.Property(x => x.Name); inputMapper.Property(x => x.RecordNumber); inputMapper.Property(x => x.CreatedOn).InputFormat("MM/dd/yyyy"); StringReader reader = new StringReader(output); var results = inputMapper.Read(reader, new SeparatedValueOptions() { IsFirstRecordSchema = true }).ToArray(); Assert.AreEqual(3, results.Length); Assert.AreEqual("Bob", results[0].Name); Assert.AreEqual(2, results[0].RecordNumber); Assert.AreEqual(new DateTime(2018, 04, 25), results[0].CreatedOn); Assert.AreEqual("Tom", results[1].Name); Assert.AreEqual(3, results[1].RecordNumber); Assert.AreEqual(new DateTime(2018, 04, 26), results[1].CreatedOn); Assert.AreEqual("Jane", results[2].Name); Assert.AreEqual(4, results[2].RecordNumber); Assert.AreEqual(new DateTime(2018, 04, 27), results[2].CreatedOn); }
public void TestIgnoredMapping_HandlePreAndPostProcessing() { ISeparatedValueTypeMapper <IgnoredOnly> mapper = SeparatedValueTypeMapper.Define(() => new IgnoredOnly()); mapper.Ignored() .ColumnName("Ignored") .NullFormatter(NullFormatter.ForValue("NULL")) .OnParsing((ctx, value) => { Assert.AreEqual("NULL", value); return(value); }) .OnParsed((ctx, value) => { Assert.IsNull(value); return(value); }) .OnFormatting((ctx, value) => { Assert.IsNull(value); return(value); }) .OnFormatted((ctx, value) => { Assert.AreEqual("NULL", value); return(value); }); var ignored = mapper.GetSchema().ColumnDefinitions["Ignored"]; object value = ignored.Parse(null, "NULL"); Assert.IsNull(value); string formatted = ignored.Format(null, value); Assert.AreEqual("NULL", formatted); }
private static ISeparatedValueTypeMapper <Person> GetTypeMapper() { var mapper = SeparatedValueTypeMapper.Define(() => new Person()); mapper.CustomMapping(new Int32Column("Id")).WithReader((ctx, person, value) => { person.Id = (int)value; }).WithWriter((ctx, person, values) => { values[ctx.LogicalIndex] = person.Id; }); mapper.CustomMapping(new StringColumn("Name")).WithReader((person, value) => { person.Name = (string)value; }).WithWriter(p => p.Name); mapper.CustomMapping(new DateTimeColumn("CreatedOn")).WithReader(p => p.CreatedOn).WithWriter(p => p.CreatedOn); mapper.CustomMapping(new DecimalColumn("Amount")).WithReader((ctx, person, value) => { person.Amount = value == null ? (decimal?)null : (decimal)value; }).WithWriter((ctx, person, values) => { values[ctx.LogicalIndex] = person.Amount; }); return(mapper); }
public void ShouldTreatConstantAsNull_TypeMapper() { var nullHandler = ConstantNullHandler.For("----"); var mapper = SeparatedValueTypeMapper.Define <Product>(); mapper.Property(p => p.Name).ColumnName("name").NullHandler(nullHandler); mapper.Property(p => p.Cost).ColumnName("cost").NullHandler(nullHandler).FormatProvider(CultureInfo.InvariantCulture); mapper.Property(p => p.Available).ColumnName("available").NullHandler(nullHandler); mapper.Property(p => p.Vendor).ColumnName("vendor").NullHandler(nullHandler); string content = "----,5.12,----,apple" + Environment.NewLine; StringReader stringReader = new StringReader(content); var products = mapper.Read(stringReader).ToArray(); Assert.Single(products); Product product = products.Single(); Assert.Null(product.Name); Assert.Equal(5.12m, product.Cost); Assert.Null(product.Available); Assert.Equal("apple", product.Vendor); StringWriter stringWriter = new StringWriter(); mapper.Write(stringWriter, products); string output = stringWriter.ToString(); Assert.Equal(content, output); }
public void RunSync() { var mapper = SeparatedValueTypeMapper.Define <Person>(() => new Person()); mapper.Property(x => x.FirstName); mapper.Property(x => x.LastName); mapper.Property(x => x.Age); mapper.Property(x => x.Street1); mapper.Property(x => x.Street2); mapper.Property(x => x.City); mapper.Property(x => x.State); mapper.Property(x => x.Zip); mapper.Property(x => x.FavoriteColor); mapper.Property(x => x.FavoriteFood); mapper.Property(x => x.FavoriteSport); mapper.Property(x => x.CreatedOn); mapper.Property(x => x.IsActive); StringReader reader = new StringReader(data); mapper.Read(reader, new SeparatedValueOptions() { IsFirstRecordSchema = true }).ToArray(); }
public void TestTypeMapper_Roundtrip() { var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(p => p.Id).ColumnName("id"); mapper.Property(p => p.Name).ColumnName("name"); mapper.Property(p => p.Created).ColumnName("created").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.Property(p => p.IsActive).ColumnName("active"); var bob = new Person() { Id = 123, Name = "Bob", Created = new DateTime(2013, 1, 19), IsActive = true }; StringWriter stringWriter = new StringWriter(); mapper.Write(stringWriter, new Person[] { bob }); StringReader stringReader = new StringReader(stringWriter.ToString()); var people = mapper.Read(stringReader).ToArray(); Assert.AreEqual(1, people.Length); var person = people.SingleOrDefault(); Assert.AreEqual(bob.Id, person.Id); Assert.AreEqual(bob.Name, person.Name); Assert.AreEqual(bob.Created, person.Created); }
public async Task TestTypeMapper_Roundtrip() { var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(p => p.Id).ColumnName("id"); mapper.Property(p => p.Name).ColumnName("name"); mapper.Property(p => p.Created).ColumnName("created").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.Property(p => p.IsActive).ColumnName("active"); var bob = new Person() { Id = 123, Name = "Bob", Created = new DateTime(2013, 1, 19), IsActive = true }; StringWriter stringWriter = new StringWriter(); await mapper.WriteAsync(stringWriter, new Person[] { bob }); StringReader stringReader = new StringReader(stringWriter.ToString()); var reader = mapper.GetReader(stringReader); var people = new List <Person>(); while (await reader.ReadAsync()) { people.Add(reader.Current); } Assert.Equal(1, people.Count()); var person = people.SingleOrDefault(); Assert.Equal(bob.Id, person.Id); Assert.Equal(bob.Name, person.Name); Assert.Equal(bob.Created, person.Created); Assert.True(person.IsActive); }
public void TestRuntimeTypeDefinition() { var mapper = SeparatedValueTypeMapper.DefineDynamic(typeof(Person)); mapper.StringProperty("Name").ColumnName("Name"); mapper.Int32Property("IQ").ColumnName("IQ"); mapper.DateTimeProperty("BirthDate").ColumnName("BirthDate"); mapper.DecimalProperty("TopSpeed").ColumnName("TopSpeed"); var people = new[] { new Person() { Name = "John", IQ = null, BirthDate = new DateTime(1954, 10, 29), TopSpeed = 3.4m }, new Person() { Name = "Susan", IQ = 132, BirthDate = new DateTime(1984, 3, 15), TopSpeed = 10.1m } }; StringWriter writer = new StringWriter(); mapper.Write(writer, people); string result = writer.ToString(); StringReader reader = new StringReader(result); var parsed = mapper.Read(reader).ToArray(); Assert.AreEqual(2, parsed.Length); Assert.IsInstanceOfType(parsed[0], typeof(Person)); Assert.IsInstanceOfType(parsed[1], typeof(Person)); assertEqual(people[0], (Person)parsed[0]); assertEqual(people[1], (Person)parsed[1]); }
public void TestTypeMapper_RoundtripWithNull() { var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(p => p.Id).ColumnName("id"); mapper.Property(p => p.Name).ColumnName("name"); mapper.Property(p => p.Created).ColumnName("created").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); using (MemoryStream stream = new MemoryStream()) { var bob = new Person() { Id = 123, Name = null, Created = new DateTime(2013, 1, 19) }; var options = new SeparatedValueOptions() { IsFirstRecordSchema = true, Separator = "\t" }; mapper.Write(stream, options, new Person[] { bob }); stream.Position = 0; // go back to the beginning of the stream var people = mapper.Read(stream, options); Assert.AreEqual(1, people.Count(), "The wrong number of people were returned."); var person = people.SingleOrDefault(); Assert.AreEqual(bob.Id, person.Id, "The ID value was not persisted."); Assert.AreEqual(bob.Name, person.Name, "The Name value was not persisted."); Assert.AreEqual(bob.Created, person.Created, "The Created value was not persisted."); } }
public void TestTypeMapper_RoundtripWithNull() { var mapper = SeparatedValueTypeMapper.Define <Person>(); mapper.Property(p => p.Id).ColumnName("id"); mapper.Property(p => p.Name).ColumnName("name"); mapper.Property(p => p.Created).ColumnName("created").InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); var bob = new Person() { Id = 123, Name = null, Created = new DateTime(2013, 1, 19) }; var options = new SeparatedValueOptions() { IsFirstRecordSchema = true, Separator = "\t" }; StringWriter stringWriter = new StringWriter(); mapper.Write(stringWriter, new Person[] { bob }, options); StringReader stringReader = new StringReader(stringWriter.ToString()); var people = mapper.Read(stringReader, options).ToArray(); Assert.Equal(1, people.Count()); var person = people.SingleOrDefault(); Assert.Equal(bob.Id, person.Id); Assert.Equal(bob.Name, person.Name); Assert.Equal(bob.Created, person.Created); }
public DirectVsDynamicTester() { var directMapper = SeparatedValueTypeMapper.Define <Person>(() => new Person()); directMapper.Property(x => x.Name).ColumnName("Name"); directMapper.Property(x => x.IQ).ColumnName("IQ"); directMapper.Property(x => x.BirthDate).ColumnName("BirthDate"); directMapper.Property(x => x.TopSpeed).ColumnName("TopSpeed"); directMapper.Property(x => x.IsActive).ColumnName("IsActive"); this.directMapper = directMapper; var dynamicMapper = SeparatedValueTypeMapper.DefineDynamic(typeof(Person)); dynamicMapper.StringProperty("Name").ColumnName("Name"); dynamicMapper.Int32Property("IQ").ColumnName("IQ"); dynamicMapper.DateTimeProperty("BirthDate").ColumnName("BirthDate"); dynamicMapper.DecimalProperty("TopSpeed").ColumnName("TopSpeed"); dynamicMapper.BooleanProperty("IsActive").ColumnName("IsActive"); this.dynamicMapper = dynamicMapper; this.people = Enumerable.Range(0, 10000).Select(i => new Person() { Name = "Susan", IQ = 132, BirthDate = new DateTime(1984, 3, 15), TopSpeed = 10.1m }).ToArray(); }
public async Task <IActionResult> GetLeadAgencyDeptListAsCsvAsync() { // By annotating the controller with ApiControllerAttribute, // the ModelStateInvalidFilter will automatically check ModelState.IsValid // see https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#automatic-http-400-responses IList <LookupValue> lookupValues = await _service .GetLookupAsync(LookupType.LeadAgencyDeptList); var values = lookupValues.Select(_ => new CommunityEmailRecipient { Community = _.Value, To = string.Empty, Cc = string.Empty, Bcc = string.Empty }); var mapper = SeparatedValueTypeMapper.Define <CommunityEmailRecipient>(); mapper.Property(c => c.Community).ColumnName("community"); mapper.Property(c => c.To).ColumnName("to"); mapper.Property(c => c.Cc).ColumnName("cc"); mapper.Property(c => c.Bcc).ColumnName("bcc"); StringBuilder buffer = new StringBuilder(); StringWriter writer = new StringWriter(buffer); await mapper.WriteAsync(writer, values, new SeparatedValueOptions { IsFirstRecordSchema = true }); return(new FileContentResult(Encoding.UTF8.GetBytes(buffer.ToString()), new MediaTypeHeaderValue("text/csv"))); }
public void MapPrivateClass() { var mapper = SeparatedValueTypeMapper.Define <PrivateClass>(); mapper.Property(x => x.Identifier); mapper.Property(x => x.Status); mapper.Property(x => x.EffectiveDate).InputFormat("yyyyMMdd"); mapper.Property(x => x.ModificationDate).InputFormat("yyyyMMddHHmmss"); string rawData = @"ABC123,Doing Fine,20180115,20180115145100"; StringReader reader = new StringReader(rawData); var data = mapper.Read(reader, new SeparatedValueOptions() { IsFirstRecordSchema = false, RecordSeparator = "\n", Separator = ",", Quote = '"' }).ToArray(); Assert.AreEqual(1, data.Length); var result = data[0]; Assert.AreEqual("ABC123", result.Identifier); Assert.AreEqual("Doing Fine", result.Status); Assert.AreEqual(new DateTime(2018, 1, 15), result.EffectiveDate); Assert.AreEqual(new DateTime(2018, 1, 15, 14, 51, 00), result.ModificationDate); }
public void MapInternalClass_Dynamic() { var mapper = SeparatedValueTypeMapper.DefineDynamic(typeof(InternalClass)); mapper.StringProperty("Identifier"); mapper.StringProperty("Status"); mapper.DateTimeProperty("EffectiveDate").InputFormat("yyyyMMdd"); mapper.DateTimeProperty("ModificationDate").InputFormat("yyyyMMddHHmmss"); mapper.BooleanProperty("IsInternal"); string rawData = @"ABC123,Doing Fine,20180115,20180115145100,true"; StringReader reader = new StringReader(rawData); var data = mapper.Read(reader, new SeparatedValueOptions() { IsFirstRecordSchema = false, RecordSeparator = "\n", Separator = ",", Quote = '"' }).ToArray(); Assert.AreEqual(1, data.Length); dynamic result = data[0]; Assert.AreEqual("ABC123", result.Identifier); Assert.AreEqual("Doing Fine", result.Status); Assert.AreEqual(new DateTime(2018, 1, 15), result.EffectiveDate); Assert.AreEqual(new DateTime(2018, 1, 15, 14, 51, 00), result.ModificationDate); Assert.AreEqual(true, result.IsInternal); }
private static ISeparatedValueTypeMapper <HeaderRecord> getHeaderTypeMapper() { var mapper = SeparatedValueTypeMapper.Define(() => new HeaderRecord()); mapper.Property(x => x.BatchName); mapper.Property(x => x.RecordCount); return(mapper); }
public void ShouldPassCorrectIndexesWhenReading() { const string data = "1,,Bob,,2018-06-30,,True"; var mapper = SeparatedValueTypeMapper.Define(() => new Person()); var colPhysicalIndexes = new List <int>(); var colLogicalIndexes = new List <int>(); var propPhysicalIndexes = new List <int>(); var propLogicalIndexes = new List <int>(); mapper.CustomMapping(new IndexTrackingColumn(new Int32Column("Id"), colPhysicalIndexes, colLogicalIndexes)) .WithReader((ctx, x, v) => { propPhysicalIndexes.Add(ctx.PhysicalIndex); propLogicalIndexes.Add(ctx.LogicalIndex); x.Id = (int)v; }); mapper.Ignored(); mapper.CustomMapping(new IndexTrackingColumn(new StringColumn("Name"), colPhysicalIndexes, colLogicalIndexes)) .WithReader((ctx, x, v) => { propPhysicalIndexes.Add(ctx.PhysicalIndex); propLogicalIndexes.Add(ctx.LogicalIndex); x.Name = (string)v; }); mapper.Ignored(); mapper.CustomMapping(new IndexTrackingColumn(new DateTimeColumn("CreatedOn") { InputFormat = "yyyy-MM-dd" }, colPhysicalIndexes, colLogicalIndexes)) .WithReader((ctx, x, v) => { propPhysicalIndexes.Add(ctx.PhysicalIndex); propLogicalIndexes.Add(ctx.LogicalIndex); x.CreatedOn = (DateTime)v; }); mapper.Ignored(); mapper.CustomMapping(new IndexTrackingColumn(new BooleanColumn("IsActive"), colPhysicalIndexes, colLogicalIndexes)) .WithReader((ctx, x, v) => { propPhysicalIndexes.Add(ctx.PhysicalIndex); propLogicalIndexes.Add(ctx.LogicalIndex); x.IsActive = (bool)v; }); var reader = new StringReader(data); var typedReader = mapper.GetReader(reader); var results = typedReader.ReadAll().ToArray(); var expectedPhysicalIndexes = new int[] { 0, 2, 4, 6 }; CollectionAssert.AreEqual(expectedPhysicalIndexes, colPhysicalIndexes); CollectionAssert.AreEqual(expectedPhysicalIndexes, propPhysicalIndexes); var expectedLogicalIndexes = new int[] { 0, 1, 2, 3 }; CollectionAssert.AreEqual(expectedLogicalIndexes, colLogicalIndexes); CollectionAssert.AreEqual(expectedLogicalIndexes, propLogicalIndexes); }
private ISeparatedValueTypeMapper <FooterRecord> getFooterTypeMapper() { var mapper = SeparatedValueTypeMapper.Define(() => new FooterRecord()); mapper.Property(x => x.TotalAmount); mapper.Property(x => x.AverageAmount); mapper.Property(x => x.IsCriteriaMet); return(mapper); }
private static ISeparatedValueTypeMapper <DataRecord> getRecordTypeMapper() { var mapper = SeparatedValueTypeMapper.Define(() => new DataRecord()); mapper.Property(x => x.Id); mapper.Property(x => x.Name); mapper.Property(x => x.CreatedOn).InputFormat("yyyyMMdd").OutputFormat("yyyyMMdd"); mapper.Property(x => x.TotalAmount); return(mapper); }