Files
faunosteo/supabase/tests/008_sheet_document_schema_v2.sql
T

91 lines
4.2 KiB
PL/PgSQL

-- Document schema_version 2: references registry, review status, count
-- ranges. Only version 2 is stored; the SQL gate re-checks the new rules.
begin;
select plan(8);
insert into auth.users (id, email) values ('50000000-0000-0000-0000-00000000000a', 'alice-v2@test.local');
create function pg_temp.doc(p_refs jsonb, p_cited jsonb, p_range jsonb)
returns jsonb
language sql
as $$
select jsonb_build_object(
'schema_version', 2,
'species_id', 'equine',
'name', jsonb_build_object('fr', 'Cheval', 'en', 'Horse'),
'description', '{}'::jsonb,
'scope_disclaimer', '{}'::jsonb,
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
'vertebral_config', jsonb_build_object('regions', jsonb_build_array(
jsonb_build_object('code', 'Cd', 'name', jsonb_build_object('fr', 'Caudales', 'en', 'Caudal'), 'count', 18, 'count_range', p_range)
)),
'references', p_refs,
'categories', jsonb_build_array(
jsonb_build_object('id', 'os', 'name', jsonb_build_object('fr', 'Os', 'en', 'Bones'), 'color', null)
),
'views', jsonb_build_array(
jsonb_build_object('id', 'main', 'kind', 'main', 'name', jsonb_build_object('fr', 'Vue', 'en', 'View'),
'svg_asset_id', '00000000-0000-0000-0000-000000000001', 'view_box', null)
),
'annotations', jsonb_build_array(
jsonb_build_object('id', 'hip', 'kind', 'point', 'view_id', 'main', 'category_id', 'os', 'laterality', 'bilateral',
'geometry', null, 'review_status', 'inherited', 'name', jsonb_build_object('fr', 'Hanche', 'en', 'Hip'),
'finding_zone_id', 'hip', 'vertebral_region_code', null)
),
'finding_zones', jsonb_build_array(
jsonb_build_object('id', 'hip', 'name', jsonb_build_object('fr', 'Hanche', 'en', 'Hip'), 'categories', jsonb_build_array(
jsonb_build_object('id', 'hip-dysfunctions', 'kind', 'dysfunction',
'name', jsonb_build_object('fr', 'Dysfonctions', 'en', 'Dysfunctions'),
'reference_ids', '[]'::jsonb,
'children', jsonb_build_array(
jsonb_build_object('id', 'articular', 'name', jsonb_build_object('fr', 'Articulaire', 'en', 'Articular'),
'children', jsonb_build_array(
jsonb_build_object('id', 'flexion', 'name', jsonb_build_object('fr', 'Flexion', 'en', 'Flexion'), 'reference_ids', p_cited)
))
))
))
)
);
$$;
grant execute on function pg_temp.doc(jsonb, jsonb, jsonb) to authenticated;
select is(
private.sheet_document_issues(pg_temp.doc('[{"id":"nav","type":"standard","citation":"NAV 2017","url":null,"doi":null}]', '["nav"]', '{"min":15,"max":21}')),
'{}'::text[], 'a v2 document citing a declared reference inside its count range passes'
);
select is(
private.sheet_document_issues(pg_temp.doc('[]', '["nav"]', 'null')),
'{finding.reference.missing}'::text[], 'a citation of an undeclared reference is reported (nested node)'
);
select is(
private.sheet_document_issues(pg_temp.doc('[{"id":"nav","citation":"a"},{"id":"nav","citation":"b"}]', '[]', 'null')),
'{reference.id.duplicate}'::text[], 'duplicate reference ids are reported'
);
select is(
private.sheet_document_issues(pg_temp.doc('[]', '[]', '{"min":19,"max":21}')),
'{region.count_range.invalid}'::text[], 'a usual count outside its range is reported'
);
select is(
private.sheet_document_issues(pg_temp.doc('[]', '[]', '{"min":18,"max":18}')),
'{}'::text[], 'a count on the bounds of its range passes'
);
select is(
private.sheet_document_issues(jsonb_set(pg_temp.doc('[]', '[]', 'null'), '{references}', '"x"')),
'{document.shape}'::text[], 'a non-array references registry is a shape error'
);
-- Only schema_version 2 rows are stored.
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"50000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select throws_ok(
$$ select public.create_anatomical_sheet('equine', 'Old', 'old-v1', jsonb_set(pg_temp.doc('[]', '[]', 'null'), '{schema_version}', '1')) $$,
'23514', null, 'a schema_version 1 document is refused'
);
select lives_ok(
$$ select public.create_anatomical_sheet('equine', 'New', 'new-v2', pg_temp.doc('[]', '[]', 'null')) $$,
'a schema_version 2 document is accepted as a draft'
);
select * from finish();
rollback;