public void DataReaderToList_WhenMultipleDictionaryJoins_DoesNotUseSameExpandoObjectMap() { var dataReader = new MockDataReader(); var rootTable = dataReader.AddTable( "Id" ); var users = dataReader.AddTable( "Key", "Value" ); var places = dataReader.AddTable( "Key", "Value" ); dataReader.AddRow( rootTable, 1 ); dataReader.AddRow( users, 1, "User 1" ); dataReader.AddRow( places, "Place 1", "Location" ); var result = dataReader.DataReaderToList<WhenMultipleDictionaryJoins>( typeof( WhenMultipleDictionaryJoinsConfiguration ) ); }
public void ContainsMappingCache_WhenDoesNotContainCache_ReturnsFalse() { var dataReader = new MockDataReader(); dataReader.AddTable(); Assert.IsFalse( CacheManager.ContainsMappingCache<TestClass1>( dataReader, null ) ); }
public void ContainsMappingCache_WhenContainsCache_ReturnsFalse() { var dataReader = new MockDataReader(); dataReader.AddTable(); CacheManager.StoreMappingCache( () => new MappingCache<TestClass1>( null, null ), dataReader, null ); Assert.IsTrue( CacheManager.ContainsMappingCache<TestClass1>( dataReader, null ) ); }
public void DataReaderToList_WhenPropertyHasCorrosponingColmunButHasPropertyMap_OnlyUsesPropertyMap() { var dataReader = new MockDataReader(); var rootTable = dataReader.AddTable( "IdVal", "Id" ); dataReader.AddRow( rootTable, 1, 2 ); dataReader.AddRow( rootTable, 1, DBNull.Value ); var result = dataReader.DataReaderToList<PropertyMapTest>( typeof( PropertyMapTestConfiguration ) ); }
public void DataReaderToList_WithMappingThatSetsVirtualStringProperty_ReturnsList() { var dataReader = new MockDataReader(); var table = dataReader.AddTable( "Id" ); dataReader.AddRow( table, 1 ); var result = dataReader.DataReaderToList<TestObject>( typeof ( TestObjectConfiguration ) ); Assert.AreEqual( "Hello World", result[ 0 ].Name ); }
public void GetMappingCache_WhenContainsCacheSameDataReaderButDifferentConfiguration_ReturnsFalse() { var dataReader = new MockDataReader(); var table1 = dataReader.AddTable( "Name" ); dataReader.AddRow( table1, "" ); var mappingCache = new MappingCache<TestClass1>( null, null ); CacheManager.StoreMappingCache( () => mappingCache, dataReader, typeof( Configuration1 ) ); Assert.AreEqual( mappingCache, CacheManager.GetMappingCache<TestClass1>( dataReader, typeof( Configuration1 ) ) ); }
public void GenerateToObject_WhenCalledWithMultipeTablesUsingJoinIsJoinedCorrectly_SetsPropertiesCorrectly() { IMapping mapping = new Mapping(); Action<TestClass, List<TestContent>> listSet = ( x, y ) => x.TestContents = y; mapping.Join( listSet ); mapping.Table<TestClass>( 0 ); mapping.Table<TestContent>( 1 ); var classMapResult = new ClassMapResult { Mappings = (Mapping)mapping }; var mockDataReader = new MockDataReader(); var table = mockDataReader.AddTable( "Length2" ); var contentTable = mockDataReader.AddTable( "Id", "Name" ); mockDataReader.AddRow( table, 1 ); mockDataReader.AddRow( contentTable, 1, "Name1" ); mockDataReader.AddRow( contentTable, 2, "Name2" ); var func = MappingGenerator.Generate<TestClass>( new MockDataReader(), classMapResult ); var result = func( mockDataReader, new Dictionary<MappingEnum, object> { { MappingEnum.Join, new List<object> { listSet } } }, new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>() ); Assert.AreEqual( 2, result.TestContents.Count ); Assert.AreEqual( "Name1", result.TestContents[ 0 ].Name ); Assert.AreEqual( "Name2", result.TestContents[ 1 ].Name ); }
public void DataReaderToList_WhenUsingConfiguration_ReturnsCorrectResult() { var dataReader = new MockDataReader(); var rootTable = dataReader.AddTable( "Name", "Size", "DueDate" ); var products = dataReader.AddTable( "Id", "Name", "Price", "Available" ); var stock = dataReader.AddTable( "ProductId", "Id", "Quantity" ); var ratings = dataReader.AddTable( "ProductId", "Rating" ); dataReader.AddRow( rootTable, "The Shop", 12, DateTime.Now ); dataReader.AddRow( products, 1, "Drink", 1.99m, true ); dataReader.AddRow( products, 2, "Snack", 0.78m, false ); dataReader.AddRow( stock, 1, 1, 20 ); dataReader.AddRow( stock, 1, 2, 5 ); dataReader.AddRow( stock, 2, 3, 0 ); dataReader.AddRow( ratings, 2, (double)2 ); var result = dataReader.DataReaderToList<RootTable>( typeof( ShopConfiguration ) ); Assert.AreEqual( "The Shop", result[ 0 ].Name ); Assert.AreEqual( 24, result[ 0 ].Size ); Assert.AreEqual( 2, result[ 0 ].Products.Count ); Assert.AreEqual( 1, result[ 0 ].Products[ 0 ].Id ); Assert.AreEqual( "Drink", result[ 0 ].Products[ 0 ].Name ); Assert.AreEqual( 1.99M, result[ 0 ].Products[ 0 ].Price ); Assert.AreEqual( true, result[ 0 ].Products[ 0 ].Available ); Assert.AreEqual( 2, result[ 0 ].Products[ 1 ].Id ); Assert.AreEqual( "Snack", result[ 0 ].Products[ 1 ].Name ); Assert.AreEqual( 0.78M, result[ 0 ].Products[ 1 ].Price ); Assert.AreEqual( false, result[ 0 ].Products[ 1 ].Available ); Assert.AreEqual( 2, result[ 0 ].Products[ 0 ].Stock.Count ); Assert.AreEqual( 20, result[ 0 ].Products[ 0 ].Stock[ 1 ] ); Assert.AreEqual( 5, result[ 0 ].Products[ 0 ].Stock[ 2 ] ); Assert.AreEqual( 1, result[ 0 ].Products[ 1 ].Stock.Count ); Assert.AreEqual( 0, result[ 0 ].Products[ 1 ].Stock[ 3 ] ); Assert.AreEqual( 0, result[ 0 ].Products[ 0 ].ProductRatings.Count ); Assert.AreEqual( 1, result[ 0 ].Products[ 1 ].ProductRatings.Count ); Assert.AreEqual( 2, result[ 0 ].Products[ 1 ].ProductRatings[ 0 ].Rating ); }
public void ContainsMappingCache_WhenContainsCacheButDataReaderDoesNotMatch_ReturnsFalse() { var dataReader1 = new MockDataReader(); var table1 = dataReader1.AddTable( "Name" ); dataReader1.AddRow( table1, "" ); var dataReader2 = new MockDataReader(); var table2 = dataReader2.AddTable( "Age" ); dataReader1.AddRow( table2, 1 ); CacheManager.StoreMappingCache( () => new MappingCache<TestClass1>( null, null ), dataReader1, null ); Assert.IsTrue( CacheManager.ContainsMappingCache<TestClass1>( dataReader1, null ) ); Assert.IsFalse( CacheManager.ContainsMappingCache<TestClass1>( dataReader2, null ) ); }
public void Generate_WhenInputTypeIsList_ReturnsListOfData() { var classMapResult = new ClassMapResult { Mappings = new Mapping { TableMaps = new Dictionary<int, Type> { { 0, typeof( TestClass ) } } } }; var mockDataReader = new MockDataReader(); var table = mockDataReader.AddTable( "Name1", "Name 2", "Length1" ); mockDataReader.AddRow( table, "Name 1", DBNull.Value, 1 ); var func = MappingGenerator.Generate<List<TestClass>>( new MockDataReader(), classMapResult ); var result = func( mockDataReader, new Dictionary<MappingEnum, object>(), new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>() ); Assert.AreEqual( "Name 1", result[ 0 ].Name1 ); Assert.AreEqual( null, result[ 0 ].Name2 ); Assert.AreEqual( 1, result[ 0 ].Length1 ); }
public void GenerateToObject_WhenCalled_DoesNotThrowExeception() { var classMapResult = new ClassMapResult { Mappings = new Mapping { TableMaps = new Dictionary<int, Type> { { 0, typeof( TestClass ) } } } }; var mockDataReader = new MockDataReader(); var table = mockDataReader.AddTable(); mockDataReader.AddRow( table ); var result = MappingGenerator.Generate<TestClass>( new MockDataReader(), classMapResult ); result( mockDataReader, new Dictionary<MappingEnum, object>(), new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>() ); }
public void DataReaderToList_WithTwoTableJoinsForSameTable_JoinsTables() { var dataReader = new MockDataReader(); var rootTable = dataReader.AddTable( "HomeTeamId", "HomeTeamId1", "AwayTeamId" ); dataReader.AddRow( rootTable, DBNull.Value, 2, 1 ); var teamTable = dataReader.AddTable( "Id", "Name" ); dataReader.AddRow( teamTable, 1, "Team A" ); dataReader.AddRow( teamTable, 2, "Team B" ); var result = dataReader.DataReaderToList<Score>( typeof( ScoreConfiguration ) ); Assert.AreEqual( "Team A", result[ 0 ].AwayTeam.Name ); Assert.AreEqual( "Team B", result[ 0 ].HomeTeam.Name ); }
public void GenerateToObject_WhenCalledWithVariableTableTypeAndPropertyMap_UsesPropertyMap() { IMapping mapping = new Mapping(); Func<IDataRecord, Type> variableTableType = dataRecord => typeof( StringScore ); mapping.VariableTableType<BaseScore>( variableTableType ); Func<IDataRecord, string> action = dataRecord => "The Score Is: " + dataRecord.GetString( 2 ); mapping.PropertyMap<StringScore, String>( x => x.ValueString, action ); mapping.Table<BaseScore>( 0 ); var classMapResult = new ClassMapResult { Mappings = (Mapping)mapping }; var mockDataReader = new MockDataReader(); var scores = mockDataReader.AddTable( "Id", "ResultId", "ValueString", "ResultType" ); mockDataReader.AddRow( scores, 1, 1, "10", 2 ); var func = MappingGenerator.Generate<List<BaseScore>>( new MockDataReader(), classMapResult ); var dictionary = new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>(); var result = func( mockDataReader, new Dictionary<MappingEnum, object> { { MappingEnum.VariableTableType, new Dictionary<Type, object> { { typeof( BaseScore ), variableTableType } } }, { MappingEnum.PropertyMap, new Dictionary<PropertyInfo, PropertyMap> { { typeof( StringScore ).GetProperty( "ValueString" ), new PropertyMap( action ) } } } }, dictionary ); Assert.AreEqual( 1, result[ 0 ].Id ); Assert.IsInstanceOf<StringScore>( result[ 0 ] ); Assert.AreEqual( "The Score Is: 10", ( result[ 0 ] as StringScore ).ValueString ); }
public void GenerateToObject_WhenCalledWithSingleTable_SetsPropertiesCorrectly() { var classMapResult = new ClassMapResult { Mappings = new Mapping { TableMaps = new Dictionary<int, Type> { { 0, typeof( TestClass ) } } } }; var mockDataReader = new MockDataReader(); var table = mockDataReader.AddTable( "Name1", "Name 2", "Length1" ); mockDataReader.AddRow( table, "Name 1", DBNull.Value, 1 ); var func = MappingGenerator.Generate<TestClass>( new MockDataReader(), classMapResult ); var result = func( mockDataReader, new Dictionary<MappingEnum, object>(), new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>() ); Assert.AreEqual( "Name 1", result.Name1 ); Assert.AreEqual( null, result.Name2 ); Assert.AreEqual( 1, result.Length1 ); }
public void GenerateToObject_WhenCalledWithPropertyMapColumnName_ReturnsCorrectData() { IMapping mapping = new Mapping(); mapping.Table<TestClass>( 0 ); mapping.PropertyMap<TestClass,string>( x => x.Name1, "Name" ); var classMapResult = new ClassMapResult { Mappings = (Mapping)mapping }; var mockDataReader = new MockDataReader(); var scores = mockDataReader.AddTable( "Name" ); string expectedName = "The name"; mockDataReader.AddRow( scores, expectedName ); var func = MappingGenerator.Generate<List<TestClass>>( new MockDataReader(), classMapResult ); var dictionary = new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>(); var result = func( mockDataReader, new Dictionary<MappingEnum, object> { { MappingEnum.PropertyMap, new Dictionary<PropertyInfo, PropertyMap> { { typeof( TestClass ).GetProperty( "Name1" ), new PropertyMap( "Name" ) } } } }, dictionary ); Assert.AreEqual( expectedName, result[ 0 ].Name1 ); }
public void GenerateToObject_WhenCalledWithMultipeTablesWithResultingDictionary_FillsDictionaryCorrectly() { IMapping mapping = new Mapping(); Func<TestClass, dynamic, bool> condition = ( @class, o ) => true; Action<TestClass, Dictionary<int, string>> destination = ( @class, values ) => @class.TestContentDictionary = values; mapping.AddJoin( x => x.DictionaryTableJoin<TestClass>() .Condition( condition ) .KeyColumn( "Id" ) .ValueColumn( "Name" ) .SetDestinationProperty( destination ) .ChildTable( 1 ) ); mapping.Table<TestClass>( 0 ); var classMapResult = new ClassMapResult { Mappings = (Mapping)mapping }; var mockDataReader = new MockDataReader(); var table = mockDataReader.AddTable( "Length2", "TestContentId" ); var contentTable = mockDataReader.AddTable( "Id", "Name" ); mockDataReader.AddRow( table, 1, 1 ); mockDataReader.AddRow( contentTable, 1, "Name1" ); mockDataReader.AddRow( contentTable, 2, "Name2" ); var func = MappingGenerator.Generate<TestClass>( new MockDataReader(), classMapResult ); var result = func( mockDataReader, new Dictionary<MappingEnum, object> { { MappingEnum.DictionaryJoin, new List<KeyValuePair<object, object>> { new KeyValuePair<object, object>( condition, destination ) } } }, new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>() ); Assert.AreEqual( 2, result.TestContentDictionary.Count ); Assert.AreEqual( "Name1", result.TestContentDictionary[ 1 ] ); Assert.AreEqual( "Name2", result.TestContentDictionary[ 2 ] ); }
public void GenerateToObject_WhenCalledWithVariableTableType_UsesCorrectType() { IMapping mapping = new Mapping(); Func<Result, BaseScore, bool> canJoin = ( r, b ) => r.Id == b.ResultId; Action<Result, List<BaseScore>> listSet = ( r, b ) => r.Scores = b; Func<IDataRecord, Type> variableTableType = dataRecord => { switch ( dataRecord.GetInt32( 4 ) ) { case 1: return typeof( IntScore ); default: return typeof( StringScore ); } }; mapping.TableJoin<Result, BaseScore>( canJoin, listSet ); mapping.VariableTableType<BaseScore>( variableTableType ); mapping.Table<Result>( 0 ); mapping.Table<BaseScore>( 1 ); var classMapResult = new ClassMapResult { Mappings = (Mapping)mapping }; var mockDataReader = new MockDataReader(); var results = mockDataReader.AddTable( "Id", "Name" ); var scores = mockDataReader.AddTable( "Id", "ResultId", "Value", "ValueString", "ResultType" ); mockDataReader.AddRow( results, 1, "Result 1" ); mockDataReader.AddRow( results, 2, "Result 2" ); mockDataReader.AddRow( scores, 1, 1, 10, "", 1 ); mockDataReader.AddRow( scores, 1, 2, DBNull.Value, "10", 2 ); var func = MappingGenerator.Generate<List<Result>>( new MockDataReader(), classMapResult ); var dictionary = new Dictionary<Tuple<int, Type>, Func<IDataRecord, Dictionary<MappingEnum, object>, object>>(); var result = func( mockDataReader, new Dictionary<MappingEnum, object> { { MappingEnum.TableJoin, new List<TableJoinMap> { new TableJoinMap { CanJoin = canJoin, ListSet = listSet } } }, { MappingEnum.VariableTableType, new Dictionary<Type, object> { { typeof( BaseScore ), variableTableType } } } }, dictionary ); Assert.AreEqual( 1, result[ 0 ].Id ); Assert.AreEqual( 2, result[ 1 ].Id ); Assert.AreEqual( 1, result[ 0 ].Scores.Count ); Assert.AreEqual( 1, result[ 1 ].Scores.Count ); Assert.IsInstanceOf<IntScore>( result[ 0 ].Scores[ 0 ] ); Assert.IsInstanceOf<StringScore>( result[ 1 ].Scores[ 0 ] ); Assert.AreEqual( 10, ( result[ 0 ].Scores[ 0 ] as IntScore ).Value ); Assert.AreEqual( "10", ( result[ 1 ].Scores[ 0 ] as StringScore ).ValueString ); }
public void DataReaderToList_WithNullablePropertyThatHasAValueOnTheFirstRowAndNullOnTheSecondRow_SetsTheSecondObjectsPropertyToNull() { var dataReader = new MockDataReader(); var rootTable = dataReader.AddTable( "Name", "Score" ); dataReader.AddRow( rootTable, "Team A", (int?)12 ); dataReader.AddRow( rootTable, "Team B", DBNull.Value ); var result = dataReader.DataReaderToList<NullableTest>(); Assert.AreEqual( 12, result[ 0 ].Score ); Assert.IsNull( result[ 1 ].Score ); }