Skip to content

Instantly share code, notes, and snippets.

@steve-chavez
Forked from Su-Shee/pgtap example
Last active March 14, 2017 04:58
Show Gist options
  • Save steve-chavez/4c489c0dc697835ebedca1213e18a934 to your computer and use it in GitHub Desktop.
Save steve-chavez/4c489c0dc697835ebedca1213e18a934 to your computer and use it in GitHub Desktop.
basic pgtap testing
begin;
create extension pgtap;
create table users(
username text primary key,
email text not null,
firstname text,
lastname text
);
select plan(9);
select 'Table structure' as describe;
select
has_table('users'),
has_pk('users'),
has_column('users', 'username'),
col_not_null('users', 'email'),
col_type_is('users', 'username', 'text');
select 'Table constraints' as describe;
select
lives_ok(
$$
insert into users (username, lastname, firstname, email)
values ('poppi', 'schneider', 'petra', '[email protected]');
$$,
'inserting a new user with a unique username should be ok'
),
throws_ok(
$$
insert into users (username, lastname, firstname, email)
values ('poppi', 'müller', 'peter', '[email protected]');
$$,
'duplicate key value violates unique constraint "users_pkey"',
'inserting a username already existing should throw a unique constraint violation'
),
throws_ok(
$$
insert into users (username, lastname, firstname, email)
values ('petermüller', 'müller', 'peter', null);
$$,
'null value in column "email" violates not-null constraint',
'inserting a null email should throw a not-null constraint'
);
select 'Table data' as describe;
select results_eq (
$$
select username, firstname
from users
where lastname = 'schneider'
$$,
$$
values('poppi', 'petra')
$$,
'should select all users with a lastname "schneider"'
);
select * from finish();
rollback;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment