plpgsql测试用例中的SELECT查询未返回数组

时间:2015-12-10 06:04:49

标签: arrays unit-testing aggregate-functions plpgsql sql-function

我正在使用的SELECT查询:

SELECT ARRAY[table_name,pg_size_pretty(table_size)]
FROM (
    SELECT
        table_name,
        pg_table_size(table_name) AS table_size,
        pg_indexes_size(table_name) AS indexes_size,
        pg_total_relation_size(table_name) AS total_size
    FROM (
        SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
        FROM information_schema.tables where table_schema not in ('pg_catalog', 'information_schema')and table_schema not like 'pg_toast%'
    ) AS all_tables
    ORDER BY total_size DESC
) AS have ;

这会将输出返回为: Query Output

但是当我在plpgsql测试用例中使用相同的查询时,它不会返回相同的数组。

测试用例代码:

DROP FUNCTION IF EXISTS unit_tests.example2();

CREATE FUNCTION unit_tests.example2()
RETURNS test_result
AS
$$
DECLARE message test_result;
DECLARE result boolean;
DECLARE have text[][];
DECLARE want text[][];
BEGIN
    want := array[['"unit_tests"."tests"','8192 bytes'],
['"unit_tests"."test_details"','16 KB'],
['"unit_tests"."dependencies"','8192 bytes'],
['"DVDRental"."dvd_genre"','8192 bytes'],
['"DVDRental"."dvd_stock"','0 bytes']];

SELECT ARRAY[table_name,pg_size_pretty(table_size)] INTO have
FROM (
    SELECT
        table_name,
        pg_table_size(table_name) AS table_size,
        pg_indexes_size(table_name) AS indexes_size,
        pg_total_relation_size(table_name) AS total_size
    FROM (
        SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
        FROM information_schema.tables where table_schema not in ('pg_catalog', 'information_schema')and table_schema not like 'pg_toast%'
    ) AS all_tables
    ORDER BY total_size DESC
) AS have ;

    SELECT * FROM assert.is_equal(have, want) INTO message, result;

    --Test failed.
    IF result = false THEN
        RETURN message;
    END IF;

    --Test passed.
    SELECT assert.ok('End of test.') INTO message;  
    RETURN message; 
END
$$
LANGUAGE plpgsql;

--BEGIN TRANSACTION;
SELECT * FROM unit_tests.begin();
--ROLLBACK TRANSACTION;

代码返回结果:

INFO:  Test started from : 2015-12-10 05:50:37.291
INFO:  Running test unit_tests.example2() : 2015-12-10 05:50:37.291
INFO:  Test failed unit_tests.example2() : ASSERT IS_EQUAL FAILED.

Have -> {"\"unit_tests\".\"tests\"","8192 bytes"}
Want -> {{"\"unit_tests\".\"tests\"","8192 bytes"},{"\"unit_tests\".\"test_details\"","16 KB"},{"\"unit_tests\".\"dependencies\"","8192 bytes"},{"\"DVDRental\".\"dvd_genre\"","8192 bytes"},{"\"DVDRental\".\"dvd_stock\"","0 bytes"}}
INFO:  Test completed on : 2015-12-10 05:50:37.322 UTC. 

此处have变量表示返回查询的结果,want变量具有我们期望查询返回的值。但正如我们所看到的,have中的值不是查询最初返回的值。

这是我使用INTO关键字或与功能相关的其他方式的问题吗?

UPDATE 看起来只有结果集的第一个值被赋给has变量,也许如果我们可以迭代返回的结果然后分配它,那可能有效。

1 个答案:

答案 0 :(得分:1)

你不是第一个碰到这个的人。目前,array_agg()(或数组构造函数ARRAY(SELECT ...)仅接受标量值作为输入,而不接受数组类型。因此您无法使用它构建多维数组。

但修复很简单。创建自定义聚合函数:

CREATE AGGREGATE array_agg_mult (anyarray)  (
    SFUNC     = array_cat
   ,STYPE     = anyarray
   ,INITCOND  = '{}'
);

在简化了其他一些事情之后,你的函数的基本版本变为:

CREATE FUNCTION example2()
  RETURNS text[] AS
$func$
SELECT array_agg_mult(ARRAY[ARRAY[tbl, pg_size_pretty(pg_table_size(tbl))]]
                      ORDER BY pg_total_relation_size(tbl) DESC) AS have
FROM (
   SELECT format('%I.%I', table_schema, table_name) AS tbl
   FROM   information_schema.tables
   WHERE  table_schema NOT LIKE 'pg_%'
   AND    table_schema <> 'information_schema'
   ) AS all_tables
$func$  LANGUAGE sql;

相关(对Postgres 9.5的展望):