From c6c550a036d441e6b20d1bde23f37308ca026ec3 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Sun, 18 Aug 2024 05:20:36 +0200 Subject: [PATCH] =?UTF-8?q?Add=20check=20constraint=20to=20payment?= =?UTF-8?q?=E2=80=99s=20amount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/payment.sql | 2 +- test/payment.sql | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/deploy/payment.sql b/deploy/payment.sql index adb560a..8928df7 100644 --- a/deploy/payment.sql +++ b/deploy/payment.sql @@ -19,7 +19,7 @@ create table payment ( description text not null, payment_date date not null default current_date, 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, tags tag_name[] not null default '{}', payment_status text not null default 'complete' references payment_status, diff --git a/test/payment.sql b/test/payment.sql index c471d2a..dac7be5 100644 --- a/test/payment.sql +++ b/test/payment.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(68); +select plan(71); 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 has_column('payment', 'payment_id'); +select col_is_pk('payment', 'payment_id'); select col_type_is('payment', 'payment_id', 'integer'); select col_not_null('payment', 'payment_id'); select col_hasnt_default('payment', 'payment_id'); @@ -165,6 +166,18 @@ select throws_ok( ); 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 * from finish();