Fix following in other version of article

This commit is contained in:
2020-03-22 03:44:26 +01:00
parent 589b6f5245
commit 75af31ab73
5 changed files with 45 additions and 24 deletions

View File

@@ -55,13 +55,14 @@ sealed class Follow<IN : TargetRef, OUT : TargetRef>(override var requester: Req
open fun findFollow( open fun findFollow(
citizen: CitizenI, citizen: CitizenI,
target: UuidEntity target: TargetRef
): FollowEntity<OUT>? = ): FollowEntity<OUT>? =
requester requester
.getFunction("find_follow") .getFunction("find_follow")
.selectOne( .selectOne(
"citizen_id" to citizen.id, "citizen_id" to citizen.id,
"target_id" to target.id "target_id" to target.id,
"target_reference" to target.reference
) )
fun findFollowsByTarget( fun findFollowsByTarget(

View File

@@ -1,11 +1,31 @@
create or replace function find_follow( create or replace function find_follow(
_target_id uuid, _target_id uuid,
_citizen_id uuid, _citizen_id uuid,
_target_reference regclass,
out resource json, out resource json,
out following boolean out following boolean
) language plpgsql as ) language plpgsql as
$$ $$
declare
_target_ids uuid[];
begin begin
if (_target_reference = 'article'::regclass) then
select array_agg(a2.id) into _target_ids
from article a1
join article a2 using (version_id)
where a1.id = _target_id;
select to_json(t) into resource from (
select
f.*,
json_build_object('id', f.target_id, 'reference', f.target_reference) as target,
find_citizen_by_id(f.created_by_id) as created_by
from follow as f
where f.created_by_id = _citizen_id
and array[f.target_id] <@ _target_ids
limit 1
) as t;
else
select to_json(t) select to_json(t)
into resource into resource
from ( from (
@@ -17,6 +37,7 @@ begin
where f.created_by_id = _citizen_id where f.created_by_id = _citizen_id
and f.target_id = _target_id and f.target_id = _target_id
) as t; ) as t;
end if;
select resource is not null into following; select resource is not null into following;
end; end;

View File

@@ -1,10 +1,9 @@
create or replace function fixture_article(in name text default 'love', _citizen_id uuid default fixture_citizen(), out _article_id uuid) create or replace function fixture_article(in name text default 'love', _citizen_id uuid default fixture_citizen(), _version_id uuid default uuid_generate_v4(), out _article_id uuid)
language plpgsql as language plpgsql as
$$ $$
declare declare
created_article json := $json$ created_article json := $json$
{ {
"version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9",
"title": "Love the world", "title": "Love the world",
"anonymous": false, "anonymous": false,
"content": "bla bal bla", "content": "bla bal bla",
@@ -19,6 +18,7 @@ begin
if (name = 'love') then if (name = 'love') then
-- set citizen id to article -- set citizen id to article
created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json; created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
created_article := jsonb_set(created_article::jsonb, '{version_id}'::text[], to_jsonb(_version_id), true)::json;
assert created_article#>>'{created_by, id}' = _citizen_id::text, format('citizenId in article must be the same as citizen, %s != %s', created_article#>>'{created_by, id}', _citizen_id::text); assert created_article#>>'{created_by, id}' = _citizen_id::text, format('citizenId in article must be the same as citizen, %s != %s', created_article#>>'{created_by, id}', _citizen_id::text);
-- upsert article -- upsert article
select (a->>'id')::uuid into _article_id from upsert_article(created_article) a; select (a->>'id')::uuid into _article_id from upsert_article(created_article) a;

View File

@@ -4,12 +4,13 @@ declare
_citizen_id uuid := fixture_citizen('george'); _citizen_id uuid := fixture_citizen('george');
_citizen_id2 uuid := fixture_citizen('john'); _citizen_id2 uuid := fixture_citizen('john');
first_article_id uuid := fixture_article(_citizen_id := _citizen_id); _version_id1 uuid = uuid_generate_v4();
first_article_id uuid := fixture_article(_citizen_id := _citizen_id, _version_id := _version_id1);
first_article_updated_id uuid; first_article_updated_id uuid;
begin begin
perform follow('citizen'::regclass, _citizen_id, _citizen_id2); perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 1 from follow), 'follow must be inserted'; assert (select count(*) = 1 from follow), 'follow must be inserted';
assert (select following = true from find_follow(_citizen_id, _citizen_id2)), 'find_follow must return the following'; assert (select following = true from find_follow(_citizen_id, _citizen_id2, 'citizen')), 'find_follow must return the following';
perform follow('citizen'::regclass, _citizen_id, _citizen_id2); perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 1 from follow), 'follow must be inserted'; assert (select count(*) = 1 from follow), 'follow must be inserted';
@@ -18,13 +19,15 @@ begin
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow'; assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow';
perform follow('article'::regclass, first_article_id, _citizen_id); perform follow('article'::regclass, first_article_id, _citizen_id);
assert (select following = true from find_follow(first_article_id, _citizen_id)), 'find_follow must return the following'; assert (select following = true from find_follow(first_article_id, _citizen_id, 'article')), 'find_follow must return the following';
assert (select following = false from find_follow(first_article_id, _citizen_id2)), 'find_follow must not return the following if not followinf'; assert (select following = false from find_follow(first_article_id, _citizen_id2, 'article')), 'find_follow must not return the following if not followinf';
assert (select count(*) = 1 from follow), 'must have 1 following'; assert (select count(*) = 1 from follow), 'must have 1 following';
-- add new version for article, then unfollow the new one -- add new version for article, then unfollow the new one
select fixture_article(_citizen_id := _citizen_id) into first_article_updated_id; select fixture_article(_citizen_id := _citizen_id, _version_id := _version_id1) into first_article_updated_id;
assert first_article_id != first_article_updated_id; assert first_article_id != first_article_updated_id;
assert (select following = true from find_follow(first_article_id, _citizen_id, 'article')), '(v1) find_follow must return the following';
assert (select following = true from find_follow(first_article_updated_id, _citizen_id, 'article')), '(v2) find_follow must return the following';
perform unfollow('article'::regclass, first_article_id, _citizen_id); perform unfollow('article'::regclass, first_article_id, _citizen_id);
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version'; assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version';

View File

@@ -87,7 +87,3 @@ begin
raise notice 'workgroup test pass'; raise notice 'workgroup test pass';
end end
$$; $$;
-- select w->>'id' from json_array_elements('[{"id":"plop"}]') w