From ad09162baafbd0d450fdd735b7092989d6c6323c Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Tue, 5 Oct 2021 12:56:36 -0700 Subject: [PATCH 1/7] chore: sqitch no longer allows duplicate project URIs --- mocks_schema/sqitch.plan | 1 + test_helper_schema/sqitch.plan | 1 + 2 files changed, 2 insertions(+) diff --git a/mocks_schema/sqitch.plan b/mocks_schema/sqitch.plan index 78d89171b0..5a53cf686e 100644 --- a/mocks_schema/sqitch.plan +++ b/mocks_schema/sqitch.plan @@ -1,5 +1,6 @@ %syntax-version=1.0.0 %project=mocks +%uri=mocks schema_mocks 2020-10-28T17:42:06Z Pierre Bastianelli # Creating schema for mocked functions mock_now_method [schema_mocks] 2020-10-28T17:59:38Z Pierre Bastianelli # Mock method to override the default now() behaviour diff --git a/test_helper_schema/sqitch.plan b/test_helper_schema/sqitch.plan index eda6ca9745..92bfeac371 100644 --- a/test_helper_schema/sqitch.plan +++ b/test_helper_schema/sqitch.plan @@ -1,5 +1,6 @@ %syntax-version=1.0.0 %project=test_helpers +%uri=test_helpers schema_test_helper 2020-09-24T20:39:13Z Dylan Leard # schema to house test helper functions modify_triggers [schema_test_helper] 2020-09-29T17:16:23Z Dylan Leard # function enables/disables triggers for test setup From 24d8d22c2f699cc15ade30d954044fd4cf27ef7e Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Tue, 5 Oct 2021 12:57:23 -0700 Subject: [PATCH 2/7] feat: update validation function to ignore non carbon-taxed fuels --- .../emission_category_missing_fuel.sql | 3 ++ ...emission_category_missing_fuel@v2.13.0.sql | 32 +++++++++++++++++++ ...emission_category_missing_fuel@v2.13.0.sql | 7 ++++ ...emission_category_missing_fuel@v2.13.0.sql | 7 ++++ 4 files changed, 49 insertions(+) create mode 100644 schema/deploy/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql create mode 100644 schema/revert/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql create mode 100644 schema/verify/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql diff --git a/schema/deploy/application_validation_functions/emission_category_missing_fuel.sql b/schema/deploy/application_validation_functions/emission_category_missing_fuel.sql index 62e9002902..40bc7e2af8 100644 --- a/schema/deploy/application_validation_functions/emission_category_missing_fuel.sql +++ b/schema/deploy/application_validation_functions/emission_category_missing_fuel.sql @@ -11,6 +11,9 @@ create or replace function ggircs_portal.emission_category_missing_fuel(app_revi with emissions as ( select distinct(source_type_name) as reported_via_emission from ggircs_portal.application_revision_emission_form_data(app_revision) ed + join ggircs_portal.emission_category ec + on ed.source_type_name = ec.display_name + and ec.carbon_taxed=true where ed.annual_co2e > 0 ), fuel_data as ( diff --git a/schema/deploy/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql b/schema/deploy/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql new file mode 100644 index 0000000000..62e9002902 --- /dev/null +++ b/schema/deploy/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql @@ -0,0 +1,32 @@ +-- Deploy ggircs-portal:application_validation_functions/emission_category_missing_fuel to pg +-- requires: tables/application_revision_validation_function +-- requires: computed_columns/application_revision_validation + +begin; + +create or replace function ggircs_portal.emission_category_missing_fuel(app_revision ggircs_portal.application_revision) + returns boolean + as $$ + + with emissions as ( + select distinct(source_type_name) as reported_via_emission + from ggircs_portal.application_revision_emission_form_data(app_revision) ed + where ed.annual_co2e > 0 + ), + fuel_data as ( + select * from ggircs_portal.application_revision_fuel_form_data(app_revision) + ), + fuels as ( + select distinct(display_name) as reported_via_fuel from ggircs_portal.emission_category ec + join fuel_data + on fuel_data.emission_category_id = ec.id + and fuel_data.quantity > 0 + ) select (select count(*) from emissions where reported_via_emission not in (select reported_via_fuel from fuels)) = 0; + +$$ language sql stable; + +grant execute on function ggircs_portal.emission_category_missing_fuel to ciip_administrator, ciip_analyst, ciip_industry_user; + +comment on function ggircs_portal.emission_category_missing_fuel(ggircs_portal.application_revision) is 'This validation function for a CIIP (CleanBC Industrial Incentive Program) application determines if any emission categories have emissions reported in a category, but no corresponding fuels reported for that category'; + +commit; diff --git a/schema/revert/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql b/schema/revert/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql new file mode 100644 index 0000000000..961adb3e7a --- /dev/null +++ b/schema/revert/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql @@ -0,0 +1,7 @@ +-- Revert ggircs-portal:application_validation_functions/emission_category_missing_fuel from pg + +begin; + +drop function ggircs_portal.emission_category_missing_fuel; + +commit; diff --git a/schema/verify/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql b/schema/verify/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql new file mode 100644 index 0000000000..fd9de47dd7 --- /dev/null +++ b/schema/verify/application_validation_functions/emission_category_missing_fuel@v2.13.0.sql @@ -0,0 +1,7 @@ +-- Verify ggircs-portal:application_validation_functions/emission_category_missing_fuel on pg + +begin; + +select pg_get_functiondef('ggircs_portal.application_revision_ciip_incentive(ggircs_portal.application_revision)'::regprocedure); + +rollback; From f5bf13fe8523524d03d6ee818bdd19101d87f723 Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Tue, 5 Oct 2021 12:57:39 -0700 Subject: [PATCH 3/7] test: update pgTap test --- .../emission_category_missing_fuel_test.sql | 48 +++++++------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/schema/test/unit/application_validation_functions/emission_category_missing_fuel_test.sql b/schema/test/unit/application_validation_functions/emission_category_missing_fuel_test.sql index 5a3d936f05..a11ce78061 100644 --- a/schema/test/unit/application_validation_functions/emission_category_missing_fuel_test.sql +++ b/schema/test/unit/application_validation_functions/emission_category_missing_fuel_test.sql @@ -28,19 +28,7 @@ $$; select test_helper.mock_open_window(); select test_helper.create_test_users(); -select test_helper.create_applications(2, True, True); - -update ggircs_portal.form_result -set form_result = -'[ - { - "fuelRowId": 13, - "quantity": 10, - "fuelUnits": "t", - "emissionCategoryRowId": 1 - } - ]' -where form_id=3; +select test_helper.create_applications(1, True, True); update ggircs_portal.form_result set form_result = @@ -65,34 +53,30 @@ set form_result = }' where application_id=1 and version_number=1 and form_id=2; -update ggircs_portal.form_result -set form_result = -' -{ - "sourceTypes": [ - { - "gases": - [ - {"gwp": 1, "gasType": "CO2nonbio", "annualCO2e": 10, "annualEmission": 5, "gasDescription": "Carbon dioxide from non-biomass"} - ], - "sourceTypeName": "General Stationary Combustion" - } - ] -}' -where application_id=2 and version_number=1 and form_id=2; - select is( (with record as (select row(application_revision.*)::ggircs_portal.application_revision from ggircs_portal.application_revision where application_id=1 and version_number=1) select ggircs_portal.emission_category_missing_fuel((select * from record))), false, - 'Function returns false when there are emission categories with an emission reported, but no corresponding fuel reported' + 'Function returns false when there are carbon taxed emission categories with an emission reported, but no corresponding fuel reported' ); +update ggircs_portal.form_result +set form_result = +'[ + { + "fuelRowId": 13, + "quantity": 10, + "fuelUnits": "t", + "emissionCategoryRowId": 1 + } + ]' +where form_id=3; + select is( - (with record as (select row(application_revision.*)::ggircs_portal.application_revision from ggircs_portal.application_revision where application_id=2 and version_number=1) + (with record as (select row(application_revision.*)::ggircs_portal.application_revision from ggircs_portal.application_revision where application_id=1 and version_number=1) select ggircs_portal.emission_category_missing_fuel((select * from record))), true, - 'Function returns true when all emission categories with an emission reported have a corresponding fuel reported' + 'Function returns true when all carbon taxed emission categories with an emission reported have a corresponding fuel reported' ); select finish(); From 8dab9f2d170de9bf9c57dd5e77e8ee05c0a761e4 Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Tue, 5 Oct 2021 14:14:53 -0700 Subject: [PATCH 4/7] refactor: fix broken sqitch plan --- schema/sqitch.plan | 1 + 1 file changed, 1 insertion(+) diff --git a/schema/sqitch.plan b/schema/sqitch.plan index c635ccd25b..94edde1dd7 100644 --- a/schema/sqitch.plan +++ b/schema/sqitch.plan @@ -385,3 +385,4 @@ mutations/create_application_revision_mutation_chain [mutations/create_applicati @v2.13.0 2021-09-28T16:52:13Z Dylan Leard # release v2.13.0 @v2.13.1 2021-09-28T19:27:43Z Pierre Bastianelli # release v2.13.1 @v2.13.2 2021-10-05T19:51:38Z Matthieu Foucault # release v2.13.2 +application_validation_functions/emission_category_missing_fuel [application_validation_functions/emission_category_missing_fuel@v2.13.0] 2021-04-27T21:54:17Z Dylan Leard # Validation function to determine if there a missing fuels based on the emission categories with reported emissions From 6ef560c9fad4d9b44490fed6d4b0f7856d76a8e6 Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Tue, 5 Oct 2021 14:30:26 -0700 Subject: [PATCH 5/7] refactor: fix broken revert file --- .../emission_category_missing_fuel.sql | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/schema/revert/application_validation_functions/emission_category_missing_fuel.sql b/schema/revert/application_validation_functions/emission_category_missing_fuel.sql index 961adb3e7a..b84d71c89e 100644 --- a/schema/revert/application_validation_functions/emission_category_missing_fuel.sql +++ b/schema/revert/application_validation_functions/emission_category_missing_fuel.sql @@ -2,6 +2,29 @@ begin; -drop function ggircs_portal.emission_category_missing_fuel; +create or replace function ggircs_portal.emission_category_missing_fuel(app_revision ggircs_portal.application_revision) + returns boolean + as $$ + + with emissions as ( + select distinct(source_type_name) as reported_via_emission + from ggircs_portal.application_revision_emission_form_data(app_revision) ed + where ed.annual_co2e > 0 + ), + fuel_data as ( + select * from ggircs_portal.application_revision_fuel_form_data(app_revision) + ), + fuels as ( + select distinct(display_name) as reported_via_fuel from ggircs_portal.emission_category ec + join fuel_data + on fuel_data.emission_category_id = ec.id + and fuel_data.quantity > 0 + ) select (select count(*) from emissions where reported_via_emission not in (select reported_via_fuel from fuels)) = 0; + +$$ language sql stable; + +grant execute on function ggircs_portal.emission_category_missing_fuel to ciip_administrator, ciip_analyst, ciip_industry_user; + +comment on function ggircs_portal.emission_category_missing_fuel(ggircs_portal.application_revision) is 'This validation function for a CIIP (CleanBC Industrial Incentive Program) application determines if any emission categories have emissions reported in a category, but no corresponding fuels reported for that category'; commit; From f1472e33cb1db1b1a43b3d40f3095d16129de66f Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Thu, 7 Oct 2021 09:10:59 -0700 Subject: [PATCH 6/7] chore: add proper URIs to mock schema sqitch plans --- mocks_schema/sqitch.plan | 2 +- test_helper_schema/sqitch.plan | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mocks_schema/sqitch.plan b/mocks_schema/sqitch.plan index 5a53cf686e..dd84294e3a 100644 --- a/mocks_schema/sqitch.plan +++ b/mocks_schema/sqitch.plan @@ -1,6 +1,6 @@ %syntax-version=1.0.0 %project=mocks -%uri=mocks +ttps://github.com/bcgov/cas-ciip-portal/tree/develop/mocks_schema schema_mocks 2020-10-28T17:42:06Z Pierre Bastianelli # Creating schema for mocked functions mock_now_method [schema_mocks] 2020-10-28T17:59:38Z Pierre Bastianelli # Mock method to override the default now() behaviour diff --git a/test_helper_schema/sqitch.plan b/test_helper_schema/sqitch.plan index 92bfeac371..69d95ff691 100644 --- a/test_helper_schema/sqitch.plan +++ b/test_helper_schema/sqitch.plan @@ -1,6 +1,6 @@ %syntax-version=1.0.0 %project=test_helpers -%uri=test_helpers +ttps://github.com/bcgov/cas-ciip-portal/tree/develop/test_helper_schema schema_test_helper 2020-09-24T20:39:13Z Dylan Leard # schema to house test helper functions modify_triggers [schema_test_helper] 2020-09-29T17:16:23Z Dylan Leard # function enables/disables triggers for test setup From cd2b301212941b6134f524cedc2258f1657d4a3e Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Thu, 7 Oct 2021 10:01:01 -0700 Subject: [PATCH 7/7] chore: fix copy paste error --- mocks_schema/sqitch.plan | 2 +- test_helper_schema/sqitch.plan | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mocks_schema/sqitch.plan b/mocks_schema/sqitch.plan index dd84294e3a..1a912ac291 100644 --- a/mocks_schema/sqitch.plan +++ b/mocks_schema/sqitch.plan @@ -1,6 +1,6 @@ %syntax-version=1.0.0 %project=mocks -ttps://github.com/bcgov/cas-ciip-portal/tree/develop/mocks_schema +%uri=https://github.com/bcgov/cas-ciip-portal/tree/develop/mocks_schema schema_mocks 2020-10-28T17:42:06Z Pierre Bastianelli # Creating schema for mocked functions mock_now_method [schema_mocks] 2020-10-28T17:59:38Z Pierre Bastianelli # Mock method to override the default now() behaviour diff --git a/test_helper_schema/sqitch.plan b/test_helper_schema/sqitch.plan index 69d95ff691..ada1278dda 100644 --- a/test_helper_schema/sqitch.plan +++ b/test_helper_schema/sqitch.plan @@ -1,6 +1,6 @@ %syntax-version=1.0.0 %project=test_helpers -ttps://github.com/bcgov/cas-ciip-portal/tree/develop/test_helper_schema +%uri=https://github.com/bcgov/cas-ciip-portal/tree/develop/test_helper_schema schema_test_helper 2020-09-24T20:39:13Z Dylan Leard # schema to house test helper functions modify_triggers [schema_test_helper] 2020-09-29T17:16:23Z Dylan Leard # function enables/disables triggers for test setup