Move SQL test files

This commit is contained in:
2021-03-20 04:23:31 +01:00
parent 29d4d6ec25
commit f458d7b674
17 changed files with 14 additions and 12 deletions

1
src/test/resources/sql/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
allSql.sql

View File

@@ -0,0 +1,78 @@
do
$$
declare
_citizen_id uuid := fixture_citizen();
_workgroup_id uuid := fixture_workgroup(_citizen_id => _citizen_id);
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}';
created_article_grouped json := '{"version_id":"35f5c4c3-0629-4405-9956-12557020c9f5", "title": "Love my group", "anonymous": false, "content": "groupy groupy", "tags": ["love", "group"], "draft":false}';
created_article_v2 json;
first_article_id uuid;
second_article_id uuid;
selected_article json;
selected_total int;
begin
created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), 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);
-- upsert article
select upsert_article(created_article) into created_article;
assert created_article->>'version_id' is not null, 'version_id should not be null';
assert (created_article->>'version_number')::int = 1, format('version_number must be equal to 1, %s instead', created_article->>'version_number');
assert (created_article->>'last_version')::bool = true, 'The first insert must be set to the last version';
first_article_id = (created_article->>'id')::uuid;
-- try to create new version
select upsert_article(created_article) into created_article_v2;
assert (created_article_v2->>'version_number')::int = 2, format('version_number must be equal to 2, %s instead', created_article_v2->>'version_number');
assert (created_article_v2->>'last_version')::bool = true, 'The second insert must be set to the last version';
second_article_id = (created_article_v2->>'id')::uuid;
created_article_grouped := jsonb_set(created_article_grouped::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
created_article_grouped := jsonb_set(created_article_grouped::jsonb, '{workgroup}'::text[], jsonb_build_object('id', _workgroup_id::text), true)::json;
select upsert_article(created_article_grouped) into created_article_grouped;
-- get articles versions by version_id
select resource, total into selected_article, selected_total from find_articles_versions_by_version_id((created_article->>'version_id')::uuid);
assert selected_article#>>'{0,title}' = 'Love the world', format('title must be "Love the world", %s', selected_article#>>'{0,title}');
assert (selected_article#>>'{0,version_number}')::int = 2, format('version_id must be 2, %s instead', selected_article#>>'{0,version_number}');
assert selected_total = 2, format('the total must be 2, %s instead', selected_total);
-- get articles by workgroup id
select resource, total into selected_article, selected_total from find_articles(_filter => json_build_object('workgroup_id', _workgroup_id));
assert selected_article#>>'{0,title}' = 'Love my group', format('title must be "Love my group", %s', selected_article#>>'{0,title}');
assert selected_total = 1, format('the total must be 1, %s instead', selected_total);
-- get articles versions by id
select resource, total into selected_article, selected_total from find_articles_versions_by_id((created_article->>'id')::uuid);
assert selected_article#>>'{0,title}' = 'Love the world', format('title must be "Love the world", %s', selected_article#>>'{0,title}');
assert (selected_article#>>'{0,version_number}')::int = 2, format('version_id must be 2, %s instead', selected_article#>>'{0,version_number}');
assert selected_total = 2, format('the total must be 2, %s instead', selected_total);
-- get article by id and check the title
select find_article_by_id((created_article_v2->>'id')::uuid) into selected_article;
assert selected_article->>'title' = 'Love the world', format('title must be "Love the world", %s', selected_article->>'title');
-- get article by version_id and check the title
select find_last_article_by_version_id((created_article_v2->>'version_id')::uuid) into selected_article;
assert selected_article->>'title' = 'Love the world', format('title must be "Love the world", %s', selected_article->>'title');
assert (selected_article->>'version_number')::int = 2, format('version_id must be 2, %s instead', selected_article->>'version_number');
-- update to draft, then the last_version column must be change
update article
set draft = true
where id = second_article_id;
select find_last_article_by_version_id((created_article_v2->>'version_id')::uuid) into selected_article;
assert (selected_article->>'version_number')::int = 1, format('version_id must be 1, %s instead', selected_article->>'version_number');
update article
set draft = false
where id = second_article_id;
select find_last_article_by_version_id((created_article_v2->>'version_id')::uuid) into selected_article;
assert (selected_article->>'version_number')::int = 2, format('version_id must be 2, %s instead', selected_article->>'version_number');
rollback;
raise notice 'article test pass';
end
$$;

View File

@@ -0,0 +1,54 @@
do
$$
declare
wrong_citizen json;
_user_id uuid := fixture_user();
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}';
created_citizen_with_user json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel2@gmail.com", "user":{"username": "george junior", "password": "azerty", "roles": ["ROLE_USER"]}}';
selected_citizen json;
begin
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);
-- insert new citizen
select upsert_citizen(created_citizen) into created_citizen;
assert created_citizen->>'birthday' = '2001-01-01'::text, format('birthday of inserted citizen must be the same of the original object, %s != %s', created_citizen->>'birthday', '2001-01-01'::text);
-- insert new citizen
select insert_citizen_with_user(created_citizen_with_user) into created_citizen_with_user;
assert created_citizen_with_user->>'birthday' = '2001-01-01'::text, format('birthday of inserted citizen must be the same of the original object, %s != %s', created_citizen->>'birthday', '2001-01-01'::text);
assert created_citizen_with_user#>>'{user, username}' = 'george junior', 'username must be george';
-- insert citizen without first name and test if throw exception
wrong_citizen := (created_citizen::jsonb - '{name, first_name}'::text[])::json;
begin
select upsert_citizen(wrong_citizen) into wrong_citizen;
assert false, 'upsert_citizen must be throw exception if first_name not exist';
exception when not_null_violation then
end;
-- get citizen by id and check the first name
select find_citizen_by_id((created_citizen->>'id')::uuid) into selected_citizen;
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
-- get citizen by user id and check the first name
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
-- get citizen by username and check the first name
select find_citizen_by_username(created_citizen#>>'{user, username}') into selected_citizen;
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
-- get citizen by name and check the first name
select find_citizen_by_name(created_citizen->'name') into selected_citizen;
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
rollback;
-- check if find by id return null if citizen not exist
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;
assert selected_citizen is null, format('citizen must be null if not exist, %s', selected_citizen);
raise notice 'citizen test pass';
end;
$$;

View File

@@ -0,0 +1,104 @@
do
$$
declare
_comment_id uuid;
_comment_id_response uuid;
_comment_id_response2 uuid;
_selected_comments json;
_selected_comments_total int;
_find_comments_by_target_result json;
_find_comments_by_parent_result json;
_find_comments_by_id_result json;
_comment json;
_article_id uuid := fixture_article();
_citizen_id uuid := fixture_citizen('john');
begin
_comment = json_build_object(
'id', 'a2962f49-74e6-4f20-9c13-36f3ccbc4ad7',
'target', jsonb_build_object('id', _article_id),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'Ho my god !'
);
select "comment"(
reference => 'article'::regclass,
resource => _comment
) into _comment_id;
assert (select count(*) = 1 from "comment"), 'comment must be inserted, "' || (select count(*) from "comment") || '" exist';
assert (select com.content = 'Ho my god !' from "comment" com), 'the content of comment must be "Ho my god !" instead of "' || (select com.content from "comment" as com) || '"';
select find_comment_by_id(_comment_id) into _find_comments_by_id_result;
assert (_find_comments_by_id_result->>'content' = 'Ho my god !'), format('content of comment must be "Ho my god !" instead of %s', _find_comments_by_id_result->>'content');
perform edit_comment(
_id => _comment_id,
_content => 'edited content'::text
);
assert (select count(*) = 1 from "comment"), 'edit comment must not insert new comment';
assert (select count(*) = 1 from "comment" where content = 'edited content'), 'edit comment must not insert new comment';
select resource, total
into _selected_comments, _selected_comments_total
from find_comments_by_citizen(_citizen_id);
assert (_selected_comments_total = 1), 'the number of comments for this citizen must be 1, "' || _selected_comments_total || '" returned';
assert (_selected_comments#>>'{0,content}' = 'edited content'),
'the content of first comment for this citizen must be "edited content", "' || (_selected_comments#>>'{0,content}') || '" returned';
select resource, total
into _selected_comments, _selected_comments_total
from find_comments_by_citizen(_citizen_id);
assert (_selected_comments_total = 1), 'the number of comments for this citizen must be 1, "' || _selected_comments_total || '" returned';
assert (_selected_comments#>>'{0,content}' = 'edited content'),
'the content of first comment for this citizen must be "edited content", "' || (_selected_comments#>>'{0,content}') || '" returned';
select resource, total
into _selected_comments, _selected_comments_total
from find_comments_by_citizen(_citizen_id, 'constitution'::regclass);
assert (_selected_comments_total = 0), 'the number of comments for this citizen must be 0, "' || _selected_comments_total || '" returned';
_comment = json_build_object(
'id', '50962646-07b6-42a3-9798-d756b9b6e2ba',
'target', jsonb_build_object('id', _article_id),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'God not exist',
'parent', json_build_object('id', _comment_id)
);
select "comment"(
reference => 'article'::regclass,
resource => _comment
) into _comment_id_response;
_comment = json_build_object(
'id', 'ce82e683-23a8-4977-92fb-8d61a3ec995a',
'target', jsonb_build_object('id', _article_id),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'are you really sure ?',
'parent', json_build_object('id', _comment_id_response)
);
select "comment"(
reference => 'article'::regclass,
resource => _comment
) into _comment_id_response2;
assert (select count(*) = 3 from "comment"), 'response must be inserted';
assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response);
assert (select com.parents_ids @> ARRAY[_comment_id_response] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id_response::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2);
assert (select com.parents_ids @> ARRAY[_comment_id] from "comment" com where id = _comment_id_response2), 'parents_ids not contain "' || _comment_id::text || '" ' || (select com.parents_ids::text[] from "comment" com where id = _comment_id_response2);
select resource into _find_comments_by_target_result
from find_comments_by_target((_article_id)::uuid);
assert json_array_length(_find_comments_by_target_result) = 1,
'the result should contain 1 comment, ' || json_array_length(_find_comments_by_target_result) || ' returned';
assert (_find_comments_by_target_result#>>'{0,content}') = 'edited content', 'the first content must contain "edited content", "' || (_find_comments_by_target_result#>>'{0,content}') || '" returned';
select resource into _find_comments_by_parent_result
from find_comments_by_parent(_comment_id_response);
assert json_array_length(_find_comments_by_parent_result) = 1,
'the result should contain 1 comment, ' || json_array_length(_find_comments_by_parent_result) || ' returned';
assert (_find_comments_by_parent_result#>>'{0,content}') = 'are you really sure ?', 'the third content must contain "are you really sure ?", "' || (_find_comments_by_parent_result#>>'{1,content}') || '" returned';
rollback;
raise notice 'comment test pass';
end
$$;

View File

@@ -0,0 +1,32 @@
do
$$
declare
_article_id uuid := fixture_article();
_citizen_id uuid := fixture_citizen('john');
created_constitution json := $json$
{
"version_id": "18ff6dd6-3bc1-4c59-82f0-5e2a8d54ae3e",
"title": "Love the world",
"anonymous": false,
"titles": [
{
"name": "titleOne"
},
{
"name": "titleTwo"
}
]
}
$json$;
begin
-- create new constitution
created_constitution := jsonb_set(created_constitution::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
created_constitution := jsonb_set(created_constitution::jsonb, '{titles, 0, articles}'::text[], jsonb_build_array(jsonb_build_object('id', _article_id)), true)::json;
select upsert_constitution(created_constitution) into created_constitution;
assert (created_constitution->>'version_number')::int = 1, format('version_number must be equal to 1, %s instead', created_constitution->>'version_number');
assert created_constitution#>>'{titles, 0, name}' = 'titleOne'::text, format('the name of the first title of contitution must be %s, not %s', 'titleOne', created_constitution#>>'{titles, 0, name}');
rollback;
raise notice 'constitution test pass';
end
$$;

View File

@@ -0,0 +1,18 @@
create or replace function fixture_user(in name text default 'george', out user_id uuid)
language plpgsql as
$$
declare
created_user json;
begin
if (name = 'george') then
created_user = '{"username": "george", "password": "azerty", "roles": ["ROLE_USER"]}';
elseif (name = 'john') then
created_user = '{"username": "john", "password": "qwerty", "roles": ["ROLE_USER"]}';
elseif (name = 'tesla') then
created_user = '{"username": "tesla", "password": "azerty", "roles": ["ROLE_USER"]}';
end if;
select insert_user(created_user) into created_user;
user_id := created_user->>'id';
end
$$;

View File

@@ -0,0 +1,24 @@
create or replace function fixture_citizen(in name text default 'george', out _citizen_id uuid)
language plpgsql as
$$
declare
_user_id uuid;
created_citizen json;
begin
_user_id = fixture_user(name);
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;
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;
_citizen_id := created_citizen->>'id';
end
$$;

View File

@@ -0,0 +1,29 @@
create or replace function fixture_workgroup(in name text default 'vert', _citizen_id uuid default fixture_citizen(), out _workgroup_id uuid)
language plpgsql as
$$
declare
created_workgroup json;
begin
if (name = 'vert') then
created_workgroup = '{
"name": "Le groupe des vert",
"description": "test",
"anonymous": false
}';
elseif (name = 'rouge') then
created_workgroup = '{
"name": "Le groupe des rouge",
"description": "test",
"anonymous": false
}';
end if;
created_workgroup := jsonb_set(created_workgroup::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
assert created_workgroup#>>'{created_by, id}' = _citizen_id::text, format('citizenId in workgroup must be the same as citizen, %s != %s', created_workgroup#>>'{created_by, id}', _citizen_id::text);
-- upsert workgroup
select upsert_workgroup(created_workgroup) into created_workgroup;
select (created_workgroup->>'id')::uuid into _workgroup_id;
assert created_workgroup->>'description' is not null, 'description should not be null';
end;
$$;

View File

@@ -0,0 +1,27 @@
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
$$
declare
created_article json := $json$
{
"title": "Love the world",
"anonymous": false,
"content": "bla bal bla",
"tags": [
"love",
"test"
],
"draft":false
}
$json$;
begin
if (name = 'love') then
-- 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, '{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);
-- upsert article
select (a->>'id')::uuid into _article_id from upsert_article(created_article) a;
end if;
end;
$$;

View File

@@ -0,0 +1,38 @@
do
$$
declare
_citizen_id uuid := fixture_citizen('george');
_citizen_id2 uuid := fixture_citizen('john');
_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;
begin
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 1 from follow), 'follow must be inserted';
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);
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';
perform follow('article'::regclass, first_article_id, _citizen_id);
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, 'article')), 'find_follow must not return the following if not followinf';
assert (select count(*) = 1 from follow), 'must have 1 following';
-- add new version for article, then unfollow the new one
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 (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);
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version';
rollback;
raise notice 'follow test pass';
end;
$$;

View File

@@ -0,0 +1,168 @@
do
$$
declare
created_user json := '{"username": "george", "password": "azerty", "roles": ["ROLE_USER"]}';
created_user2 json := '{"username": "george2", "password": "azerty", "roles": ["ROLE_USER"]}';
_citizen_id uuid;
_citizen_id2 uuid;
created_citizen json := $json$
{
"name": {
"first_name": "George",
"last_name": "MICHEL"
},
"birthday": "2001-01-01",
"email":"george.michel@gmail.com"
}
$json$;
created_citizen2 json := $json$
{
"name": {
"first_name": "George2",
"last_name": "MICHEL2"
},
"birthday": "2001-01-02",
"email":"george.michel2@gmail.com"
}
$json$;
created_article json := $json$
{
"version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9",
"title": "Love the world",
"anonymous": false,
"content": "bla bal bla",
"tags": [
"love",
"test"
],
"draft":false
}
$json$;
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;
select insert_user(created_user2) into created_user2;
created_citizen := jsonb_set(created_citizen::jsonb, '{user}'::text[], jsonb_build_object('id', created_user->>'id'), true)::json;
created_citizen2 := jsonb_set(created_citizen2::jsonb, '{user}'::text[], jsonb_build_object('id', created_user2->>'id'), true)::json;
-- insert new citizen for context
select upsert_citizen(created_citizen) into created_citizen;
select upsert_citizen(created_citizen2) into created_citizen2;
_citizen_id := created_citizen->>'id';
_citizen_id2 := created_citizen2->>'id';
created_article := jsonb_set(created_article::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), 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);
-- upsert article
select upsert_article(created_article) into created_article;
select (h->>'id')::uuid into opinion_choice1_id from upsert_opinion_choice('{"name": "Opinion1", "target":["article"]}') h;
assert opinion_choice1_id is not null, 'Opinion choice must be return json with id';
select (h->>'id')::uuid into opinion_choice2_id from upsert_opinion_choice('{"name": "Opinion2"}') h;
perform upsert_opinion_choice('{"name": "Opinion3", "target":["article"]}') h;
perform upsert_opinion(
resource => json_build_object(
'target', json_build_object('id', (created_article->'id'), 'reference', 'article'),
'created_by', json_build_object('id', _citizen_id),
'choice', json_build_object('id', opinion_choice1_id)
)
);
select upsert_opinion(
resource => json_build_object(
'target', json_build_object('id', (created_article->'id'), 'reference', 'article'),
'created_by', json_build_object('id', _citizen_id),
'choice', json_build_object('id', opinion_choice2_id)
)
) into opinion2;
assert (select count(*) = 2 from opinion_on_article), 'opinions must be inserted';
assert (select choice_id = opinion_choice1_id 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 (opinion2#>>'{choice, id}')::uuid = opinion_choice2_id), 'opinion2 is not inserted';
assert(select (opinion2#>>'{choice, name}') = 'Opinion2'), 'no name for opinion2';
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';
assert(
select (o#>>'{0, choice, name}') = 'Opinion1'
from find_citizen_opinions_by_target_ids(_citizen_id, array[(created_article->>'id')::uuid]) o),
'The first opinion must have a name';
assert(
select find_opinion_choices()#>>'{0, name}' = 'Opinion1'
), 'find_opinion_choices must be return all opinions';
assert(
select find_opinion_choices('{}')#>>'{0, name}' = 'Opinion1'
), 'find_opinion_choices must be return all opinions if no target is defined';
assert(
select (find_opinion_choice_by_id(opinion_choice1_id)->>'name') = 'Opinion1'
), 'find_opinion_choice_by_id must return the opinion_choice';
assert(
select json_array_length(resource) = 1 from find_citizen_opinions(_citizen_id, null, null, 1, 1)
), 'find_citizen_opinions must return only 1 result if limit is set to 1';
assert(
select total = 2 from find_citizen_opinions(_citizen_id, null, null, 2, 1)
), 'find_citizen_opinions must return the total and it should be 2';
assert(
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
$$;

View File

@@ -0,0 +1,43 @@
#!/bin/bash
options=("All" "RESET DB" "article" "citizen" "comment" "constitution" "follow" "opinion" "user" "vote" "workgroup" "Quit")
if [ -z "$1" ]; then
PS3='Please enter your choice: '
select ch in "${options[@]}"
do
opt=$ch
break
done
else
opt=${options[${1}-1]}
fi
case $opt in
"RESET DB")
awk 'FNR==1{print "--------------------"}1' \
../../main/resources/sql/migrations/*.down.sql \
../../main/resources/sql/migrations/*.up.sql > ./allSQL.sql
docker exec -i dc-project_postgresql_test psql test test -q -b -v "ON_ERROR_STOP=1" < ./allSQL.sql
rm ./allSQL.sql
;;
"All")
echo "Start ALL tests"
awk 'FNR==1{print "--------------------"}1' \
../../main/resources/sql/functions/*/*.sql \
./fixtures/*.sql \
./*.sql > ./allSQL.sql
docker exec -i dc-project_postgresql_test psql test test -q -b -v "ON_ERROR_STOP=1" < ./allSQL.sql
rm ./allSQL.sql
;;
"Quit")
;;
*)
echo "Start tests $opt"
awk 'FNR==1{print "--------------------"}1' \
../../main/resources/sql/functions/*/*.sql \
./fixtures/*.sql \
./"$opt".sql > ./allSQL.sql
docker exec -i dc-project_postgresql_test psql test test -q -b -v "ON_ERROR_STOP=1" < ./allSQL.sql
rm ./allSQL.sql
;;
esac

View File

@@ -0,0 +1,38 @@
do
$$
declare
created_user json := '{"username": "george", "password": "azerty", "roles": ["ROLE_USER"]}';
user_with_other_password json;
selected_user json;
exist_user json;
begin
-- Insert user and check if username and password is correct
select insert_user(created_user) into created_user;
assert created_user->>'username' = 'george', 'username must be george';
assert created_user->>'password' is null, 'password must not be returned';
-- get user by there id and check the username is correct
select find_user_by_id((created_user->>'id')::uuid) into selected_user;
assert selected_user->>'username' = 'george', format('username must be george, %s instead', selected_user);
-- get user by username and check the username is correct
select find_user_by_username(created_user->>'username') into selected_user;
assert selected_user->>'username' = 'george', 'username must be george';
-- check if user exist with username and password and verify the reterned user
select check_user('george', 'azerty') into exist_user;
assert exist_user is not null, format('the function check_user must be return user object if username and password is correct, %s is return', exist_user::text);
assert exist_user->>'username' = 'george', format('the function check_user must be return user object with username is "george", %s is return', exist_user::text);
assert exist_user->>'password' is null, format('the function check_user must not be return the password, %s is return', exist_user::text);
-- test change password
user_with_other_password = jsonb_set(created_user::jsonb, '{password}', '"qwerty"'::jsonb);
perform change_user_password(user_with_other_password);
select check_user('george', 'qwerty') into exist_user;
assert exist_user->>'username' = 'george', format('the function change_user_password must change password: %s', exist_user::text);
rollback;
raise notice 'user test pass';
end;
$$;

View File

@@ -0,0 +1,60 @@
do
$$
declare
_citizen_id uuid := fixture_citizen('george');
_citizen_id2 uuid := fixture_citizen('john');
_article_id uuid := fixture_article(_citizen_id := _citizen_id);
votes jsonb;
votes_of_citizen json;
votes_of_citizen_for_targets json;
votes_total int;
begin
perform vote(
reference => 'article'::regclass,
_target_id => _article_id,
_created_by_id => _citizen_id,
_note => 1
);
assert (select count(*) = 1 from vote_for_article), 'vote must be inserted';
assert (select note = 1 from vote_for_article limit 1), 'vote must be equal to 1';
perform vote(
reference => 'article'::regclass,
_target_id => _article_id,
_created_by_id => _citizen_id,
_note => -1
);
assert (select count(*) = 1 from vote_for_article), 'vote must be inserted';
assert (select note = -1 from vote_for_article limit 1), 'vote must be equal to -1';
begin
perform vote(
reference => 'article'::regclass,
_target_id => _article_id,
_created_by_id => _citizen_id,
_note => -10
);
assert false, 'vote must be throw exception if note is not -1, 0 or 1';
exception when check_violation then
end;
select count_vote('933b6a1b-50c9-42b6-989f-c02a57814ef9') into votes;
assert ((votes->>'up')::int = 0), 'vote.up must be 0';
-- Test "find_votes_by_citizen"
select resource, total into votes_of_citizen, votes_total from find_votes_by_citizen(_citizen_id2);
assert (votes_total = 0), format('votes count for user %s must be 0, instead of %s', _citizen_id2, votes_total);
select resource, total into votes_of_citizen, votes_total from find_votes_by_citizen(_citizen_id);
assert (votes_total = 1), format('votes count for user %s must be 1, instead of %s', _citizen_id, votes_total);
assert ((votes_of_citizen#>>'{0,note}')::int = -1), format('the note must be -1, instead of %s', (votes_of_citizen#>>'{0,note}'));
-- test "find_citizen_votes_by_target_ids"
select find_citizen_votes_by_target_ids(_citizen_id, array[_article_id]::uuid[]) into votes_of_citizen_for_targets;
assert (json_array_length(votes_of_citizen_for_targets) = 1), format('the function must be return 1 vote, instead of %s', json_array_length(votes_of_citizen_for_targets));
assert (votes_of_citizen_for_targets#>>'{0,target_id}' = _article_id::text), format('target_id of vote must be %s, instead of %s', _article_id, votes_of_citizen_for_targets#>>'{0,target_id}');
rollback;
raise notice 'vote test pass';
end;
$$;

View File

@@ -0,0 +1,135 @@
do
$$
declare
_citizen_id uuid := fixture_citizen('george');
_citizen_id2 uuid := fixture_citizen('john');
_citizen_id3 uuid := fixture_citizen('tesla');
created_workgroup json := '{
"name": "Le groupe des vert",
"description": "test",
"anonymous": false
}';
created_workgroup_updated json := '{
"name": "Le groupe des rouge",
"description": "red",
"anonymous": false
}';
created_workgroup_2 json := '{
"name": "hello",
"description": "super",
"anonymous": false
}';
selected_workgroup json;
-- selected_workgroups json;
members json;
selected_citizen json;
begin
created_workgroup := jsonb_set(created_workgroup::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
assert created_workgroup#>>'{created_by, id}' = _citizen_id::text, format('citizenId in workgroup must be the same as citizen, %s != %s', created_workgroup#>>'{created_by, id}', _citizen_id::text);
-- insert workgroup
select upsert_workgroup(created_workgroup) into created_workgroup;
assert created_workgroup->>'description' is not null, 'description should not be null';
assert (created_workgroup->>'name') = 'Le groupe des vert', format('name must be equal to "Le groupe des vert", %s instead', created_workgroup->>'name');
assert (created_workgroup#>>'{members, 0, citizen, id}') = _citizen_id::text, 'workgroup must have creator in members on creation';
-- update workgroup
created_workgroup_updated := jsonb_set(created_workgroup_updated::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
select upsert_workgroup(created_workgroup_updated) into created_workgroup_updated;
assert created_workgroup_updated->>'description' is not null, 'description should not be null';
assert (created_workgroup_updated->>'name') = 'Le groupe des rouge', format('name must be equal to "Le groupe des rouge", %s instead', created_workgroup_updated->>'name');
assert (created_workgroup_updated#>>'{members, 0, citizen, id}') = _citizen_id::text, 'workgroup must have creator in members on update';
-- insert another workgroup
created_workgroup_2 := jsonb_set(created_workgroup_2::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
assert created_workgroup_2#>>'{created_by, id}' = _citizen_id::text, format('citizenId in workgroup must be the same as citizen, %s != %s', created_workgroup_2#>>'{created_by, id}', _citizen_id::text);
select upsert_workgroup(created_workgroup_2) into created_workgroup_2;
-- get workgroup by id and check the name
select find_workgroup_by_id((created_workgroup->>'id')::uuid) into selected_workgroup;
assert selected_workgroup->>'name' = 'Le groupe des vert', format('name must be "Le groupe des vert", %s', selected_workgroup->>'name');
-- search workgroups and check the name
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'));
-------------
-- members --
-------------
-- add
select m into members from add_workgroup_members((created_workgroup->>'id')::uuid, json_build_array(
json_build_object('citizen', json_build_object('id', _citizen_id2), 'roles', '{MASTER}'::text[]),
json_build_object('citizen', json_build_object('id', _citizen_id3), 'roles', '{MASTER}'::text[])
)) m;
assert json_array_length(members) = 3, 'The members count must be equal to 3';
assert (members::jsonb) @> jsonb_build_array(jsonb_build_object(
'id', (created_workgroup->>'id'),
'citizen', jsonb_build_object('id', _citizen_id3),
'roles', jsonb_build_array('MASTER')
)),
'Members must contain citizen3';
-- select resource into selected_workgroups
-- from find_workgroups(_filter := json_build_object('members', json_build_array(_citizen_id2)));
-- assert (select selected_workgroups#>>'{0,id}' = (created_workgroup->>'id'));
-- Check if "find_citizen_by_id" retrun citizen
assert (select find_citizen_by_id(_citizen_id2)#>>'{id}')::uuid = _citizen_id2, 'find_citizen_by_id must return citizen';
-- Check if "find_citizen_by_id_with_user_and_workgroups" retrun workgroups of citizen
select find_citizen_by_id_with_user_and_workgroups(_citizen_id3) into selected_citizen;
assert selected_citizen#>>'{workgroups, 0, roles, 0}' = 'MASTER', format('workgroup must have MASTER role, %s', selected_citizen#>>'{workgroups, 0, roles, 0}');
-- update
select m into members from update_workgroup_members((created_workgroup->>'id')::uuid, json_build_array(
json_build_object('citizen', json_build_object('id', _citizen_id2), 'roles', '{MASTER}'::text[]),
json_build_object('citizen', json_build_object('id', _citizen_id), 'roles', '{MASTER}'::text[])
)) 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', (created_workgroup->>'id'),
'citizen', jsonb_build_object('id', _citizen_id),
'roles', jsonb_build_array('MASTER')
)), 'Members must contain citizen1';
assert not (members::jsonb) @> jsonb_build_array(jsonb_build_object(
'citizen', 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(jsonb_build_object(
'citizen', 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(
'citizen', jsonb_build_object('id', _citizen_id)
)), 'Members must contain citizen1';
assert not (members::jsonb) @> jsonb_build_array(jsonb_build_object(
'citizen', jsonb_build_object('id', _citizen_id2)
)), 'Members must NOT contain citizen2';
select m into members from find_workgroup_members((created_workgroup->>'id')::uuid) m;
assert json_array_length(members) = 1, 'The members count must be equal to 1';
assert (members::jsonb) @> jsonb_build_array(jsonb_build_object(
'citizen', jsonb_build_object('id', _citizen_id)
)), 'Members must contain citizen1';
assert not (members::jsonb) @> jsonb_build_array(jsonb_build_object(
'citizen', jsonb_build_object('id', _citizen_id2)
)), 'Members must NOT contain citizen2';
-- Check if find_workgroup_by_id return members
select find_workgroup_by_id((created_workgroup->>'id')::uuid) into selected_workgroup;
assert json_array_length(selected_workgroup->'members') = 1, 'Workgroup must have members';
perform delete_workgroup((created_workgroup->>'id')::uuid);
select find_workgroup_by_id((created_workgroup->>'id')::uuid) into selected_workgroup;
assert selected_workgroup is null, 'Workgroup must be null after deleted';
select m into members from find_workgroup_members((created_workgroup->>'id')::uuid) m;
assert json_array_length(members) = 0, 'The members count must be equal to 0 on deleted workgroup';
rollback;
raise notice 'workgroup test pass';
end
$$;