Exemple #1
0
        public void Should_Handle_Mapping_Duplicate_Fields_In_A_Dynamic_Object()
        {
            // Arrange
            var superHeroId          = new KeyValuePair <string, object>("SuperHeroId", "500");
            var superHeroName        = new KeyValuePair <string, object>("SuperHeroName", "Superman");
            var alterEgoFirstName    = new KeyValuePair <string, object>("AlterEgoFirstName", null);
            var alterEgoLastName     = new KeyValuePair <string, object>("AlterEgoLastName", null);
            var superHeroIdNumberTwo = new KeyValuePair <string, object>("SuperHeroId", null);  // The second occurrence has a null value

            var keyValuePairs = new List <KeyValuePair <string, object> > {
                superHeroId, superHeroName, alterEgoFirstName, alterEgoLastName, superHeroIdNumberTwo
            };

            var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

            // Act
            var obj = dataRecord.MapDynamic();

            // Assert
            Assert.That(obj.SuperHeroId != superHeroId.Value);          // Under test
            Assert.That(obj.SuperHeroId == superHeroIdNumberTwo.Value); // Under test
            Assert.That(obj.SuperHeroName == superHeroName.Value);
            Assert.That(obj.AlterEgoFirstName == alterEgoFirstName.Value);
            Assert.That(obj.AlterEgoLastName == alterEgoLastName.Value);
        }
            public void Should_Handle_Mapping_Null_Values_To_Nullable_Type_Properties()
            {
                // Arrange
                var superHeroId       = new KeyValuePair <string, object>("SuperHeroId", null); // Assign to a nullable type
                var superHeroName     = new KeyValuePair <string, object>("SuperHeroName", "Superman");
                var alterEgoFirstName = new KeyValuePair <string, object>("AlterEgoFirstName", "Clark");
                var alterEgoLastName  = new KeyValuePair <string, object>("AlterEgoLastName", "Kent");
                var dateOfBirth       = new KeyValuePair <string, object>("DateOfBirth", null); // Assign to a nullable type

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    superHeroId, superHeroName, alterEgoFirstName, alterEgoLastName, dateOfBirth
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var superHero = dataRecord.Map <SuperHeroWithProperties>();

                // Assert
                Assert.NotNull(superHero);
                Assert.That(superHero.SuperHeroId == (int?)superHeroId.Value);   // Under test
                Assert.That(superHero.SuperHeroName == superHeroName.Value.ToString());
                Assert.That(superHero.AlterEgoFirstName == alterEgoFirstName.Value.ToString());
                Assert.That(superHero.AlterEgoLastName == alterEgoLastName.Value.ToString());
                Assert.That(superHero.DateOfBirth == (DateTime?)dateOfBirth.Value);   // Under test
            }
            public void Should_Throw_An_Exception_When_Mapping_An_Invalid_Value_To_A_Guid_On_Properties()
            {
                // Arrange
                var monsterId   = new KeyValuePair <string, object>("MonsterId", "asdf;lkj");
                var monstername = new KeyValuePair <string, object>("MonsterName", "Frankenstein");

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    monsterId, monstername
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                TestDelegate action = () => dataRecord.Map <MonsterWithProperties>();

                // Assert
                Assert.Throws <TypeConverter.TypeConversionException>(action);
            }
            public void Should_Throw_An_Exception_When_Mapping_An_Invalid_Value_To_An_Enum_On_Properties()
            {
                // Arrange
                var accountHolderFullName = new KeyValuePair <string, object>("AccountHolderFullName", "Clark Kent");
                var accountType           = new KeyValuePair <string, object>("AccountType", "asdf;lkj");

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    accountHolderFullName, accountType
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                TestDelegate action = () => dataRecord.Map <BankAccountWithFields>();

                // Assert
                Assert.Throws <TypeConverter.TypeConversionException>(action);
            }
            public void Should_Convert_A_Null_To_A_Default_Guid_When_To_Guid_Values_On_Properties()
            {
                // Arrange
                var monsterId   = new KeyValuePair <string, object>("MonsterId", null);
                var monstername = new KeyValuePair <string, object>("MonsterName", "Frankenstein");

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    monsterId, monstername
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var monster = dataRecord.Map <MonsterWithProperties>();

                // Assert
                Assert.NotNull(monster);
                Assert.That(monster.MonsterId.ToString() == default(Guid).ToString());
                Assert.That(monster.MonsterName == monstername.Value.ToString());
            }
            public void Should_Handle_Mapping_ByteArray_To_Guid_Values_On_Properties()
            {
                // Arrange
                var monsterId   = new KeyValuePair <string, object>("MonsterId", Guid.NewGuid().ToByteArray());
                var monstername = new KeyValuePair <string, object>("MonsterName", "Frankenstein");

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    monsterId, monstername
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var monster = dataRecord.Map <MonsterWithProperties>();

                // Assert
                Assert.NotNull(monster);
                Assert.That(monster.MonsterId.ToByteArray().ToString() == monsterId.Value.ToString());
                Assert.That(monster.MonsterName == monstername.Value.ToString());
            }
            public void Should_Use_The_Default_Value_When_Mapping_A_Null_To_An_Enum_On_Properties()
            {
                // Arrange
                var accountHolderFullName = new KeyValuePair <string, object>("AccountHolderFullName", "Clark Kent");
                var accountType           = new KeyValuePair <string, object>("AccountType", null);

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    accountHolderFullName, accountType
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var bankAccount = dataRecord.Map <BankAccountWithFields>();

                // Assert
                Assert.NotNull(bankAccount);
                Assert.That(bankAccount.AccountHolderFullName == accountHolderFullName.Value.ToString());
                Assert.That(bankAccount.AccountType == null);   // Checking if null
            }
            public void Should_Handle_Mapping_Integers_To_Enum_Values_On_Properties()
            {
                // Arrange
                var accountHolderFullName = new KeyValuePair <string, object>("AccountHolderFullName", "Clark Kent");
                var accountType           = new KeyValuePair <string, object>("AccountType", 1);

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    accountHolderFullName, accountType
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var bankAccount = dataRecord.Map <BankAccountWithFields>();

                // Assert
                Assert.NotNull(bankAccount);
                Assert.That(bankAccount.AccountHolderFullName == accountHolderFullName.Value.ToString());
                Assert.That(bankAccount.AccountType.ToString() == Enum.ToObject(typeof(BankAccountWithFields.BankAccountType), accountType.Value).ToString());
            }
            public void Should_Throw_An_Exception_When_Converting_NonStandard_Values_On_Properties()
            {
                // Arrange
                var customerId  = new KeyValuePair <string, object>("CustomerId", Convert.ToInt64(5000));
                var firstName   = new KeyValuePair <string, object>("FirstName", "Clark");
                var lastName    = new KeyValuePair <string, object>("LastName", "Kent");
                var dateOfBirth = new KeyValuePair <string, object>("DateOfBirth", "June 18th, 1938");  // Non-standard DateTime value

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    customerId, firstName, lastName, dateOfBirth
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                TestDelegate action = () => dataRecord.Map <CustomerWithProperties>();

                // Assert
                Assert.Throws <TypeConverter.TypeConversionException>(action);
            }
            public void Should_Handle_Mapping_To_Types_With_Properties()
            {
                // Arrange
                var superHeroId = new KeyValuePair<string, object>( "SuperHeroId", 0 );
                var superHeroName = new KeyValuePair<string, object>( "SuperHeroName", "Superman" );
                var alterEgoFirstName = new KeyValuePair<string, object>( "AlterEgoFirstName", "Clark" );
                var alterEgoLastName = new KeyValuePair<string, object>( "AlterEgoLastName", "Kent" );

                var keyValuePairs = new List<KeyValuePair<string, object>> { superHeroId, superHeroName, alterEgoFirstName, alterEgoLastName };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var superHero = dataRecord.Map<SuperHeroWithProperties>();

                // Assert
                Assert.NotNull( superHero );
                Assert.That( superHero.SuperHeroId == (int)superHeroId.Value );
                Assert.That( superHero.SuperHeroName == superHeroName.Value.ToString() );
                Assert.That( superHero.AlterEgoFirstName == alterEgoFirstName.Value.ToString() );
                Assert.That( superHero.AlterEgoLastName == alterEgoLastName.Value.ToString() );
            }
