fix: add test script

This commit is contained in:
Orion Kindel 2023-06-11 12:01:51 -05:00
parent 2cf11bcb2b
commit 65b6f0f129
Signed by untrusted user who does not match committer: orion
GPG Key ID: 6D4165AE4C928719
4 changed files with 102 additions and 9 deletions

View File

@ -99,8 +99,8 @@ create function setup_audit_and_soft_delete( schema text
create_do_table_audit := concat( create_do_table_audit create_do_table_audit := concat( create_do_table_audit
, E' end if;\n' , E' end if;\n'
, E' insert into public.usr_audit\n' , E' insert into ', audit_table, E'\n'
, E' (kind, usr, actor' , ' (kind, ', table_, ', actor'
); );
foreach col in array tracked_columns loop foreach col in array tracked_columns loop
@ -216,8 +216,8 @@ create function setup_audit_and_hard_delete( schema text
create_do_table_audit := concat( create_do_table_audit create_do_table_audit := concat( create_do_table_audit
, E' end if;\n' , E' end if;\n'
, E' insert into public.usr_audit\n' , E' insert into ', audit_table, E'\n'
, E' (kind, usr, actor' , ' (kind, ', table_, ', actor'
); );
foreach col in array tracked_columns loop foreach col in array tracked_columns loop

View File

@ -9,6 +9,10 @@ create table public.usr
, email public.email not null unique , email public.email not null unique
); );
insert into public.usr (tag, password, email)
overriding system value
values (usr_tag_of_string('system'), hashed_text_of_string(''), email_of_string(''));
select audit( 'public' select audit( 'public'
, 'usr' , 'usr'
, array[ row('tag', 'public.usr_tag') , array[ row('tag', 'public.usr_tag')

View File

@ -10,7 +10,6 @@ create table public.community
select audit( 'public' select audit( 'public'
, 'community' , 'community'
, array[ row('tag', 'public.community_tag') , array[ row('tag', 'public.community_tag')
, row('role_nonmember', 'int')
] :: audited_column[] ] :: audited_column[]
, soft_delete => true , soft_delete => true
); );
@ -50,6 +49,7 @@ select immutable( 'public'
create table public.community_member_role create table public.community_member_role
( id int not null primary key generated always as identity ( id int not null primary key generated always as identity
, uid uuid not null unique default gen_random_uuid() , uid uuid not null unique default gen_random_uuid()
, community int not null references public.community (id)
, title text not null , title text not null
, description text not null , description text not null
); );
@ -66,6 +66,7 @@ select immutable( 'public'
, 'community_member_role' , 'community_member_role'
, array[ 'id' , array[ 'id'
, 'uid' , 'uid'
, 'community'
] ]
); );
@ -98,6 +99,7 @@ create table public.community_member
( id int not null primary key generated always as identity ( id int not null primary key generated always as identity
, uid uuid not null unique default gen_random_uuid() , uid uuid not null unique default gen_random_uuid()
, usr int not null references public.usr (id) , usr int not null references public.usr (id)
, community int not null references public.community (id)
, role_ int not null references public.community_member_role (id) , role_ int not null references public.community_member_role (id)
); );

87
test.sql Normal file
View File

@ -0,0 +1,87 @@
create or replace function test_schema() returns void language plpgsql as $$
declare
usr_id_foo int;
usr_id_bar int;
usr_id_baz int;
comm_id_ask_dnim int;
comm_id_pics int;
begin
insert into public.usr
( tag -- public.usr_tag
, password -- public.hashed_text
, email -- public.email
)
values
(usr_tag_of_string('foo'), hash_text('password123'), email_of_string('foo@gmail.com'))
, (usr_tag_of_string('bar'), hash_text('password243'), email_of_string('bar@gmail.com'))
, (usr_tag_of_string('baz'), hash_text('hunter2'), email_of_string('baz@gmail.com'))
;
insert into public.community
( tag -- public.community_tag
)
values
(community_tag_of_string('ask_dnim'))
, (community_tag_of_string('pics'));
insert into public.community_member_role (community, title, description)
select comm.id, role_.title, role_.description
from community comm
, ( values ('owner', 'community owner')
, ('member', 'community member')
) as role_ (title, description)
;
insert into public.community_member_role_scope (role_, scope)
select role.id, s.scope
from public.community_member_role as role
, ( values (authz_scope_of_string('threads.read'))
, (authz_scope_of_string('threads.create'))
, (authz_scope_of_string('threads.update'))
, (authz_scope_of_string('threads.delete'))
, (authz_scope_of_string('members.read'))
, (authz_scope_of_string('members.create'))
, (authz_scope_of_string('members.delete'))
, (authz_scope_of_string('roles.read'))
, (authz_scope_of_string('roles.create'))
, (authz_scope_of_string('roles.update'))
, (authz_scope_of_string('roles.delete'))
, (authz_scope_of_string('update'))
, (authz_scope_of_string('delete'))
) as s (scope)
where role.title = 'owner';
insert into public.community_member_role_scope (role_, scope)
select role.id, s.scope
from public.community_member_role as role
, ( values (authz_scope_of_string('threads.read'))
, (authz_scope_of_string('threads.create'))
, (authz_scope_of_string('members.read'))
) as s (scope)
where role.title = 'member';
insert into public.community_member (community, usr, role_)
select comm.id, u.id, role_.id
from community comm
inner join usr u on u.tag = usr_tag_of_string('foo')
inner join community_member_role role_ on role_.community = comm.id and role_.title = 'owner'
where comm.tag = community_tag_of_string('ask_dnim');
insert into public.community_member (community, usr, role_)
select comm.id, u.id, role_.id
from community comm
inner join usr u on u.tag = usr_tag_of_string('bar')
inner join community_member_role role_ on role_.community = comm.id and role_.title = 'member'
where comm.tag = community_tag_of_string('ask_dnim');
insert into public.community_member (community, usr, role_)
select comm.id, u.id, role_.id
from community comm
inner join usr u on u.tag = usr_tag_of_string('baz')
inner join community_member_role role_ on role_.community = comm.id and role_.title = 'member'
where comm.tag = community_tag_of_string('ask_dnim');
end;
$$;
select test_schema();
drop function test_schema;