Beispiel #1
0
        public void should_ignore_field_if_column_not_exists_and_optional_is_set()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(1);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name2");
            Mock
            .Arrange(() => reader.GetOrdinal("name2"))
            .Returns(1);
            Mock
            .Arrange(() => reader.GetString(0))
            .Throws <ArgumentException>();

            var mapper =
                new DataReaderMapperBuilder <IgnoreType>(
                    "should_ignore_field_if_column_not_exists_and_optional_is_set")
                .Map(x => x.Name, "nohros", true)
                .Build();

            try {
                IgnoreType type = mapper.Map(reader);
                Assert.That(type.Name, Is.Null);
            } catch {
                Assert.Fail("Should not throws any exception");
            }
        }
Beispiel #2
0
        public void should_apply_transformation_before_post_action()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(1);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");

            var mapper =
                new DataReaderMapperBuilder <SimpleType>(
                    "should_apply_transformation_before_post_action")
                .Map(x => x.Name, "name")
                .Transform((r, t) => t.Name += "_1")
                .Transform((r, t) => t.Name += "_2")
                .Build();

            SimpleType type = mapper.Map(reader, t => t.Name += "_3");

            Assert.That(type.Name, Is.EqualTo("nohros_1_2_3"));
        }
Beispiel #3
0
        public void should_not_map_ignored_property()
        {
            var reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(2);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetName(1))
            .Returns("location");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");

            var mapper =
                new DataReaderMapperBuilder <IgnoreType>(
                    "should_not_map_ignored_property")
                .AutoMap()
                .Ignore("name")
                .Build();

            Assert.That(() => mapper.Map(reader).Name, Is.Null);
        }
Beispiel #4
0
        should_not_ignore_field_if_column_not_exists_and_optional_is_not_set()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(1);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name2");
            Mock
            .Arrange(() => reader.GetOrdinal("name2"))
            .Returns(1);
            Mock
            .Arrange(() => reader.GetString(0))
            .Throws <ArgumentException>();

            var mapper =
                new DataReaderMapperBuilder <IgnoreType>(
                    "should_not_ignore_field_if_column_not_exists_and_optional_is_not_set")
                .Map(x => x.Name, "nohros")
                .Build();

            Assert.That(() => mapper.Map(reader).Name, Throws.InstanceOf <Exception>());
        }
Beispiel #5
0
        public void should_map_interfaces_when_no_factory_supplied()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(1);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");
            var mapper =
                new DataReaderMapperBuilder <ISimpleType>(
                    "should_map_interfaces_when_no_factory_supplied")
                .Map(x => x.Name, "name")
                .Build();
            ISimpleType obj = mapper.Map(reader);

            Assert.That(obj.Name, Is.EqualTo("nohros"));
        }
Beispiel #6
0
        public void should_map_field_if_column_exists_and_optional_is_set()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(1);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");

            var mapper =
                new DataReaderMapperBuilder <IgnoreType>(
                    "should_map_field_if_column_exists_and_optional_is_set")
                .Map(x => x.Name, "name", true)
                .Build();

            IgnoreType type = mapper.Map(reader);

            Assert.That(type.Name, Is.EqualTo("nohros"));
        }
Beispiel #7
0
        public void ShouldMapInternalClass()
        {
            var builder = new SqlConnectionStringBuilder();

            builder.DataSource = "192.168.203.207";
            builder.UserID     = "nohros";
            builder.Password   = "******";

            using (var conn = new SqlConnection(builder.ToString()))
                using (
                    var cmd =
                        new SqlCommand(
                            "select 15 as Id, 'post' as text, Getdate() as CreationDate, 9 as Counter1, 10 as Counter2",
                            conn)) {
                    conn.Open();
                    using (var reader = cmd.ExecuteReader()) {
                        var mapper = new DataReaderMapperBuilder <PostPoco>()
                                     .Map <int, TimeSpan>(poco => poco.Counter1, "counter1",
                                                          i => FromSeconds(i))
                                     .Build();
                        //Assert.Pass("Value:" + mapper.Map(reader).Counter1);
                        mapper.Map(reader);
                    }
                }
            Dynamics_.AssemblyBuilder.Save("dynamics.dll");
        }
