public void TestListHandler()
        {
            var generator = new TestFactoryBuilder()
                            .SetSql("/*% var ids = values; */ WHERE Id IN /*@ ids */(1)")
                            .Config(config =>
            {
                config.ConfigureTypeHandlers(c =>
                {
                    c[typeof(string)] = new DummyTypeHandler();
                });
            })
                            .Build();

            var accessor = generator.Create <IListHandlerAccessor>();

            var con = new MockDbConnection();

            con.SetupCommand(cmd =>
            {
                cmd.Executing = c =>
                {
                    Assert.Equal(5, c.Parameters[0].Size);
                    Assert.Equal(5, c.Parameters[1].Size);
                };
                cmd.SetupResult(0);
            });

            accessor.Execute(con, new List <string>(new[] { "1", "2" }));

            var controller = (IEngineController)generator.Engine;

            Assert.Equal(1, controller.Diagnostics.DynamicSetupCacheCount);
            controller.ClearDynamicSetupCache();
            Assert.Equal(0, controller.Diagnostics.DynamicSetupCacheCount);
        }
Esempio n. 2
0
        public void TestDbType()
        {
            var generator = new TestFactoryBuilder()
                            .SetSql("/*@ value */")
                            .Config(config =>
            {
                config.ConfigureTypeHandlers(handlers =>
                {
                    handlers[typeof(string)] = new DummyTypeHandler();
                });
            })
                            .Build();

            var accessor = generator.Create <IDbTypeAccessor>();

            var con = new MockDbConnection();

            // IN
            con.SetupCommand(cmd =>
            {
                cmd.Executing = c => Assert.Equal(5, c.Parameters[0].Size);
                cmd.SetupResult(0);
            });

            accessor.ExecuteIn(con, "x");

            con.SetupCommand(cmd =>
            {
                cmd.Executing = c => Assert.Equal(DBNull.Value, c.Parameters[0].Value);
                cmd.SetupResult(0);
            });

            accessor.ExecuteIn(con, null);

            // IN/OUT
            con.SetupCommand(cmd =>
            {
                cmd.Executing = c => Assert.Equal(5, c.Parameters[0].Size);
                cmd.SetupResult(0);
            });

            var value = "x";

            accessor.ExecuteInOut(con, ref value);

            con.SetupCommand(cmd =>
            {
                cmd.Executing = c => Assert.Equal(DBNull.Value, c.Parameters[0].Value);
                cmd.SetupResult(0);
            });

            value = null;
            accessor.ExecuteInOut(con, ref value);

            // OUT
            con.SetupCommand(cmd =>
            {
                cmd.Executing = c => Assert.Equal(5, c.Parameters[0].Size);
                cmd.SetupResult(0);
            });

            accessor.ExecuteOut(con, out _);

            // Array
            con.SetupCommand(cmd =>
            {
                cmd.Executing = c =>
                {
                    Assert.Equal(5, c.Parameters[0].Size);
                    Assert.Equal(DBNull.Value, c.Parameters[1].Value);
                };
                cmd.SetupResult(0);
            });

            accessor.ExecuteArray(con, new[] { string.Empty, null });

            con.SetupCommand(cmd =>
            {
                cmd.Executing = c =>
                {
                    Assert.Equal(0, c.Parameters.Count);
                };
                cmd.SetupResult(0);
            });

            accessor.ExecuteArray(con, null);

            // List
            con.SetupCommand(cmd =>
            {
                cmd.Executing = c =>
                {
                    Assert.Equal(5, c.Parameters[0].Size);
                    Assert.Equal(DBNull.Value, c.Parameters[1].Value);
                };
                cmd.SetupResult(0);
            });

            accessor.ExecuteList(con, new List <string> {
                string.Empty, null
            });

            con.SetupCommand(cmd =>
            {
                cmd.Executing = c =>
                {
                    Assert.Equal(0, c.Parameters.Count);
                };
                cmd.SetupResult(0);
            });

            accessor.ExecuteList(con, null);
        }