Skip to content

Commit

Permalink
[test] Add tests for statistical_history
Browse files Browse the repository at this point in the history
  • Loading branch information
hhssb authored and jhf committed Oct 22, 2024
1 parent 3f6bbcf commit 3cbd9c1
Show file tree
Hide file tree
Showing 15 changed files with 558 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/data/40_legal-units-sector-change-middle-of-month.csv
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/data/40_legal-units-sector-change-start-of-year.csv
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/data/41_legal-units-births-middle-of-month.csv
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/data/41_legal-units-births-start-of-second-month.csv
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/data/41_legal-units-births-start-of-year.csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/data/42_legal-units-deaths-end-of-first-month.csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/data/42_legal-units-deaths-end-of-year.csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/data/42_legal-units-deaths-start-of-month.csv
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
Empty file.
174 changes: 174 additions & 0 deletions test/sql/40_history_legal_units_changes_over_time.sql
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 3cbd9c1

Please sign in to comment.