Add positive_integer and nonnegative_integer domains

This is easier to read and requires less unit tests, but i only used
them in the new relations and fields for HEAD, because i do not see any
point on creating migrations just for that.
This commit is contained in:
jordi fita mas 2024-02-13 22:12:30 +01:00
parent 7e39e5f549
commit af31daba8a
17 changed files with 190 additions and 146 deletions

View File

@ -1,15 +1,18 @@
-- Deploy camper:campsite_type_cost__per_age to pg
-- requires: campsite_type_cost
-- requires: nonnegative_integer
begin;
alter table camper.campsite_type_cost
add column cost_per_adult integer not null default 0 constraint per_adult_not_negative check(cost_per_adult >= 0)
, add column cost_per_teenager integer not null default 0 constraint per_teenager_not_negative check(cost_per_teenager >= 0)
, add column cost_per_child integer not null default 0 constraint per_child_not_negative check(cost_per_child >= 0)
set search_path to camper, public;
alter table campsite_type_cost
add column cost_per_adult nonnegative_integer not null default 0
, add column cost_per_teenager nonnegative_integer not null default 0
, add column cost_per_child nonnegative_integer not null default 0
;
alter table camper.campsite_type_cost
alter table campsite_type_cost
alter column cost_per_adult drop default
, alter column cost_per_teenager drop default
, alter column cost_per_child drop default

View File

@ -3,6 +3,7 @@
-- requires: schema_camper
-- requires: campsite_type
-- requires: user_profile
-- requires: nonnegative_integer
begin;
@ -10,7 +11,7 @@ set search_path to camper, public;
create table campsite_type_pet_cost (
campsite_type_id integer primary key references campsite_type,
cost_per_night integer not null constraint cost_not_negative check(cost_per_night >= 0)
cost_per_night nonnegative_integer not null
);
grant select on table campsite_type_pet_cost to guest;

View File

@ -0,0 +1,14 @@
-- Deploy camper:nonnegative_integer to pg
-- requires: schema_camper
begin;
set search_path to camper, public;
create domain nonnegative_integer as integer
check (value >= 0)
;
comment on domain nonnegative_integer is 'an integer that is either zero or positive';
commit;

View File

