Skip to content

Commit 431fb73

Browse files
oliricedarora
authored andcommitted
add pg_jsonschema tests
1 parent 7c1f769 commit 431fb73

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

nix/tests/expected/pg_jsonschema.out

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
begin;
2+
-- Test json_matches_schema
3+
create table customer(
4+
id serial primary key,
5+
metadata json,
6+
check (
7+
json_matches_schema(
8+
'{
9+
"type": "object",
10+
"properties": {
11+
"tags": {
12+
"type": "array",
13+
"items": {
14+
"type": "string",
15+
"maxLength": 16
16+
}
17+
}
18+
}
19+
}',
20+
metadata
21+
)
22+
)
23+
);
24+
insert into customer(metadata)
25+
values ('{"tags": ["vip", "darkmode-ui"]}');
26+
-- Test jsonb_matches_schema
27+
select
28+
jsonb_matches_schema(
29+
'{
30+
"type": "object",
31+
"properties": {
32+
"tags": {
33+
"type": "array",
34+
"items": {
35+
"type": "string",
36+
"maxLength": 16
37+
}
38+
}
39+
}
40+
}',
41+
'{"tags": ["vip", "darkmode-ui"]}'::jsonb
42+
);
43+
jsonb_matches_schema
44+
----------------------
45+
t
46+
(1 row)
47+
48+
-- Test jsonschema_is_valid
49+
select
50+
jsonschema_is_valid(
51+
'{
52+
"type": "object",
53+
"properties": {
54+
"tags": {
55+
"type": "array",
56+
"items": {
57+
"type": "string",
58+
"maxLength": 16
59+
}
60+
}
61+
}
62+
}');
63+
jsonschema_is_valid
64+
---------------------
65+
t
66+
(1 row)
67+
68+
-- Test invalid payload
69+
insert into customer(metadata)
70+
values ('{"tags": [1, 3]}');
71+
ERROR: new row for relation "customer" violates check constraint "customer_metadata_check"
72+
DETAIL: Failing row contains (2, {"tags": [1, 3]}).
73+
rollback;

nix/tests/sql/pg_jsonschema.sql

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
begin;
2+
3+
-- Test json_matches_schema
4+
create table customer(
5+
id serial primary key,
6+
metadata json,
7+
8+
check (
9+
json_matches_schema(
10+
'{
11+
"type": "object",
12+
"properties": {
13+
"tags": {
14+
"type": "array",
15+
"items": {
16+
"type": "string",
17+
"maxLength": 16
18+
}
19+
}
20+
}
21+
}',
22+
metadata
23+
)
24+
)
25+
);
26+
27+
insert into customer(metadata)
28+
values ('{"tags": ["vip", "darkmode-ui"]}');
29+
30+
-- Test jsonb_matches_schema
31+
select
32+
jsonb_matches_schema(
33+
'{
34+
"type": "object",
35+
"properties": {
36+
"tags": {
37+
"type": "array",
38+
"items": {
39+
"type": "string",
40+
"maxLength": 16
41+
}
42+
}
43+
}
44+
}',
45+
'{"tags": ["vip", "darkmode-ui"]}'::jsonb
46+
);
47+
48+
-- Test jsonschema_is_valid
49+
select
50+
jsonschema_is_valid(
51+
'{
52+
"type": "object",
53+
"properties": {
54+
"tags": {
55+
"type": "array",
56+
"items": {
57+
"type": "string",
58+
"maxLength": 16
59+
}
60+
}
61+
}
62+
}');
63+
64+
-- Test invalid payload
65+
insert into customer(metadata)
66+
values ('{"tags": [1, 3]}');
67+
68+
rollback;

0 commit comments

Comments
 (0)