From bd3407b0329631b2f36f336e3f57c100e88b571d Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Mon, 16 Oct 2023 15:17:11 -0700 Subject: [PATCH] test: add pgTap tests --- .../materialized_view_eio_emission_test.sql | 126 ++++++++++++++++++ test/unit/table_emission_test.sql | 81 ++++++++++- 2 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 test/unit/materialized_view_eio_emission_test.sql diff --git a/test/unit/materialized_view_eio_emission_test.sql b/test/unit/materialized_view_eio_emission_test.sql new file mode 100644 index 00000000..12f3dfbf --- /dev/null +++ b/test/unit/materialized_view_eio_emission_test.sql @@ -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 ($$ + + + eio + + + + + + + + + + + + false + 12345 + 99999 + + + + + + + + + + + + + + + true + 54321 + 88888 + + + + + + + + + + + +$$); + + +-- 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; diff --git a/test/unit/table_emission_test.sql b/test/unit/table_emission_test.sql index e2736bf1..c914f479 100644 --- a/test/unit/table_emission_test.sql +++ b/test/unit/table_emission_test.sql @@ -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 ($$ @@ -376,6 +376,25 @@ $$), ($$ + + + + + + + + + false + 12345 + 99999 + + + + + + + + 168389 @@ -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' ); @@ -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') @@ -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 @@ -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;