feature #4: create procedure for citizen

This commit is contained in:
2019-07-26 14:29:48 +02:00
parent 0a92316128
commit fc06965cda
8 changed files with 110 additions and 11 deletions

View File

@@ -0,0 +1,46 @@
do
$$
declare
wrong_citizen json;
created_user json := '{"username": "george", "plain_password": "azerty"}';
_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
call insert_user(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
call upsert_citizen(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
call upsert_citizen(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
call find_citizen_by_id((created_citizen->>'id')::uuid, 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
call find_citizen_by_user_id((created_citizen->>'user_id')::uuid, 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
call find_citizen_by_user_id((created_citizen->>'user_id')::uuid, 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

@@ -5,23 +5,27 @@ declare
selected_user json;
exist_user json;
begin
-- Insert user and check if username and password is correct
call insert_user(created_user);
assert created_user->>'username' = 'george', 'username must be george';
assert created_user->>'password' is not null, 'password must be generated';
-- get user by there id and check the username is correct
call find_user_by_id((created_user->>'id')::uuid, selected_user);
assert selected_user->>'username' = 'george', 'username must be george';
-- get user by username and check the username is correct
call find_user_by_username(created_user->>'username', 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);