feat(domain): sheet document schema v2 — references registry, review_status, vertebral count_range; SQL gate + pgTAP 008

This commit is contained in:
bchanot committed 2026-10-09 16:30:58 +02:00
1 parent 6c5b2b7754
commit 3dbbfdf1df
19 files changed
+474 -71

No files matched your search

+6 -5
View File
@@ -21,7 +21,7 @@ returns jsonb
language sql
as $$
select jsonb_build_object(
'schema_version', 1,
'schema_version', 2,
'species_id', 'equine',
'name', jsonb_build_object('fr', 'Cheval test', 'en', 'Test horse'),
'description', '{}'::jsonb,
@@ -30,6 +30,7 @@ as $$
'vertebral_config', jsonb_build_object('regions', jsonb_build_array(
jsonb_build_object('code', 'C', 'name', jsonb_build_object('fr', 'Cervicales', 'en', 'Cervical'), 'count', 7)
)),
'references', '[]'::jsonb,
'categories', jsonb_build_array(
jsonb_build_object('id', 'articulations', 'name', jsonb_build_object('fr', 'Articulations', 'en', 'Joints'), 'color', null)
),
@@ -160,7 +161,7 @@ select throws_ok(
);
select throws_ok(
$$ update public.anatomical_sheet_versions
set content = jsonb_build_object('schema_version', 1, 'species_id', 'equine', 'pad', repeat('x', 2200000))
set content = jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'pad', repeat('x', 2200000))
where id = pg_temp.id('v1') $$,
'23514', null, 'a document over 2 MiB is refused'
);
@@ -405,7 +406,7 @@ select throws_ok(
);
select throws_ok(
$$ insert into public.anatomical_sheet_versions (sheet_id, version_number, document_schema_version, content)
values (pg_temp.id('sheet'), 9, 1, pg_temp.sheet_doc(null)) $$,
values (pg_temp.id('sheet'), 9, 2, pg_temp.sheet_doc(null)) $$,
'42501', null, 'bob cannot insert a version row directly'
);
@@ -429,7 +430,7 @@ select is(
(select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v3') and content = '{}'::jsonb),
0::bigint, 'bob cannot read the draft'
);
update public.anatomical_sheet_versions set content = '{"schema_version":1}'::jsonb where id = pg_temp.id('v3');
update public.anatomical_sheet_versions set content = '{"schema_version":2}'::jsonb where id = pg_temp.id('v3');
select throws_ok(
$$ select public.fork_anatomical_sheet(pg_temp.id('v3'), 'Copie', 'copie-bob') $$,
'42501', null, 'a draft cannot be copied (hidden from non-editors)'
@@ -517,7 +518,7 @@ select is(
update public.anatomical_sheet_versions set content = '{}'::jsonb where id = pg_temp.id('v2');
select is(
(select content ->> 'schema_version' from public.anatomical_sheet_versions where id = pg_temp.id('v2')),
'1', 'a deprecated version is hidden from editor UPDATE'
'2', 'a deprecated version is hidden from editor UPDATE'
);
reset role;
select throws_ok(
+1 -1
View File
@@ -17,7 +17,7 @@ grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
-- Minimal publishable document with one main view on a given asset.
create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$
select jsonb_build_object(
'schema_version', 1, 'species_id', 'equine',
'schema_version', 2, 'species_id', 'equine',
'name', jsonb_build_object('fr', 'A', 'en', 'A'),
'description', '{}'::jsonb, 'scope_disclaimer', '{}'::jsonb,
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
+1 -1
View File
@@ -28,7 +28,7 @@ select results_eq(
select results_eq(
$$ select version_number, status::text, document_schema_version, published_at
from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet') $$,
$$ values (1, 'draft', 1, null::timestamptz) $$,
$$ values (1, 'draft', 2, null::timestamptz) $$,
'exactly one version, draft v1, never published'
);
select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 2::bigint, 'two SVG assets registered');
@@ -0,0 +1,90 @@
-- 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;