Move SQL tests
This commit is contained in:
53
src/test/sql/article.sql
Normal file
53
src/test/sql/article.sql
Normal file
@@ -0,0 +1,53 @@
|
||||
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"}';
|
||||
created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "annonymous": false, "content": "bla bal bla", "tags": ["love", "test"]}';
|
||||
selected_article 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);
|
||||
|
||||
-- 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;
|
||||
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');
|
||||
-- try tu create new version
|
||||
select upsert_article(created_article) into created_article;
|
||||
assert (created_article->>'version_number')::int = 2, format('version_number must be equal to 2, %s instead', created_article->>'version_number');
|
||||
|
||||
-- get article by id and check the title
|
||||
select find_article_by_id((created_article->>'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->>'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');
|
||||
-- -- 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);
|
||||
|
||||
raise notice 'article test pass';
|
||||
end;
|
||||
$$;
|
||||
46
src/test/sql/citizen.sql
Normal file
46
src/test/sql/citizen.sql
Normal file
@@ -0,0 +1,46 @@
|
||||
do
|
||||
$$
|
||||
declare
|
||||
wrong_citizen json;
|
||||
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||
_user_id uuid;
|
||||
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}';
|
||||
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);
|
||||
|
||||
-- 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 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}');
|
||||
|
||||
-- delete citizen
|
||||
delete from citizen where user_id = _user_id;
|
||||
delete from "user" where username = 'george';
|
||||
|
||||
-- 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;
|
||||
assert selected_citizen is null, format('citizen must be null if not exist, %s', selected_citizen);
|
||||
|
||||
raise notice 'citizen test pass';
|
||||
end;
|
||||
$$;
|
||||
71
src/test/sql/comment.sql
Normal file
71
src/test/sql/comment.sql
Normal file
@@ -0,0 +1,71 @@
|
||||
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"
|
||||
}
|
||||
$json$;
|
||||
created_article json := $json$
|
||||
{
|
||||
"version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9",
|
||||
"title": "Love the world",
|
||||
"annonymous": false,
|
||||
"content": "bla bal bla",
|
||||
"tags": [
|
||||
"love",
|
||||
"test"
|
||||
]
|
||||
}
|
||||
$json$;
|
||||
_comment_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;
|
||||
|
||||
-- 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;
|
||||
|
||||
|
||||
select comment(
|
||||
reference => 'article'::regclass,
|
||||
target_id => (created_article->>'id')::uuid,
|
||||
citizen_id => _citizen_id,
|
||||
content => 'Ho my god !'::text
|
||||
) into _comment_id;
|
||||
assert (select count(*) = 1 from "comment"), 'comment must be inserted';
|
||||
|
||||
perform edit_comment(
|
||||
reference => 'article'::regclass,
|
||||
id => _comment_id,
|
||||
content => 'edited'::text
|
||||
);
|
||||
assert (select count(*) = 1 from "comment"), 'edit comment must not insert new comment';
|
||||
assert (select count(*) = 1 from "comment" where content = 'edited'), 'edit comment must not insert new comment';
|
||||
|
||||
-- delete comment and context
|
||||
delete from "comment";
|
||||
delete from article;
|
||||
delete from citizen;
|
||||
delete from "user";
|
||||
|
||||
raise notice 'comment test pass';
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
-- select uuid_generate_v4();
|
||||
83
src/test/sql/constitution.sql
Normal file
83
src/test/sql/constitution.sql
Normal file
@@ -0,0 +1,83 @@
|
||||
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"
|
||||
}
|
||||
$json$;
|
||||
created_article json := $json$
|
||||
{
|
||||
"version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9",
|
||||
"title": "Love the world",
|
||||
"annonymous": false,
|
||||
"content": "bla bal bla",
|
||||
"tags": [
|
||||
"love",
|
||||
"test"
|
||||
]
|
||||
}
|
||||
$json$;
|
||||
created_constitution json := $json$
|
||||
{
|
||||
"version_id": "18ff6dd6-3bc1-4c59-82f0-5e2a8d54ae3e",
|
||||
"title": "Love the world",
|
||||
"annonymous": false,
|
||||
"titles": [
|
||||
{
|
||||
"name": "titleOne"
|
||||
},
|
||||
{
|
||||
"name": "titleTwo"
|
||||
}
|
||||
]
|
||||
}
|
||||
$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;
|
||||
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";
|
||||
|
||||
raise notice 'constitution test pass';
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
-- select uuid_generate_v4();
|
||||
59
src/test/sql/follow.sql
Normal file
59
src/test/sql/follow.sql
Normal file
@@ -0,0 +1,59 @@
|
||||
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"
|
||||
}
|
||||
$json$;
|
||||
created_citizen2 json := $json$
|
||||
{
|
||||
"name": {
|
||||
"first_name": "John",
|
||||
"last_name": "Doe"
|
||||
},
|
||||
"birthday": "2002-01-01"
|
||||
}
|
||||
$json$;
|
||||
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(*) > 0 from follow), 'follow must be inserted';
|
||||
|
||||
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
|
||||
assert (select count(*) > 0 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';
|
||||
|
||||
-- delete follow and context
|
||||
delete from citizen;
|
||||
delete from "user";
|
||||
|
||||
raise notice 'follow test pass';
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
-- select uuid_generate_v4();
|
||||
35
src/test/sql/user.sql
Normal file
35
src/test/sql/user.sql
Normal file
@@ -0,0 +1,35 @@
|
||||
do
|
||||
$$
|
||||
declare
|
||||
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||
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);
|
||||
|
||||
-- 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);
|
||||
|
||||
raise notice 'user test pass';
|
||||
end;
|
||||
$$;
|
||||
|
||||
82
src/test/sql/vote.sql
Normal file
82
src/test/sql/vote.sql
Normal file
@@ -0,0 +1,82 @@
|
||||
do
|
||||
$$
|
||||
declare
|
||||
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||
_citizen_id uuid;
|
||||
created_citizen json := $json$
|
||||
{
|
||||
"name": {
|
||||
"first_name": "George",
|
||||
"last_name": "MICHEL"
|
||||
},
|
||||
"birthday": "2001-01-01"
|
||||
}
|
||||
$json$;
|
||||
created_article json := $json$
|
||||
{
|
||||
"version_id": "933b6a1b-50c9-42b6-989f-c02a57814ef9",
|
||||
"title": "Love the world",
|
||||
"annonymous": false,
|
||||
"content": "bla bal bla",
|
||||
"tags": [
|
||||
"love",
|
||||
"test"
|
||||
]
|
||||
}
|
||||
$json$;
|
||||
begin
|
||||
-- insert user for context
|
||||
select insert_user(created_user) into created_user;
|
||||
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;
|
||||
|
||||
|
||||
perform vote(
|
||||
reference => 'article'::regclass,
|
||||
_target_id => (created_article->>'id')::uuid,
|
||||
_citizen_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 => (created_article->>'id')::uuid,
|
||||
_citizen_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 => (created_article->>'id')::uuid,
|
||||
_citizen_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;
|
||||
|
||||
|
||||
-- delete vote and context
|
||||
delete from vote;
|
||||
delete from article;
|
||||
delete from citizen;
|
||||
delete from "user";
|
||||
|
||||
raise notice 'vote test pass';
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
-- select uuid_generate_v4();
|
||||
Reference in New Issue
Block a user