Skip to content

Commit

Permalink
Merge pull request #373 from hs2361/test-emission-factor-region
Browse files Browse the repository at this point in the history
feat (test): Test if emission factors for the correct region are used
  • Loading branch information
sichen1234 authored Dec 8, 2021
2 parents 35f923c + c3510c0 commit 1c5bc0a
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class UtilityEmissionsFactorState extends WorldState<UtilityEmissionsFact
lookup: UtilityLookupItemInterface,
thruDate: string,
): Promise<UtilityEmissionsFactor> {
const hasStateData = lookup.state_province.length > 0;
const hasStateData = lookup.state_province !== undefined;
const isNercRegion = lookup.divisions.division_type.toLowerCase() === 'nerc_region';
const isNonUSCountry =
lookup.divisions.division_type.toLowerCase() === 'country' &&
Expand Down Expand Up @@ -163,6 +163,7 @@ export class UtilityEmissionsFactorState extends WorldState<UtilityEmissionsFact
divisionType,
year,
);
console.log(utilityFactors);
if (utilityFactors.length === 0) {
throw new Error('No utility emissions factor found for given query');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,120 @@ describe('UtilityemissionchannelGateway', () => {
agent.close();
});

it('should use emissions factors for the correct region', async () => {
const mockUtilityID_DE = 'RWE_AG';
const mockPartyID2 = uuid4();
const usage = 100;
const usage_uom_conversion = 1 / 1000;
const agent = chai.request.agent('http://127.0.0.1:5984');

const emissionUSA = await utilityEmissionsGateway.recordEmissions(adminCaller, {
utilityId: mockUtilityID,
partyId: mockPartyID2,
fromDate: '2019-01-01T00:00:00Z',
thruDate: '2019-01-31T00:00:00Z',
energyUseAmount: usage,
energyUseUom: 'kWh',
url: '',
md5: '',
});

const emissionDE = await utilityEmissionsGateway.recordEmissions(adminCaller, {
utilityId: mockUtilityID_DE,
partyId: mockPartyID2,
fromDate: '2019-01-01T00:00:00Z',
thruDate: '2019-01-31T00:00:00Z',
energyUseAmount: usage,
energyUseUom: 'kWh',
url: '',
md5: '',
});

emissionDE.emissionsAmount.should.not.eq(emissionUSA.emissionsAmount);

await agent.post('/_session').set('content-type', 'application/json').send({
name: 'admin',
password: 'adminpw',
});

await agent
.post('/utilityemissionchannel_utilityemissions/_find')
.set('content-type', 'application/json')
.send({
selector: {
class: {
$eq: 'org.hyperledger.blockchain-carbon-accounting.utilityemissionsfactoritem',
},
division_id: {
$eq: 'WECC',
},
division_type: {
$eq: 'NERC_REGION',
},
year: {
$eq: '2019',
},
},
execution_stats: false,
})
.then((response) => {
response.status.should.be.eq(200);
const data = response.body;
const utilityFactor = data.docs[0];
const emissions_uom_conversion = 1;

const emissions_value =
(Number(utilityFactor.co2_equivalent_emissions) /
Number(utilityFactor.net_generation)) *
usage *
usage_uom_conversion *
emissions_uom_conversion;

emissionUSA.emissionsAmount.should.be.eq(emissions_value);
});

await agent
.post('/utilityemissionchannel_utilityemissions/_find')
.set('content-type', 'application/json')
.send({
selector: {
class: {
$eq: 'org.hyperledger.blockchain-carbon-accounting.utilityemissionsfactoritem',
},
division_id: {
$eq: 'Germany',
},
division_type: {
$eq: 'Country',
},
year: {
$eq: '2019',
},
},
execution_stats: false,
})
.then((response) => {
response.status.should.be.eq(200);
const data = response.body;
const utilityFactor = data.docs[0];
const emissions_uom_conversion = 1000;

const emissions_value =
Number(utilityFactor.co2_equivalent_emissions) *
usage *
(usage_uom_conversion / emissions_uom_conversion);

const percent_of_renewables = Number(utilityFactor.percent_of_renewables) / 100;

emissionDE.emissionsAmount.should.be.eq(emissions_value);
emissionDE.renewableEnergyUseAmount.should.be.eq(usage * percent_of_renewables);
emissionDE.nonrenewableEnergyUseAmount.should.be.eq(
usage * (1 - percent_of_renewables),
);
});
agent.close();
});

const mockTokenId = '0xMockToken';
it('should update token if for minted records', async () => {
await utilityEmissionsGateway.updateEmissionsMintedToken(adminCaller, {
Expand Down Expand Up @@ -269,7 +383,7 @@ describe('UtilityemissionchannelGateway', () => {
thruDate: '2021-05-07T10:10:09Z',
energyUseAmount: 100,
energyUseUom: 'kWh',
url: 'localost:///tmp/filename',
url: 'localhost:///tmp/filename',
md5: '',
});
const documentUrl = data.url;
Expand Down
50 changes: 47 additions & 3 deletions utility-emissions-channel/typescript_app/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,28 @@ async function mockEmissionsRecord() {
],
});

// import mock utility factor
const p2 = hlfConnector.transact({
signingCredential: signer,
channelName: channelName,
contractName: ccName,
invocationType: FabricContractInvocationType.Send,
methodName: 'importUtilityIdentifier',
params: [
'RWE_AG',
'2019',
'1',
'RWE AG',
'DE',
'',
JSON.stringify({
division_type: 'COUNTRY',
division_id: 'Germany',
}),
],
});

// import mock utility factor
const p3 = hlfConnector.transact({
signingCredential: signer,
channelName: channelName,
contractName: ccName,
Expand All @@ -78,7 +98,7 @@ async function mockEmissionsRecord() {
],
});

const p3 = hlfConnector.transact({
const p4 = hlfConnector.transact({
signingCredential: signer,
channelName: channelName,
contractName: ccName,
Expand All @@ -102,7 +122,31 @@ async function mockEmissionsRecord() {
],
});

await Promise.all([p1, p2, p3]);
const p5 = hlfConnector.transact({
signingCredential: signer,
channelName: channelName,
contractName: ccName,
invocationType: FabricContractInvocationType.Send,
methodName: 'importUtilityFactor',
params: [
'COUNTRY_DE_2019',
'2019',
'Germany',
'Country',
'Germany',
'Germany',
'',
'',
'338',
'g/KWH',
'https://www.eea.europa.eu/data-and-maps/data/approximated-estimates-for-the-share-4/eea-2017-res-share-proxies/2016-res_proxies_eea_csv/at_download/file;https://www.eea.europa.eu/data-and-maps/daviz/co2-emission-intensity-9',
'',
'',
'41.03',
],
});

await Promise.all([p1, p2, p3, p4, p5]);
}

async function setupVault() {
Expand Down

0 comments on commit 1c5bc0a

Please sign in to comment.