Add check constraint to payment’s amount

This commit is contained in:
jordi fita mas 2024-08-18 05:20:36 +02:00
parent 4f646e35d6
commit c6c550a036
2 changed files with 15 additions and 2 deletions

View File

@ -19,7 +19,7 @@ create table payment (
description text not null, description text not null,
payment_date date not null default current_date, payment_date date not null default current_date,
payment_account_id integer not null references payment_account, payment_account_id integer not null references payment_account,
amount integer not null, amount integer not null constraint payment_amount_positive check (amount > 0),
currency_code text not null references currency, currency_code text not null references currency,
tags tag_name[] not null default '{}', tags tag_name[] not null default '{}',
payment_status text not null default 'complete' references payment_status, payment_status text not null default 'complete' references payment_status,

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(68); select plan(71);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -17,6 +17,7 @@ select table_privs_are('payment', 'admin', array ['SELECT', 'INSERT', 'UPDATE',
select table_privs_are('payment', 'authenticator', array []::text[]); select table_privs_are('payment', 'authenticator', array []::text[]);
select has_column('payment', 'payment_id'); select has_column('payment', 'payment_id');
select col_is_pk('payment', 'payment_id');
select col_type_is('payment', 'payment_id', 'integer'); select col_type_is('payment', 'payment_id', 'integer');
select col_not_null('payment', 'payment_id'); select col_not_null('payment', 'payment_id');
select col_hasnt_default('payment', 'payment_id'); select col_hasnt_default('payment', 'payment_id');
@ -165,6 +166,18 @@ select throws_ok(
); );
reset role; reset role;
select throws_ok(
$$ insert into payment (company_id, description, payment_account_id, amount, currency_code) values (2, 'Nope', 221, 0, 'EUR') $$,
'23514', 'new row for relation "payment" violates check constraint "payment_amount_positive"',
'Should not allow empty payments'
);
select throws_ok(
$$ insert into payment (company_id, description, payment_account_id, amount, currency_code) values (2, 'Nope', 221, -1, 'EUR') $$,
'23514', 'new row for relation "payment" violates check constraint "payment_amount_positive"',
'Should not allow negative payments'
);
select * select *
from finish(); from finish();