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

标签 c postgresql unit-testing

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

我正在尝试使用 CMocka 进行单元测试。 fmgr.h 中的 PG_FUNCTION_ARGSPG_FUNCTION_INFO_V1 宏使 C 函数变得复杂与 PostgreSQL 接口(interface)。这些宏倾向于抽象一些参数处理,因此混淆了这个功能。结果,我在单元测试中调用我的 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 语言函数进行单元测试的任何帮助,我们将不胜感激。谢谢!

最佳答案

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

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

这不仅会非常困难,而且您必须使用每个新服务器版本更新您的模型(内部 API 往往会偶尔更改),而且您还冒着引入一些新错误的不可忽视的风险到您的测试代码中,这会使此类测试的值(value)受到质疑。

我的建议是不要走那条路。

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

关于c - 如何对 PostgreSQL C 语言函数进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55480318/

相关文章:

c - 受限3人体模拟无法正常工作

postgresql - 如何在 psql 文件中嵌入多行 shell 命令

android - Android 单元测试框架

python - 如何使用 SQLAlchemy + PostgreSQL 删除自定义类型?

unit-testing - 如何在向 API 发送 http 请求时进行模拟

.net - 使用 ThreadStatic 而不是 DI 容器有什么问题

C 自定义二分搜索不遵循元素大小倍数规则并崩溃

c - 鼠标光标位图

c - 确定 utf-8 字符的宽度(以字节为单位)

postgresql - PostgreSQL : Return a trigger error (raise exception) in JSON format