Skip to content

Commit

Permalink
Merge pull request #69 from Green-Software-Foundation/boavizta-test
Browse files Browse the repository at this point in the history
Boavizta test coverage
  • Loading branch information
MariamKhalatova authored Apr 4, 2024
2 parents 5eba722 + e8da5dc commit b95b271
Show file tree
Hide file tree
Showing 4 changed files with 396 additions and 164 deletions.
305 changes: 165 additions & 140 deletions src/__mocks__/boavizta/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,146 @@ import * as PROVIDERS from './providers.json';
import * as COUNTRIES from './countries.json';
import * as INSTANCE_TYPES from './instance_types.json';

const resolveData = {
impacts: {
gwp: {
embedded: {
value: 0.0016,
min: 0.000831,
max: 0.006226,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 0.1924, min: 0.1924, max: 0.1924},
unit: 'kgCO2eq',
description: 'Total climate change',
},
adp: {
embedded: {
value: 0.000001553,
min: 0.000001553,
max: 0.000001554,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.126e-8, min: 5.126e-8, max: 5.126e-8},
unit: 'kgSbeq',
description: 'Use of minerals and fossil ressources',
},
pe: {
embedded: {
value: 0.023,
min: 0.01369,
max: 0.08627,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.907, min: 5.907, max: 5.907},
unit: 'MJ',
description: 'Consumption of primary energy',
},
},
verbose: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
min: 0.000831,
max: 0.006226,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 0.1924, min: 0.1924, max: 0.1924},
unit: 'kgCO2eq',
description: 'Total climate change',
},
adp: {
embedded: {
value: 0.000001553,
min: 0.000001553,
max: 0.000001554,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.126e-8, min: 5.126e-8, max: 5.126e-8},
unit: 'kgSbeq',
description: 'Use of minerals and fossil ressources',
},
pe: {
embedded: {
value: 0.023,
min: 0.01369,
max: 0.08627,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.907, min: 5.907, max: 5.907},
unit: 'MJ',
description: 'Consumption of primary energy',
},
},
units: {value: 1, status: 'ARCHETYPE', min: 1, max: 1},
die_size: {
value: 521,
status: 'COMPLETED',
unit: 'mm2',
source: 'Average value for all families',
min: 41.2,
max: 3640,
},
duration: {value: 2, unit: 'hours'},
avg_power: {
value: 260.05,
status: 'COMPLETED',
unit: 'W',
min: 260.05,
max: 260.05,
},
time_workload: {value: 100, status: 'INPUT', unit: '%'},
usage_location: {
value: 'USA',
status: 'INPUT',
unit: 'CodSP3 - NCS Country Codes - NATO',
},
use_time_ratio: {
value: 1,
status: 'ARCHETYPE',
unit: '/1',
min: 1,
max: 1,
},
hours_life_time: {
value: 26280,
status: 'ARCHETYPE',
unit: 'hours',
min: 26280,
max: 26280,
},
params: {
value: {a: 171.2, b: 0.0354, c: 36.89, d: -10.13},
status: 'ARCHETYPE',
},
gwp_factor: {
value: 0.37,
status: 'COMPLETED',
unit: 'kg CO2eq/kWh',
source: 'https://ember-climate.org/data/data-explorer',
min: 0.37,
max: 0.37,
},
adp_factor: {
value: 9.85548e-8,
status: 'COMPLETED',
unit: 'kg Sbeq/kWh',
source: 'ADEME Base Impacts®',
min: 9.85548e-8,
max: 9.85548e-8,
},
pe_factor: {
value: 11.358,
status: 'COMPLETED',
unit: 'MJ/kWh',
source: 'ADPf / (1-%renewable_energy)',
min: 11.358,
max: 11.358,
},
},
};

export function mockPost<T = any, R = AxiosResponse<T, any>>(
url: string
): Promise<R> {
Expand Down Expand Up @@ -48,7 +188,17 @@ export function mockPost<T = any, R = AxiosResponse<T, any>>(
},
},
} as R);
case 'https://api.boavizta.org/v1/component/cpu?verbose=false&duration=3':
return Promise.resolve({
data: {},
} as R);

