forked from timescale/pgai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sql
164 lines (135 loc) · 3.49 KB
/
test.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-------------------------------------------------------------------------------
-- pgai tests
\set VERBOSITY verbose
\set SHOW_CONTEXT errors
\x auto
\set ON_ERROR_ROLLBACK off
\set ON_ERROR_STOP on
-------------------------------------------------------------------------------
-- drop (if exists), recreate, and connect to the "test" database
select current_database() != 'postgres' as switch_db
\gset
\if :switch_db
\c postgres
\endif
select count(*) > 0 as drop_test_db
from pg_catalog.pg_database
where datname = 'test'
\gset
\if :drop_test_db
drop database test;
\endif
create database test;
\c test
create extension if not exists ai cascade;
-------------------------------------------------------------------------------
-- use an unprivileged user "tester"
reset role; -- just in case
select count(1) filter (where rolname = 'tester') = 0 as create_tester
from pg_roles
\gset
\if :create_tester
create user tester;
\endif
select not has_schema_privilege('tester', 'public', 'create') as grant_public
\gset
\if :grant_public
grant create on schema public to tester;
\endif
select not pg_has_role(current_user, 'tester', 'member') as grant_tester
\gset
\if :grant_tester
select format('grant tester to %I', current_user)
\gexec
\endif
select not has_function_privilege('tester', 'pg_read_binary_file(text)', 'execute') as grant_pg_read_server_files
\gset
\if :grant_pg_read_server_files
grant execute on function pg_read_binary_file(text) to tester;
\endif
select not pg_has_role('tester', 'pg_read_server_files', 'member') as grant_tester
\gset
\if :grant_tester
grant pg_read_server_files to tester;
\endif
set role tester;
-------------------------------------------------------------------------------
-- test setup
drop table if exists tests;
create table tests
( test text not null primary key
, expected text
, actual text
, passed boolean generated always as (actual = expected) stored
);
-------------------------------------------------------------------------------
-- openai tests
\getenv enable_openai_tests ENABLE_OPENAI_TESTS
\if :enable_openai_tests
\set ON_ERROR_ROLLBACK on
\set ON_ERROR_STOP off
\i tests/openai.sql
\set ON_ERROR_ROLLBACK off
\set ON_ERROR_STOP on
\endif
-------------------------------------------------------------------------------
-- ollama tests
\getenv enable_ollama_tests ENABLE_OLLAMA_TESTS
\if :enable_ollama_tests
\set ON_ERROR_ROLLBACK on
\set ON_ERROR_STOP off
\i tests/ollama.sql
\set ON_ERROR_ROLLBACK off
\set ON_ERROR_STOP on
\endif
-------------------------------------------------------------------------------
-- anthropic tests
\getenv enable_anthropic_tests ENABLE_ANTHROPIC_TESTS
\if :enable_anthropic_tests
\set ON_ERROR_ROLLBACK on
\set ON_ERROR_STOP off
\i tests/anthropic.sql
\set ON_ERROR_ROLLBACK off
\set ON_ERROR_STOP on
\endif
-------------------------------------------------------------------------------
-- test results
\echo
\echo
\echo test results
\echo
\echo
\set ON_ERROR_STOP on
\set ON_ERROR_ROLLBACK off
\echo test results
select test, passed
from tests
;
\echo failed tests
select *
from tests
where not passed
;
\echo test stats
select
count(*) as run
, count(*) filter (where passed = true) as passed
, count(*) filter (where passed = false) as failed
, count(*) filter (where passed is null) as did_not_run
from tests
;
select count(*) filter (where passed = false or passed is null) = 0 as result
from tests
\gset
reset role; -- no longer tester
\if :result
\echo PASSED!
\else
\warn FAILED!
do $$
begin
raise exception 'FAILED!';
end;
$$;
\endif
\q