相关疑难解决方法(0)

如何在 PostgreSQL 中为单元测试伪造 inet_client_addr()?

我有一个简单的存储过程,其返回值取决于inet_client_addr(). inet_client_addr()在测试我的存储过程时,如何为了单元测试的目的而覆盖?

到目前为止,我想出的唯一解决方案是围绕inet_client_addr()以下内容创建一个包装函数:

CREATE FUNCTION my_inet_client_addr() RETURNS INET AS $$
    SELECT inet_client_addr();
$$ LANGUAGE sql;
Run Code Online (Sandbox Code Playgroud)

然后在我的函数中使用它:

CREATE local_connection() RETURNS BOOLEAN AS $$
    SELECT my_inet_client_addr() = '127.0.0.1';
$$ LANGUAGE sql;
Run Code Online (Sandbox Code Playgroud)

然后在我的单元测试中,我可以重新定义my_inet_client_addr()

BEGIN;
SELECT PLAN(2);
REPLACE FUNCTION my_inet_client_addr() RETURNS INET AS $$
    SELECT '127.0.0.1'::INET;
$$ LANGUAGE sql;

is(local_connection(),TRUE,'Connection from 127.0.0.1 is local');

REPLACE FUNCTION my_inet_client_addr() RETURNS INET AS $$
    SELECT '192.168.1.1'::INET;
$$ LANGUAGE sql;

is(local_connection(),FALSE,'Connection from 192.168.1.1. is not local');

ROLLBACK;
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有包装功能的情况下完成相同的任务my_inet_client_addr()

postgresql stored-procedures unit-test

8
推荐指数
1
解决办法
689
查看次数

日期范围内未使用的索引查询

我有一个未使用现有索引的查询,我不明白为什么。

桌子:

mustang=# \d+ bss.amplifier_saturation
                                               Table "bss.amplifier_saturation"
 Column |           Type           |                             Modifiers                             | Storage | Description 
--------+--------------------------+-------------------------------------------------------------------+---------+-------------
 value  | integer                  | not null                                                          | plain   | 
 target | integer                  | not null                                                          | plain   | 
 start  | timestamp with time zone | not null                                                          | plain   | 
 end    | timestamp with time zone | not null                                                          | plain   | 
 id     | integer                  | not null default nextval('amplifier_saturation_id_seq'::regclass) | plain   | 
 lddate | timestamp with time zone | not null default …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index order-by query-performance

6
推荐指数
1
解决办法
3718
查看次数