Add opinion on find_article_by_id & find_articles request
create request for find_citizen_opinions_by_target_id create fixture for opinions
This commit is contained in:
28
.idea/runConfigurations/Fixtures_on_Test.xml
generated
Normal file
28
.idea/runConfigurations/Fixtures_on_Test.xml
generated
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Fixtures on Test" type="RunSql" factoryName="Run SQL">
|
||||||
|
<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" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/01-user.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/02-citizen.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/03-workgroup.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/04-article.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/05-constitution.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/06-follow.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/07-comment.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/08-vote.sql" />
|
||||||
|
<option value="$PROJECT_DIR$/src/main/resources/sql/fixtures/09-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" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
38
.idea/runConfigurations/Lint__Test___Run.xml
generated
Normal file
38
.idea/runConfigurations/Lint__Test___Run.xml
generated
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Lint, Test & Run" type="GradleRunConfiguration" factoryName="Gradle">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="env">
|
||||||
|
<map>
|
||||||
|
<entry key="SEND_GRID_KEY" value="$SEND_GRID_KEY$" />
|
||||||
|
</map>
|
||||||
|
</option>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="run" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<extension name="net.ashald.envfile">
|
||||||
|
<option name="IS_ENABLED" value="false" />
|
||||||
|
<option name="IS_SUBST" value="false" />
|
||||||
|
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||||
|
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||||
|
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||||
|
<ENTRIES>
|
||||||
|
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||||
|
</ENTRIES>
|
||||||
|
</extension>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2">
|
||||||
|
<option name="RunConfigurationTask" enabled="true" run_configuration_name="All Tests + Lint" run_configuration_type="JUnit" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
38
.idea/runConfigurations/Reset_DB___Run.xml
generated
Normal file
38
.idea/runConfigurations/Reset_DB___Run.xml
generated
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Reset DB & Run" type="GradleRunConfiguration" factoryName="Gradle">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="env">
|
||||||
|
<map>
|
||||||
|
<entry key="SEND_GRID_KEY" value="$SEND_GRID_KEY$" />
|
||||||
|
</map>
|
||||||
|
</option>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="run" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<extension name="net.ashald.envfile">
|
||||||
|
<option name="IS_ENABLED" value="false" />
|
||||||
|
<option name="IS_SUBST" value="false" />
|
||||||
|
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||||
|
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||||
|
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||||
|
<ENTRIES>
|
||||||
|
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||||
|
</ENTRIES>
|
||||||
|
</extension>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2">
|
||||||
|
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Fixtures on Dev" run_configuration_type="RunSql" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
26
.idea/runConfigurations/Test.xml
generated
Normal file
26
.idea/runConfigurations/Test.xml
generated
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Test" type="GradleRunConfiguration" factoryName="Gradle">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="env">
|
||||||
|
<map>
|
||||||
|
<entry key="SEND_GRID_KEY" value="$SEND_GRID_KEY$" />
|
||||||
|
</map>
|
||||||
|
</option>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="test" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
4
.idea/runConfigurations/Test_All_SQL.xml
generated
4
.idea/runConfigurations/Test_All_SQL.xml
generated
@@ -48,6 +48,10 @@
|
|||||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/vote/count_vote.sql" />
|
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/vote/count_vote.sql" />
|
||||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/vote/find_citizen_votes_by_target_ids.sql" />
|
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/vote/find_citizen_votes_by_target_ids.sql" />
|
||||||
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/vote/find_votes_by_citizen.sql" />
|
<option value="$PROJECT_DIR$/src/main/resources/sql/functions/vote/find_votes_by_citizen.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_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/opinion.sql" />
|
||||||
<option value="$PROJECT_DIR$/src/test/sql/user.sql" />
|
<option value="$PROJECT_DIR$/src/test/sql/user.sql" />
|
||||||
<option value="$PROJECT_DIR$/src/test/sql/citizen.sql" />
|
<option value="$PROJECT_DIR$/src/test/sql/citizen.sql" />
|
||||||
<option value="$PROJECT_DIR$/src/test/sql/article.sql" />
|
<option value="$PROJECT_DIR$/src/test/sql/article.sql" />
|
||||||
|
|||||||
39
.idea/runConfigurations/Test__Reset_DB___Run.xml
generated
Normal file
39
.idea/runConfigurations/Test__Reset_DB___Run.xml
generated
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Test, Reset DB & Run" type="GradleRunConfiguration" factoryName="Gradle">
|
||||||
|
<ExternalSystemSettings>
|
||||||
|
<option name="env">
|
||||||
|
<map>
|
||||||
|
<entry key="SEND_GRID_KEY" value="$SEND_GRID_KEY$" />
|
||||||
|
</map>
|
||||||
|
</option>
|
||||||
|
<option name="executionName" />
|
||||||
|
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||||
|
<option name="externalSystemIdString" value="GRADLE" />
|
||||||
|
<option name="scriptParameters" value="" />
|
||||||
|
<option name="taskDescriptions">
|
||||||
|
<list />
|
||||||
|
</option>
|
||||||
|
<option name="taskNames">
|
||||||
|
<list>
|
||||||
|
<option value="run" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="vmOptions" value="" />
|
||||||
|
</ExternalSystemSettings>
|
||||||
|
<extension name="net.ashald.envfile">
|
||||||
|
<option name="IS_ENABLED" value="false" />
|
||||||
|
<option name="IS_SUBST" value="false" />
|
||||||
|
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||||
|
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||||
|
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||||
|
<ENTRIES>
|
||||||
|
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||||
|
</ENTRIES>
|
||||||
|
</extension>
|
||||||
|
<GradleScriptDebugEnabled>true</GradleScriptDebugEnabled>
|
||||||
|
<method v="2">
|
||||||
|
<option name="RunConfigurationTask" enabled="true" run_configuration_name="All Tests + Lint" run_configuration_type="JUnit" />
|
||||||
|
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Fixtures on Dev" run_configuration_type="RunSql" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -1029,6 +1029,7 @@ components:
|
|||||||
- $ref: '#/components/schemas/CreatedAt'
|
- $ref: '#/components/schemas/CreatedAt'
|
||||||
- $ref: '#/components/schemas/lastVersion'
|
- $ref: '#/components/schemas/lastVersion'
|
||||||
- $ref: '#/components/schemas/Votable'
|
- $ref: '#/components/schemas/Votable'
|
||||||
|
- $ref: '#/components/schemas/Opinionable'
|
||||||
|
|
||||||
ArticleRequest:
|
ArticleRequest:
|
||||||
$ref: '#/components/schemas/ArticleBase'
|
$ref: '#/components/schemas/ArticleBase'
|
||||||
@@ -1210,6 +1211,15 @@ components:
|
|||||||
properties:
|
properties:
|
||||||
votes:
|
votes:
|
||||||
$ref: '#/components/schemas/VoteAggregation'
|
$ref: '#/components/schemas/VoteAggregation'
|
||||||
|
Opinionable:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
opinions:
|
||||||
|
type: string
|
||||||
|
additionalProperties: true
|
||||||
|
example:
|
||||||
|
Opinion1: 1
|
||||||
|
Opinion2: 55
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
29
src/main/resources/sql/fixtures/09-opinion.sql
Normal file
29
src/main/resources/sql/fixtures/09-opinion.sql
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
do
|
||||||
|
$$
|
||||||
|
declare
|
||||||
|
article_count int = (select count(*) from article);
|
||||||
|
_citizensIds uuid[] = (select array_agg(id) from citizen);
|
||||||
|
begin
|
||||||
|
delete from opinion_on_article;
|
||||||
|
delete from opinion_list;
|
||||||
|
|
||||||
|
insert into opinion_list (id, name, target)
|
||||||
|
select uuid_in(md5('opinion_list'||row_number() over ())::cstring), 'Opinion'||row_number() over (), 'article'
|
||||||
|
from generate_series(0,20);
|
||||||
|
|
||||||
|
for i in 0..9 loop
|
||||||
|
insert into opinion_on_article (id, created_by_id, target_id, opinion)
|
||||||
|
select
|
||||||
|
uuid_in(md5('opinion_on_article'||rn+(article_count*i))::cstring),
|
||||||
|
z.id,
|
||||||
|
a.id,
|
||||||
|
uuid_in(md5('opinion_list'||((rn+i) % 5 +1))::cstring)
|
||||||
|
from (select *, row_number() over ()+i+5 % 5 rn from citizen) z
|
||||||
|
join (select *, row_number() over () rn from article) a using (rn);
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
raise notice '% opinion inserted', (select count(*) from opinion_on_article);
|
||||||
|
|
||||||
|
raise notice 'opinions fixtures done';
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
@@ -8,7 +8,8 @@ begin
|
|||||||
select
|
select
|
||||||
a.*,
|
a.*,
|
||||||
find_citizen_by_id(a.created_by_id) as created_by,
|
find_citizen_by_id(a.created_by_id) as created_by,
|
||||||
count_vote(a.id) as votes
|
count_vote(a.id) as votes,
|
||||||
|
count_opinion(a.id) as opinions
|
||||||
into resource
|
into resource
|
||||||
from article as a
|
from article as a
|
||||||
where a.id = _id
|
where a.id = _id
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ begin
|
|||||||
a.*,
|
a.*,
|
||||||
find_citizen_by_id(a.created_by_id) as created_by,
|
find_citizen_by_id(a.created_by_id) as created_by,
|
||||||
count_vote(a.id) as votes,
|
count_vote(a.id) as votes,
|
||||||
|
count_opinion(a.id) as opinions,
|
||||||
zdb.score(a.ctid) _score
|
zdb.score(a.ctid) _score
|
||||||
from article as a
|
from article as a
|
||||||
where (
|
where (
|
||||||
@@ -60,3 +61,4 @@ end;
|
|||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- drop function if exists find_articles(text, json, text, text, int, int);
|
-- drop function if exists find_articles(text, json, text, text, int, int);
|
||||||
|
-- select find_article_by_id('d91aa0cd-61d6-83cc-41bb-8d5656e130f7');
|
||||||
@@ -3,7 +3,7 @@ create or replace function count_opinion(_target_id uuid, out resource json)
|
|||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
agg jsonb;
|
agg jsonb;
|
||||||
empty jsonb = '[]'::jsonb;
|
empty jsonb = '{}'::jsonb;
|
||||||
begin
|
begin
|
||||||
select jsonb_object_agg(t.label, t.total)
|
select jsonb_object_agg(t.label, t.total)
|
||||||
into agg
|
into agg
|
||||||
@@ -18,10 +18,10 @@ begin
|
|||||||
order by ol.name
|
order by ol.name
|
||||||
) t;
|
) t;
|
||||||
|
|
||||||
resource = empty || coalesce(agg, empty);
|
resource = coalesce(agg, empty);
|
||||||
end;
|
end;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
-- drop function if exists count_opinion(uuid);
|
-- drop function if exists count_opinion(uuid);
|
||||||
|
|
||||||
-- select * from count_opinion('ced1563f-ecf5-4f11-8518-8aeceff3c13a');
|
-- select * from count_opinion('d91aa0cd-61d6-83cc-41bb-8d5656e130f7');
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
create or replace function find_citizen_opinions_by_target_id(
|
||||||
|
_citizen_id uuid,
|
||||||
|
_id uuid,
|
||||||
|
out resource json
|
||||||
|
) language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
select
|
||||||
|
json_agg(t)
|
||||||
|
into resource
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
o.*,
|
||||||
|
ol.name
|
||||||
|
from opinion as o
|
||||||
|
join opinion_list ol on o.opinion = ol.id
|
||||||
|
|
||||||
|
where target_id = _id
|
||||||
|
and created_by_id = _citizen_id
|
||||||
|
|
||||||
|
order by
|
||||||
|
ol.name
|
||||||
|
limit 100
|
||||||
|
) as t;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists find_citizen_votes_by_target_ids(uuid, uuid[], regclass);
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
create or replace function find_citizen_opinions_by_target_ids(
|
||||||
|
_citizen_id uuid,
|
||||||
|
_ids uuid[],
|
||||||
|
out resource json
|
||||||
|
) language plpgsql as
|
||||||
|
$$
|
||||||
|
begin
|
||||||
|
select
|
||||||
|
jsonb_agg(find_citizen_opinions_by_target_id(_citizen_id, o)) into resource
|
||||||
|
from unnest(_ids) o
|
||||||
|
|
||||||
|
order by
|
||||||
|
_ids
|
||||||
|
limit 100;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists find_citizen_votes_by_target_ids(uuid, uuid[], regclass);
|
||||||
@@ -76,6 +76,22 @@ begin
|
|||||||
assert (select count(*) = 1 from opinion_on_article), 'opinion must be inserted';
|
assert (select count(*) = 1 from opinion_on_article), 'opinion must be inserted';
|
||||||
assert (select opinion = opinion1 from opinion_on_article limit 1), 'opinion must be inserted';
|
assert (select opinion = opinion1 from opinion_on_article limit 1), 'opinion must be inserted';
|
||||||
|
|
||||||
|
assert(select (a#>>'{opinions, Opinion1}')::int = 1
|
||||||
|
from find_article_by_id((created_article->>'id')::uuid) a), 'the article must be have a opinion';
|
||||||
|
|
||||||
|
assert(
|
||||||
|
select (o#>>'{0, name}') = 'Opinion1'
|
||||||
|
from find_citizen_opinions_by_target_id(_citizen_id, (created_article->>'id')::uuid) o),
|
||||||
|
'The opinion must have a name';
|
||||||
|
|
||||||
|
raise notice '%', (
|
||||||
|
select o
|
||||||
|
from find_citizen_opinions_by_target_ids(_citizen_id, array[(created_article->>'id')::uuid]) o);
|
||||||
|
|
||||||
|
assert(
|
||||||
|
select (o#>>'{0, 0, name}') = 'Opinion1'
|
||||||
|
from find_citizen_opinions_by_target_ids(_citizen_id, array[(created_article->>'id')::uuid]) o),
|
||||||
|
'The first opinion must have a name';
|
||||||
|
|
||||||
-- delete vote and context
|
-- delete vote and context
|
||||||
delete from opinion;
|
delete from opinion;
|
||||||
@@ -85,5 +101,5 @@ begin
|
|||||||
delete from "user";
|
delete from "user";
|
||||||
|
|
||||||
raise notice 'opinion test pass';
|
raise notice 'opinion test pass';
|
||||||
end;
|
end
|
||||||
$$;
|
$$;
|
||||||
|
|||||||
Reference in New Issue
Block a user