コード例 #1
0
        public void TestInsertOperationPostGres()
        {
            var connection = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["postgres"].ConnectionString);

            connection.Open();

            var trans = connection.BeginTransaction();

            using (var conn = connection)
            {
                IScriptBuilder builder = new ScriptPostGresBuilder();

                var cliente = new Cliente()
                {
                    Id = 1, Nome = "Moisés", Ativo = true
                };

                var createTableScript = builder.GetCreateTableCommand <Cliente>();
                builder.Execute(createTableScript, conn, trans);

                var lastId = conn.InsertReturningId <Cliente>(cliente, trans);
                Assert.AreEqual(1, lastId);

                conn.Execute("drop table \"Cliente\"");
                //conn.Execute("drop sequence \"sequence_cliente_id\"");
            }
        }
コード例 #2
0
        public void TestPostGresSelectScript()
        {
            IScriptBuilder builder = new ScriptPostGresBuilder();

            var cliente = new Cliente()
            {
                Id = 1, Nome = "Moisés", Ativo = true
            };

            var sqlUpdate         = builder.GetSelectCommand <Cliente>(cliente);
            var resultadoEsperado = "select \"Id\", \"Nome\", \"Ativo\", \"TotalPedidos\", \"ValorTotalNotasFiscais\", \"Credito\", \"UltimoValorDeCompra\" from \"Cliente\"";

            Assert.AreEqual(resultadoEsperado, sqlUpdate);
        }
コード例 #3
0
        public void TestPostGresUpdateScriptComValorNulo()
        {
            IScriptBuilder builder = new ScriptPostGresBuilder();

            var cliente = new Cliente()
            {
                Id = 1, Nome = "Moisés", Ativo = true, TotalPedidos = 55, ValorTotalNotasFiscais = 1000.55, Credito = 2000.53m, UltimoValorDeCompra = null
            };

            var sqlUpdate         = builder.GetUpdateCommand <Cliente>(cliente);
            var resultadoEsperado = "update \"Cliente\" set \"Nome\"='Moisés', \"Ativo\"=true, \"TotalPedidos\"=55, \"ValorTotalNotasFiscais\"=1000.55, \"Credito\"=2000.53, \"UltimoValorDeCompra\"=null where \"Id\"=1";

            Assert.AreEqual(resultadoEsperado, sqlUpdate);
        }
コード例 #4
0
        public void TestPostGresInsertScript()
        {
            IScriptBuilder builder = new ScriptPostGresBuilder();

            var cliente = new Cliente()
            {
                Id = 1, Nome = "Moisés", Ativo = true, TotalPedidos = 20, ValorTotalNotasFiscais = 2000.95, Credito = 10, UltimoValorDeCompra = 1000.95m
            };

            var sqlDelete         = builder.GetInsertCommand <Cliente>(cliente);
            var resultadoEsperado = "insert into \"Cliente\" (\"Nome\", \"Ativo\", \"TotalPedidos\", \"ValorTotalNotasFiscais\", \"Credito\", \"UltimoValorDeCompra\") values ('Moisés', true, 20, 2000.95, 10, 1000.95)";

            Assert.AreEqual(resultadoEsperado, sqlDelete);
        }
コード例 #5
0
        public void TestPostGresDeleteScript()
        {
            IScriptBuilder builder = new ScriptPostGresBuilder();

            var cliente = new Cliente()
            {
                Id = 1, Nome = "Moisés", Ativo = true
            };

            var sqlDelete         = builder.GetDeleteCommand <Cliente>(cliente, 1);
            var resultadoEsperado = "delete from \"Cliente\" where \"Id\"=1";

            Assert.AreEqual(resultadoEsperado, sqlDelete);
        }
コード例 #6
0
        public void TestCreateTablePostGres()
        {
            IScriptBuilder builder = new ScriptPostGresBuilder();

            var cliente = new Cliente()
            {
                Id = 1, Nome = "Moisés", Ativo = true
            };

            var createTableScript = builder.GetCreateTableCommand <Cliente>();
            var resultadoEsperado =
                $"create sequence \"sequence_cliente_id\";{Environment.NewLine}create table \"Cliente\" (\"Id\" integer primary key not null default nextval ('sequence_cliente_id'), \"Nome\" character varying(255), \"Ativo\" boolean, \"TotalPedidos\" integer null, \"ValorTotalNotasFiscais\" double precision, \"Credito\" numeric(18,6), \"UltimoValorDeCompra\" numeric(18,6));{Environment.NewLine}alter sequence sequence_cliente_id owned by \"Cliente\".\"Id\";";

            Assert.AreEqual(resultadoEsperado, createTableScript);
        }
コード例 #7
0
        public void TestSelectOperationPostGres()
        {
            var connection = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["postgres"].ConnectionString);

            connection.Open();

            using (var scope = new TransactionScope())
            {
                using (var conn = connection)
                {
                    IScriptBuilder builder = new ScriptPostGresBuilder();

                    var cliente = new Cliente()
                    {
                        Id = 1, Nome = "Moisés", Ativo = true
                    };
                    var cliente2 = new Cliente()
                    {
                        Id = 2, Nome = "José", Ativo = true
                    };

                    var createTableScript = builder.GetCreateTableCommand <Cliente>();

                    var insertScript1 = builder.GetInsertCommand <Cliente>(cliente);
                    var insertScript2 = builder.GetInsertCommand <Cliente>(cliente2);

                    builder.Execute(createTableScript, conn);
                    builder.Execute(insertScript1, conn);
                    builder.Execute(insertScript2, conn);

                    var clientes = conn.GetAll <Cliente>();
                    Assert.AreEqual(2, clientes.Count());
                    Assert.AreEqual("Moisés", clientes.ToList()[0].Nome);
                    Assert.AreEqual("José", clientes.ToList()[1].Nome);

                    conn.Execute("drop table \"Cliente\"");
                }
            }
        }