public void DataTableMapper_Map_WhenValidDataTableWithHTMLData_ExpectValidPaths() { //------------Setup for test-------------------------- var htmlFragment = @"<html xmlns=""http://www.w3.org/1999/xhtml""> <head><title> All Build Definitions - Microsoft Team Foundation Server </title> </head> </html>"; var dataTableMapper = new DataTableMapper(); DataTable obj = new DataTable("Foo"); obj.Columns.Add("Col1"); obj.Columns.Add("Col2"); obj.Rows.Add(new object[] { "a", "b" }); obj.Rows.Add(new object[] { "c", htmlFragment }); //------------Execute Test--------------------------- var result = dataTableMapper.Map(obj).ToList(); //------------Assert Results------------------------- Assert.AreEqual(2, result.Count); Assert.AreEqual("Foo().Col1", result[0].ActualPath); Assert.AreEqual("Foo().Col1", result[0].DisplayPath); Assert.AreEqual("a__COMMA__c", result[0].SampleData); Assert.AreEqual("Foo().Col2", result[1].ActualPath); Assert.AreEqual("Foo().Col2", result[1].DisplayPath); Assert.AreEqual("b__COMMA__" + htmlFragment, result[1].SampleData); }
public void DataTableMapper_Map_WhenValidDataTable_ExpectValidPaths() { //------------Setup for test-------------------------- var dataTableMapper = new DataTableMapper(); DataTable obj = new DataTable("Foo"); obj.Columns.Add("Col1"); obj.Columns.Add("Col2"); obj.Rows.Add(new object[] { "a", "b" }); obj.Rows.Add(new object[] { "c", "d" }); obj.Rows.Add(new object[] { "e", "f" }); //------------Execute Test--------------------------- var result = dataTableMapper.Map(obj).ToList(); //------------Assert Results------------------------- Assert.AreEqual(2, result.Count); Assert.AreEqual("Foo().Col1", result[0].ActualPath); Assert.AreEqual("Foo().Col1", result[0].DisplayPath); Assert.AreEqual("a__COMMA__c__COMMA__e", result[0].SampleData); Assert.AreEqual("Foo().Col2", result[1].ActualPath); Assert.AreEqual("Foo().Col2", result[1].DisplayPath); Assert.AreEqual("b__COMMA__d__COMMA__f", result[1].SampleData); }
public void DataTableMapper_Map_WhenNull_ExpectNull() { //------------Setup for test-------------------------- var dataTableMapper = new DataTableMapper(); //------------Execute Test--------------------------- dataTableMapper.Map(null).ToList(); }
public void Map_GivenUnMatchingColumns_ShouldThrowMappingException() { var mappings = new List <IPropertyMapping <TestC> >(); mappings.Add(new ColumnToPropertyMapping <TestC>("Name", "testNameX")); var mapper = new DataTableMapper <TestC>(); Assert.Throws <MappingException>(() => mapper.Map(mappings, dataTable)); }
/// <summary> /// DataTable转化为实体列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <returns></returns> public virtual List <T> DataTableToEntityList <T>(DataTable dt) { List <T> entityList = null; if (dt == null) { return(entityList); } entityList = new List <T>(); foreach (DataRow dataRow in dt.Rows) { DataTableMapper <T> dataRowMapper = DataTableMapper <T> .GetInstance(dataRow); var entity = dataRowMapper.Map(dataRow); if (entity != null) { entityList.Add(dataRowMapper.Map(dataRow)); } } return(entityList); }
public void Map_GivenMatchingColumns_ShouldReturnExpectedList() { var mappings = new List <IPropertyMapping <TestC> >(); mappings.Add(new ColumnToPropertyMapping <TestC>("Name", "testName")); mappings.Add(new ColumnToPropertyMapping <TestC>("Id", "testId")); var mapper = new DataTableMapper <TestC>(); var result = mapper.Map(mappings, dataTable).ToList(); Assert.AreEqual(2, result.Count); Assert.AreEqual("X001", result[0].Name); Assert.AreEqual(1, result[0].Id); Assert.AreEqual("X002", result[1].Name); Assert.AreEqual(2, result[1].Id); }