@ -4,6 +4,8 @@
-- requires: company
-- requires: campsite_type
-- requires: payment_status
-- requires: positive_integer
-- requires: nonnegative_integer
begin;
@ -16,17 +18,17 @@ create table payment (
campsite_type_id integer not null references campsite_type,
arrival_date date not null,
departure_date date not null constraint departure_after_arrival check (departure_date > arrival_date),
subtotal_nights integer not null constraint subtotal_nights_not_negative check (subtotal_nights >= 0),
number_adults integer not null constraint number_adults_positive check (number_adults > 0),
subtotal_adults integer not null constraint subtotal_adults_not_negative check (subtotal_adults >= 0),
number_teenagers integer not null constraint number_teenagers_not_negative check (number_teenagers >= 0),
subtotal_teenagers integer not null constraint subtotal_teenagers_not_negative check (subtotal_teenagers >= 0),
number_children integer not null constraint number_children_not_negative check (number_children >= 0),
subtotal_children integer not null constraint subtotal_children_not_negative check (subtotal_children >= 0),
number_dogs integer not null constraint number_dogs_not_negative check (number_dogs >= 0),
subtotal_dogs integer not null constraint subtotal_dogs_not_negative check (subtotal_dogs >= 0),
subtotal_tourist_tax integer not null constraint subtotal_tourist_tax_not_negative check (subtotal_tourist_tax >= 0),
total integer not null constraint total_not_negative check (total >= 0),
subtotal_nights nonnegative_integer not null,
number_adults positive_integer not null,
subtotal_adults nonnegative_integer not null,
number_teenagers nonnegative_integer not null,
subtotal_teenagers nonnegative_integer not null,
number_children nonnegative_integer not null,
subtotal_children nonnegative_integer not null,
number_dogs nonnegative_integer not null,
subtotal_dogs nonnegative_integer not null,
subtotal_tourist_tax nonnegative_integer not null,
total nonnegative_integer not null,
zone_preferences text not null,
payment_status text not null default 'draft' references payment_status,
created_at timestamp with time zone not null default current_timestamp,

View File

@ -3,6 +3,8 @@
-- requires: schema_camper
-- requires: payment
-- requires: campsite_type_option
-- requires: positive_integer
-- requires: nonnegative_integer
begin;
@ -11,8 +13,8 @@ set search_path to camper, public;
create table payment_option (
payment_id integer not null references payment,
campsite_type_option_id integer not null references campsite_type_option,
units integer not null constraint units_positive check (units > 0),
subtotal integer not null constraint subtotal_not_negative check (subtotal >= 0),
units positive_integer not null,
subtotal nonnegative_integer not null,
primary key (payment_id, campsite_type_option_id)
);

View File

@ -0,0 +1,14 @@
-- Deploy camper:positive_integer to pg
-- requires: schema_camper
begin;
set search_path to camper, public;
create domain positive_integer as integer
check (value > 0)
;
comment on domain positive_integer is 'an integer that can not be negative or zero; a natural number';
commit;

View File

@ -0,0 +1,7 @@
-- Revert camper:nonnegative_integer from pg
begin;
drop domain if exists camper.nonnegative_integer;
commit;

View File

@ -0,0 +1,7 @@
-- Revert camper:positive_integer from pg
begin;
drop domain if exists camper.positive_integer;
commit;

View File

@ -225,10 +225,11 @@ edit_campsite_type [edit_campsite_type@v2 campsite_type__overflow_allowed campsi
@v3 2024-01-31T20:40:16Z jordi fita mas <jordi@tandem.blog> # Tag v3
campsite_type__bookable_nights [campsite_type campsite_type_cost] 2024-01-31T19:45:46Z jordi fita mas <jordi@tandem.blog> # Add bookable_nights to campsite_type
campsite_type_cost__per_age [campsite_type_cost] 2024-02-03T20:48:54Z jordi fita mas <jordi@tandem.blog> # Add cost_per_adult, cost_per_teenager, and cost_per_child fields to campsite_type_cost
nonnegative_integer [schema_camper] 2024-02-13T20:31:38Z jordi fita mas <jordi@tandem.blog> # Add nonnegative integer domain
campsite_type_cost__per_age [campsite_type_cost nonnegative_integer] 2024-02-03T20:48:54Z jordi fita mas <jordi@tandem.blog> # Add cost_per_adult, cost_per_teenager, and cost_per_child fields to campsite_type_cost
set_campsite_type_cost [set_campsite_type_cost@v3 campsite_type__bookable_nights campsite_type_cost__per_age] 2024-01-31T21:02:06Z jordi fita mas <jordi@tandem.blog> # Remove min_nights parameter from set_campsite_type_cost, and add per_adult, per_teenager, and per_child parameters
campsite_type_cost__-min_nights [campsite_type__bookable_nights set_campsite_type_cost] 2024-01-31T21:46:33Z jordi fita mas <jordi@tandem.blog> # Remove min_nights field from campsite_type_cost relation
campsite_type_pet_cost [roles schema_camper campsite_type user_profile] 2024-02-10T03:24:04Z jordi fita mas <jordi@tandem.blog> # Add relation of pet cost of campsite type
campsite_type_pet_cost [roles schema_camper campsite_type user_profile nonnegative_integer] 2024-02-10T03:24:04Z jordi fita mas <jordi@tandem.blog> # Add relation of pet cost of campsite type
set_campsite_type_pet_cost [roles schema_camper campsite_type campsite_type_pet_cost parse_price] 2024-02-10T03:49:54Z jordi fita mas <jordi@tandem.blog> # Add function to set pet cost of campsite type
unset_campsite_type_pet_cost [roles schema_camper campsite_type campsite_type_pet_cost] 2024-02-10T04:00:11Z jordi fita mas <jordi@tandem.blog> # Add function to remove pet cost from campsite type
add_campsite_type [add_campsite_type@v3 campsite_type__bookable_nights] 2024-01-31T20:46:47Z jordi fita mas <jordi@tandem.blog> # Add bookable_nights param to, and remove dogs_allowed param from add_campsite_type
@ -243,9 +244,10 @@ campsite_type_option_cost__-cost_per_night [campsite_type_option_cost campsite_t
payment_status [roles schema_camper] 2024-02-11T21:13:32Z jordi fita mas <jordi@tandem.blog> # Add relation of payment statuses
payment_status_i18n [roles schema_camper payment_status language] 2024-02-11T21:20:11Z jordi fita mas <jordi@tandem.blog> # Add relation for translation of payment status
available_payment_status [payment_status payment_status_i18n] 2024-02-11T21:22:38Z jordi fita mas <jordi@tandem.blog> # Add available payment statuses
payment [roles schema_camper company campsite_type payment_status] 2024-02-11T21:54:13Z jordi fita mas <jordi@tandem.blog> # Add relation for payments
positive_integer [schema_camper] 2024-02-13T20:24:09Z jordi fita mas <jordi@tandem.blog> # Add positive integer domain
payment [roles schema_camper company campsite_type payment_status positive_integer nonnegative_integer] 2024-02-11T21:54:13Z jordi fita mas <jordi@tandem.blog> # Add relation for payments
payment_customer [roles schema_camper payment country country_code extension_pg_libphonenumber] 2024-02-12T00:10:20Z jordi fita mas <jordi@tandem.blog> # Add relation of payment customer
payment_option [roles schema_camper payment campsite_type_option] 2024-02-12T00:58:07Z jordi fita mas <jordi@tandem.blog> # Add relation of payment for campsite type options
payment_option [roles schema_camper payment campsite_type_option positive_integer nonnegative_integer] 2024-02-12T00:58:07Z jordi fita mas <jordi@tandem.blog> # Add relation of payment for campsite type options
draft_payment [roles schema_camper season_calendar season campsite_type campsite_type_pet_cost campsite_type_cost campsite_type_option_cost campsite_type_option payment payment_option] 2024-02-12T01:31:52Z jordi fita mas <jordi@tandem.blog> # Add function to create a payment draft
ready_payment [roles schema_camper payment payment_customer country_code email extension_pg_libphonenumber] 2024-02-12T12:57:24Z jordi fita mas <jordi@tandem.blog> # Add function to ready a draft payment
redsys_response [schema_camper currency_code] 2024-02-12T19:49:29Z jordi fita mas <jordi@tandem.blog> # Add type for Redsys responses

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin;
select plan(57);
select plan(54);
set search_path to camper, public;
@ -37,17 +37,17 @@ select col_not_null('campsite_type_cost', 'cost_per_night');
select col_hasnt_default('campsite_type_cost', 'cost_per_night');
select has_column('campsite_type_cost', 'cost_per_adult');
select col_type_is('campsite_type_cost', 'cost_per_adult', 'integer');
select col_type_is('campsite_type_cost', 'cost_per_adult', 'nonnegative_integer');
select col_not_null('campsite_type_cost', 'cost_per_adult');
select col_hasnt_default('campsite_type_cost', 'cost_per_adult');
select has_column('campsite_type_cost', 'cost_per_teenager');
select col_type_is('campsite_type_cost', 'cost_per_teenager', 'integer');
select col_type_is('campsite_type_cost', 'cost_per_teenager', 'nonnegative_integer');
select col_not_null('campsite_type_cost', 'cost_per_teenager');
select col_hasnt_default('campsite_type_cost', 'cost_per_teenager');
select has_column('campsite_type_cost', 'cost_per_child');
select col_type_is('campsite_type_cost', 'cost_per_child', 'integer');
select col_type_is('campsite_type_cost', 'cost_per_child', 'nonnegative_integer');
select col_not_null('campsite_type_cost', 'cost_per_child');
select col_hasnt_default('campsite_type_cost', 'cost_per_child');
@ -240,24 +240,6 @@ select throws_ok(
'Should not be able to insert campsite type costs with negative cost per night.'
);
select throws_ok(
$$ insert into campsite_type_cost (campsite_type_id, season_id, cost_per_night, cost_per_adult, cost_per_teenager, cost_per_child) values (16, 27, 1, -1, 3, 4) $$,
'23514', 'new row for relation "campsite_type_cost" violates check constraint "per_adult_not_negative"',
'Should not be able to insert campsite type costs with negative cost per adult.'
);
select throws_ok(
$$ insert into campsite_type_cost (campsite_type_id, season_id, cost_per_night, cost_per_adult, cost_per_teenager, cost_per_child) values (16, 27, 1, 2, -1, 4) $$,
'23514', 'new row for relation "campsite_type_cost" violates check constraint "per_teenager_not_negative"',
'Should not be able to insert campsite type costs with negative cost per teenager.'
);
select throws_ok(
$$ insert into campsite_type_cost (campsite_type_id, season_id, cost_per_night, cost_per_adult, cost_per_teenager, cost_per_child) values (16, 27, 1, 2, 3, -1) $$,
'23514', 'new row for relation "campsite_type_cost" violates check constraint "per_child_not_negative"',
'Should not be able to insert campsite type costs with negative cost per child.'
);
reset role;

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin;
select plan(31);
select plan(30);
set search_path to camper, public;
@ -25,7 +25,7 @@ select col_not_null('campsite_type_pet_cost', 'campsite_type_id');
select col_hasnt_default('campsite_type_pet_cost', 'campsite_type_id');
select has_column('campsite_type_pet_cost', 'cost_per_night');
select col_type_is('campsite_type_pet_cost', 'cost_per_night', 'integer');
select col_type_is('campsite_type_pet_cost', 'cost_per_night', 'nonnegative_integer');
select col_not_null('campsite_type_pet_cost', 'cost_per_night');
select col_hasnt_default('campsite_type_pet_cost', 'cost_per_night');
@ -178,12 +178,6 @@ select bag_eq(
'No row should have been changed'
);
select throws_ok(
$$ insert into campsite_type_pet_cost (campsite_type_id, cost_per_night) values (17, -1) $$,
'23514', 'new row for relation "campsite_type_pet_cost" violates check constraint "cost_not_negative"',
'Should not be able to insert negative pet costs.'
);
reset role;

View File

@ -0,0 +1,37 @@
-- Test nonnegative_integer
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(10);
set search_path to camper, public;
select has_domain('nonnegative_integer');
select domain_type_is('nonnegative_integer', 'integer');
select lives_ok($$ select 0::nonnegative_integer $$);
select lives_ok($$ select 1::nonnegative_integer $$);
select lives_ok($$ select 2::nonnegative_integer $$);
select lives_ok($$ select 10::nonnegative_integer $$);
select lives_ok($$ select 31289::nonnegative_integer $$);
select lives_ok($$ select 8891892::nonnegative_integer $$);
select throws_ok(
$$ select (-1)::nonnegative_integer $$,
23514, null,
'No negative numbers allowed'
);
select is(
1.123::nonnegative_integer,
1::nonnegative_integer,
'Integers only'
);
select *
from finish();
rollback;

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin;
select plan(114);
select plan(103);
set search_path to camper, public;
@ -54,57 +54,57 @@ select col_not_null('payment', 'departure_date');
select col_hasnt_default('payment', 'departure_date');
select has_column('payment', 'subtotal_nights');
select col_type_is('payment', 'subtotal_nights', 'integer');
select col_type_is('payment', 'subtotal_nights', 'nonnegative_integer');
select col_not_null('payment', 'subtotal_nights');
select col_hasnt_default('payment', 'subtotal_nights');
select has_column('payment', 'number_adults');
select col_type_is('payment', 'number_adults', 'integer');
select col_type_is('payment', 'number_adults', 'positive_integer');
select col_not_null('payment', 'number_adults');
select col_hasnt_default('payment', 'number_adults');
select has_column('payment', 'subtotal_adults');
select col_type_is('payment', 'subtotal_adults', 'integer');
select col_type_is('payment', 'subtotal_adults', 'nonnegative_integer');
select col_not_null('payment', 'subtotal_adults');
select col_hasnt_default('payment', 'subtotal_adults');
select has_column('payment', 'number_teenagers');
select col_type_is('payment', 'number_teenagers', 'integer');
select col_type_is('payment', 'number_teenagers', 'nonnegative_integer');
select col_not_null('payment', 'number_teenagers');
select col_hasnt_default('payment', 'number_teenagers');
select has_column('payment', 'subtotal_teenagers');
select col_type_is('payment', 'subtotal_teenagers', 'integer');
select col_type_is('payment', 'subtotal_teenagers', 'nonnegative_integer');
select col_not_null('payment', 'subtotal_teenagers');
select col_hasnt_default('payment', 'subtotal_teenagers');
select has_column('payment', 'number_children');
select col_type_is('payment', 'number_children', 'integer');
select col_type_is('payment', 'number_children', 'nonnegative_integer');
select col_not_null('payment', 'number_children');
select col_hasnt_default('payment', 'number_children');
select has_column('payment', 'subtotal_children');
select col_type_is('payment', 'subtotal_children', 'integer');
select col_type_is('payment', 'subtotal_children', 'nonnegative_integer');
select col_not_null('payment', 'subtotal_children');
select col_hasnt_default('payment', 'subtotal_children');
select has_column('payment', 'number_dogs');
select col_type_is('payment', 'number_dogs', 'integer');
select col_type_is('payment', 'number_dogs', 'nonnegative_integer');
select col_not_null('payment', 'number_dogs');
select col_hasnt_default('payment', 'number_dogs');
select has_column('payment', 'subtotal_dogs');
select col_type_is('payment', 'subtotal_dogs', 'integer');
select col_type_is('payment', 'subtotal_dogs', 'nonnegative_integer');
select col_not_null('payment', 'subtotal_dogs');
select col_hasnt_default('payment', 'subtotal_dogs');
select has_column('payment', 'subtotal_tourist_tax');
select col_type_is('payment', 'subtotal_tourist_tax', 'integer');
select col_type_is('payment', 'subtotal_tourist_tax', 'nonnegative_integer');
select col_not_null('payment', 'subtotal_tourist_tax');
select col_hasnt_default('payment', 'subtotal_tourist_tax');
select has_column('payment', 'total');
select col_type_is('payment', 'total', 'integer');
select col_type_is('payment', 'total', 'nonnegative_integer');
select col_not_null('payment', 'total');
select col_hasnt_default('payment', 'total');
@ -165,72 +165,6 @@ select throws_ok(
'Should not be able to insert a payment with a departure date equal or before the arrival date (i.e., at least one night)'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "subtotal_nights_not_negative"',
'Should not be able to insert a payment with negative subtotal for nights'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "number_adults_positive"',
'Should not be able to insert a payment with no adults'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "subtotal_adults_not_negative"',
'Should not be able to insert a payment with a negative subtotal for adults'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "number_teenagers_not_negative"',
'Should not be able to insert a payment with a negative number of teenagers'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "subtotal_teenagers_not_negative"',
'Should not be able to insert a payment with a negative subtotal for teenagers'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "number_children_not_negative"',
'Should not be able to insert a payment with a negative number of children'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "subtotal_children_not_negative"',
'Should not be able to insert a payment with a negative subtotal for children'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "number_dogs_not_negative"',
'Should not be able to insert a payment with a negative number of dogs'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "subtotal_dogs_not_negative"',
'Should not be able to insert a payment with a negative subtotal for dogs'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0, '') $$,
'23514', 'new row for relation "payment" violates check constraint "subtotal_tourist_tax_not_negative"',
'Should not be able to insert a payment with a negative subtotal for tourist tax'
);
select throws_ok(
$$ insert into payment (company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, zone_preferences) values (1, 10, '2024-07-07', '2024-07-09', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1, '') $$,
'23514', 'new row for relation "payment" violates check constraint "total_not_negative"',
'Should not be able to insert a payment with a negative total'
);
select *
from finish();

View File

@ -5,7 +5,7 @@ reset client_min_messages;
begin;
select plan(29);
select plan(27);
set search_path to camper, public;
@ -32,12 +32,12 @@ select col_not_null('payment_option', 'campsite_type_option_id');
select col_hasnt_default('payment_option', 'campsite_type_option_id');
select has_column('payment_option', 'units');
select col_type_is('payment_option', 'units', 'integer');
select col_type_is('payment_option', 'units', 'positive_integer');
select col_not_null('payment_option', 'units');
select col_hasnt_default('payment_option', 'units');
select has_column('payment_option', 'subtotal');
select col_type_is('payment_option', 'subtotal', 'integer');
select col_type_is('payment_option', 'subtotal', 'nonnegative_integer');
select col_not_null('payment_option', 'subtotal');
select col_hasnt_default('payment_option', 'subtotal');
@ -77,18 +77,6 @@ insert into payment (payment_id, company_id, campsite_type_id, arrival_date, dep
values (15, 1, 10, '2024-07-07', '2024-07-08', 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')
;
select throws_ok(
$$ insert into payment_option (payment_id, campsite_type_option_id, units, subtotal) values (15, 11, 0, 0) $$,
'23514', 'new row for relation "payment_option" violates check constraint "units_positive"',
'Should not be able to insert a payment option with zero units'
);
select throws_ok(
$$ insert into payment_option (payment_id, campsite_type_option_id, units, subtotal) values (15, 12, 1, -1) $$,
'23514', 'new row for relation "payment_option" violates check constraint "subtotal_not_negative"',
'Should not be able to insert a payment option with a negative subtotal'
);
select *
from finish();

43
test/positive_integer.sql Normal file
View File

@ -0,0 +1,43 @@
-- Test positive_integer
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(10);
set search_path to camper, public;
select has_domain('positive_integer');
select domain_type_is('positive_integer', 'integer');
select lives_ok($$ select 1::positive_integer $$);
select lives_ok($$ select 2::positive_integer $$);
select lives_ok($$ select 10::positive_integer $$);
select lives_ok($$ select 31289::positive_integer $$);
select lives_ok($$ select 8891892::positive_integer $$);
select throws_ok(
$$ select (-1)::positive_integer $$,
23514, null,
'No negative numbers allowed'
);
select throws_ok(
$$ select 0::positive_integer $$,
23514, null,
'Zero is also not allowed'
);
select is(
1.123::positive_integer,
1::positive_integer,
'Integers only'
);
select *
from finish();
rollback;

View File

@ -0,0 +1,7 @@
-- Verify camper:nonnegative_integer on pg
begin;
select pg_catalog.has_type_privilege('camper.nonnegative_integer', 'usage');
rollback;

View File

@ -0,0 +1,7 @@
-- Verify camper:positive_integer on pg
begin;
select pg_catalog.has_type_privilege('camper.positive_integer', 'usage');
rollback;