Skip to content

Commit 2189cd9

Browse files
committed
Improve testing
- `make test` now works whether you are inside or outside the docker container - tests now better handle errors that cause the `actual` psql variable to be unset - fail if no tests were run
1 parent b4158bf commit 2189cd9

File tree

8 files changed

+395
-214
lines changed

8 files changed

+395
-214
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# syntax=docker/dockerfile:1.3-labs
22
FROM postgres:16
3-
3+
ENV WHERE_AM_I=docker
44
ENV DEBIAN_FRONTEND=noninteractive
55
USER root
66

test.sh

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,12 @@ if [ -f .env ]; then
88
set +a
99
fi
1010

11-
if [ -z "$ENABLE_OPENAI_TESTS" ]; then
12-
export ENABLE_OPENAI_TESTS=0
11+
if [ -n "$WHERE_AM_I" ] && [ "$WHERE_AM_I" == "docker" ]; then
12+
if [ "$(whoami)" == "root" ]; then
13+
echo switching to postgres user...
14+
su postgres -
15+
fi
16+
psql -d postgres -f test.sql
17+
else
18+
psql --no-psqlrc -d 'postgres://[email protected]:9876/postgres' -f test.sql
1319
fi
14-
15-
if [ "$ENABLE_OPENAI_TESTS" ] && [ -z "$OPENAI_API_KEY" ]; then
16-
echo "OPENAI_API_KEY must be set if running OpenAI tests"
17-
exit 3
18-
fi
19-
20-
if [ -z "$ENABLE_OLLAMA_TESTS" ]; then
21-
export ENABLE_OLLAMA_TESTS=0
22-
fi
23-
24-
if [ -z "$ENABLE_ANTHROPIC_TESTS" ]; then
25-
export ENABLE_ANTHROPIC_TESTS=0
26-
fi
27-
28-
if [ "$ENABLE_ANTHROPIC_TESTS" ] && [ -z "$ANTHROPIC_API_KEY" ]; then
29-
echo "ANTHROPIC_API_KEY must be set if running Anthropic tests"
30-
exit 3
31-
fi
32-
33-
if [ -z "$ENABLE_COHERE_TESTS" ]; then
34-
export ENABLE_COHERE_TESTS=0
35-
fi
36-
37-
if [ "$ENABLE_COHERE_TESTS" ] && [ -z "$COHERE_API_KEY" ]; then
38-
echo "COHERE_API_KEY must be set if running Cohere tests"
39-
exit 3
40-
fi
41-
42-
psql -d postgres -f test.sql

test.sql

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ create table tests
8383
);
8484

8585
-------------------------------------------------------------------------------
86-
-- convenience functions for recording test results
86+
-- convenience function for recording test results
8787
create function result(_test text, _expected text, _actual text) returns bool
8888
as $func$
8989
merge into tests as t
@@ -95,92 +95,121 @@ when not matched then insert (test, actual) values (x.test, x.actual)
9595
select passed from tests where test = _test;
9696
$func$ language sql;
9797

98-
create function result(_test text, _expected int, _actual int) returns bool
99-
return (select result(_test, _expected::text, _actual::text))
100-
;
101-
102-
create function result(_test text, _expected bool, _actual bool) returns bool
103-
return (select result(_test, _expected::text, _actual::text))
104-
;
105-
10698
\pset tuples_only on
10799
-------------------------------------------------------------------------------
108100
-- openai tests
109101
\getenv enable_openai_tests ENABLE_OPENAI_TESTS
102+
\if :{?enable_openai_tests}
103+
\else
104+
\set enable_openai_tests 0
105+
\endif
110106
\if :enable_openai_tests
111107
\set ON_ERROR_ROLLBACK on
112108
\set ON_ERROR_STOP off
113109
\i tests/openai.sql
114110
\set ON_ERROR_ROLLBACK off
115111
\set ON_ERROR_STOP on
112+
\else
113+
\echo Skipped OpenAI tests
116114
\endif
117115

118116
-------------------------------------------------------------------------------
119117
-- ollama tests
120118
\getenv enable_ollama_tests ENABLE_OLLAMA_TESTS
119+
\if :{?enable_ollama_tests}
120+
\else
121+
\set enable_ollama_tests 0
122+
\endif
121123
\if :enable_ollama_tests
122124
\set ON_ERROR_ROLLBACK on
123125
\set ON_ERROR_STOP off
124126
\i tests/ollama.sql
125127
\set ON_ERROR_ROLLBACK off
126128
\set ON_ERROR_STOP on
129+
\else
130+
\echo Skipped Ollama tests
127131
\endif
128132

129133
-------------------------------------------------------------------------------
130134
-- anthropic tests
131135
\getenv enable_anthropic_tests ENABLE_ANTHROPIC_TESTS
136+
\if :{?enable_anthropic_tests}
137+
\else
138+
\set enable_anthropic_tests 0
139+
\endif
132140
\if :enable_anthropic_tests
133141
\set ON_ERROR_ROLLBACK on
134142
\set ON_ERROR_STOP off
135143
\i tests/anthropic.sql
136144
\set ON_ERROR_ROLLBACK off
137145
\set ON_ERROR_STOP on
146+
\else
147+
\echo Skipped Anthropic tests
138148
\endif
139149

