Skip to content

Commit 1873f59

Browse files
authored
Merge pull request #365 from hs2361/test-emission-factor-year
feat (test): Test if emission factors for the correct year are used
2 parents ee598ff + 6787c8e commit 1873f59

File tree

4 files changed

+142
-17
lines changed

4 files changed

+142
-17
lines changed

utility-emissions-channel/typescript_app/tests/blockchain-gateway/datalock.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('DataLockGateway', () => {
4141
fabricConnector: org.connector,
4242
signer: signer,
4343
}).recordEmissions(adminCaller, {
44-
utilityId: 'USA_EIA_252522444142552441242521',
44+
utilityId: 'USA_EIA_11208',
4545
partyId: uuid4(),
4646
fromDate: '2020-05-07T10:10:09Z',
4747
thruDate: '2021-05-07T10:10:09Z',

utility-emissions-channel/typescript_app/tests/blockchain-gateway/utilityEmissionsChannel.test.ts

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chai from 'chai';
2+
import chaiHttp from 'chai-http';
23
import { SHA256 } from 'crypto-js';
34
import { config } from 'dotenv';
45
import { v4 as uuid4 } from 'uuid';
@@ -13,12 +14,13 @@ import ClientError from '../../src/errors/clientError';
1314
import { setupWebSocket } from '../setup-ws';
1415
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
1516
const should = chai.should();
17+
chai.use(chaiHttp);
1618

1719
setup('DEBUG', 'DEBUG');
1820
// env
1921
config();
2022

21-
const mockUtilityID = 'USA_EIA_252522444142552441242521';
23+
const mockUtilityID = 'USA_EIA_11208';
2224

2325
describe('UtilityemissionchannelGateway', () => {
2426
const bcConfig = new BCGatewayConfig();
@@ -94,6 +96,102 @@ describe('UtilityemissionchannelGateway', () => {
9496
}
9597
});
9698

99+
it('should use emissions factors for the correct year', async () => {
100+
const mockPartyID2 = uuid4();
101+
const usage = 100;
102+
const usage_uom_conversion = 1 / 1000;
103+
const emissions_uom_conversion = 1;
104+
const agent = chai.request.agent('http://127.0.0.1:5984');
105+
106+
const emission2018 = await utilityEmissionsGateway.recordEmissions(adminCaller, {
107+
utilityId: mockUtilityID,
108+
partyId: mockPartyID2,
109+
fromDate: '2018-01-01T00:00:00Z',
110+
thruDate: '2018-01-31T00:00:00Z',
111+
energyUseAmount: usage,
112+
energyUseUom: 'kWh',
113+
url: '',
114+
md5: '',
115+
});
116+
117+
const emission2019 = await utilityEmissionsGateway.recordEmissions(adminCaller, {
118+
utilityId: mockUtilityID,
119+
partyId: mockPartyID2,
120+
fromDate: '2019-01-01T00:00:00Z',
121+
thruDate: '2019-01-31T00:00:00Z',
122+
energyUseAmount: usage,
123+
energyUseUom: 'kWh',
124+
url: '',
125+
md5: '',
126+
});
127+
128+
emission2018.emissionsAmount.should.not.eq(emission2019.emissionsAmount);
129+
130+
await agent.post('/_session').set('content-type', 'application/json').send({
131+
name: 'admin',
132+
password: 'adminpw',
133+
});
134+
135+
const emissionSelector = (year: string) => ({
136+
selector: {
137+
class: {
138+
$eq: 'org.hyperledger.blockchain-carbon-accounting.utilityemissionsfactoritem',
139+
},
140+
division_id: {
141+
$eq: 'WECC',
142+
},
143+
division_type: {
144+
$eq: 'NERC_REGION',
145+
},
146+
year: {
147+
$eq: year,
148+
},
149+
},
150+
execution_stats: false,
151+
});
152+
153+
await agent
154+
.post('/utilityemissionchannel_utilityemissions/_find')
155+
.set('content-type', 'application/json')
156+
.send(emissionSelector('2018'))
157+
.then((response) => {
158+
response.status.should.be.eq(200);
159+
const data = response.body;
160+
161+
const utilityFactor = data.docs[0];
162+
163+
const emissions_value =
164+
(Number(utilityFactor.co2_equivalent_emissions) /
165+
Number(utilityFactor.net_generation)) *
166+
usage *
167+
usage_uom_conversion *
168+
emissions_uom_conversion;
169+
170+
emission2018.emissionsAmount.should.be.eq(emissions_value);
171+
});
172+
173+
await agent
174+
.post('/utilityemissionchannel_utilityemissions/_find')
175+
.set('content-type', 'application/json')
176+
.send(emissionSelector('2019'))
177+
.then((response) => {
178+
response.status.should.be.eq(200);
179+
const data = response.body;
180+
181+
const utilityFactor = data.docs[0];
182+
183+
const emissions_value =
184+
(Number(utilityFactor.co2_equivalent_emissions) /
185+
Number(utilityFactor.net_generation)) *
186+
usage *
187+
usage_uom_conversion *
188+
emissions_uom_conversion;
189+
190+
emission2019.emissionsAmount.should.be.eq(emissions_value);
191+
});
192+
agent.close();
193+
});
194+
97195
const mockTokenId = '0xMockToken';
98196
it('should update token if for minted records', async () => {
99197
await utilityEmissionsGateway.updateEmissionsMintedToken(adminCaller, {

utility-emissions-channel/typescript_app/tests/e2e.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const apiEndpoints = {
2626

2727
const adminVaultToken = 'tokenId';
2828
const userId = 'admin';
29-
const mockUtilityID = 'USA_EIA_252522444142552441242521';
29+
const mockUtilityID = 'USA_EIA_11208';
3030

3131
describe('E2E-vault', () => {
3232
tests('vault_token', adminVaultToken);
@@ -111,7 +111,7 @@ function tests(headerKey, headerValue) {
111111
});
112112
const partyId = uuid4();
113113
let uuid: string;
114-
it('should record a emissions', (done) => {
114+
it('should record an emission', (done) => {
115115
chai.request(v1Base)
116116
.post(apiEndpoints.recordEmissions)
117117
.set('content-type', 'application/x-www-form-urlencoded')

utility-emissions-channel/typescript_app/tests/setup.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
FabricContractInvocationType,
55
FabricSigningCredential,
66
} from '@hyperledger/cactus-plugin-ledger-connector-fabric';
7+
// import { execSync } from 'child_process';
78
import { config } from 'dotenv';
89
import axios from 'axios';
910
config();
1011

1112
const bcConfig = new BCGatewayConfig();
1213

1314
// insert mock utility identifier and factors
14-
const mockUtilityID = 'USA_EIA_252522444142552441242521';
1515
async function mockEmissionsRecord() {
1616
const signer: FabricSigningCredential = {
1717
keychainId: 'inMemoryKeychain',
@@ -27,9 +27,12 @@ async function mockEmissionsRecord() {
2727
caId: org.caID,
2828
mspId: org.orgMSP,
2929
});
30+
3031
const channelName = 'utilityemissionchannel';
3132
const ccName = 'utilityemissions';
3233
// import utility identifier
34+
const mockUtilityID = 'USA_EIA_11208';
35+
3336
const p1 = hlfConnector.transact({
3437
signingCredential: signer,
3538
channelName: channelName,
@@ -39,13 +42,13 @@ async function mockEmissionsRecord() {
3942
params: [
4043
mockUtilityID,
4144
'2019',
42-
'252522444142552441242521',
43-
'test-utility-name',
45+
'11208',
46+
'Los Angeles Department of Water & Power',
4447
'USA',
4548
'',
4649
JSON.stringify({
4750
division_type: 'NERC_REGION',
48-
division_id: 'MRO',
51+
division_id: 'WECC',
4952
}),
5053
],
5154
});
@@ -58,24 +61,48 @@ async function mockEmissionsRecord() {
5861
invocationType: FabricContractInvocationType.Send,
5962
methodName: 'importUtilityFactor',
6063
params: [
61-
'mock-utility-factor',
62-
'2019',
64+
'USA_2018_NERC_REGION_WECC',
65+
'2018',
6366
'USA',
6467
'NERC_REGION',
65-
'MRO',
66-
'SERC_Reliability_Corporation',
67-
'46112136.165',
68+
'WECC',
69+
'Western_Electricity_Coordinating_Council',
70+
'743291275',
6871
'MWH',
69-
'47582155.875',
72+
'288021204',
7073
'tons',
7174
'https://www.epa.gov/sites/production/files/2020-01/egrid2018_all_files.zip',
72-
'41078452.268',
73-
'5033683.71',
75+
'443147683',
76+
'300143593',
77+
'',
78+
],
79+
});
80+
81+
const p3 = hlfConnector.transact({
82+
signingCredential: signer,
83+
channelName: channelName,
84+
contractName: ccName,
85+
invocationType: FabricContractInvocationType.Send,
86+
methodName: 'importUtilityFactor',
87+
params: [
88+
'USA_2019_NERC_REGION_WECC',
89+
'2019',
90+
'USA',
91+
'NERC_REGION',
92+
'WECC',
93+
'Western_Electricity_Coordinating_Council',
94+
'738835346',
95+
'MWH',
96+
'285747759',
97+
'tons',
98+
'https://www.epa.gov/sites/production/files/2021-02/egrid2019_data.xlsx',
99+
'447805417',
100+
'291029929',
74101
'',
75102
],
76103
});
77104

78-
await Promise.all([p1, p2]);
105+
await Promise.all([p1, p2, p3]);
79106
}
80107

81108
async function setupVault() {

0 commit comments

Comments
 (0)