Beispiel #8
0
        public void ShoudMapToConstValues()
        {
            var reader = Mock.Create <IDataReader>();
            var mapper = new DataReaderMapperBuilder <IgnoreMapperTest>()
                         .Map("name", new ConstStringMapType("myname"))
                         .Build();

            Assert.That(mapper.Map(reader).Name, Is.EqualTo("myname"));
        }
Beispiel #9
0
        public void shoud_map_to_a_constant()
        {
            IDataReader reader = GetDataReader();
            var         mapper =
                new DataReaderMapperBuilder <IgnoreType>("shoud_map_to_a_constant")
                .Map("name", TypeMaps.String("myname"))
                .Build();

            Assert.That(mapper.Map(reader).Name, Is.EqualTo("myname"));
        }
Beispiel #10
0
        public void should_map_composite_objects()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(2);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetName(1))
            .Returns("location");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetOrdinal("location"))
            .Returns(1);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");
            Mock
            .Arrange(() => reader.GetString(1))
            .Returns("127.0.0.1");

            var simple =
                new DataReaderMapperBuilder <ISimpleType>(
                    "should_map_composite_objects")
                .Map(x => x.Name, "name")
                .Build();

            var ignore =
                new DataReaderMapperBuilder <IgnoreType>(
                    "should_map_composite_objects")
                .Map(x => x.Name, "name")
                .Map(x => x.Location, "location")
                .Build();

            var composite =
                new DataReaderMapperBuilder <ICompositeType>(
                    "should_map_composite_objects")
                .Transform((r, c) => simple.MapCurrent(r, (ISimpleType)c))
                .Transform((r, c) => c.IgnoreType = ignore.MapCurrent(r))
                .Build();

            ICompositeType type = composite.Map(reader);

            Assert.That(type.IgnoreType, Is.Not.Null);
            Assert.That(type.Name, Is.EqualTo("nohros"));
            //Dynamics.Dynamics_.AssemblyBuilder.Save("test.dll");
        }
Beispiel #11
0
        public void should_map_all_implemented_interfaces()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(3);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetName(1))
            .Returns("name1");
            Mock
            .Arrange(() => reader.GetName(2))
            .Returns("name2");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetOrdinal("name1"))
            .Returns(1);
            Mock
            .Arrange(() => reader.GetOrdinal("name2"))
            .Returns(2);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");
            Mock
            .Arrange(() => reader.GetString(1))
            .Returns("nohros1");
            Mock
            .Arrange(() => reader.GetString(2))
            .Returns("nohros2");

            var mapper =
                new DataReaderMapperBuilder <IDerivedType2>(
                    "should_map_all_implemented_interfaces")
                .Map(x => x.Name, "name")
                .Map(x => x.Name2, "name1")
                .Map(x => x.Name3, "name2")
                .Build();

            IDerivedType2 obj = mapper.Map(reader);

            Assert.That(obj.Name, Is.EqualTo("nohros"));
            Assert.That(obj.Name2, Is.EqualTo("nohros1"));
            Assert.That(obj.Name3, Is.EqualTo("nohros2"));
            //Dynamics.Dynamics_.AssemblyBuilder.Save("test.dll");
        }
Beispiel #12
0
        public void ShouldBuildNestedDynamicType()
        {
            var reader = Mock.Create <IDataReader>();

            Mock
            .Arrange(() => reader.GetOrdinal(Arg.AnyString))
            .Returns(0);
            var mapper = new DataReaderMapperBuilder <NestedMapperTest>()
                         .Map("usuario_nome", "name")
                         .Build();
            var inner = mapper.Map(reader).Nested;

            Assert.That(inner, Is.AssignableTo <DataReaderMapper <MapperTest> >());
            Assert.That(inner, Is.AssignableTo <MapperTest>());
            Assert.That(mapper, Is.AssignableTo <DataReaderMapper <NestedMapperTest> >());
            Assert.That(mapper, Is.AssignableTo <NestedMapperTest>());
        }
