【发布时间】:2015-12-10 06:04:48
【问题描述】:
我正在使用的 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 ;
但是当我在 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 关键字或其他与函数相关的方式的问题吗?
更新看起来只有结果集的第一个值被分配给 have 变量,也许如果我们可以迭代返回的结果然后将其分配给 have,这可能会起作用。
【问题讨论】:
标签: arrays unit-testing aggregate-functions plpgsql sql-function