diff --git a/deploy/company.sql b/deploy/company.sql index 442ec5c..d07096f 100644 --- a/deploy/company.sql +++ b/deploy/company.sql @@ -14,7 +14,7 @@ set search_path to numerus,public; create table company ( company_id serial primary key, 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, trade_name text not null, phone packed_phone_number not null, diff --git a/deploy/contact.sql b/deploy/contact.sql index 6c44ca0..912c8db 100644 --- a/deploy/contact.sql +++ b/deploy/contact.sql @@ -18,7 +18,7 @@ create table contact ( contact_id serial primary key, company_id integer not null references company, 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, trade_name text not null, phone packed_phone_number not null, diff --git a/deploy/invoice.sql b/deploy/invoice.sql index b644a68..9c5f4e2 100644 --- a/deploy/invoice.sql +++ b/deploy/invoice.sql @@ -13,7 +13,7 @@ create table invoice ( invoice_id serial primary key, company_id integer not null references company, 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, contact_id integer not null references contact, invoice_status text not null default 'created' references invoice_status, diff --git a/deploy/invoice_product.sql b/deploy/invoice_product.sql index a1d787b..b43f6d4 100644 --- a/deploy/invoice_product.sql +++ b/deploy/invoice_product.sql @@ -11,10 +11,10 @@ create table invoice_product ( invoice_product_id serial primary key, invoice_id integer not null references invoice, product_id integer not null references product, - name text not null, - description text not null, + name text not null constraint name_not_empty check(length(trim(name)) > 0), + description text not null default '', price integer not null, - quantity integer not null, + quantity integer not null default 1, discount_rate discount_rate not null default 0.0 ); diff --git a/deploy/product.sql b/deploy/product.sql index 5d6232c..bd2abdd 100644 --- a/deploy/product.sql +++ b/deploy/product.sql @@ -11,8 +11,8 @@ create table product ( product_id serial primary key, company_id integer not null references company, slug uuid not null default gen_random_uuid(), - name text not null, - description text not null, + name text not null constraint name_not_empty check(length(trim(name)) > 0), + description text not null default '', price integer not null, created_at timestamptz not null default current_timestamp ); diff --git a/deploy/tax.sql b/deploy/tax.sql index 0894333..26b8244 100644 --- a/deploy/tax.sql +++ b/deploy/tax.sql @@ -10,7 +10,7 @@ set search_path to numerus, public; create table tax ( tax_id serial primary key, 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 ); diff --git a/revert/new_invoice_product.sql b/revert/new_invoice_product.sql index 88c39be..1113686 100644 --- a/revert/new_invoice_product.sql +++ b/revert/new_invoice_product.sql @@ -1,6 +1,6 @@ -- Revert numerus:new_invoice_product from pg -begin +begin; drop type if exists numerus.new_invoice_product; diff --git a/test/add_invoice.sql b/test/add_invoice.sql index 553a1c6..a18ef76 100644 --- a/test/add_invoice.sql +++ b/test/add_invoice.sql @@ -42,12 +42,12 @@ values (3, 1, 'IRPF -15 %', -0.15) , (6, 2, 'IVA 10 %', 0.10) ; -insert into product (product_id, company_id, name, description, price) -values ( 7, 1, 'Product 2.1', '', 1212) - , ( 8, 1, 'Product 2.2', '', 2424) - , ( 9, 2, 'Product 4.1', '', 4848) - , (10, 2, 'Product 4.2', '', 9696) - , (11, 2, 'Product 4.3', '', 1010) +insert into product (product_id, company_id, name, price) +values ( 7, 1, 'Product 2.1', 1212) + , ( 8, 1, 'Product 2.2', 2424) + , ( 9, 2, 'Product 4.1', 4848) + , (10, 2, 'Product 4.2', 9696) + , (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) diff --git a/test/company.sql b/test/company.sql index fb4a8bd..fa0cff5 100644 --- a/test/company.sql +++ b/test/company.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(79); +select plan(80); set search_path to numerus, auth, public; @@ -162,5 +162,13 @@ select throws_ok( ); 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(); rollback; diff --git a/test/contact.sql b/test/contact.sql index 6243c5b..b07ac53 100644 --- a/test/contact.sql +++ b/test/contact.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(84); +select plan(85); set search_path to numerus, auth, public; @@ -168,6 +168,15 @@ select throws_ok( ); 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 * from finish(); diff --git a/test/invoice.sql b/test/invoice.sql index 4f399d8..1447bfa 100644 --- a/test/invoice.sql +++ b/test/invoice.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(71); +select plan(72); set search_path to numerus, auth, public; @@ -159,6 +159,15 @@ select throws_ok( 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 * from finish(); diff --git a/test/invoice_product.sql b/test/invoice_product.sql index 2ef000e..a2b6104 100644 --- a/test/invoice_product.sql +++ b/test/invoice_product.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(54); +select plan(57); 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 col_type_is('invoice_product', 'description', 'text'); 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 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 col_type_is('invoice_product', 'quantity', 'integer'); 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 col_type_is('invoice_product', 'discount_rate', 'discount_rate'); @@ -151,6 +153,16 @@ select throws_ok( ); 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 * from finish(); diff --git a/test/invoice_product_tax.sql b/test/invoice_product_tax.sql index 3f1f51d..c5309d9 100644 --- a/test/invoice_product_tax.sql +++ b/test/invoice_product_tax.sql @@ -69,9 +69,9 @@ values (3, 2, 'IVA 21 %', 0.21) , (6, 4, 'IVA 10 %', 0.10) ; -insert into product (product_id, company_id, name, description, price) -values (7, 2, 'Product 1', 'Description 1', 1200) - , (8, 4, 'Product 2', 'Description 2', 2400) +insert into product (product_id, company_id, name, price) +values (7, 2, 'Product 1', 1200) + , (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) @@ -84,9 +84,9 @@ values (11, 2, 'INV001', 9, 'EUR') , (12, 4, 'INV002', 10, 'EUR') ; -insert into invoice_product (invoice_product_id, invoice_id, product_id, name, description, price, quantity) -values (13, 11, 7, 'Product 1', '', 1200, 1) - , (14, 12, 8, 'Product 2', '', 2400, 2) +insert into invoice_product (invoice_product_id, invoice_id, product_id, name, price) +values (13, 11, 7, 'Product 1', 1200) + , (14, 12, 8, 'Product 2', 2400) ; insert into invoice_product_tax (invoice_product_id, tax_id, tax_rate) diff --git a/test/product.sql b/test/product.sql index b92abd6..e2f76bc 100644 --- a/test/product.sql +++ b/test/product.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(49); +select plan(51); set search_path to numerus, auth, public; @@ -50,7 +50,8 @@ select col_hasnt_default('product', 'name'); select has_column('product', 'description'); select col_type_is('product', 'description', 'text'); 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 col_type_is('product', 'price', 'integer'); @@ -127,6 +128,15 @@ select throws_ok( 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 * from finish(); diff --git a/test/tax.sql b/test/tax.sql index 3ca72cb..0137dfd 100644 --- a/test/tax.sql +++ b/test/tax.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(35); +select plan(36); set search_path to numerus, auth, public; @@ -119,6 +119,13 @@ select throws_ok( ); 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 * from finish();