Refactoring: user fixtures in sql tests

This commit is contained in:
2020-03-06 09:54:58 +01:00
parent 4a4e9651fd
commit 559890000d
11 changed files with 101 additions and 281 deletions

View File

@@ -1,10 +1,7 @@
do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
_user_id uuid;
_citizen_id uuid;
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}';
_citizen_id uuid := fixture_citizen();
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_v2 json;
first_article_id uuid;
@@ -12,15 +9,6 @@ declare
selected_article json;
selected_total int;
begin
-- insert user for context
select insert_user(created_user) into created_user;
_user_id := created_user->>'id';
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 for context
select upsert_citizen(created_citizen) into created_citizen;
_citizen_id := created_citizen->>'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);
@@ -73,18 +61,7 @@ begin
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');
-- -- check if user id is returned
-- assert (selected_article#>>'{created_by, user, id}')::uuid = _user_id, format('user_id must be %s instead of %s', _user_id, (selected_article#>>'{created_by, user, id}')::uuid);
-- delete article and context
delete from article;
delete from citizen;
delete from "user";
-- check if find by id return null if article not exist
select find_citizen_by_user_id((created_citizen->>'id')::uuid) into selected_article;
assert selected_article is null, format('article must be null if not exist, %s', selected_article);
rollback;
raise notice 'article test pass';
end;
end
$$;

View File

@@ -2,15 +2,11 @@ do
$$
declare
wrong_citizen json;
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
_user_id uuid;
_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", "plain_password": "azerty", "roles": ["ROLE_USER"]}}';
selected_citizen json;
begin
-- insert user for context
select insert_user(created_user) into created_user;
_user_id := created_user->>'id';
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);
@@ -39,9 +35,7 @@ begin
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}');
-- delete citizen
delete from citizen;
delete from "user";
rollback;
-- check if fint by id return null if citizen not exist
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;

View File

