Skip to content

Commit

Permalink
Merge pull request #8 from Green-Software-Foundation/IF-288-Unit-Tests
Browse files Browse the repository at this point in the history
Unit Tests & Coverage for Boavizta & WattTime Models
  • Loading branch information
jmcook1186 authored Dec 1, 2023
2 parents 6caf728 + 5bbaa32 commit 8c3135a
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 202 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"scripts": {
"build": "rm -rf build && tsc --project tsconfig.build.json",
"fix": "gts fix",
"coverage": "jest --verbose --coverage",
"fix:package": "fixpack",
"lint": "gts lint",
"prepublish": "yarn build",
Expand Down
202 changes: 90 additions & 112 deletions src/__tests__/unit/lib/boavizta/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import {
BoaviztaCpuOutputModel,
} from '../../../../lib/boavizta/index';

import {ERRORS} from '../../../../util/errors';

import * as PROVIDERS from '../../../../__mocks__/boavizta/providers.json';
import * as COUNTRIES from '../../../../__mocks__/boavizta/countries.json';
import * as INSTANCE_TYPES from '../../../../__mocks__/boavizta/instance_types.json';

const {InputValidationError} = ERRORS;

async function axiosGet<T = any, R = AxiosResponse<T, any>>(
url: string
): Promise<R> {
Expand All @@ -38,7 +34,7 @@ mockAxios.post.mockImplementation(
case 'https://api.boavizta.org/v1/component/cpu?verbose=false&duration=1':
return Promise.resolve({
data: {
outputs: {
impacts: {
gwp: {
embedded: {
value: 0.0008,
Expand Down Expand Up @@ -78,7 +74,7 @@ mockAxios.post.mockImplementation(
case 'https://api.boavizta.org/v1/component/cpu?verbose=true&duration=2':
return Promise.resolve({
data: {
outputs: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
Expand Down Expand Up @@ -114,7 +110,7 @@ mockAxios.post.mockImplementation(
},
},
verbose: {
outputs: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
Expand Down Expand Up @@ -208,7 +204,7 @@ mockAxios.post.mockImplementation(
value: 9.85548e-8,
status: 'COMPLETED',
unit: 'kg Sbeq/kWh',
source: 'ADEME Base outputS ®',
source: 'ADEME Base Impacts ®',
min: 9.85548e-8,
max: 9.85548e-8,
},
Expand All @@ -226,7 +222,7 @@ mockAxios.post.mockImplementation(
case 'https://api.boavizta.org/v1/cloud/instance?verbose=false&duration=0.004166666666666667':
return Promise.resolve({
data: {
outputs: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
Expand Down Expand Up @@ -262,7 +258,7 @@ mockAxios.post.mockImplementation(
},
},
verbose: {
outputs: {
impacts: {
gwp: {
embedded: {
value: 0.0016,
Expand Down Expand Up @@ -356,7 +352,7 @@ mockAxios.post.mockImplementation(
value: 9.85548e-8,
status: 'COMPLETED',
unit: 'kg Sbeq/kWh',
source: 'ADEME Base outputS ®',
source: 'ADEME Base Impacts®',
min: 9.85548e-8,
max: 9.85548e-8,
},
Expand All @@ -381,52 +377,40 @@ describe('cpu:configure test', () => {
const outputModel = new BoaviztaCpuOutputModel();
await expect(
outputModel.configure({allocation: 'wrong'})
).rejects.toThrowError();
).rejects.toThrow();
});

// test('initialize without params throws error for parameter and call execute without params throws error for input', async () => {
// const outputModel = new BoaviztaCpuOutputModel();
// const outputModelConfigFail = new BoaviztaCpuOutputModel();
// await expect(outputModel.authenticate({})).resolves.toBe(undefined);
// await expect(
// outputModelConfigFail.execute([
// {
// timestamp: '2021-01-01T00:00:00Z',
// duration: 3600,
// 'cpu-util': 50,
// },
// ])
// ).rejects.toThrowError();
test('initialize without params throws error for parameter and call execute without params throws error for input', async () => {
const outputModel = new BoaviztaCpuOutputModel();
const outputModelConfigFail = new BoaviztaCpuOutputModel();
await expect(outputModel.authenticate({})).resolves.toBe(undefined); // authenticate does not have any params / output
await expect(
outputModelConfigFail.execute([
{
timestamp: '2021-01-01T00:00:00Z',
duration: 3600,
'cpu-util': 50,
},
])
).rejects.toThrow();

// await expect(outputModel.configure({})).rejects.toThrow(
// Error('Improper configure: Missing processor parameter')
// );
// await expect(
// outputModel.configure({
// 'physical-processor': 'Intel Xeon Gold 6138f',
// })
// ).rejects.toThrow(
// Error('Improper configure: Missing core-units parameter')
// );
// await expect(
// outputModel.configure({
// 'physical-processor': 'Intel Xeon Gold 6138f',
// 'core-units': 24,
// 'expected-lifespan': 4 * 365 * 24 * 60 * 60,
// })
// ).resolves.toBeInstanceOf(BoaviztaCpuOutputModel);
await expect(outputModel.configure({})).rejects.toThrow();
await expect(
outputModel.configure({
'physical-processor': 'Intel Xeon Gold 6138f',
})
).rejects.toThrow();
await expect(
outputModel.configure({
'physical-processor': 'Intel Xeon Gold 6138f',
'core-units': 24,
'expected-lifespan': 4 * 365 * 24 * 60 * 60,
})
).resolves.toBeInstanceOf(BoaviztaCpuOutputModel);

// // not providing inputs will throw a missing inputs error
// await expect(outputModel.execute(undefined)).rejects.toStrictEqual(
// Error(
// 'Parameter Not Given: invalid inputs parameter. Expecting an array of inputs'
// )
// );
// // improper inputs will throw an invalid inputs error
// await expect(
// outputModel.execute([{invalid: 'input'}])
// ).rejects.toStrictEqual(Error('Invalid Input: Invalid inputs parameter'));
// });
// not providing inputs will throw a missing inputs error
await expect(outputModel.execute([])).rejects.toThrow();
});
});

describe('cpu:initialize with params', () => {
Expand Down Expand Up @@ -458,6 +442,16 @@ describe('cpu:initialize with params', () => {
});
test('initialize with params and call multiple usages in IMPL format:verbose', async () => {
const outputModel = new BoaviztaCpuOutputModel();
// test configuration with verbose false
await expect(
outputModel.configure({
'physical-processor': 'Intel Xeon Gold 6138f',
'core-units': 24,
location: 'USA',
verbose: false,
})
).resolves.toBeInstanceOf(BoaviztaCpuOutputModel);
// test configuration with verbose true
await expect(
outputModel.configure({
'physical-processor': 'Intel Xeon Gold 6138f',
Expand All @@ -467,7 +461,7 @@ describe('cpu:initialize with params', () => {
})
).resolves.toBeInstanceOf(BoaviztaCpuOutputModel);

// configure without static params will cause improper configure error
// verbose still results in same output
await expect(
outputModel.execute([
{
Expand All @@ -488,15 +482,6 @@ describe('cpu:initialize with params', () => {
describe('cloud:initialize with params', () => {
test('initialize with params and call usage in RAW Format', async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.validateLocation({location: 'SomethingFail'})
).rejects.toThrowError();
await expect(
outputModel.validateInstanceType({'instance-type': 'SomethingFail'})
).rejects.toThrowError();
await expect(
outputModel.validateProvider({provider: 'SomethingFail'})
).rejects.toThrowError();
await expect(
outputModel.configure({
'instance-type': 't2.micro',
Expand Down Expand Up @@ -527,31 +512,62 @@ describe('cloud:initialize with params', () => {

// configure without static params will cause improper configure error
});

test("correct 'instance-type': initialize with params and call usage in IMPL Format", async () => {
test('invalid input for location throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.validateLocation({location: 'SomethingFail'})
).rejects.toThrow();
});
test('invalid input for instance type throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.validateInstanceType({'instance-type': 'SomethingFail'})
).rejects.toThrow();
});
test('invalid input for provider throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.validateProvider({provider: 'SomethingFail'})
).rejects.toThrow();
});
test('missing provider throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();

await expect(
outputModel.configure({
'instance-type': 't2.micro',
location: 'USA',
})
).rejects.toThrowError();
).rejects.toThrow();
});
test('missing `instance-type` throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.configure({
provider: 'aws',
location: 'USA',
})
).rejects.toThrowError();
).rejects.toThrow();
});

test('wrong `instance-type` throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.configure({
'instance-type': 't5.micro',
location: 'USA',
provider: 'aws',
})
).rejects.toThrow();
});
test("correct 'instance-type': initialize with params and call usage in IMPL Format", async () => {
const outputModel = new BoaviztaCloudOutputModel();
await expect(
outputModel.configure({
'instance-type': 't2.micro',
location: 'USA',
provider: 'aws',
})
).resolves.toBeInstanceOf(BoaviztaCloudOutputModel);

// mockAxios.get.mockResolvedValue({data: {}});
await expect(
outputModel.execute([
{
Expand All @@ -566,44 +582,14 @@ describe('cloud:initialize with params', () => {
energy: 1.6408333333333334,
},
]);
});

test('wrong "instance-type": initialize with params and call usage in IMPL Format throws error', async () => {
const outputModel = new BoaviztaCloudOutputModel();

await expect(
outputModel.configure({
'instance-type': 't5.micro',
location: 'USA',
provider: 'aws',
})
).rejects.toThrowError();

// configure without static params will cause improper configure error
await expect(
outputModel.execute([
{
timestamp: '2021-01-01T00:00:00Z',
duration: 15,
'cpu-util': 34,
},
{
timestamp: '2021-01-01T00:00:15Z',
duration: 15,
'cpu-util': 12,
},
{
timestamp: '2021-01-01T00:00:30Z',
duration: 15,
'cpu-util': 1,
},
{
timestamp: '2021-01-01T00:00:45Z',
duration: 15,
'cpu-util': 78,
},
])
).rejects.toThrowError();
).rejects.toThrow();
});

test('without "instance-type": initialize with params and call usage in IMPL Format throws error', async () => {
Expand All @@ -614,18 +600,14 @@ describe('cloud:initialize with params', () => {
location: 'USA',
provider: 'aws',
})
).rejects.toStrictEqual(
new InputValidationError(
"BoaviztaOutputModel: Missing 'instance-type' parameter from configuration."
)
);
).rejects.toThrow();
await expect(
outputModel.configure({
location: 'USAF',
provider: 'aws',
'instance-type': 't2.micro',
})
).rejects.toThrowError();
).rejects.toThrow();

// configure without static params will cause improper configure error
await expect(
Expand All @@ -651,10 +633,6 @@ describe('cloud:initialize with params', () => {
'cpu-util': 78,
},
])
).rejects.toStrictEqual(
new InputValidationError(
'BoaviztaOutputModel: Missing configuration parameters.'
)
);
).rejects.toThrow();
});
});
Loading

0 comments on commit 8c3135a

Please sign in to comment.