public void Can_Convert_Anonymous_Types_With_CustomTypeConverter()
        {
            AutoMappingUtils.RegisterConverter((DateTimeOffset from) => new WrappedDateTimeOffset(from));

            var personWithDateOfBirth = new
            {
                FirstName   = "Foo",
                LastName    = "Bar",
                DateOfBirth = new DateTimeOffset(1971, 3, 23, 4, 30, 0, TimeSpan.Zero)
            };

            var personWithWrappedDoB = personWithDateOfBirth.ConvertTo <PersonWithWrappedDateOfBirth>();

            Assert.That(personWithWrappedDoB.FirstName, Is.EqualTo("Foo"));
            Assert.That(personWithWrappedDoB.LastName, Is.EqualTo("Bar"));
            Assert.That(personWithWrappedDoB.DateOfBirth, Is.Not.Null);
            var dto = personWithWrappedDoB.DateOfBirth.ToDateTimeOffset();

            Assert.That(dto, Is.Not.Null);
            Assert.That(dto.Year, Is.EqualTo(1971));
            Assert.That(dto.Month, Is.EqualTo(3));
            Assert.That(dto.Day, Is.EqualTo(23));
            Assert.That(dto.Hour, Is.EqualTo(4));
            Assert.That(dto.Minute, Is.EqualTo(30));
            Assert.That(dto.Second, Is.EqualTo(0));

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 2
0
        public void Does_ignore_collection_mappings()
        {
            AutoMapping.IgnoreMapping <List <User>, List <UserDto> >();
            AutoMapping.IgnoreMapping <Dictionary <int, User>, Dictionary <int, UserDto> >();

            var users = new UsersData {
                Id        = 1,
                UsersList = new List <User> {
                    new User {
                        FirstName = "John",
                        LastName  = "Doe",
                        Car       = new Car {
                            Name = "BMW X6", Age = 3
                        }
                    }
                }
            };

            var dtoUsers = users.ConvertTo <UsersDto>();

            dtoUsers.PrintDump();

            Assert.That(dtoUsers.Id, Is.EqualTo(users.Id));
            Assert.That(dtoUsers.UsersList, Is.Empty);
            Assert.That(dtoUsers.UsersMap, Is.Empty);

            AutoMappingUtils.Reset();
        }
        public void Can_convert_prop_with_CustomTypeConverter()
        {
            AutoMappingUtils.RegisterConverter((WrappedDateTimeOffset from) => from.ToDateTimeOffset());

            var map = new Dictionary <string, object>
            {
                { "FirstName", "Foo" },
                { "LastName", "Bar" },
                { "DateOfBirth", new WrappedDateTimeOffset(
                      new DateTimeOffset(1971, 3, 23, 4, 30, 0, TimeSpan.Zero)) }
            };

            var personWithDoB = map.FromObjectDictionary <PersonWithDateOfBirth>();

            Assert.That(personWithDoB.FirstName, Is.EqualTo("Foo"));
            Assert.That(personWithDoB.LastName, Is.EqualTo("Bar"));
            Assert.That(personWithDoB.DateOfBirth, Is.Not.Null);
            Assert.That(personWithDoB.DateOfBirth.Year, Is.EqualTo(1971));
            Assert.That(personWithDoB.DateOfBirth.Month, Is.EqualTo(3));
            Assert.That(personWithDoB.DateOfBirth.Day, Is.EqualTo(23));
            Assert.That(personWithDoB.DateOfBirth.Hour, Is.EqualTo(4));
            Assert.That(personWithDoB.DateOfBirth.Minute, Is.EqualTo(30));
            Assert.That(personWithDoB.DateOfBirth.Second, Is.EqualTo(0));

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 4
0
        public void Does_ignore_POCO_mappings()
        {
            AutoMapping.IgnoreMapping <User, UserDto>();

            var user = new User {
                FirstName = "John",
                LastName  = "Doe",
                Car       = new Car {
                    Name = "BMW X6", Age = 3
                }
            };
            var users = new UsersData {
                Id        = 1,
                User      = user,
                UsersList = { user },
                UsersMap  = { { 1, user } }
            };

            var dtoUsers = users.ConvertTo <UsersDto>();

            Assert.That(dtoUsers.Id, Is.EqualTo(users.Id));

            Assert.That(user.ConvertTo <UserDto>(), Is.Null);
            Assert.That(dtoUsers.User, Is.Null);
            Assert.That(dtoUsers.UsersList, Is.Empty);
            Assert.That(dtoUsers.UsersMap, Is.Empty);

            AutoMappingUtils.Reset();
        }
        public void RegisterLocalDateTimeToDateTimeConverter_Should_Register_Converters()
        {
            Neo4jMapperConfig.RegisterLocalDateTimeToDateTimeConverter();

            AssertLocalDateTimeConvertersAreRegistered();

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 6
0
        public void RegisterZonedDateTimeToDateTimeOffsetConverter_Should_Register_Converters()
        {
            MapperConfig.RegisterZonedDateTimeToDateTimeOffsetConverter();

            AssertZonedDateTimeConvertersAreRegistered();

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 7
0
        public void RegisterLocalTimeToTimeSpanConverter_Should_Register_Converters()
        {
            MapperConfig.RegisterLocalTimeToTimeSpanConverter();

            AssertLocalTimeConvertersAreRegistered();

            AutoMappingUtils.Reset();
        }
        public void RegisterTypeConverters_Should_Register_All_Converters()
        {
            Neo4jMapperConfig.RegisterTypeConverters();

            AssertLocalDateConvertersAreRegistered();
            AssertLocalTimeConvertersAreRegistered();
            AssertZonedDateTimeConvertersAreRegistered();
            AssertLocalDateTimeConvertersAreRegistered();

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 9
0
        public void Should_Convert_LocalDate_To_DateTime()
        {
            MapperConfig.RegisterLocalDateToDateTimeConverter();

            var map = new Dictionary <string, object>
            {
                { nameof(EntityWithClrTypes.DateValue), new LocalDate(2019, 1, 12) }
            };

            var entity = ValueMapper.MapValue <EntityWithClrTypes>(map);

            entity.DateValue.Year.Should().Be(2019);
            entity.DateValue.Month.Should().Be(1);
            entity.DateValue.Day.Should().Be(12);

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 10
0
        public void Should_Convert_LocalDate_To_DateTime()
        {
            Neo4jMapperConfig.RegisterLocalDateToDateTimeConverter();

            var map = new Dictionary <string, object>
            {
                { nameof(EntityWithClrTypes.DateValue), new LocalDate(2019, 1, 12) }
            };

            var entity = ValueMapper.MapValue <EntityWithClrTypes>(map);

            Assert.AreEqual(2019, entity.DateValue.Year);
            Assert.AreEqual(1, entity.DateValue.Month);
            Assert.AreEqual(12, entity.DateValue.Day);

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 11
0
        public void Should_Convert_Enum_Type_To_String()
        {
            var entity = new MovieWithType
            {
                Title     = "Top Gun",
                Released  = 1986,
                Tagline   = "As students at the United States Navy's elite fighter weapons school compete to be best in the class, one daring young pilot learns a few things from a civilian instructor that are not taught in the classroom.",
                MovieType = MovieType.Action
            };

            try
            {
                AutoMapping.RegisterConverter <MovieWithType, Dictionary <string, object> >(movieWithType =>
                {
                    var dictionary = movieWithType.ConvertTo <Dictionary <string, object> >(true);
                    dictionary[nameof(movieWithType.MovieType)] = movieWithType.MovieType.ToString();
                    return(dictionary);
                });

                var parameter = entity.ToParameterMap("entity");

                Assert.AreEqual("entity", parameter.Key);
                Assert.AreEqual(4, parameter.Value.Count);

                Assert.IsTrue(parameter.Value.ContainsKey("Title"));
                var titleValue = parameter.Value["Title"];
                Assert.IsInstanceOf <string>(titleValue);
                Assert.AreEqual("Top Gun", titleValue);

                Assert.IsTrue(parameter.Value.ContainsKey("Released"));
                var releasedValue = parameter.Value["Released"];
                Assert.IsInstanceOf <int>(releasedValue);
                Assert.AreEqual(1986, releasedValue);

                Assert.IsTrue(parameter.Value.ContainsKey("MovieType"));
                var movieTypeValue = parameter.Value["MovieType"];
                Assert.IsInstanceOf <string>(movieTypeValue);
                Assert.AreEqual("Action", movieTypeValue);
            }
            finally
            {
                AutoMappingUtils.Reset();
            }
        }
        public void Should_Not_Throw_Exception_When_Multiple_Same_Type_CustomTypeConverters_Found()
        {
            AutoMappingUtils.RegisterConverter((DateTimeOffset from) => new WrappedDateTimeOffset(from));

            var personWithWrappedDateOfBirth = new PersonWithWrappedDateOfBirth
            {
                DateOfBirth = new WrappedDateTimeOffset(
                    new DateTimeOffset(1971, 3, 23, 4, 30, 0, TimeSpan.Zero))
            };

            var personWithDoB = personWithWrappedDateOfBirth.ConvertTo <PersonWithDateOfBirth>();

            // Object returned but mapping failed
            Assert.That(personWithDoB.FirstName, Is.Null);
            Assert.That(personWithDoB.LastName, Is.Null);
            Assert.That(personWithDoB.DateOfBirth, Is.EqualTo(DateTimeOffset.MinValue));

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 13
0
        public void Can_Convert_POCO_collections_with_custom_Converter()
        {
            AutoMapping.RegisterConverter((User from) => {
                var to        = from.ConvertTo <UserDto>(skipConverters: true); // avoid infinite recursion
                to.FirstName += "!";
                to.LastName  += "!";
                return(to);
            });
            AutoMapping.RegisterConverter((Car from) => $"{from.Name} ({from.Age})");

            var user = new User {
                FirstName = "John",
                LastName  = "Doe",
                Car       = new Car {
                    Name = "BMW X6", Age = 3
                }
            };
            var users = new UsersData {
                Id        = 1,
                User      = user,
                UsersList = { user },
                UsersMap  = { { 1, user } }
            };

            var dtoUsers = users.ConvertTo <UsersDto>();

            Assert.That(dtoUsers.Id, Is.EqualTo(users.Id));

            void AssertUser(UserDto userDto)
            {
                Assert.That(userDto.FirstName, Is.EqualTo(user.FirstName + "!"));
                Assert.That(userDto.LastName, Is.EqualTo(user.LastName + "!"));
                Assert.That(userDto.Car, Is.EqualTo($"{user.Car.Name} ({user.Car.Age})"));
            }

            AssertUser(user.ConvertTo <UserDto>());
            AssertUser(dtoUsers.User);
            AssertUser(dtoUsers.UsersList[0]);
            AssertUser(dtoUsers.UsersMap[1]);

            AutoMappingUtils.Reset();
        }
Ejemplo n.º 14
0
 public void OneTimeTearDown()
 {
     AutoMappingUtils.Reset();
 }