Add constraints to reject empty product and companies names

This commit is contained in:
jordi fita mas 2023-02-17 12:39:32 +01:00
parent 245cccd85a
commit de7d167e65
15 changed files with 86 additions and 31 deletions

View File

@ -14,7 +14,7 @@ set search_path to numerus,public;
create table company ( create table company (
company_id serial primary key, company_id serial primary key,
slug uuid not null unique default gen_random_uuid(), slug uuid not null unique default gen_random_uuid(),
business_name text not null, business_name text not null constraint business_name_not_empty check (length(trim(business_name)) > 1),
vatin vatin not null, vatin vatin not null,
trade_name text not null, trade_name text not null,
phone packed_phone_number not null, phone packed_phone_number not null,

View File

@ -18,7 +18,7 @@ create table contact (
contact_id serial primary key, contact_id serial primary key,
company_id integer not null references company, company_id integer not null references company,
slug uuid not null unique default gen_random_uuid(), slug uuid not null unique default gen_random_uuid(),
business_name text not null, business_name text not null constraint business_name_not_empty check(length(trim(business_name)) > 1),
vatin vatin not null, vatin vatin not null,
trade_name text not null, trade_name text not null,
phone packed_phone_number not null, phone packed_phone_number not null,

View File

@ -13,7 +13,7 @@ create table invoice (
invoice_id serial primary key, invoice_id serial primary key,
company_id integer not null references company, company_id integer not null references company,
slug uuid not null unique default gen_random_uuid(), slug uuid not null unique default gen_random_uuid(),
invoice_number text not null, invoice_number text not null constraint invoice_number_not_empty check(length(trim(invoice_number)) > 1),
invoice_date date not null default current_date, invoice_date date not null default current_date,
contact_id integer not null references contact, contact_id integer not null references contact,
invoice_status text not null default 'created' references invoice_status, invoice_status text not null default 'created' references invoice_status,

View File

@ -11,10 +11,10 @@ create table invoice_product (
invoice_product_id serial primary key, invoice_product_id serial primary key,
invoice_id integer not null references invoice, invoice_id integer not null references invoice,
product_id integer not null references product, product_id integer not null references product,
name text not null, name text not null constraint name_not_empty check(length(trim(name)) > 0),
description text not null, description text not null default '',
price integer not null, price integer not null,
quantity integer not null, quantity integer not null default 1,
discount_rate discount_rate not null default 0.0 discount_rate discount_rate not null default 0.0
); );

View File

@ -11,8 +11,8 @@ create table product (
product_id serial primary key, product_id serial primary key,
company_id integer not null references company, company_id integer not null references company,
slug uuid not null default gen_random_uuid(), slug uuid not null default gen_random_uuid(),
name text not null, name text not null constraint name_not_empty check(length(trim(name)) > 0),
description text not null, description text not null default '',
price integer not null, price integer not null,
created_at timestamptz not null default current_timestamp created_at timestamptz not null default current_timestamp
); );

View File

@ -10,7 +10,7 @@ set search_path to numerus, public;
create table tax ( create table tax (
tax_id serial primary key, tax_id serial primary key,
company_id integer not null references company, company_id integer not null references company,
name text not null, name text not null constraint name_not_empty check(length(trim(name)) > 0),
rate tax_rate not null rate tax_rate not null
); );

View File

@ -1,6 +1,6 @@
-- Revert numerus:new_invoice_product from pg -- Revert numerus:new_invoice_product from pg
begin begin;
drop type if exists numerus.new_invoice_product; drop type if exists numerus.new_invoice_product;

View File

@ -42,12 +42,12 @@ values (3, 1, 'IRPF -15 %', -0.15)
, (6, 2, 'IVA 10 %', 0.10) , (6, 2, 'IVA 10 %', 0.10)
; ;
insert into product (product_id, company_id, name, description, price) insert into product (product_id, company_id, name, price)
values ( 7, 1, 'Product 2.1', '', 1212) values ( 7, 1, 'Product 2.1', 1212)
, ( 8, 1, 'Product 2.2', '', 2424) , ( 8, 1, 'Product 2.2', 2424)
, ( 9, 2, 'Product 4.1', '', 4848) , ( 9, 2, 'Product 4.1', 4848)
, (10, 2, 'Product 4.2', '', 9696) , (10, 2, 'Product 4.2', 9696)
, (11, 2, 'Product 4.3', '', 1010) , (11, 2, 'Product 4.3', 1010)
; ;
insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code) insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code)

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(79); select plan(80);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -162,5 +162,13 @@ select throws_ok(
); );
reset role; reset role;
select throws_ok( $$
insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code)
values (7, ' ', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR')
$$,
'23514', 'new row for relation "company" violates check constraint "business_name_not_empty"',
'Should not allow companies with blank business name'
);
select finish(); select finish();
rollback; rollback;

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(84); select plan(85);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -168,6 +168,15 @@ select throws_ok(
); );
reset role; reset role;
select throws_ok( $$
insert into contact (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code)
values (2, ' ', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES')
$$,
'23514', 'new row for relation "contact" violates check constraint "business_name_not_empty"',
'Should not allow contacts with blank business name'
);
select * select *
from finish(); from finish();

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(71); select plan(72);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -159,6 +159,15 @@ select throws_ok(
reset role; reset role;
select throws_ok( $$
insert into invoice (company_id, invoice_number, contact_id, currency_code)
values (2, ' ', 6, 'EUR')
$$,
'23514', 'new row for relation "invoice" violates check constraint "invoice_number_not_empty"',
'Should not allow invoice with blank number'
);
select * select *
from finish(); from finish();

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(54); select plan(57);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -51,7 +51,8 @@ select col_hasnt_default('invoice_product', 'name');
select has_column('invoice_product', 'description'); select has_column('invoice_product', 'description');
select col_type_is('invoice_product', 'description', 'text'); select col_type_is('invoice_product', 'description', 'text');
select col_not_null('invoice_product', 'description'); select col_not_null('invoice_product', 'description');
select col_hasnt_default('invoice_product', 'description'); select col_has_default('invoice_product', 'description');
select col_default_is('invoice_product', 'description', '');
select has_column('invoice_product', 'price'); select has_column('invoice_product', 'price');
select col_type_is('invoice_product', 'price', 'integer'); select col_type_is('invoice_product', 'price', 'integer');
@ -61,7 +62,8 @@ select col_hasnt_default('invoice_product', 'price');
select has_column('invoice_product', 'quantity'); select has_column('invoice_product', 'quantity');
select col_type_is('invoice_product', 'quantity', 'integer'); select col_type_is('invoice_product', 'quantity', 'integer');
select col_not_null('invoice_product', 'quantity'); select col_not_null('invoice_product', 'quantity');
select col_hasnt_default('invoice_product', 'quantity'); select col_has_default('invoice_product', 'quantity');
select col_default_is('invoice_product', 'quantity', 1);
select has_column('invoice_product', 'discount_rate'); select has_column('invoice_product', 'discount_rate');
select col_type_is('invoice_product', 'discount_rate', 'discount_rate'); select col_type_is('invoice_product', 'discount_rate', 'discount_rate');
@ -151,6 +153,16 @@ select throws_ok(
); );
reset role; reset role;
select throws_ok( $$
insert into invoice_product (invoice_id, product_id, name, description, price, quantity)
values (10, 14, ' ', '', 1212, 1)
$$,
'23514', 'new row for relation "invoice_product" violates check constraint "name_not_empty"',
'Should not allow invoice products with blank name'
);
select * select *
from finish(); from finish();

View File

@ -69,9 +69,9 @@ values (3, 2, 'IVA 21 %', 0.21)
, (6, 4, 'IVA 10 %', 0.10) , (6, 4, 'IVA 10 %', 0.10)
; ;
insert into product (product_id, company_id, name, description, price) insert into product (product_id, company_id, name, price)
values (7, 2, 'Product 1', 'Description 1', 1200) values (7, 2, 'Product 1', 1200)
, (8, 4, 'Product 2', 'Description 2', 2400) , (8, 4, 'Product 2', 2400)
; ;
insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code) insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code)
@ -84,9 +84,9 @@ values (11, 2, 'INV001', 9, 'EUR')
, (12, 4, 'INV002', 10, 'EUR') , (12, 4, 'INV002', 10, 'EUR')
; ;
insert into invoice_product (invoice_product_id, invoice_id, product_id, name, description, price, quantity) insert into invoice_product (invoice_product_id, invoice_id, product_id, name, price)
values (13, 11, 7, 'Product 1', '', 1200, 1) values (13, 11, 7, 'Product 1', 1200)
, (14, 12, 8, 'Product 2', '', 2400, 2) , (14, 12, 8, 'Product 2', 2400)
; ;
insert into invoice_product_tax (invoice_product_id, tax_id, tax_rate) insert into invoice_product_tax (invoice_product_id, tax_id, tax_rate)

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(49); select plan(51);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -50,7 +50,8 @@ select col_hasnt_default('product', 'name');
select has_column('product', 'description'); select has_column('product', 'description');
select col_type_is('product', 'description', 'text'); select col_type_is('product', 'description', 'text');
select col_not_null('product', 'description'); select col_not_null('product', 'description');
select col_hasnt_default('product', 'description'); select col_has_default('product', 'description');
select col_default_is('product', 'description', '');
select has_column('product', 'price'); select has_column('product', 'price');
select col_type_is('product', 'price', 'integer'); select col_type_is('product', 'price', 'integer');
@ -127,6 +128,15 @@ select throws_ok(
reset role; reset role;
select throws_ok( $$
insert into product (company_id, name, description, price)
values (2, ' ', '', 1200)
$$,
'23514', 'new row for relation "product" violates check constraint "name_not_empty"',
'Should not allow product with blank name'
);
select * select *
from finish(); from finish();

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin; begin;
select plan(35); select plan(36);
set search_path to numerus, auth, public; set search_path to numerus, auth, public;
@ -119,6 +119,13 @@ select throws_ok(
); );
reset role; reset role;
select throws_ok( $$
insert into tax (company_id, name, rate)
values (2, ' ', 0.22)
$$,
'23514', 'new row for relation "tax" violates check constraint "name_not_empty"',
'Should not allow taxs with blank name'
);
select * select *
from finish(); from finish();