Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: ignore non carbon taxed emissions #2002

Merged
merged 7 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mocks_schema/sqitch.plan
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%syntax-version=1.0.0
%project=mocks
%uri=https://github.com/bcgov/cas-ciip-portal/tree/develop/mocks_schema

schema_mocks 2020-10-28T17:42:06Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # Creating schema for mocked functions
mock_now_method [schema_mocks] 2020-10-28T17:59:38Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # Mock method to override the default now() behaviour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,4 @@ mutations/create_application_revision_mutation_chain [mutations/create_applicati
@v2.13.0 2021-09-28T16:52:13Z Dylan Leard <dylan@button.is> # release v2.13.0
@v2.13.1 2021-09-28T19:27:43Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # release v2.13.1
@v2.13.2 2021-10-05T19:51:38Z Matthieu Foucault <matthieu@button.is> # 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 <dylan@button.is> # Validation function to determine if there a missing fuels based on the emission categories with reported emissions
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions test_helper_schema/sqitch.plan
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%syntax-version=1.0.0
%project=test_helpers
%uri=https://github.com/bcgov/cas-ciip-portal/tree/develop/test_helper_schema

schema_test_helper 2020-09-24T20:39:13Z Dylan Leard <dylan@button.is> # schema to house test helper functions
modify_triggers [schema_test_helper] 2020-09-29T17:16:23Z Dylan Leard <dylan@button.is> # function enables/disables triggers for test setup
Expand Down