case 'https://api.boavizta.org/v1/component/cpu?verbose=true&duration=2':
if (process.env.CPU_WRONG_DATA) {
return Promise.reject({
data: {response: {detail: 'error message'}},
});
}
return Promise.resolve({
data: {
impacts: {
Expand Down Expand Up @@ -175,7 +325,7 @@ export function mockPost<T = any, R = AxiosResponse<T, any>>(
value: 9.85548e-8,
status: 'COMPLETED',
unit: 'kg Sbeq/kWh',
source: 'ADEME Base Impacts ®',
source: 'ADEME Base Impacts®',
min: 9.85548e-8,
max: 9.85548e-8,
},
Expand All @@ -191,146 +341,21 @@ export function mockPost<T = any, R = AxiosResponse<T, any>>(
},
} as R);
case 'https://api.boavizta.org/v1/cloud/instance?verbose=false&duration=0.004166666666666667':
if (process.env.WRONG_DATA) {
return Promise.reject({
data: {response: {detail: 'error message'}},
});
}
return Promise.resolve({
data: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
min: 0.000831,
max: 0.006226,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 0.1924, min: 0.1924, max: 0.1924},
unit: 'kgCO2eq',
description: 'Total climate change',
},
adp: {
embedded: {
value: 0.000001553,
min: 0.000001553,
max: 0.000001554,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.126e-8, min: 5.126e-8, max: 5.126e-8},
unit: 'kgSbeq',
description: 'Use of minerals and fossil ressources',
},
pe: {
embedded: {
value: 0.023,
min: 0.01369,
max: 0.08627,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.907, min: 5.907, max: 5.907},
unit: 'MJ',
description: 'Consumption of primary energy',
},
},
verbose: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
min: 0.000831,
max: 0.006226,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 0.1924, min: 0.1924, max: 0.1924},
unit: 'kgCO2eq',
description: 'Total climate change',
},
adp: {
embedded: {
value: 0.000001553,
min: 0.000001553,
max: 0.000001554,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.126e-8, min: 5.126e-8, max: 5.126e-8},
unit: 'kgSbeq',
description: 'Use of minerals and fossil ressources',
},
pe: {
embedded: {
value: 0.023,
min: 0.01369,
max: 0.08627,
warnings: ['End of life is not included in the calculation'],
},
use: {value: 5.907, min: 5.907, max: 5.907},
unit: 'MJ',
description: 'Consumption of primary energy',
},
},
units: {value: 1, status: 'ARCHETYPE', min: 1, max: 1},
die_size: {
value: 521,
status: 'COMPLETED',
unit: 'mm2',
source: 'Average value for all families',
min: 41.2,
max: 3640,
},
duration: {value: 2, unit: 'hours'},
avg_power: {
value: 260.05,
status: 'COMPLETED',
unit: 'W',
min: 260.05,
max: 260.05,
},
time_workload: {value: 100, status: 'INPUT', unit: '%'},
usage_location: {
value: 'USA',
status: 'INPUT',
unit: 'CodSP3 - NCS Country Codes - NATO',
},
use_time_ratio: {
value: 1,
status: 'ARCHETYPE',
unit: '/1',
min: 1,
max: 1,
},
hours_life_time: {
value: 26280,
status: 'ARCHETYPE',
unit: 'hours',
min: 26280,
max: 26280,
},
params: {
value: {a: 171.2, b: 0.0354, c: 36.89, d: -10.13},
status: 'ARCHETYPE',
},
gwp_factor: {
value: 0.37,
status: 'COMPLETED',
unit: 'kg CO2eq/kWh',
source: 'https://ember-climate.org/data/data-explorer',
min: 0.37,
max: 0.37,
},
adp_factor: {
value: 9.85548e-8,
status: 'COMPLETED',
unit: 'kg Sbeq/kWh',
source: 'ADEME Base Impacts®',
min: 9.85548e-8,
max: 9.85548e-8,
},
pe_factor: {
value: 11.358,
status: 'COMPLETED',
unit: 'MJ/kWh',
source: 'ADPf / (1-%renewable_energy)',
min: 11.358,
max: 11.358,
},
},
},
data: resolveData,
} as R);
case 'https://api.boavizta.org/v1/cloud/instance?verbose=false&duration=0.002777777777777778':
if (process.env.WRONG_DATA_DETAIL) {
return Promise.reject('error message');
}

return Promise.resolve({
data: resolveData,
} as R);
}
return Promise.resolve({} as R);
Expand Down
Loading

0 comments on commit b95b271

Please sign in to comment.