diff --git a/test/data/40_legal-units-sector-change-middle-of-month.csv b/test/data/40_legal-units-sector-change-middle-of-month.csv new file mode 100644 index 000000000..fa5e0435a --- /dev/null +++ b/test/data/40_legal-units-sector-change-middle-of-month.csv @@ -0,0 +1,3 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2011-01-14,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-15,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,8200,AS diff --git a/test/data/40_legal-units-sector-change-start-of-second-month.csv b/test/data/40_legal-units-sector-change-start-of-second-month.csv new file mode 100644 index 000000000..b7c8caeb1 --- /dev/null +++ b/test/data/40_legal-units-sector-change-start-of-second-month.csv @@ -0,0 +1,3 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2011-01-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-02-01,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,8200,AS diff --git a/test/data/40_legal-units-sector-change-start-of-year.csv b/test/data/40_legal-units-sector-change-start-of-year.csv new file mode 100644 index 000000000..6d8d44a21 --- /dev/null +++ b/test/data/40_legal-units-sector-change-start-of-year.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2011-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,7000,AS +2012-01-01,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,8200,AS diff --git a/test/data/41_legal-units-births-middle-of-month.csv b/test/data/41_legal-units-births-middle-of-month.csv new file mode 100644 index 000000000..1b0301969 --- /dev/null +++ b/test/data/41_legal-units-births-middle-of-month.csv @@ -0,0 +1,5 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-15,2010-12-31,823573673,Kranløft Vestland,2010-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-14,823573673,Kranløft Vestland,2010-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-15,infinity,823573673,Kranløft Vestland,2010-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-15,infinity,921835809,Kranløft Østland,2011-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/41_legal-units-births-start-of-second-month.csv b/test/data/41_legal-units-births-start-of-second-month.csv new file mode 100644 index 000000000..02bd0193d --- /dev/null +++ b/test/data/41_legal-units-births-start-of-second-month.csv @@ -0,0 +1,5 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-02-01,2010-12-31,823573673,Kranløft Vestland,2010-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-14,823573673,Kranløft Vestland,2010-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-15,infinity,823573673,Kranløft Vestland,2010-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-02-01,infinity,921835809,Kranløft Østland,2011-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/41_legal-units-births-start-of-year.csv b/test/data/41_legal-units-births-start-of-year.csv new file mode 100644 index 000000000..12523ec0f --- /dev/null +++ b/test/data/41_legal-units-births-start-of-year.csv @@ -0,0 +1,5 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-14,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-15,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,infinity,921835809,Kranløft Østland,2011-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-end-of-first-month.csv b/test/data/42_legal-units-deaths-end-of-first-month.csv new file mode 100644 index 000000000..939293748 --- /dev/null +++ b/test/data/42_legal-units-deaths-end-of-first-month.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2011-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-01-31,921835809,Kranløft Østland,2010-01-01,2011-01-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-end-of-year.csv b/test/data/42_legal-units-deaths-end-of-year.csv new file mode 100644 index 000000000..20da52831 --- /dev/null +++ b/test/data/42_legal-units-deaths-end-of-year.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-12-31,921835809,Kranløft Østland,2010-01-01,2011-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-start-of-month.csv b/test/data/42_legal-units-deaths-start-of-month.csv new file mode 100644 index 000000000..bbe84fafa --- /dev/null +++ b/test/data/42_legal-units-deaths-start-of-month.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-01,823573673,Kranløft Vestland,2010-01-01,2012-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-01-01,921835809,Kranløft Østland,2010-01-01,2011-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/expected/40_history_legal_units_changes_over_time.out b/test/expected/40_history_legal_units_changes_over_time.out new file mode 100644 index 000000000..e69de29bb diff --git a/test/expected/41_history_legal_units_births.out b/test/expected/41_history_legal_units_births.out new file mode 100644 index 000000000..e69de29bb diff --git a/test/expected/42_history_legal_units_deaths.out b/test/expected/42_history_legal_units_deaths.out new file mode 100644 index 000000000..e69de29bb diff --git a/test/sql/40_history_legal_units_changes_over_time.sql b/test/sql/40_history_legal_units_changes_over_time.sql new file mode 100644 index 000000000..a0cd51882 --- /dev/null +++ b/test/sql/40_history_legal_units_changes_over_time.sql @@ -0,0 +1,174 @@ +SET datestyle TO 'ISO, DMY'; + +BEGIN; + +\echo "Setting up Statbus to test enterprise grouping and primary" + +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + +SAVEPOINT before_loading_units; + +\echo "Test sector changes in the middle of a month" + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-middle-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Test statistical unit history by year - sector_change_count should be 1 for year 2011" +SELECT resolution, year, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Test statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test sector changes at the start of the second month" + + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-start-of-second-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year - sector_change_count should be 1 for year 2011" +SELECT resolution, year,month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2012 +AND unit_type = 'legal_unit'; + + + +\echo "Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-2" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2012 +AND unit_type = 'legal_unit'; + +\x + +ROLLBACK TO before_loading_units; + +\echo "Test sector changes at the start of the year" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-start-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + + +\echo "Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year - sector_change_count should be 1 for year 2011 and 2012" +SELECT resolution, year,month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2014 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1 and 2012-1" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\x + +ROLLBACK; \ No newline at end of file diff --git a/test/sql/41_history_legal_units_births.sql b/test/sql/41_history_legal_units_births.sql new file mode 100644 index 000000000..ed24b5880 --- /dev/null +++ b/test/sql/41_history_legal_units_births.sql @@ -0,0 +1,174 @@ +SET datestyle TO 'ISO, DMY'; + +BEGIN; + +\echo "Setting up Statbus to test enterprise grouping and primary" + +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + +SAVEPOINT before_loading_units; + +\echo "Test births at the start of the year" + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-start-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + + +\echo "Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test births at the start of the second month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-start-of-second-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-2 and 2011-2" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + +ROLLBACK TO before_loading_units; + +\echo "Test births in the middle of a month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-middle-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\x + +ROLLBACK; \ No newline at end of file diff --git a/test/sql/42_history_legal_units_deaths.sql b/test/sql/42_history_legal_units_deaths.sql new file mode 100644 index 000000000..5486a314d --- /dev/null +++ b/test/sql/42_history_legal_units_deaths.sql @@ -0,0 +1,173 @@ +SET datestyle TO 'ISO, DMY'; + +BEGIN; + +\echo "Setting up Statbus to test enterprise grouping and primary" + +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + +SAVEPOINT before_loading_units; + +\echo "Test deaths at the end of the year" + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-end-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-12 and 2012-12" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test deaths at the end of the first month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-end-of-first-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-12" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test deaths in the start of a month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-start-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + + +\x + +ROLLBACK;