Refactoring of updateOpinions (route/repo/query)

Can nw be set multiple opinion on sigle query
fix OpinionVoter on CREATE
This commit is contained in:
2020-03-22 00:53:08 +01:00
parent 479793503c
commit 589b6f5245
10 changed files with 151 additions and 56 deletions

View File

@@ -45,15 +45,22 @@ class OpinionSteps : En, KoinTest {
}
}
private fun createOpinion(opinionChoiceName: String, articleId: String, firstName: String, lastName: String, id: String? = null) {
private fun createOpinion(
opinionChoiceName: String,
articleId: String,
firstName: String,
lastName: String,
id: String? = null
) {
val opinion = OpinionArticle(
id = id?.toUUID() ?: UUID.randomUUID(),
choice = get<OpinionChoiceRepository>().findOpinionsChoiceByName(opinionChoiceName)
?: error("Opinion Choice not exist"),
target = get<ArticleRepository>().findById(articleId.toUUID()) ?: error("Article not exist"),
createdBy = get<CitizenRepository>().findByUsername("$firstName-$lastName".toLowerCase().replace(' ', '-')) ?: error("Citizen not exist")
createdBy = get<CitizenRepository>().findByUsername("$firstName-$lastName".toLowerCase().replace(' ', '-'))
?: error("Citizen not exist")
)
get<OpinionRepository>().opinion(opinion)
get<OpinionRepository>().updateOpinions(opinion.choice, opinion.createdBy, opinion.target)
}
private fun createOpinionOnArticle(extraInfo: DataTable? = null) {
@@ -69,6 +76,6 @@ class OpinionSteps : En, KoinTest {
} ?: error("You must provide the 'article' parameter"),
createdBy = get<CitizenRepository>().findByUsername(username) ?: error("Citizen not exist")
)
get<OpinionRepository>().opinion(opinion)
get<OpinionRepository>().updateOpinions(opinion.choice, opinion.createdBy, opinion.target)
}
}

View File

@@ -65,7 +65,8 @@ internal class OpinionVoterTest {
every { user } returns tesla.user
}.let {
supports(OpinionVoter.Action.VIEW, it, opinion1) `should be` true
supports(OpinionVoter.Action.VIEW, it, article1) `should be` false
supports(OpinionVoter.Action.VIEW, it, article1) `should be` true
supports(OpinionVoter.Action.VIEW, it, einstein) `should be` false
supports(p, it, opinion1) `should be` false
}
}

View File

@@ -41,6 +41,8 @@ declare
opinion_choice1_id uuid = uuid_generate_v4();
opinion_choice2_id uuid = uuid_generate_v4();
opinion2 json;
_opinions json;
_opinions_deleted_ids uuid[];
begin
-- insert user for context
select insert_user(created_user) into created_user;
@@ -120,6 +122,46 @@ begin
select (resource#>>'{0, choice, name}') = 'Opinion1' from find_citizen_opinions(_citizen_id, null, null, 1, 0)
), 'find_citizen_opinions must return a list of opinion with name';
-- test update_citizen_opinions_by_target_id
select opinions into _opinions
from update_citizen_opinions_by_target_id(
array[opinion_choice1_id]::uuid[],
_citizen_id,
(created_article->>'id')::uuid,
'article'
);
assert (json_array_length(_opinions) = 1), format('Opinions updated must be count of 1. instead of: %s', json_array_length(_opinions));
assert(select (_opinions#>>'{0, choice, id}')::uuid = opinion_choice1_id), 'opinion1 is not inserted';
assert(
select (o#>>'{0, choice, name}') = 'Opinion1'
from find_citizen_opinions_by_target_id(_citizen_id, (created_article->>'id')::uuid) o),
'The opinion must have a name';
-- test update_citizen_opinions_by_target_id with multiple ids
select opinions into _opinions
from update_citizen_opinions_by_target_id(
array[opinion_choice1_id, opinion_choice2_id]::uuid[],
_citizen_id,
(created_article->>'id')::uuid,
'article'
);
assert (json_array_length(_opinions) = 2), format('(on multi update) Opinions updated must be count of 1. instead of: %s', json_array_length(_opinions));
assert(select (_opinions#>>'{0, choice, id}')::uuid = opinion_choice1_id), '(on multi update) opinion1 is not inserted';
assert(select (_opinions#>>'{1, choice, id}')::uuid = opinion_choice2_id), '(on multi update) opinion2 is not inserted';
assert(
select (o#>>'{0, choice, name}') = 'Opinion1'
from find_citizen_opinions_by_target_id(_citizen_id, (created_article->>'id')::uuid) o),
'(on multi update) The opinion must have a name';
-- test update_citizen_opinions_by_target_id if empty
select opinions, ids_deleted into _opinions, _opinions_deleted_ids
from update_citizen_opinions_by_target_id(
'{}'::uuid[],
_citizen_id,
(created_article->>'id')::uuid,
'article'
);
assert json_array_length(_opinions) = 0;
rollback;
raise notice 'opinion test pass';
end