@@ -1,32 +1,6 @@
do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
created_user2 json := '{"username": "john", "plain_password": "qwerty", "roles": ["ROLE_USER"]}';
_citizen_id uuid;
created_citizen json := $json$
{
"name": {
"first_name": "George",
"last_name": "MICHEL"
},
"birthday": "2001-01-01",
"email":"george.michel@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$;
_comment_id uuid;
_comment_id_response uuid;
_comment_id_response2 uuid;
@@ -36,24 +10,12 @@ declare
_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
-- 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;
-- insert new citizen for context
select upsert_citizen(created_citizen) into created_citizen;
_citizen_id := created_citizen->>'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;
_comment = json_build_object(
'id', 'a2962f49-74e6-4f20-9c13-36f3ccbc4ad7',
'target', jsonb_build_object('id', created_article->>'id'),
'target', jsonb_build_object('id', _article_id),
'created_by', jsonb_build_object('id', _citizen_id),
'content', 'Ho my god !'
);
@@ -97,7 +59,7 @@ begin
_comment = json_build_object(
'id', '50962646-07b6-42a3-9798-d756b9b6e2ba',
'target', jsonb_build_object('id', created_article->>'id'),
'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)
@@ -110,7 +72,7 @@ begin
_comment = json_build_object(
'id', 'ce82e683-23a8-4977-92fb-8d61a3ec995a',
'target', jsonb_build_object('id', created_article->>'id'),
'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)
@@ -125,7 +87,7 @@ begin
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((created_article->>'id')::uuid);
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';
@@ -136,15 +98,7 @@ begin
'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';
-- delete comment and context
delete from "comment";
delete from article;
delete from citizen;
delete from "user";
rollback;
raise notice 'comment test pass';
end;
end
$$;
-- select uuid_generate_v4();

View File

@@ -1,32 +1,8 @@
do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
_user_id uuid;
_citizen_id uuid;
created_citizen json := $json$
{
"name": {
"first_name": "George",
"last_name": "MICHEL"
},
"birthday": "2001-01-01",
"email":"george.michel@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$;
_article_id uuid := fixture_article();
_citizen_id uuid := fixture_citizen('john');
created_constitution json := $json$
{
"version_id": "18ff6dd6-3bc1-4c59-82f0-5e2a8d54ae3e",
@@ -43,42 +19,16 @@ declare
}
$json$;
begin
-- insert user for context
select insert_user(created_user) into created_user;
raise notice '%', created_user;
_user_id := created_user->>'id';
raise notice '%', _user_id;
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 for context
select upsert_citizen(created_citizen) into created_citizen;
_citizen_id := created_citizen->>'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 for context
select upsert_article(created_article) into created_article;
assert created_article->>'version_id' is not null, 'version_id should not be null';
-- 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', created_article->>'id')), 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}');
-- delete article and context
delete from article_in_title;
delete from title;
delete from constitution;
delete from article;
delete from citizen;
delete from "user";
rollback;
raise notice 'constitution test pass';
end;
end
$$;

View File

@@ -0,0 +1,27 @@
create or replace function fixture_article(in name text default 'love', _citizen_id uuid default fixture_citizen(), out _article_id uuid)
language plpgsql as
$$
declare
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$;
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;
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,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 := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "email":"george.michel@gmail.com"}';
created_citizen2 json := '{"name": {"first_name":"john", "last_name":"DOE"}, "birthday": "2001-01-01", "email":"john.doe@gmail.com"}';
begin
_user_id = fixture_user(name);
if (name = 'george') then
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';
elseif (name = 'john') then
created_citizen2 := jsonb_set(created_citizen2::jsonb, '{user}'::text[], jsonb_build_object('id', _user_id::text), true)::json;
assert created_citizen2#>>'{user, id}' = _user_id::text, format('userId in citizen must be the same as user, %s = %s', created_citizen2#>>'{user, id}', _user_id::text);
select upsert_citizen(created_citizen2) into created_citizen2;
_citizen_id := created_citizen2->>'id';
end if;
end
$$

View File

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

View File

@@ -1,49 +1,12 @@
do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
created_user2 json := '{"username": "john", "plain_password": "qwerty", "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": "John",
"last_name": "Doe"
},
"birthday": "2002-01-01",
"email":"george.michel2@gmail.com"
}
$json$;
_citizen_id uuid := fixture_citizen('george');
_citizen_id2 uuid := fixture_citizen('john');
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;
first_article_id uuid := fixture_article(_citizen_id := _citizen_id);
first_article_updated_id 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;
_citizen_id := created_citizen->>'id';
-- insert new citizen for context
select upsert_citizen(created_citizen2) into created_citizen2;
_citizen_id2 := created_citizen2->>'id';
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)), 'find_follow must return the following';
@@ -54,35 +17,19 @@ begin
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';
assert (select count(*) = 1 from follow), 'must have 1 following';
-- add new version for article, then unfollow the new one
select upsert_article(created_article) into created_article;
first_article_updated_id = (created_article->>'id')::uuid;
select fixture_article(_citizen_id := _citizen_id) into first_article_updated_id;
assert first_article_id != first_article_updated_id;
perform unfollow('article'::regclass, first_article_id, _citizen_id);
-- perform unfollow('article'::regclass, first_article_updated_id, _citizen_id);
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version';
-- delete follow and context
delete from follow;
delete from article;
delete from citizen;
delete from "user";
rollback;
raise notice 'follow test pass';
end;
$$;
-- select uuid_generate_v4();

View File

@@ -126,13 +126,7 @@ 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';
-- delete vote and context
delete from opinion;
delete from opinion_choice;
delete from article;
delete from citizen;
delete from "user";
rollback;
raise notice 'opinion test pass';
end
$$;

View File

@@ -31,11 +31,7 @@ begin
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);
-- delete user and check if user is really not exists
delete from "user" where username = 'george';
select check_user('george', 'azerty') into exist_user;
assert exist_user is null, format('the function check_user must be return null if user not exist, %s is return', exist_user::text);
rollback;
raise notice 'user test pass';
end;
$$;

View File

@@ -1,68 +1,17 @@
do
$$
declare
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
created_user2 json := '{"username": "george2", "plain_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$;
_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
-- 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;
perform vote(
reference => 'article'::regclass,
_target_id => (created_article->>'id')::uuid,
_target_id => _article_id,
_created_by_id => _citizen_id,
_note => 1
);
@@ -71,7 +20,7 @@ begin
perform vote(
reference => 'article'::regclass,
_target_id => (created_article->>'id')::uuid,
_target_id => _article_id,
_created_by_id => _citizen_id,
_note => -1
);
@@ -81,7 +30,7 @@ begin
begin
perform vote(
reference => 'article'::regclass,
_target_id => (created_article->>'id')::uuid,
_target_id => _article_id,
_created_by_id => _citizen_id,
_note => -10
);
@@ -101,19 +50,11 @@ begin
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[(created_article->>'id')]::uuid[]) into votes_of_citizen_for_targets;
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}' = created_article->>'id'), format('target_id of vote must be %s, instead of %s', created_article->>'id', votes_of_citizen_for_targets#>>'{0,target_id}');
-- delete vote and context
delete from vote;
delete from article;
delete from citizen;
delete from "user";
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;
$$;
-- select uuid_generate_v4();