Exemple #11
0
        public void Should_Handle_Mapping_To_A_Dynamic_Object()
        {
            // Arrange
            var superHeroId       = new KeyValuePair <string, object>("SuperHeroId", "500");
            var superHeroName     = new KeyValuePair <string, object>("SuperHeroName", "Superman");
            var alterEgoFirstName = new KeyValuePair <string, object>("AlterEgoFirstName", "Clark");
            var alterEgoLastName  = new KeyValuePair <string, object>("AlterEgoLastName", "Kent");

            var keyValuePairs = new List <KeyValuePair <string, object> > {
                superHeroId, superHeroName, alterEgoFirstName, alterEgoLastName
            };

            var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

            // Act
            var obj = dataRecord.MapDynamic();

            // Assert
            Assert.That(obj.SuperHeroId == superHeroId.Value);
            Assert.That(obj.SuperHeroName == superHeroName.Value);
            Assert.That(obj.AlterEgoFirstName == alterEgoFirstName.Value);
            Assert.That(obj.AlterEgoLastName == alterEgoLastName.Value);
        }
Exemple #12
0
            public void Should_Handle_Mapping_To_Types_With_Properties()
            {
                // Arrange
                var superHeroId       = new KeyValuePair <string, object>("SuperHeroId", 0);
                var superHeroName     = new KeyValuePair <string, object>("SuperHeroName", "Superman");
                var alterEgoFirstName = new KeyValuePair <string, object>("AlterEgoFirstName", "Clark");
                var alterEgoLastName  = new KeyValuePair <string, object>("AlterEgoLastName", "Kent");

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    superHeroId, superHeroName, alterEgoFirstName, alterEgoLastName
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var superHero = dataRecord.Map <SuperHeroWithProperties>();

                // Assert
                Assert.NotNull(superHero);
                Assert.That(superHero.SuperHeroId == (int)superHeroId.Value);
                Assert.That(superHero.SuperHeroName == superHeroName.Value.ToString());
                Assert.That(superHero.AlterEgoFirstName == alterEgoFirstName.Value.ToString());
                Assert.That(superHero.AlterEgoLastName == alterEgoLastName.Value.ToString());
            }
