我使用pgtap来测试postgresql存储过程。其results_eq函数获取存储过程的结果,将其与预期结果进行比较,如果两者不相等则报告失败。
这是我正在运行的代码:
PREPARE result_have AS SELECT select_some_data(12345, 'test_string');
PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)');
SELECT results_eq('result_have', 'result_want');
这是失败输出:
not ok 21
# Failed test 21: "this should return a result"
# Columns differ between queries:
# have: ("(""2010-09-07 06:05:00+00"",100.0)")
# want: ("(""2010-09-07 06:05:00+00"",100.0)")
# Looks like you failed 1 test of 21
我可能真的睡不着觉,但想要和我看起来很相似。
有谁知道为什么这会被报告为失败?
更新:这是我定义相关存储过程的方式:
CREATE OR REPLACE FUNCTION select_some_data
(
IN p_some_pkey integer,
IN p_some_code varchar(16)
)
RETURNS TABLE(timestamp_utc timestamp with time zone, value varchar)
...
所以要关注peters advice,我尝试更改我的代码,但没有成功:
PREPARE result_have AS SELECT select_some_data(12345, 'test_string');
-- TODO: none of these work, syntax error at or near "TABLE"
-- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::TABLE(timestamp with time zone, varchar));
-- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::'TABLE(timestamp with time zone, varchar)');
-- this is the old code...
PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)');
SELECT results_eq('result_have', 'result_want');
正如你可能会说的那样,即使是基本的postgresql语法,我也会在黑暗中刺伤 - 在搜索::
时,谷歌和postgresql.org上的搜索都不会返回任何有用的信息。我最终冒了一个幸运的猜测,这可能是一个操作员,发现::
是type cast。 CREATE FUNCTION的column_name
参数文档说'RETURNS TABLE
也暗示RETURNS SETOF
'让我得到了here,也许here和here。新的尝试:
PREPARE result_have AS SELECT select_some_data(12345, 'test_string');
-- TODO: doesn't work, syntax error at or near "("
-- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::SETOF(timestamp with time zone, varchar));
-- TODO: doesn't work, syntax error at or near ","
-- PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::SETOF RECORD(timestamp with time zone, varchar));
-- this is the old code...
PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)');
SELECT results_eq('result_have', 'result_want');
这是毫无意义的,我只是在做猜测。任何人都可以用正确的语法帮助我吗?请注意,该函数只是RETURNS TABLE
因为这是我能够开始工作的第一件事,所以如果有一个解决方案需要改变它,我很乐意改变它。
更新2 :postgresql IRC频道上的RhodiumToad(irc://irc.freenode.net/#postgresql)帮助我使用了正确的语法。如果我对postgresql了解得多一些,我可以认为这只是有意义的:两种数据类型,两个演员阵容(DOH!):o)。
此时,测试数据库中只有一个数据集,因此上面使用的语法可能仍然有效。据我所知,一旦返回多个数据集,它可能会失败,因此它应该是SELECT * FROM
,而不仅仅是SELECT
:
PREPARE result_have AS SELECT * FROM select_some_data(12345, 'test_param_code');
PREPARE result_want AS VALUES ('2010-09-07 06:05:00+00'::timestamp with time zone, '100.0'::varchar);
SELECT results_eq('result_have', 'result_want', 'have and want should be equal');
现在已经并且希望结果被比较为相等并且测试通过。运行测试时的日志输出:
ok 21 - have and want should be equal
ok
All tests successful.
Files=1, Tests=21, 1 wallclock secs ( 0.02 usr 0.00 sys + 0.05 cusr 0.03 csys = 0.10 CPU)
Result: PASS
WOOT !! : - )
答案 0 :(得分:4)
您没有提供所有细节,但我怀疑这是数据类型不匹配。 pgTAP往往需要完全匹配。试试这个:
PREPARE result_want AS VALUES ('("2010-09-07 06:05:00+00",100.0)'::foo);
其中foo
是函数select_some_data
的返回类型。