1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Update postgres/client unit test to conform to current patterns.

This includes adding test titles and using constants for query and error values that repeat.
This commit is contained in:
David Steele 2022-04-18 11:53:31 -04:00
parent bc5f6fac34
commit 9751ddc4f8

View File

@ -28,9 +28,10 @@ testRun(void)
// *****************************************************************************************************************************
if (testBegin("pgClient"))
{
// Create and start the test database
// -------------------------------------------------------------------------------------------------------------------------
#ifdef HARNESS_PQ_REAL
TEST_TITLE("create/start test database");
#define TEST_PG_VERSION "14"
HRN_SYSTEM("sudo pg_createcluster " TEST_PG_VERSION " test");
@ -38,8 +39,9 @@ testRun(void)
HRN_SYSTEM("cd /var/lib/postgresql && sudo -u postgres psql -c 'create user " TEST_USER " superuser'");
#endif
// Test connection error
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("connection error");
#define TEST_PQ_ERROR \
"connection to server on socket \"/var/run/postgresql/.s.PGSQL.5433\" failed: No such file or directory\n" \
"\tIs the server running locally and accepting connections on that socket?"
@ -71,15 +73,19 @@ testRun(void)
#undef TEST_PQ_ERROR
// Test send error
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("send error");
#define TEST_PQ_ERROR "another command is already in progress"
#define TEST_QUERY "select bogus from pg_class"
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_CONNECTDB, .param = "[\"dbname='postgres' port=5432\"]"},
{.function = HRNPQ_STATUS, .resultInt = CONNECTION_OK},
{.function = HRNPQ_SENDQUERY, .param = "[\"select bogus from pg_class\"]", .resultInt = 0},
{.function = HRNPQ_ERRORMESSAGE, .resultZ = "another command is already in progress\n"},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 0},
{.function = HRNPQ_ERRORMESSAGE, .resultZ = TEST_PQ_ERROR "\n"},
{.function = HRNPQ_FINISH},
{.function = NULL}
});
@ -88,19 +94,20 @@ testRun(void)
TEST_ASSIGN(client, pgClientOpen(pgClientNew(NULL, 5432, STRDEF("postgres"), NULL, 3000)), "new client");
#ifdef HARNESS_PQ_REAL
PQsendQuery(client->connection, "select bogus from pg_class");
PQsendQuery(client->connection, TEST_QUERY);
#endif
const String *query = STRDEF("select bogus from pg_class");
TEST_ERROR(
pgClientQuery(client, query), DbQueryError,
"unable to send query 'select bogus from pg_class': another command is already in progress");
pgClientQuery(client, STRDEF(TEST_QUERY)), DbQueryError, "unable to send query '" TEST_QUERY "': " TEST_PQ_ERROR);
TEST_RESULT_VOID(pgClientFree(client), "free client");
// Connect
#undef TEST_PQ_ERROR
#undef TEST_QUERY
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("connect");
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
@ -115,40 +122,45 @@ testRun(void)
client, pgClientOpen(pgClientNew(STRDEF("/var/run/postgresql"), 5432, STRDEF("postgres"), TEST_USER_STR, 500)),
"new client");
// Invalid query
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("invalid query");
#define TEST_PQ_ERROR \
"ERROR: column \"bogus\" does not exist\n" \
"LINE 1: select bogus from pg_class\n" \
" ^"
#define TEST_QUERY "select bogus from pg_class"
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param = "[\"select bogus from pg_class\"]", .resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
{.function = HRNPQ_ISBUSY},
{.function = HRNPQ_GETRESULT},
{.function = HRNPQ_RESULTSTATUS, .resultInt = PGRES_FATAL_ERROR},
{.function = HRNPQ_RESULTERRORMESSAGE, .resultZ =
"ERROR: column \"bogus\" does not exist\n"
"LINE 1: select bogus from pg_class\n"
" ^ \n"},
{.function = HRNPQ_RESULTERRORMESSAGE, .resultZ = TEST_PQ_ERROR " \n"},
{.function = HRNPQ_CLEAR},
{.function = HRNPQ_GETRESULT, .resultNull = true},
{.function = NULL}
});
#endif
query = STRDEF("select bogus from pg_class");
TEST_ERROR(
pgClientQuery(client, query), DbQueryError,
"unable to execute query 'select bogus from pg_class': ERROR: column \"bogus\" does not exist\n"
"LINE 1: select bogus from pg_class\n"
" ^");
pgClientQuery(client, STRDEF(TEST_QUERY)), DbQueryError, "unable to execute query '" TEST_QUERY "': " TEST_PQ_ERROR);
#undef TEST_PQ_ERROR
#undef TEST_QUERY
// Timeout query
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("query timeout");
#define TEST_QUERY "select pg_sleep(3000)"
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param = "[\"select pg_sleep(3000)\"]", .resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT, .sleep = 600},
{.function = HRNPQ_ISBUSY, .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
@ -163,38 +175,46 @@ testRun(void)
});
#endif
query = STRDEF("select pg_sleep(3000)");
TEST_ERROR(pgClientQuery(client, STRDEF(TEST_QUERY)), DbQueryError, "query '" TEST_QUERY "' timed out after 500ms");
TEST_ERROR(pgClientQuery(client, query), DbQueryError, "query 'select pg_sleep(3000)' timed out after 500ms");
#undef TEST_QUERY
// Cancel error (can only be run with the scripted tests
// -------------------------------------------------------------------------------------------------------------------------
#ifndef HARNESS_PQ_REAL
TEST_TITLE("cancel error");
#define TEST_PQ_ERROR "test error"
#define TEST_QUERY "select pg_sleep(3000)"
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param = "[\"select pg_sleep(3000)\"]", .resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT, .sleep = 600},
{.function = HRNPQ_ISBUSY, .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
{.function = HRNPQ_ISBUSY, .resultInt = 1},
{.function = HRNPQ_GETCANCEL},
{.function = HRNPQ_CANCEL, .resultInt = 0, .resultZ = "test error"},
{.function = HRNPQ_CANCEL, .resultInt = 0, .resultZ = TEST_PQ_ERROR},
{.function = HRNPQ_FREECANCEL},
{.function = NULL}
});
query = STRDEF("select pg_sleep(3000)");
TEST_ERROR(
pgClientQuery(client, STRDEF(TEST_QUERY)), DbQueryError, "unable to cancel query '" TEST_QUERY "': " TEST_PQ_ERROR);
TEST_ERROR(pgClientQuery(client, query), DbQueryError, "unable to cancel query 'select pg_sleep(3000)': test error");
#undef TEST_PQ_ERROR
#undef TEST_QUERY
#endif
// -------------------------------------------------------------------------------------------------------------------------
#ifndef HARNESS_PQ_REAL
TEST_TITLE("PQgetCancel() returns NULL");
#define TEST_QUERY "select 1"
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param = "[\"select 1\"]", .resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT, .sleep = 600},
{.function = HRNPQ_ISBUSY, .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
@ -204,15 +224,21 @@ testRun(void)
});
TEST_ERROR(
pgClientQuery(client, STRDEF("select 1")), DbQueryError, "unable to cancel query 'select 1': connection was lost");
pgClientQuery(client, STRDEF(TEST_QUERY)), DbQueryError,
"unable to cancel query '" TEST_QUERY "': connection was lost");
#undef TEST_QUERY
#endif
// Execute do block and raise notice
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("execute do block and raise notice");
#define TEST_QUERY "do $$ begin raise notice 'mememe'; end $$"
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param = "[\"do $$ begin raise notice 'mememe'; end $$\"]", .resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
{.function = HRNPQ_ISBUSY},
{.function = HRNPQ_GETRESULT},
@ -223,16 +249,19 @@ testRun(void)
});
#endif
query = STRDEF("do $$ begin raise notice 'mememe'; end $$");
TEST_RESULT_PTR(pgClientQuery(client, STRDEF(TEST_QUERY)), NULL, "execute do block");
TEST_RESULT_PTR(pgClientQuery(client, query), NULL, "execute do block");
#undef TEST_QUERY
// Unsupported type
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("unsupported type");
#define TEST_QUERY "select clock_timestamp()"
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param = "[\"select clock_timestamp()\"]", .resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
{.function = HRNPQ_ISBUSY},
{.function = HRNPQ_GETRESULT},
@ -247,22 +276,24 @@ testRun(void)
});
#endif
query = STRDEF("select clock_timestamp()");
TEST_ERROR(
pgClientQuery(client, query), FormatError,
"unable to parse type 1184 in column 0 for query 'select clock_timestamp()'");
pgClientQuery(client, STRDEF(TEST_QUERY)), FormatError,
"unable to parse type 1184 in column 0 for query '" TEST_QUERY "'");
#undef TEST_QUERY
// Successful query
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("successful query");
#define TEST_QUERY \
"select oid, case when relname = 'pg_class' then null::text else '' end, relname, relname = 'pg_class'" \
" from pg_class where relname in ('pg_class', 'pg_proc')" \
" order by relname"
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{
{.function = HRNPQ_SENDQUERY, .param =
"[\"select oid, case when relname = 'pg_class' then null::text else '' end, relname, relname = 'pg_class'"
" from pg_class where relname in ('pg_class', 'pg_proc')"
" order by relname\"]",
.resultInt = 1},
{.function = HRNPQ_SENDQUERY, .param = "[\"" TEST_QUERY "\"]", .resultInt = 1},
{.function = HRNPQ_CONSUMEINPUT},
{.function = HRNPQ_ISBUSY},
{.function = HRNPQ_GETRESULT},
@ -293,17 +324,15 @@ testRun(void)
});
#endif
query = STRDEF(
"select oid, case when relname = 'pg_class' then null::text else '' end, relname, relname = 'pg_class'"
" from pg_class where relname in ('pg_class', 'pg_proc')"
" order by relname");
TEST_RESULT_STR_Z(
jsonFromVar(varNewVarLst(pgClientQuery(client, query))),
jsonFromVar(varNewVarLst(pgClientQuery(client, STRDEF(TEST_QUERY)))),
"[[1259,null,\"pg_class\",true],[1255,\"\",\"pg_proc\",false]]", "simple query");
// Close connection
#undef TEST_QUERY
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("close connection");
#ifndef HARNESS_PQ_REAL
harnessPqScriptSet((HarnessPq [])
{