Exemple #13
0
            public void Should_Handle_Mapping_Simple_Type_Conversions_On_Properties()
            {
                // Arrange
                var customerId  = new KeyValuePair <string, object>("CustomerId", Convert.ToInt64(5000));   // Assign to int
                var firstName   = new KeyValuePair <string, object>("FirstName", "Clark");
                var lastName    = new KeyValuePair <string, object>("LastName", "Kent");
                var dateOfBirth = new KeyValuePair <string, object>("DateOfBirth", "06/18/1938");

                var keyValuePairs = new List <KeyValuePair <string, object> > {
                    customerId, firstName, lastName, dateOfBirth
                };

                var dataRecord = new TestHelpers.DataRecord(keyValuePairs);

                // Act
                var customer = dataRecord.Map <CustomerWithProperties>();

                // Assert
                Assert.NotNull(customer);
                Assert.That(customer.CustomerId == Convert.ToInt64(customerId.Value));     // Under test
                Assert.That(customer.FirstName == firstName.Value.ToString());
                Assert.That(customer.LastName == lastName.Value.ToString());
                Assert.That(customer.DateOfBirth == Convert.ToDateTime(dateOfBirth.Value));
            }
            public void Should_Handle_Mapping_Simple_Type_Conversions_On_Properties()
            {
                // Arrange
                var customerId = new KeyValuePair<string, object>( "CustomerId", Convert.ToInt64( 5000 ) ); // Assign to int
                var firstName = new KeyValuePair<string, object>( "FirstName", "Clark" );
                var lastName = new KeyValuePair<string, object>( "LastName", "Kent" );
                var dateOfBirth = new KeyValuePair<string, object>( "DateOfBirth", "06/18/1938" );

                var keyValuePairs = new List<KeyValuePair<string, object>> { customerId, firstName, lastName, dateOfBirth };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var customer = dataRecord.Map<CustomerWithProperties>();

                // Assert
                Assert.NotNull( customer );
                Assert.That( customer.CustomerId == Convert.ToInt64( customerId.Value ) ); // Under test
                Assert.That( customer.FirstName == firstName.Value.ToString() );
                Assert.That( customer.LastName == lastName.Value.ToString() );
                Assert.That( customer.DateOfBirth == Convert.ToDateTime( dateOfBirth.Value ) );
            }
            public void Should_Handle_Mapping_Strings_To_Guid_Values_On_Properties()
            {
                // Arrange
                var monsterId = new KeyValuePair<string, object>( "MonsterId", Guid.NewGuid() );
                var monstername = new KeyValuePair<string, object>( "MonsterName", "Frankenstein" );

                var keyValuePairs = new List<KeyValuePair<string, object>> { monsterId, monstername };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var monster = dataRecord.Map<MonsterWithProperties>();

                // Assert
                Assert.NotNull( monster );
                Assert.That( monster.MonsterId.ToString() == monsterId.Value.ToString() );
                Assert.That( monster.MonsterName == monstername.Value.ToString() );
            }
            public void Should_Throw_An_Exception_When_Mapping_An_Invalid_Value_To_A_Guid_On_Fields()
            {
                // Arrange
                var monsterId = new KeyValuePair<string, object>( "MonsterId", "asdf;lkj" );
                var monstername = new KeyValuePair<string, object>( "MonsterName", "Frankenstein" );

                var keyValuePairs = new List<KeyValuePair<string, object>> { monsterId, monstername };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                TestDelegate action = () => dataRecord.Map<MonsterWithFields>();

                // Assert
                Assert.Throws<TypeConverter.TypeConversionException>( action );
            }
            public void Should_Convert_A_Null_To_A_Default_Guid_When_To_Guid_Values_On_Fields()
            {
                // Arrange
                var monsterId = new KeyValuePair<string, object>( "MonsterId", null );
                var monstername = new KeyValuePair<string, object>( "MonsterName", "Frankenstein" );

                var keyValuePairs = new List<KeyValuePair<string, object>> { monsterId, monstername };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var monster = dataRecord.Map<MonsterWithFields>();

                // Assert
                Assert.NotNull( monster );
                Assert.That( monster.MonsterId == null ); // Ensure is null
                Assert.That( monster.MonsterName == monstername.Value.ToString() );
            }
            public void Should_Throw_An_Exception_When_Mapping_An_Invalid_Value_To_An_Enum_On_Properties()
            {
                // Arrange
                var accountHolderFullName = new KeyValuePair<string, object>( "AccountHolderFullName", "Clark Kent" );
                var accountType = new KeyValuePair<string, object>( "AccountType", "asdf;lkj" );

                var keyValuePairs = new List<KeyValuePair<string, object>> { accountHolderFullName, accountType };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                TestDelegate action = () => dataRecord.Map<BankAccountWithFields>();

                // Assert
                Assert.Throws<TypeConverter.TypeConversionException>( action );
            }
            public void Should_Use_The_Default_Value_When_Mapping_A_Null_To_An_Enum_On_Properties()
            {
                // Arrange
                var accountHolderFullName = new KeyValuePair<string, object>( "AccountHolderFullName", "Clark Kent" );
                var accountType = new KeyValuePair<string, object>( "AccountType", null );

                var keyValuePairs = new List<KeyValuePair<string, object>> { accountHolderFullName, accountType };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var bankAccount = dataRecord.Map<BankAccountWithFields>();

                // Assert
                Assert.NotNull( bankAccount );
                Assert.That( bankAccount.AccountHolderFullName == accountHolderFullName.Value.ToString() );
                Assert.That( bankAccount.AccountType == null ); // Checking if null
            }
            public void Should_Handle_Mapping_Integers_To_Enum_Values_On_Properties()
            {
                // Arrange
                var accountHolderFullName = new KeyValuePair<string, object>( "AccountHolderFullName", "Clark Kent" );
                var accountType = new KeyValuePair<string, object>( "AccountType", 1 );

                var keyValuePairs = new List<KeyValuePair<string, object>> { accountHolderFullName, accountType };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var bankAccount = dataRecord.Map<BankAccountWithFields>();

                // Assert
                Assert.NotNull( bankAccount );
                Assert.That( bankAccount.AccountHolderFullName == accountHolderFullName.Value.ToString() );
                Assert.That( bankAccount.AccountType.ToString() == Enum.ToObject( typeof( BankAccountWithFields.BankAccountType ), accountType.Value ).ToString() );
            }
            public void Should_Handle_Mapping_Null_Values_To_Nullable_Type_Properties()
            {
                // Arrange
                var superHeroId = new KeyValuePair<string, object>( "SuperHeroId", null ); // Assign to a nullable type
                var superHeroName = new KeyValuePair<string, object>( "SuperHeroName", "Superman" );
                var alterEgoFirstName = new KeyValuePair<string, object>( "AlterEgoFirstName", "Clark" );
                var alterEgoLastName = new KeyValuePair<string, object>( "AlterEgoLastName", "Kent" );
                var dateOfBirth = new KeyValuePair<string, object>( "DateOfBirth", null ); // Assign to a nullable type

                var keyValuePairs = new List<KeyValuePair<string, object>> { superHeroId, superHeroName, alterEgoFirstName, alterEgoLastName, dateOfBirth };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                var superHero = dataRecord.Map<SuperHeroWithProperties>();

                // Assert
                Assert.NotNull( superHero );
                Assert.That( superHero.SuperHeroId == (int?)superHeroId.Value ); // Under test
                Assert.That( superHero.SuperHeroName == superHeroName.Value.ToString() );
                Assert.That( superHero.AlterEgoFirstName == alterEgoFirstName.Value.ToString() );
                Assert.That( superHero.AlterEgoLastName == alterEgoLastName.Value.ToString() );
                Assert.That( superHero.DateOfBirth == (DateTime?)dateOfBirth.Value ); // Under test
            }
            public void Should_Throw_An_Exception_When_Converting_NonStandard_Values_On_Properties()
            {
                // Arrange
                var customerId = new KeyValuePair<string, object>( "CustomerId", Convert.ToInt64( 5000 ) );
                var firstName = new KeyValuePair<string, object>( "FirstName", "Clark" );
                var lastName = new KeyValuePair<string, object>( "LastName", "Kent" );
                var dateOfBirth = new KeyValuePair<string, object>( "DateOfBirth", "June 18th, 1938" ); // Non-standard DateTime value

                var keyValuePairs = new List<KeyValuePair<string, object>> { customerId, firstName, lastName, dateOfBirth };

                var dataRecord = new TestHelpers.DataRecord( keyValuePairs );

                // Act
                TestDelegate action = () => dataRecord.Map<CustomerWithProperties>();

                // Assert
                Assert.Throws<TypeConverter.TypeConversionException>( action );
            }