Skip to content

Commit

Permalink
All files added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Melekhin committed Jan 18, 2014
1 parent eb7203a commit 982bb00
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pg_skeleton - PostgreSQL project skeleton
==========================================

This is simple skeleton for postgresql-based projects.
It includes install and uninstall scripts, one example schema (test_user) and
pgTAP tests for it.
2 changes: 2 additions & 0 deletions create_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create user :db_user with encrypted password :db_pass;
create database :db_name with owner :db_user;
2 changes: 2 additions & 0 deletions drop_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
drop database :db_name;
drop user :db_user;
3 changes: 3 additions & 0 deletions extensions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create language plpgsql;
create extension pgtap;

5 changes: 5 additions & 0 deletions install.cfg.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Example settings file for installation script
db_host=localhost
db_port=5432
db_user=skel_test
db_name=skel_test
28 changes: 28 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
set -e

if [ ! -f install.cfg ] ; then
echo 'Error! File install.cfg does not exist. Please create it:
$ cp install.cfg.example install.cfg
$ vi install.cfg'

exit 1
fi;

# Load settings:
. install.cfg
read -p "Please input database schema owner ($db_user) password:" db_pass

echo "Creating database."
psql -v db_user=$db_user -v db_pass="'$db_pass'" -v db_name=$db_name -h $db_host -p $db_port -U postgres -f create_db.sql &>install.log

echo "Enabling extensions in database. Enter your postgres user password."
psql -h $db_host -p $db_port -d $db_name -U postgres -f extensions.sql &>>install.log

echo "Creating schema. Enter your schema user password."
psql -h $db_host -p $db_port -d $db_name -U $db_user -f install.sql &>> install.log

cd 'test'
./run_tests.sh

echo "Done!"
10 changes: 10 additions & 0 deletions install.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\set ECHO all
\qecho 'Installing DB schema'

/* Creating tables first */
\i test_user/create_tables.sql

/* Stored functions */
\i test_user/functions.sql

\qecho 'Done.'
7 changes: 7 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pgTAP example test suite
========================

Test functions should be placed in corresponding functions_<schema>.sql files.
One-liner tests and test function calls should be in tests/<schema>_<test-category>.sql
You should always update number of tests in plan(n) call in run_<schema>.sql file when
you add new test to schema test suite.
2 changes: 2 additions & 0 deletions test/create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
create schema test;
comment on schema test is 'Schema for tests';
24 changes: 24 additions & 0 deletions test/functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

create or replace function test.test_scheme_check_func(p_scheme varchar)
returns setof text as
$$
declare
v_array text[];
begin
select array_agg(f_name)
into v_array
from (
select distinct(n.nspname || '.' || p.proname) as f_name
from pg_proc p,
pg_namespace n
where n.oid = p.pronamespace
and n.nspname = p_scheme
except
select distinct(array_to_string(regexp_matches (descr,'\w+.\w+'),',','*')) as f_name
from __tresults__
) as r;
return next is(v_array,null,'Проверка наличия тестов для функций cхемы '||p_scheme||'.');
end;
$$ language plpgsql;

\i ../test/functions_users.sql
16 changes: 16 additions & 0 deletions test/functions_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
create or replace function test.test_user_0010()
returns setof text as
$$
declare
v_user_id integer;
begin
return next lives_ok('select test_user.add_user(''testuser unique''::varchar);','test_user.add_user doesnt throw exception');

v_user_id := test_user.add_user('blah blah');
return next cmp_ok(v_user_id,'>',0,'test_user.add_user: returns ok');

return next is(test_user.alter_user(v_user_id,'new user name blah'),v_user_id,'test_user.alter_user: returns ok');

return next is(test_user.delete_user(v_user_id),v_user_id,'test_user.delete_user: returns ok');
end;
$$ language plpgsql;
15 changes: 15 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e
if [ ! -f ../install.cfg ] ;
then
echo 'Error! File install.cfg does not exist. Please create it:
$ cp install.cfg.example install.cfg
$ vi install.cfg'
exit 1;
fi;

# Load settings:
. ../install.cfg

echo "Enter your schema user password"
pg_prove -h $db_host -p $db_port -d $db_name -U $db_user tests/run_*.sql
18 changes: 18 additions & 0 deletions test/setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
\set QUIET 1

-- Format the output for nice TAP.
\pset format unaligned
\pset tuples_only true
\pset pager

-- Revert all changes on failure.
\set ON_ERROR_ROLLBACK 1
\set ON_ERROR_STOP true

-- Load the TAP functions.
BEGIN;

\i create_tables.sql
\i test_data.sql
--\i ../test/pgtap.sql
\i functions.sql
3 changes: 3 additions & 0 deletions test/test_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
insert into test_user.users(user_id,user_name)
values(100500,'Test uesr 1');

11 changes: 11 additions & 0 deletions test/tests/run_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
\i setup.sql
select plan(4+2+1);

--4 tests
\i tests/user_crud.sql
--2 tests
\i tests/user_schema.sql
--1 test
select * from test.test_scheme_check_func('test_user');

ROLLBACK;
1 change: 1 addition & 0 deletions test/tests/user_crud.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from test.test_user_0010(); --4 tests
7 changes: 7 additions & 0 deletions test/tests/user_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select tables_are('test_user',
ARRAY ['users'],
'Schema test_user contains users table');
select columns_are('test_user',
'users',
ARRAY [ 'user_id', 'user_name', 'ts'],
'test_user.users column check');
9 changes: 9 additions & 0 deletions test_user/create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
create schema test_user;
comment on schema test_user is 'Схема для примеров pgTAP: пользователи';

create table test_user.users
(
user_id serial primary key,
user_name varchar(128) unique not null,
ts timestamp default now()
);
1 change: 1 addition & 0 deletions test_user/functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\i test_user/users_crud.sql
42 changes: 42 additions & 0 deletions test_user/users_crud.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**Adds user record
*@function test_user.add_user
*@param in varchar p_user_name user name
*@return int user_id
*/
create or replace function test_user.add_user(p_user_name in varchar)
returns int
as $$
insert into test_user.users(user_name)
values(p_user_name)
returning user_id;
$$ language sql;

/**Updates user record
*@function test_user.alter_user
*@param in int p_user_id user id
*@param in varchar p_user_name user name
*@return int p_user_id
*/
create or replace function test_user.alter_user(p_user_id in integer,
p_user_name in varchar)
returns int
as $$
update test_user.users
set user_name = p_user_name,
ts = now()
where user_id = p_user_id
returning user_id;
$$ language sql;

/**Deletes user record
*@function test_user.delete_user
*@param in integer p_user_id
*@return int p_user_id
*/
create or replace function test_user.delete_user(p_user_id in integer)
returns int
as $$
delete from test_user.users
where user_id = p_user_id
returning user_id;
$$ language sql;
16 changes: 16 additions & 0 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -e

if [ ! -f install.cfg ] ; then
echo 'Error! File install.cfg does not exist. Please create it:
$ cp install.cfg.example install.cfg
$ vi install.cfg'

exit 1
fi;

# Load settings:
. install.cfg

echo "Dropping database."
psql -v db_user=$db_user -v db_pass="'$db_pass'" -v db_name=$db_name -h $db_host -p $db_port -U postgres -f drop_db.sql

0 comments on commit 982bb00

Please sign in to comment.