Skip to content

Commit c3510c0

Browse files
committed
feat (test): Test if emission factors for the correct region are used
Signed-off-by: Harsh Sharma <bcs_2019023@iiitm.ac.in>
1 parent 35f923c commit c3510c0

File tree

3 files changed

+164
-5
lines changed

3 files changed

+164
-5
lines changed

utility-emissions-channel/chaincode/typescript/src/lib/utilityEmissionsFactor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class UtilityEmissionsFactorState extends WorldState<UtilityEmissionsFact
128128
lookup: UtilityLookupItemInterface,
129129
thruDate: string,
130130
): Promise<UtilityEmissionsFactor> {
131-
const hasStateData = lookup.state_province.length > 0;
131+
const hasStateData = lookup.state_province !== undefined;
132132
const isNercRegion = lookup.divisions.division_type.toLowerCase() === 'nerc_region';
133133
const isNonUSCountry =
134134
lookup.divisions.division_type.toLowerCase() === 'country' &&
@@ -163,6 +163,7 @@ export class UtilityEmissionsFactorState extends WorldState<UtilityEmissionsFact
163163
divisionType,
164164
year,
165165
);
166+
console.log(utilityFactors);
166167
if (utilityFactors.length === 0) {
167168
throw new Error('No utility emissions factor found for given query');
168169
}

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

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,120 @@ describe('UtilityemissionchannelGateway', () => {
192192
agent.close();
193193
});
194194

