Create find_follow.sql
This commit is contained in:
18
.idea/runConfigurations/Clean_Dev_Database.xml
generated
Normal file
18
.idea/runConfigurations/Clean_Dev_Database.xml
generated
Normal file
@@ -0,0 +1,18 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Clean Dev Database" type="RunSql" factoryName="Run SQL" folderName="SQL DEV">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/migrations/0000-init_schema.down.sql" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="targets">
|
||||
<list>
|
||||
<Target>
|
||||
<option name="dsId" value="28368159-3c2d-4612-8719-e55ce11b962a" />
|
||||
<option name="namespace" value="database/"dc-project"/schema/"public"" />
|
||||
</Target>
|
||||
</list>
|
||||
</option>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
2
.idea/runConfigurations/Fixtures_on_Test.xml
generated
2
.idea/runConfigurations/Fixtures_on_Test.xml
generated
@@ -1,5 +1,5 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Fixtures on Test" type="RunSql" factoryName="Run SQL">
|
||||
<configuration default="false" name="Fixtures on Test" type="RunSql" factoryName="Run SQL" folderName="SQL TEST">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/migrations/0000-init_schema.down.sql" />
|
||||
|
||||
19
.idea/runConfigurations/Reset_Test_database.xml
generated
Normal file
19
.idea/runConfigurations/Reset_Test_database.xml
generated
Normal file
@@ -0,0 +1,19 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Reset Test database" type="RunSql" factoryName="Run SQL" folderName="SQL TEST">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/migrations/0000-init_schema.down.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/migrations/0000-init_schema.up.sql" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="targets">
|
||||
<list>
|
||||
<Target>
|
||||
<option name="dsId" value="a9a6d0e9-327d-4e7d-9b93-3cb6f7948866" />
|
||||
<option name="namespace" value="database/"test"/schema/"public"" />
|
||||
</Target>
|
||||
</list>
|
||||
</option>
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -1,5 +1,5 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Reset database schema and import Fixtures" type="RunSql" factoryName="Run SQL">
|
||||
<configuration default="false" name="Reset database schema and import Fixtures" type="RunSql" factoryName="Run SQL" folderName="SQL DEV">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/migrations/0000-init_schema.down.sql" />
|
||||
|
||||
2
.idea/runConfigurations/Test_All_SQL.xml
generated
2
.idea/runConfigurations/Test_All_SQL.xml
generated
@@ -1,5 +1,5 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Test All SQL" type="RunSql" factoryName="Run SQL">
|
||||
<configuration default="false" name="Test All SQL" type="RunSql" factoryName="Run SQL" folderName="SQL TEST">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/migrations/0000-init_schema.down.sql" />
|
||||
|
||||
27
.idea/runConfigurations/Test_Follow.xml
generated
Normal file
27
.idea/runConfigurations/Test_Follow.xml
generated
Normal file
@@ -0,0 +1,27 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Test Follow" type="RunSql" factoryName="Run SQL" folderName="SQL TEST">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/find_follow.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/find_follows_article_by_citizen.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/find_follows_article_by_target.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/find_follows_by_citizen.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/find_follows_constitution_by_citizen.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/follow.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/follow/unfollow.sql" />
|
||||
<option value="$PROJECT_DIR$/src/test/sql/follow.sql" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="targets">
|
||||
<list>
|
||||
<Target>
|
||||
<option name="dsId" value="a9a6d0e9-327d-4e7d-9b93-3cb6f7948866" />
|
||||
<option name="namespace" value="database/"test"" />
|
||||
</Target>
|
||||
</list>
|
||||
</option>
|
||||
<method v="2">
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Reset database" run_configuration_type="RunSql" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
29
.idea/runConfigurations/Test_Opinions.xml
generated
Normal file
29
.idea/runConfigurations/Test_Opinions.xml
generated
Normal file
@@ -0,0 +1,29 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Test Opinions" type="RunSql" factoryName="Run SQL" folderName="SQL TEST">
|
||||
<option name="files">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/article/find_article_by_id.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/count_opinion.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/find_citizen_opinions.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/find_citizen_opinions_by_target_id.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/find_citizen_opinions_by_target_ids.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/find_opinion_choice_by_id.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/find_opinion_choices.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/upsert_opinion.sql" />
|
||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/opinion/find_opinion_by_opinion.sql" />
|
||||
<option value="$PROJECT_DIR$/src/test/sql/opinion.sql" />
|
||||
</list>
|
||||
</option>
|
||||
<option name="targets">
|
||||
<list>
|
||||
<Target>
|
||||
<option name="dsId" value="a9a6d0e9-327d-4e7d-9b93-3cb6f7948866" />
|
||||
<option name="namespace" value="database/"test"/schema/"public"" />
|
||||
</Target>
|
||||
</list>
|
||||
</option>
|
||||
<method v="2">
|
||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Reset database" run_configuration_type="RunSql" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
25
src/main/resources/sql/functions/follow/find_follow.sql
Normal file
25
src/main/resources/sql/functions/follow/find_follow.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
create or replace function find_follow(
|
||||
_target_id uuid,
|
||||
_citizen_id uuid,
|
||||
out resource json,
|
||||
out following boolean
|
||||
) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
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 f.target_id = _target_id
|
||||
) as t;
|
||||
|
||||
select resource is not null into following;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_follow(uuid, uuid);
|
||||
@@ -25,6 +25,9 @@ declare
|
||||
"email":"george.michel2@gmail.com"
|
||||
}
|
||||
$json$;
|
||||
|
||||
created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"], "draft":false}';
|
||||
first_article_id uuid;
|
||||
begin
|
||||
-- insert user for context
|
||||
select insert_user(created_user) into created_user;
|
||||
@@ -41,15 +44,27 @@ begin
|
||||
|
||||
|
||||
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
|
||||
assert (select count(*) > 0 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';
|
||||
|
||||
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
|
||||
assert (select count(*) > 0 from follow), 'follow must be inserted';
|
||||
assert (select count(*) = 1 from follow), 'follow must be inserted';
|
||||
|
||||
perform unfollow('citizen'::regclass, _citizen_id, _citizen_id2);
|
||||
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow';
|
||||
|
||||
-- upsert article
|
||||
created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
||||
select upsert_article(created_article) into created_article;
|
||||
first_article_id = (created_article->>'id')::uuid;
|
||||
|
||||
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 = false from find_follow(first_article_id, _citizen_id2)), 'find_follow must not return the following if not followinf';
|
||||
|
||||
-- delete follow and context
|
||||
delete from follow;
|
||||
delete from article;
|
||||
delete from citizen;
|
||||
delete from "user";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user