140150
-------------------------------------------------------------------------------
141151
-- cohere tests
142152
\getenv enable_cohere_tests ENABLE_COHERE_TESTS
153+
\if :{?enable_cohere_tests}
154+
\else
155+
\set enable_cohere_tests 0
156+
\endif
143157
\if :enable_cohere_tests
144158
\set ON_ERROR_ROLLBACK on
145159
\set ON_ERROR_STOP off
146160
\i tests/cohere.sql
147161
\set ON_ERROR_ROLLBACK off
148162
\set ON_ERROR_STOP on
163+
\else
164+
\echo Skipped Cohere tests
149165
\endif
150166

151167
\pset tuples_only off
152168
-------------------------------------------------------------------------------
153169
-- test results
154170
\echo
155171
\echo
156-
\echo test results
172+
\echo
157173
\echo
158174
\echo
159175

160176
\set ON_ERROR_STOP on
161177
\set ON_ERROR_ROLLBACK off
162-
\echo test results
163-
select test, passed
164-
from tests
165-
;
166178

167-
\echo failed tests
168-
select *
179+
-- we should fail if no tests were run
180+
select count(*) > 0 as result
169181
from tests
170-
where passed is distinct from true
171-
;
182+
\gset
172183

173-
\echo test stats
174-
select
175-
count(*) as total
176-
, count(*) filter (where passed = true) as passed
177-
, count(*) filter (where passed is distinct from true) as failed
178-
from tests
179-
;
184+
\if :result
185+
186+
\echo test results
187+
select test, passed
188+
from tests
189+
;
190+
191+
\echo failed tests
192+
select *
193+
from tests
194+
where passed is distinct from true
195+
;
196+
197+
\echo test stats
198+
select
199+
count(*) as total
200+
, count(*) filter (where passed = true) as passed
201+
, count(*) filter (where passed is distinct from true) as failed
202+
from tests
203+
;
204+
205+
select count(*) filter (where passed is distinct from true) = 0 as result
206+
from tests
207+
\gset
208+
209+
\else
210+
\warn NO TESTS WERE RUN!
211+
\endif
180212

181-
select count(*) filter (where passed is distinct from true) = 0 as result
182-
from tests
183-
\gset
184213

185214
reset role; -- no longer tester
186215

tests/anthropic.sql

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
-- get our anthropic api key
33

44
\getenv anthropic_api_key ANTHROPIC_API_KEY
5+
\if :{?anthropic_api_key}
6+
\else
7+
\warn Anthropic tests are enabled but ANTHROPIC_API_KEY is not set!
8+
do $$
9+
begin
10+
raise exception 'Anthropic tests are enabled but ANTHROPIC_API_KEY is not set!';
11+
end;
12+
$$;
13+
\q
14+
\endif
15+
516
-- set our session local GUC
617
select set_config('ai.anthropic_api_key', $1, false) is not null as set_anthropic_api_key
718
\bind :anthropic_api_key
@@ -18,7 +29,9 @@ values
1829

1930
-------------------------------------------------------------------------------
2031
-- anthropic_generate
21-
\echo anthropic_generate
32+
\set testname anthropic_generate
33+
\set expected t
34+
\echo :testname
2235
select anthropic_generate
2336
( 'claude-3-5-sonnet-20240620'
2437
, jsonb_build_array
@@ -32,15 +45,18 @@ select anthropic_generate
3245
\bind :anthropic_api_key
3346
\gset
3447

48+
\if :{?actual}
3549
select jsonb_extract_path_text(:'actual'::jsonb, 'content', '0', 'text') is not null and (:'actual'::jsonb)->>'stop_reason' = 'end_turn' as actual
3650
\gset
51+
\endif
3752

38-
select result('anthropic_generate', true, :'actual');
39-
\unset actual
53+
\ir eval.sql
4054

4155
-------------------------------------------------------------------------------
4256
-- anthropic_generate-no-key
43-
\echo anthropic_generate-no-key
57+
\set testname anthropic_generate-no-key
58+
\set expected t
59+
\echo :testname
4460
select anthropic_generate
4561
( 'claude-3-5-sonnet-20240620'
4662
, jsonb_build_array
@@ -52,8 +68,9 @@ select anthropic_generate
5268
) as actual
5369
\gset
5470

71+
\if :{?actual}
5572
select jsonb_extract_path_text(:'actual'::jsonb, 'content', '0', 'text') is not null and (:'actual'::jsonb)->>'stop_reason' = 'end_turn' as actual
5673
\gset
74+
\endif
5775

58-
select result('anthropic_generate-no-key', true, :'actual');
59-
\unset actual
76+
\ir eval.sql

0 commit comments

Comments
 (0)