#29 Implement Workgroup members query
This commit is contained in:
6
.idea/runConfigurations/All_Tests__offline_.xml
generated
6
.idea/runConfigurations/All_Tests__offline_.xml
generated
@@ -3,6 +3,12 @@
|
|||||||
<output_file path="$PROJECT_DIR$/var/log/test/out.log" is_save="true" />
|
<output_file path="$PROJECT_DIR$/var/log/test/out.log" is_save="true" />
|
||||||
<module name="dcproject.test" />
|
<module name="dcproject.test" />
|
||||||
<useClassPathOnly />
|
<useClassPathOnly />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="fr.dcproject.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
<option name="PACKAGE_NAME" value="" />
|
<option name="PACKAGE_NAME" value="" />
|
||||||
<option name="MAIN_CLASS_NAME" value="RunCucumberTest" />
|
<option name="MAIN_CLASS_NAME" value="RunCucumberTest" />
|
||||||
<option name="METHOD_NAME" value="" />
|
<option name="METHOD_NAME" value="" />
|
||||||
|
|||||||
17
.idea/runConfigurations/Test_Workgroup.xml
generated
Normal file
17
.idea/runConfigurations/Test_Workgroup.xml
generated
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Test Workgroup" type="DatabaseScript" folderName="SQL TEST">
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/find_workgroup_by_id.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/find_workgroup_members.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/find_workgroups.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/upsert_workgroup.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/add_workgroup_members.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/remove_workgroup_members.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/main/resources/sql/functions/workgroup/update_workgroup_members.sql" />
|
||||||
|
<script-file value="$PROJECT_DIR$/src/test/sql/workgroup.sql" />
|
||||||
|
<script-mode>FILE</script-mode>
|
||||||
|
<data-source id="a9a6d0e9-327d-4e7d-9b93-3cb6f7948866" namespace="database/"test"/schema/"public"" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Reset Test database" run_configuration_type="DatabaseScript" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
create or replace function add_workgroup_members(in _id uuid, inout resource json)
|
||||||
|
language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
insert into citizen_in_workgroup (citizen_id, workgroup_id)
|
||||||
|
select
|
||||||
|
(z->>'id')::uuid,
|
||||||
|
_id::uuid
|
||||||
|
from json_array_elements(resource) z
|
||||||
|
where (z->>'id') is not null
|
||||||
|
on conflict do nothing;
|
||||||
|
|
||||||
|
select find_workgroup_members(_id) into resource;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop procedure if exists update_workgroup_members(in uuid, inout json);
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
create or replace function find_workgroup_members(in _id uuid, out resource json) language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
select json_agg(t) into resource
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
z.*,
|
||||||
|
find_user_by_id(z.user_id) as "user"
|
||||||
|
from citizen_in_workgroup as ciw
|
||||||
|
join citizen z on z.id = ciw.citizen_id
|
||||||
|
where ciw.workgroup_id = _id
|
||||||
|
) as t;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists find_workgroup_members(uuid, out json);
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
create or replace function remove_workgroup_members(in _id uuid, inout resource json)
|
||||||
|
language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
delete from citizen_in_workgroup
|
||||||
|
where workgroup_id = _id
|
||||||
|
and citizen_id in (
|
||||||
|
select
|
||||||
|
(z->>'id')::uuid
|
||||||
|
from json_array_elements(resource) z
|
||||||
|
);
|
||||||
|
|
||||||
|
select find_workgroup_members(_id) into resource;
|
||||||
|
end
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop procedure if exists remove_workgroup_members(in uuid, inout json);
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
create or replace function update_workgroup_members(in _id uuid, inout resource json)
|
||||||
|
language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
insert into citizen_in_workgroup (citizen_id, workgroup_id)
|
||||||
|
select
|
||||||
|
(z->>'id')::uuid,
|
||||||
|
_id::uuid
|
||||||
|
from json_array_elements(resource) z
|
||||||
|
where (z->>'id') is not null
|
||||||
|
on conflict do nothing;
|
||||||
|
|
||||||
|
delete from citizen_in_workgroup
|
||||||
|
where workgroup_id = _id
|
||||||
|
and citizen_id not in(
|
||||||
|
select
|
||||||
|
(z->>'id')::uuid
|
||||||
|
from json_array_elements(resource) z
|
||||||
|
where (z->>'id') is not null
|
||||||
|
);
|
||||||
|
|
||||||
|
select find_workgroup_members(_id) into resource;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop procedure if exists update_workgroup_members(in uuid, inout json);
|
||||||
@@ -21,6 +21,11 @@ begin
|
|||||||
logo = excluded.logo,
|
logo = excluded.logo,
|
||||||
owner_id = excluded.owner_id;
|
owner_id = excluded.owner_id;
|
||||||
|
|
||||||
|
-- insert into citizen_in_workgroup (citizen_id, workgroup_id)
|
||||||
|
-- select
|
||||||
|
-- (resource->>'id')::uuid,
|
||||||
|
-- new_id::uuid
|
||||||
|
-- from json_populate_recordset(null::workgroup, resource->'members');
|
||||||
|
|
||||||
select find_workgroup_by_id(new_id) into resource;
|
select find_workgroup_by_id(new_id) into resource;
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -3,22 +3,22 @@ create or replace function fixture_citizen(in name text default 'george', out _c
|
|||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
_user_id uuid;
|
_user_id uuid;
|
||||||
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}';
|
created_citizen json;
|
||||||
created_citizen2 json := '{"name": {"first_name":"john", "last_name":"DOE"}, "birthday": "2001-01-01", "email":"john.doe@gmail.com"}';
|
|
||||||
begin
|
begin
|
||||||
_user_id = fixture_user(name);
|
_user_id = fixture_user(name);
|
||||||
if (name = 'george') then
|
if (name = 'george') then
|
||||||
|
created_citizen = '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}';
|
||||||
|
elseif (name = 'john') then
|
||||||
|
created_citizen = '{"name": {"first_name":"john", "last_name":"DOE"}, "birthday": "2001-01-01", "email":"john.doe@gmail.com"}';
|
||||||
|
elseif (name = 'tesla') then
|
||||||
|
created_citizen = '{"name": {"first_name":"Nicolas", "last_name":"Tesla"}, "birthday": "2001-01-01", "email":"nicolas.tesla@gmail.com"}';
|
||||||
|
end if;
|
||||||
|
|
||||||
|
|
||||||
created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json;
|
created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json;
|
||||||
assert created_citizen#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen#>>'{user, id}', _user_id::text);
|
assert created_citizen#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen#>>'{user, id}', _user_id::text);
|
||||||
|
|
||||||
select upsert_citizen(created_citizen) into created_citizen;
|
select upsert_citizen(created_citizen) into created_citizen;
|
||||||
_citizen_id := created_citizen->>'id';
|
_citizen_id := created_citizen->>'id';
|
||||||
elseif (name = 'john') then
|
|
||||||
created_citizen2 := jsonb_set(created_citizen2::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json;
|
|
||||||
assert created_citizen2#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen2#>>'{user, id}', _user_id::text);
|
|
||||||
|
|
||||||
select upsert_citizen(created_citizen2) into created_citizen2;
|
|
||||||
_citizen_id := created_citizen2->>'id';
|
|
||||||
end if;
|
|
||||||
end
|
end
|
||||||
$$
|
$$
|
||||||
@@ -2,15 +2,17 @@ create or replace function fixture_user(in name text default 'george', out user_
|
|||||||
language plpgsql as
|
language plpgsql as
|
||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
created_user1 json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
created_user json;
|
||||||
created_user2 json := '{"username": "john", "plain_password": "qwerty", "roles": ["ROLE_USER"]}';
|
|
||||||
begin
|
begin
|
||||||
if (name = 'george') then
|
if (name = 'george') then
|
||||||
select insert_user(created_user1) into created_user1;
|
created_user = '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||||
user_id := created_user1->>'id';
|
|
||||||
elseif (name = 'john') then
|
elseif (name = 'john') then
|
||||||
select insert_user(created_user2) into created_user2;
|
created_user = '{"username": "john", "plain_password": "qwerty", "roles": ["ROLE_USER"]}';
|
||||||
user_id := created_user2->>'id';
|
elseif (name = 'tesla') then
|
||||||
|
created_user = '{"username": "tesla", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||||
end if;
|
end if;
|
||||||
|
|
||||||
|
select insert_user(created_user) into created_user;
|
||||||
|
user_id := created_user->>'id';
|
||||||
end
|
end
|
||||||
$$
|
$$
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
do
|
do
|
||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
_citizen_id uuid := fixture_citizen();
|
_citizen_id uuid := fixture_citizen('george');
|
||||||
|
_citizen_id2 uuid := fixture_citizen('john');
|
||||||
|
_citizen_id3 uuid := fixture_citizen('tesla');
|
||||||
created_workgroup json := '{
|
created_workgroup json := '{
|
||||||
"name": "Le groupe des vert",
|
"name": "Le groupe des vert",
|
||||||
"description": "test",
|
"description": "test",
|
||||||
@@ -13,7 +15,7 @@ declare
|
|||||||
"anonymous": false
|
"anonymous": false
|
||||||
}';
|
}';
|
||||||
selected_workgroup json;
|
selected_workgroup json;
|
||||||
selected_workgroup_2 json;
|
members json;
|
||||||
begin
|
begin
|
||||||
created_workgroup := jsonb_set(created_workgroup::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
created_workgroup := jsonb_set(created_workgroup::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
||||||
created_workgroup := jsonb_set(created_workgroup::jsonb, '{owner}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
created_workgroup := jsonb_set(created_workgroup::jsonb, '{owner}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
||||||
@@ -38,7 +40,47 @@ begin
|
|||||||
select (w.resource->0) into selected_workgroup from find_workgroups('Le groupe des vert', "limit" := 1) w;
|
select (w.resource->0) into selected_workgroup from find_workgroups('Le groupe des vert', "limit" := 1) w;
|
||||||
assert (selected_workgroup->>'name') = 'Le groupe des vert', format('name must be "Le groupe des vert" instead of : %s', (selected_workgroup->>'name'));
|
assert (selected_workgroup->>'name') = 'Le groupe des vert', format('name must be "Le groupe des vert" instead of : %s', (selected_workgroup->>'name'));
|
||||||
|
|
||||||
|
-------------
|
||||||
|
-- members --
|
||||||
|
-------------
|
||||||
|
|
||||||
|
|
||||||
|
-- add
|
||||||
|
select m into members from add_workgroup_members((created_workgroup->>'id')::uuid, json_build_array(
|
||||||
|
json_build_object('id', _citizen_id2),
|
||||||
|
json_build_object('id', _citizen_id3)
|
||||||
|
)) m;
|
||||||
|
|
||||||
|
assert json_array_length(members) = 2, 'The members count must be equal to 2';
|
||||||
|
assert members::jsonb @> jsonb_build_array(jsonb_build_object('id', _citizen_id3)),
|
||||||
|
'Members must contain citizen3';
|
||||||
|
|
||||||
|
-- update
|
||||||
|
select m into members from update_workgroup_members((created_workgroup->>'id')::uuid, json_build_array(
|
||||||
|
json_build_object('id', _citizen_id2),
|
||||||
|
json_build_object('id', _citizen_id)
|
||||||
|
)) m;
|
||||||
|
assert json_array_length(members) = 2, 'The members count must be equal to 2';
|
||||||
|
assert members::jsonb @> jsonb_build_array(jsonb_build_object('id', _citizen_id)),
|
||||||
|
'Members must contain citizen2';
|
||||||
|
assert not members::jsonb @> jsonb_build_array(jsonb_build_object('id', _citizen_id3)),
|
||||||
|
'Members must NOT contain citizen3';
|
||||||
|
|
||||||
|
-- remove
|
||||||
|
select m into members from remove_workgroup_members((created_workgroup->>'id')::uuid, json_build_array(
|
||||||
|
json_build_object('id', _citizen_id2)
|
||||||
|
)) m;
|
||||||
|
assert json_array_length(members) = 1, 'The members count must be equal to 1';
|
||||||
|
assert members::jsonb @> jsonb_build_array(jsonb_build_object('id', _citizen_id)),
|
||||||
|
'Members must contain citizen1';
|
||||||
|
assert not members::jsonb @> jsonb_build_array(jsonb_build_object('id', _citizen_id2)),
|
||||||
|
'Members must NOT contain citizen2';
|
||||||
|
|
||||||
rollback;
|
rollback;
|
||||||
raise notice 'workgroup test pass';
|
raise notice 'workgroup test pass';
|
||||||
end
|
end
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- select w->>'id' from json_array_elements('[{"id":"plop"}]') w
|
||||||
Reference in New Issue
Block a user