|
| 1 | +#ifndef SQLGEN_BUILD_DRY_TESTS_ONLY |
| 2 | + |
| 3 | +#include <gtest/gtest.h> |
| 4 | + |
| 5 | +#include <optional> |
| 6 | +#include <sqlgen/postgres.hpp> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace test_execute_params { |
| 10 | + |
| 11 | +TEST(postgres, execute_with_string_params) { |
| 12 | + const auto credentials = sqlgen::postgres::Credentials{ |
| 13 | + .user = "postgres", |
| 14 | + .password = "password", |
| 15 | + .host = "localhost", |
| 16 | + .dbname = "postgres"}; |
| 17 | + auto conn_result = sqlgen::postgres::connect(credentials); |
| 18 | + ASSERT_TRUE(conn_result); |
| 19 | + auto conn = conn_result.value(); |
| 20 | + |
| 21 | + // Create a test table |
| 22 | + auto create_result = conn->execute(R"( |
| 23 | + CREATE TABLE IF NOT EXISTS test_execute_params ( |
| 24 | + id SERIAL PRIMARY KEY, |
| 25 | + name TEXT, |
| 26 | + value INTEGER |
| 27 | + ); |
| 28 | + )"); |
| 29 | + ASSERT_TRUE(create_result) << create_result.error().what(); |
| 30 | + |
| 31 | + // Clean up any existing data |
| 32 | + auto truncate_result = conn->execute("TRUNCATE test_execute_params;"); |
| 33 | + ASSERT_TRUE(truncate_result) << truncate_result.error().what(); |
| 34 | + |
| 35 | + // Insert using parameterized execute |
| 36 | + auto insert_result = conn->execute( |
| 37 | + "INSERT INTO test_execute_params (name, value) VALUES ($1, $2);", |
| 38 | + std::string("test_name"), 42); |
| 39 | + ASSERT_TRUE(insert_result) << insert_result.error().what(); |
| 40 | + |
| 41 | + // Clean up |
| 42 | + auto drop_result = conn->execute("DROP TABLE test_execute_params;"); |
| 43 | + ASSERT_TRUE(drop_result) << drop_result.error().what(); |
| 44 | +} |
| 45 | + |
| 46 | +TEST(postgres, execute_with_null_param) { |
| 47 | + const auto credentials = sqlgen::postgres::Credentials{ |
| 48 | + .user = "postgres", |
| 49 | + .password = "password", |
| 50 | + .host = "localhost", |
| 51 | + .dbname = "postgres"}; |
| 52 | + auto conn_result = sqlgen::postgres::connect(credentials); |
| 53 | + ASSERT_TRUE(conn_result); |
| 54 | + auto conn = conn_result.value(); |
| 55 | + |
| 56 | + // Create a test table |
| 57 | + auto create_result = conn->execute(R"( |
| 58 | + CREATE TABLE IF NOT EXISTS test_execute_null ( |
| 59 | + id SERIAL PRIMARY KEY, |
| 60 | + name TEXT |
| 61 | + ); |
| 62 | + )"); |
| 63 | + ASSERT_TRUE(create_result) << create_result.error().what(); |
| 64 | + |
| 65 | + // Insert with null parameter using std::optional |
| 66 | + std::optional<std::string> null_val = std::nullopt; |
| 67 | + auto insert_result = conn->execute( |
| 68 | + "INSERT INTO test_execute_null (name) VALUES ($1);", null_val); |
| 69 | + ASSERT_TRUE(insert_result) << insert_result.error().what(); |
| 70 | + |
| 71 | + // Clean up |
| 72 | + auto drop_result = conn->execute("DROP TABLE test_execute_null;"); |
| 73 | + ASSERT_TRUE(drop_result) << drop_result.error().what(); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(postgres, execute_with_numeric_params) { |
| 77 | + const auto credentials = sqlgen::postgres::Credentials{ |
| 78 | + .user = "postgres", |
| 79 | + .password = "password", |
| 80 | + .host = "localhost", |
| 81 | + .dbname = "postgres"}; |
| 82 | + auto conn_result = sqlgen::postgres::connect(credentials); |
| 83 | + ASSERT_TRUE(conn_result); |
| 84 | + auto conn = conn_result.value(); |
| 85 | + |
| 86 | + // Create a test table |
| 87 | + auto create_result = conn->execute(R"( |
| 88 | + CREATE TABLE IF NOT EXISTS test_execute_numeric ( |
| 89 | + id SERIAL PRIMARY KEY, |
| 90 | + int_val INTEGER, |
| 91 | + float_val DOUBLE PRECISION, |
| 92 | + bool_val BOOLEAN |
| 93 | + ); |
| 94 | + )"); |
| 95 | + ASSERT_TRUE(create_result) << create_result.error().what(); |
| 96 | + |
| 97 | + // Insert with various numeric types |
| 98 | + auto insert_result = conn->execute( |
| 99 | + "INSERT INTO test_execute_numeric (int_val, float_val, bool_val) " |
| 100 | + "VALUES ($1, $2, $3);", |
| 101 | + 123, 3.14159, true); |
| 102 | + ASSERT_TRUE(insert_result) << insert_result.error().what(); |
| 103 | + |
| 104 | + // Clean up |
| 105 | + auto drop_result = conn->execute("DROP TABLE test_execute_numeric;"); |
| 106 | + ASSERT_TRUE(drop_result) << drop_result.error().what(); |
| 107 | +} |
| 108 | + |
| 109 | +TEST(postgres, execute_call_function) { |
| 110 | + const auto credentials = sqlgen::postgres::Credentials{ |
| 111 | + .user = "postgres", |
| 112 | + .password = "password", |
| 113 | + .host = "localhost", |
| 114 | + .dbname = "postgres"}; |
| 115 | + auto conn_result = sqlgen::postgres::connect(credentials); |
| 116 | + ASSERT_TRUE(conn_result); |
| 117 | + auto conn = conn_result.value(); |
| 118 | + |
| 119 | + // Create a simple test function |
| 120 | + auto create_fn_result = conn->execute(R"( |
| 121 | + CREATE OR REPLACE FUNCTION test_add(a INTEGER, b INTEGER) |
| 122 | + RETURNS INTEGER AS $$ |
| 123 | + BEGIN |
| 124 | + RETURN a + b; |
| 125 | + END; |
| 126 | + $$ LANGUAGE plpgsql; |
| 127 | + )"); |
| 128 | + ASSERT_TRUE(create_fn_result) << create_fn_result.error().what(); |
| 129 | + |
| 130 | + // Call the function with parameters |
| 131 | + auto call_result = conn->execute("SELECT test_add($1, $2);", 5, 3); |
| 132 | + ASSERT_TRUE(call_result) << call_result.error().what(); |
| 133 | + |
| 134 | + // Clean up |
| 135 | + auto drop_fn_result = conn->execute("DROP FUNCTION test_add;"); |
| 136 | + ASSERT_TRUE(drop_fn_result) << drop_fn_result.error().what(); |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace test_execute_params |
| 140 | + |
| 141 | +#endif |
0 commit comments