Skip to content

Commit

Permalink
test: add pgTap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Oct 17, 2023
1 parent 67ee425 commit bd3407b
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 2 deletions.
126 changes: 126 additions & 0 deletions test/unit/materialized_view_eio_emission_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
set client_encoding = 'utf-8';
set client_min_messages = warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(6);

select has_materialized_view(
'swrs_transform', 'eio_emission',
'swrs_transform.eio_emission should be a materialized view'
);

select has_index(
'swrs_transform', 'eio_emission', 'ggircs_eio_emission_primary_key',
'swrs_transform.eio_emission should have a primary key'
);

select columns_are('swrs_transform'::name, 'emission'::name, array[
'id'::name,
'eccc_xml_file_id'::name,
'activity_name'::name,
'sub_activity_name'::name,
'unit_name'::name,
'sub_unit_name'::name,
'process_idx'::name,
'sub_process_idx'::name,
'units_idx'::name,
'unit_idx'::name,
'substances_idx'::name,
'substance_idx'::name,
'fuel_idx'::name,
'fuel_name'::name,
'emissions_idx'::name,
'emission_idx'::name,
'emission_type'::name,
'gas_type'::name,
'methodology'::name,
'not_applicable'::name,
'quantity'::name,
'calculated_quantity'::name,
'emission_category'::name
]);

-- Insert data for fixture based testing
insert into swrs_extract.eccc_xml_file (xml_file) values ($$
<ReportData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ReportDetails>
<ReportType>eio</ReportType>
</ReportDetails>
<ActivityData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ActivityPages>
<Process ProcessName="ElectricityImportOperation">
<SubProcess SubprocessName="Electricity Import: Emissions from unspecified sources" InformationRequirement="Optional">
<Units>
<Unit>
<Fuels>
<Fuel>
<EIOEmissions>
<EIOEmission>
<NotApplicable>false</NotApplicable>
<EIOElectricityAmount>12345</EIOElectricityAmount>
<EIOEmissionAmount>99999</EIOEmissionAmount>
</EIOEmission>
</EIOEmissions>
</Fuel>
</Fuels>
</Unit>
</Units>
</SubProcess>
<SubProcess SubprocessName="Electricity Export: Emissions from specified sources" InformationRequirement="Optional">
<Units>
<Unit>
<Fuels>
<Fuel>
<EIOEmissions>
<EIOEmission>
<NotApplicable>true</NotApplicable>
<EIOElectricityAmount>54321</EIOElectricityAmount>
<EIOEmissionAmount>88888</EIOEmissionAmount>
</EIOEmission>
</EIOEmissions>
</Fuel>
</Fuels>
</Unit>
</Units>
</SubProcess>
</Process>
</ActivityPages>
</ActivityData>
</ReportData>
$$);


-- refresh necessary views with data
refresh materialized view swrs_transform.eio_emission with data;

select * from swrs_transform.eio_emission;

select is(
(select count(*) from swrs_transform.eio_emission),
2::bigint,
'swrs_transform.eio_emission has 2 rows'
);

-- ElectricityAmount
select results_eq(
$$ select quantity from swrs_transform.eio_emission where emission_idx=0 $$,
$$
values(99999::numeric), (88888::numeric)
$$,
'swrs_transform.eio_emission.quantity is extracted'
);

-- Quantity
select results_eq(
$$ select electricity_amount from swrs_transform.eio_emission where emission_idx=0 $$,
$$
values (12345::numeric), (54321::numeric)
$$,
'swrs_transform.eio_emission.quantity is extracted'
);

select * from finish();
rollback;
81 changes: 79 additions & 2 deletions test/unit/table_emission_test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ create extension if not exists pgtap;
reset client_min_messages;

begin;
select plan(14);
select plan(16);

insert into swrs_extract.eccc_xml_file (xml_file) values ($$
<ReportData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Expand Down Expand Up @@ -376,6 +376,25 @@ $$), ($$
</Units>
</SubProcess>
</Process>
<Process ProcessName="ElectricityImportOperation">
<SubProcess SubprocessName="Electricity Import: Emissions from unspecified sources" InformationRequirement="Optional">
<Units>
<Unit>
<Fuels>
<Fuel>
<EIOEmissions>
<EIOEmission>
<NotApplicable>false</NotApplicable>
<EIOElectricityAmount>12345</EIOElectricityAmount>
<EIOEmissionAmount>99999</EIOEmissionAmount>
</EIOEmission>
</EIOEmissions>
</Fuel>
</Fuels>
</Unit>
</Units>
</SubProcess>
</Process>
<Process>
<SubProcess SubprocessName="Additional Reportable Information as per WCI.352(i)(1)-(12)" InformationRequirement="MandatoryAdditional">
<Amount AmtDomain="PulpAndPaperBlackLiquor" AmtAction="Combusted" AmtPeriod="Annual">168389</Amount>
Expand Down Expand Up @@ -663,6 +682,7 @@ select set_eq(
join swrs.report
on emission.eccc_xml_file_id = report.eccc_xml_file_id
and report.report_type != 'R3'
and emission.electricity_amount is null
$$,
'Data from swrs_transform.emission is contained in swrs.emission'
);
Expand Down Expand Up @@ -701,6 +721,53 @@ select set_eq(
'R3 report data from swrs_transform.r3_emission is contained in swrs.emission'
);

-- Data in swrs.emission contains the swrs_transform.emission data
select set_eq(
$$
select
eio_emission.eccc_xml_file_id,
activity_name,
sub_activity_name,
unit_name,
sub_unit_name,
fuel_name,
emission_type,
gas_type,
not_applicable,
quantity,
calculated_quantity,
electricity_amount
from swrs_transform.eio_emission
join swrs_transform.report
on eio_emission.eccc_xml_file_id = report.eccc_xml_file_id
and report.report_type != 'R3'
order by
eccc_xml_file_id asc
$$,

$$
select
emission.eccc_xml_file_id,
activity_name,
sub_activity_name,
unit_name,
sub_unit_name,
fuel_name,
emission_type,
gas_type,
not_applicable,
quantity,
calculated_quantity,
electricity_amount
from swrs.emission
join swrs.report
on emission.eccc_xml_file_id = report.eccc_xml_file_id
and report.report_type != 'R3'
and emission.electricity_amount is not null
$$,
'Data from swrs_transform.eio_emission is contained in swrs.emission'
);

select results_eq(
$$
select emission_category from swrs.emission where fuel_mapping_id = (select id from ggircs_parameters.fuel_mapping where fuel_type = 'Vented Natural Gas CH4')
Expand All @@ -711,7 +778,7 @@ select results_eq(

select results_eq(
$$
select gas_type, quantity, ar5_calculated_quantity from swrs.emission
select gas_type, quantity, ar5_calculated_quantity from swrs.emission where electricity_amount is null
$$,
$$
values
Expand All @@ -726,5 +793,15 @@ select results_eq(
'ar5_calculated_quantity is populated with correct value'
);

select results_eq(
$$
select quantity, electricity_amount from swrs.emission where electricity_amount is not null
$$,
$$
values (99999::numeric, 12345::numeric)
$$,
'EIO emissions are properly parsed'
);

select * from finish();
rollback;

0 comments on commit bd3407b

Please sign in to comment.