195+
it('should use emissions factors for the correct region', async () => {
196+
const mockUtilityID_DE = 'RWE_AG';
197+
const mockPartyID2 = uuid4();
198+
const usage = 100;
199+
const usage_uom_conversion = 1 / 1000;
200+
const agent = chai.request.agent('http://127.0.0.1:5984');
201+
202+
const emissionUSA = await utilityEmissionsGateway.recordEmissions(adminCaller, {
203+
utilityId: mockUtilityID,
204+
partyId: mockPartyID2,
205+
fromDate: '2019-01-01T00:00:00Z',
206+
thruDate: '2019-01-31T00:00:00Z',
207+
energyUseAmount: usage,
208+
energyUseUom: 'kWh',
209+
url: '',
210+
md5: '',
211+
});
212+
213+
const emissionDE = await utilityEmissionsGateway.recordEmissions(adminCaller, {
214+
utilityId: mockUtilityID_DE,
215+
partyId: mockPartyID2,
216+
fromDate: '2019-01-01T00:00:00Z',
217+
thruDate: '2019-01-31T00:00:00Z',
218+
energyUseAmount: usage,
219+
energyUseUom: 'kWh',
220+
url: '',
221+
md5: '',
222+
});
223+
224+
emissionDE.emissionsAmount.should.not.eq(emissionUSA.emissionsAmount);
225+
226+
await agent.post('/_session').set('content-type', 'application/json').send({
227+
name: 'admin',
228+
password: 'adminpw',
229+
});
230+
231+
await agent
232+
.post('/utilityemissionchannel_utilityemissions/_find')
233+
.set('content-type', 'application/json')
234+
.send({
235+
selector: {
236+
class: {
237+
$eq: 'org.hyperledger.blockchain-carbon-accounting.utilityemissionsfactoritem',
238+
},
239+
division_id: {
240+
$eq: 'WECC',
241+
},
242+
division_type: {
243+
$eq: 'NERC_REGION',
244+
},
245+
year: {
246+
$eq: '2019',
247+
},
248+
},
249+
execution_stats: false,
250+
})
251+
.then((response) => {
252+
response.status.should.be.eq(200);
253+
const data = response.body;
254+
const utilityFactor = data.docs[0];
255+
const emissions_uom_conversion = 1;
256+
257+
const emissions_value =
258+
(Number(utilityFactor.co2_equivalent_emissions) /
259+
Number(utilityFactor.net_generation)) *
260+
usage *
261+
usage_uom_conversion *
262+
emissions_uom_conversion;
263+
264+
emissionUSA.emissionsAmount.should.be.eq(emissions_value);
265+
});
266+
267+
await agent
268+
.post('/utilityemissionchannel_utilityemissions/_find')
269+
.set('content-type', 'application/json')
270+
.send({
271+
selector: {
272+
class: {
273+
$eq: 'org.hyperledger.blockchain-carbon-accounting.utilityemissionsfactoritem',
274+
},
275+
division_id: {
276+
$eq: 'Germany',
277+
},
278+
division_type: {
279+
$eq: 'Country',
280+
},
281+
year: {
282+
$eq: '2019',
283+
},
284+
},
285+
execution_stats: false,
286+
})
287+
.then((response) => {
288+
response.status.should.be.eq(200);
289+
const data = response.body;
290+
const utilityFactor = data.docs[0];
291+
const emissions_uom_conversion = 1000;
292+
293+
const emissions_value =
294+
Number(utilityFactor.co2_equivalent_emissions) *
295+
usage *
296+
(usage_uom_conversion / emissions_uom_conversion);
297+
298+
const percent_of_renewables = Number(utilityFactor.percent_of_renewables) / 100;
299+
300+
emissionDE.emissionsAmount.should.be.eq(emissions_value);
301+
emissionDE.renewableEnergyUseAmount.should.be.eq(usage * percent_of_renewables);
302+
emissionDE.nonrenewableEnergyUseAmount.should.be.eq(
303+
usage * (1 - percent_of_renewables),
304+
);
305+
});
306+
agent.close();
307+
});
308+
195309
const mockTokenId = '0xMockToken';
196310
it('should update token if for minted records', async () => {
197311
await utilityEmissionsGateway.updateEmissionsMintedToken(adminCaller, {
@@ -269,7 +383,7 @@ describe('UtilityemissionchannelGateway', () => {
269383
thruDate: '2021-05-07T10:10:09Z',
270384
energyUseAmount: 100,
271385
energyUseUom: 'kWh',
272-
url: 'localost:///tmp/filename',
386+
url: 'localhost:///tmp/filename',
273387
md5: '',
274388
});
275389
const documentUrl = data.url;

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,28 @@ async function mockEmissionsRecord() {
5353
],
5454
});
5555

56-
// import mock utility factor
5756
const p2 = hlfConnector.transact({
57+
signingCredential: signer,
58+
channelName: channelName,
59+
contractName: ccName,
60+
invocationType: FabricContractInvocationType.Send,
61+
methodName: 'importUtilityIdentifier',
62+
params: [
63+
'RWE_AG',
64+
'2019',
65+
'1',
66+
'RWE AG',
67+
'DE',
68+
'',
69+
JSON.stringify({
70+
division_type: 'COUNTRY',
71+
division_id: 'Germany',
72+
}),
73+
],
74+
});
75+
76+
// import mock utility factor
77+
const p3 = hlfConnector.transact({
5878
signingCredential: signer,
5979
channelName: channelName,
6080
contractName: ccName,
@@ -78,7 +98,7 @@ async function mockEmissionsRecord() {
7898
],
7999
});
80100

81-
const p3 = hlfConnector.transact({
101+
const p4 = hlfConnector.transact({
82102
signingCredential: signer,
83103
channelName: channelName,
84104
contractName: ccName,
@@ -102,7 +122,31 @@ async function mockEmissionsRecord() {
102122
],
103123
});
104124

105-
await Promise.all([p1, p2, p3]);
125+
const p5 = hlfConnector.transact({
126+
signingCredential: signer,
127+
channelName: channelName,
128+
contractName: ccName,
129+
invocationType: FabricContractInvocationType.Send,
130+
methodName: 'importUtilityFactor',
131+
params: [
132+
'COUNTRY_DE_2019',
133+
'2019',
134+
'Germany',
135+
'Country',
136+
'Germany',
137+
'Germany',
138+
'',
139+
'',
140+
'338',
141+
'g/KWH',
142+
'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',
143+
'',
144+
'',
145+
'41.03',
146+
],
147+
});
148+
149+
await Promise.all([p1, p2, p3, p4, p5]);
106150
}
107151

108152
async function setupVault() {

0 commit comments

Comments
 (0)