如何对PostgreSQL C语言函数进行单元测试

问题描述 投票:1回答:1

我正在用C开发PostgreSQL的用户定义函数。该函数将用作用户定义聚合的一部分。我正在关注User-defined AggregatesC-Language Funtions的文档。我知道PostgreSQL安装提供的扩展构建基础结构,记录了here,它还提供了一种测试扩展的方法。但是,为了简化PostgreSQL扩展的开发,我想知道是否有任何方法可以使用常规的C单元测试技术来测试用户功能。

我正在尝试使用CMocka进行单元测试。 PG_FUNCTION_ARGSPG_FUNCTION_INFO_V1fmgr.h宏很复杂,这些宏是与PostgreSQL连接的C函数所需要的。这些宏倾向于抽象一些参数处理,因此混淆了这个功能。因此,我在单元测试中调用PostgreSQL用户函数时遇到困难。我遇到的错误与预期的参数类型有关。我不知道如何通过我的函数的宏手动构建参数列表。以下是我尝试运行单元测试时得到的GCC错误。

    gcc -fprofile-arcs -ftest-coverage -I '/usr/include/postgresql/10/server' ./tests/test_max_pos_min_neg.c -o ./build/tests/test_max_pos_min_neg
./tests/test_max_pos_min_neg.c: In function ‘test_get_max_pos_min_neg’:
./tests/test_max_pos_min_neg.c:21:25: warning: passing argument 1 of ‘get_max_pos_min_neg’ from incompatible pointer type [-Wincompatible-pointer-types]
     get_max_pos_min_neg((float4 *) init_cond, 10.0, -2.5, 7.5);
                         ^
In file included from ./tests/test_max_pos_min_neg.c:6:0:
./tests/../src/max_pos_min_neg.h:8:7: note: expected ‘FunctionCallInfo {aka struct FunctionCallInfoData *}’ but argument is of type ‘float4 * {aka float *}’
 Datum get_max_pos_min_neg(PG_FUNCTION_ARGS);
       ^~~~~~~~~~~~~~~~~~~
./tests/test_max_pos_min_neg.c:21:5: error: too many arguments to function ‘get_max_pos_min_neg’
     get_max_pos_min_neg((float4 *) init_cond, 10.0, -2.5, 7.5);
     ^~~~~~~~~~~~~~~~~~~
In file included from ./tests/test_max_pos_min_neg.c:6:0:
./tests/../src/max_pos_min_neg.h:8:7: note: declared here
 Datum get_max_pos_min_neg(PG_FUNCTION_ARGS);
       ^~~~~~~~~~~~~~~~~~~
./tests/test_max_pos_min_neg.c:24:5: warning: implicit declaration of function ‘assert_float_equal’; did you mean ‘assert_int_equal’? [-Wimplicit-function-declaration]
     assert_float_equal(init_cond[0], 10.0, EPSILON);
     ^~~~~~~~~~~~~~~~~~
     assert_int_equal
Makefile:59: recipe for target 'build/tests/test_max_pos_min_neg' failed
make: *** [build/tests/test_max_pos_min_neg] Error 1

这是我的源代码:

#include "postgres.h"
#include "fmgr.h"
#include "utils/array.h"

PG_MODULE_MAGIC;

Datum get_max_pos_min_neg(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(get_max_pos_min_neg);

// Computes the maximum positive and mininum negative sentiment scores.
Datum
get_max_pos_min_neg(PG_FUNCTION_ARGS)
{
    ArrayType *state_array;
    float4 *state;
    float4 pos, neg, score;

    state_array = PG_GETARG_ARRAYTYPE_P(0);
    pos = PG_GETARG_FLOAT4(1);
    neg = PG_GETARG_FLOAT4(2);
    score = PG_GETARG_FLOAT4(3);

    state = (float4 *) ARR_DATA_PTR(state_array);

    if (state[2] < score)
    {
        state[0] = pos;
        state[1] = neg;
        state[2] = score;
    }
    if (score < state[5])
    {
        state[3] = pos;
        state[4] = neg;
        state[5] = score;
    }
    PG_RETURN_ARRAYTYPE_P(state);
}

我的头文件:

#include "postgres.h"
#include "fmgr.h"
#include "utils/array.h"

Datum get_max_pos_min_neg(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(get_max_pos_min_neg);

我的单元测试:

#include "../src/max_pos_min_neg.h"
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include "cmocka.h"

const float4 EPSILON = 0.001;

void 
test_get_max_pos_min_neg(void **state)
{
    (void) state; /* unused */
    // This is the initial condition used by the SQL stored procedure
    float4 init_cond[] = {0, 0, -1, 0, 0, 100};

    get_max_pos_min_neg(init_cond, 10.0, -2.5, 7.5);

    // init_cond should now be {10.0, -2.5, 7.5, 10.0, -2.5, 7.5}
    assert_float_equal(init_cond[0], 10.0, EPSILON);
    assert_float_equal(init_cond[1], -2.5, EPSILON);
    assert_float_equal(init_cond[2], 7.5, EPSILON);
    assert_float_equal(init_cond[3], 10.0, EPSILON);
    assert_float_equal(init_cond[4], -2.5, EPSILON);
    assert_float_equal(init_cond[5], 7.5, EPSILON);
}

const struct CMUnitTest tests[] = { 
    cmocka_unit_test(test_get_max_pos_min_neg)
};

int 
main(void)
{
    return cmocka_run_group_tests(tests, NULL, NULL);
}

您可以提供有关如何在PostgreSQL之外对PostgreSQL C语言函数进行单元测试的任何帮助。谢谢!

c postgresql unit-testing
1个回答
2
投票

正如错误告诉你的那样,参数实际上必须是FunctionCallInfo(隐藏在宏后面)。

要测试PostgreSQL之外的函数,您必须为PostgreSQL服务器(函数调用接口,以及您在代码中使用的任何服务器函数)构建模型。

这不仅非常困难,而且您必须使用每个新的服务器版本更新您的模型(内部API往往会偶尔更改),但您也遇到了在测试中引入一些新鲜错误的不可忽视的风险代码,这将使这种测试的价值成为可疑的。

我的建议不是走那条路。

PostgreSQL支持单用户模式(postgres --single -D /data/directory dbname),您可以将其嵌入到测试框架中。您可以使用管道与服务器通信,一旦关闭PostgreSQL的标准输入,服务器就会关闭。

© www.soinside.com 2019 - 2024. All rights reserved.