-------------------------------------------------------
--  Create function to test with, namely: ASSERT_EQUALS 
-------------------------------------------------------

CREATE OR REPLACE FUNCTION assert_equals(message VARCHAR, expected BIGINT, actual BIGINT) RETURNS INTEGER AS $$
   BEGIN
      IF expected != actual THEN
         RAISE EXCEPTION '%: expected:<%> but actual:<%>', message, expected, actual;
      END IF;
      RETURN NULL;
   END
$$ LANGUAGE 'plpgsql';


-----------------------------------------------------------
--  Create Users
-----------------------------------------------------------

insert into persons
(id
,first_name
,last_name
,user_id
,email
,dbin_id)
values
(nextval('PERSON_ID_SEQ')
,''
,'Tom'
,'TOM'
,''
,(select id from database_instances where code = 'SYSTEM_DEFAULT') );

insert into persons
(id
,first_name
,last_name
,user_id
,email
,dbin_id)
values
(nextval('PERSON_ID_SEQ')
,''
,'Dick'
,'DICK'
,''
,(select id from database_instances where code = 'SYSTEM_DEFAULT') );

insert into persons
(id
,first_name
,last_name
,user_id
,email
,dbin_id)
values
(nextval('PERSON_ID_SEQ')
,''
,'Harry'
,'HARRY'
,''
,(select id from database_instances where code = 'SYSTEM_DEFAULT') );

insert into persons
(id
,first_name
,last_name
,user_id
,email
,dbin_id)
values
(nextval('PERSON_ID_SEQ')
,''
,'Frank'
,'FRANK'
,''
,(select id from database_instances where code = 'SYSTEM_DEFAULT') );


insert into persons
(id
,first_name
,last_name
,user_id
,email
,dbin_id)
values
(nextval('PERSON_ID_SEQ')
,''
,'John'
,'JOHN'
,''
,(select id from database_instances where code = 'SYSTEM_DEFAULT') );

insert into persons
(id
,first_name
,last_name
,user_id
,email
,dbin_id)
values
(nextval('PERSON_ID_SEQ')
,''
,'Sally'
,'SALLY'
,''
,(select id from database_instances where code = 'SYSTEM_DEFAULT') );

-----------------------------------------------------------
--  Create a Group
-----------------------------------------------------------

INSERT INTO groups(
              id
            , code
            , dbin_id
            , grou_id_parent
            , pers_id_leader
            , description
            , pers_id_registerer)
    VALUES (  nextval('GROUP_ID_SEQ')
            , 'GOOD_GUYS'
            , (select id from database_instances where code = 'SYSTEM_DEFAULT')
            , null
            , (select id from persons where user_id ='SALLY')
            , 'Good Guys Group'
            , (select id from persons where user_id ='system'));