Beispiel #13
0
        public void should_map_partial_objects_through_transformation()
        {
            IDataReader reader = GetDataReader();

            Mock
            .Arrange(() => reader.FieldCount)
            .Returns(2);
            Mock
            .Arrange(() => reader.GetName(0))
            .Returns("name");
            Mock
            .Arrange(() => reader.GetName(1))
            .Returns("location");
            Mock
            .Arrange(() => reader.GetOrdinal("name"))
            .Returns(0);
            Mock
            .Arrange(() => reader.GetOrdinal("location"))
            .Returns(1);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("nohros");
            Mock
            .Arrange(() => reader.GetString(1))
            .Returns("127.0.0.1");

            var transformer =
                new DataReaderMapperBuilder <ISimpleType>(
                    "should_map_partial_objects_through_transformation")
                .Map(x => x.Name, "location")
                .Build();

            var mapper =
                new DataReaderMapperBuilder <IDerivedType>(
                    "should_map_partial_objects_through_transformation")
                .Map(x => x.Name, "name")
                .Transform(transformer.MapCurrent)
                .Build();

            IDerivedType type = mapper.Map(reader);

            Assert.That(type.Name, Is.EqualTo("nohros"));
            Assert.That(type.Name2, Is.EqualTo("127.0.0.1"));
        }
Beispiel #14
0
        public void ShouldMapCustomColumnToProperty()
        {
            var builder = new SqlConnectionStringBuilder();

            builder.DataSource = "192.168.203.207";
            builder.UserID     = "nohros";
            builder.Password   = "******";

            using (var conn = new SqlConnection(builder.ToString()))
                using (var cmd = new SqlCommand("select 'nohros' as usuario_nome", conn)) {
                    conn.Open();
                    using (var reader = cmd.ExecuteReader()) {
                        var mapper = new DataReaderMapperBuilder <MapMapperTest>()
                                     .Map("name", "usuario_nome")
                                     .Build();
                        Assert.That(mapper.Map(reader).Name, Is.EqualTo("nohros"));
                    }
                }
        }
Beispiel #15
0
        public void ShouldIgnoreProperty()
        {
            var reader = Mock.Create <IDataReader>();

            Mock
            .Arrange(() => reader.Read())
            .Returns(true);
            Mock
            .Arrange(() => reader.GetString(0))
            .Returns("name-value");

            var mapper =
                new DataReaderMapperBuilder <IgnoreMapperTest>("ShouldIgnoreProperty")
                .AutoMap()
                .Ignore("name")
                .Build();

            Assert.That(() => mapper.Map(reader).Name, Is.Null);
            //Dynamics_.AssemblyBuilder.Save("nohros.tests.dll");
        }
Beispiel #16
0
        public void ShouldMapArrayOfKeyValuePairs()
        {
            var builder = new SqlConnectionStringBuilder();

            builder.DataSource = "itaucaj";
            builder.UserID     = "nohros";
            builder.Password   = "******";

            using (var conn = new SqlConnection(builder.ToString()))
                using (var cmd = new SqlCommand("exec fila_get_by_id @fila_id = 1", conn)) {
                    conn.Open();
                    using (var reader = cmd.ExecuteReader()) {
                        var mapper = new DataReaderMapperBuilder <KeyedMapperTest>()
                                     .Map(
                            new KeyValuePair <string, string>[] {
                            new KeyValuePair <string, string>("name", "usuario_nome")
                        })
                                     .Build();
                        Assert.That(mapper.Map(reader).Name, Is.EqualTo("nohros"));
                    }
                }
        }
Beispiel #17
0
        public void should_be_thread_safe()
        {
            IDataReader reader = GetDataReader();

            var sync = new CountdownEvent(2);

            Action method = () => {
                var mapper =
                    new DataReaderMapperBuilder <IgnoreType>("shoud_map_to_a_constant")
                    .Map("name", TypeMaps.String("myname"))
                    .Build();
                sync.Signal();
                Assert.That(mapper.Map(reader).Name, Is.EqualTo("myname"));
            };

            Action parallel = () => {
                ThreadPool.QueueUserWorkItem(state => method());
                ThreadPool.QueueUserWorkItem(state => method());
            };

            Assert.That(() => parallel(), Throws.Nothing);
            sync.Wait();
        }