Skip to content

Commit

Permalink
lint: fixes for unit test updates
Browse files Browse the repository at this point in the history
Signed-off-by: Gnanakeethan Balasubramaniam <[email protected]>
  • Loading branch information
gnanakeethan committed Nov 30, 2023
1 parent 609a93d commit 36068db
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 67 deletions.
14 changes: 7 additions & 7 deletions src/__tests__/unit/lib/boavizta/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import axios, {AxiosResponse} from 'axios';
import {BoaviztaCloudOutputModel, BoaviztaCpuOutputModel,} from '../../../../lib/boavizta/index';
import {
BoaviztaCloudOutputModel,
BoaviztaCpuOutputModel,
} from '../../../../lib/boavizta/index';

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';


async function axiosGet<T = any, R = AxiosResponse<T, any>>(
url: string
): Promise<R> {
Expand Down Expand Up @@ -518,7 +520,7 @@ 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("correct 'instance-type': initialize with params and call usage in IMPL Format", async () => {
const outputModel = new BoaviztaCloudOutputModel();

await expect(
Expand Down Expand Up @@ -612,8 +614,7 @@ describe('cloud:initialize with params', () => {
location: 'USA',
provider: 'aws',
})
).rejects.toThrow(
);
).rejects.toThrow();
await expect(
outputModel.configure({
location: 'USAF',
Expand Down Expand Up @@ -646,7 +647,6 @@ describe('cloud:initialize with params', () => {
'cpu-util': 78,
},
])
).rejects.toThrow(
);
).rejects.toThrow();
});
});
91 changes: 53 additions & 38 deletions src/__tests__/unit/lib/watt-time/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {WattTimeGridEmissions} from '../../../../lib';
import * as DATA from '../../../../__mocks__/watt-time/data.json';
import axios from 'axios';


jest.setTimeout(30000);

jest.mock('axios');
Expand All @@ -14,7 +13,10 @@ const mockAxios = axios as jest.Mocked<typeof axios>;
mockAxios.get.mockImplementation((url, data) => {
switch (url) {
case 'https://api2.watttime.org/v2/login':
if (data?.auth?.username === 'test1' && data?.auth?.password === 'test2') {
if (
data?.auth?.username === 'test1' &&
data?.auth?.password === 'test2'
) {
return Promise.resolve({
status: 200,
data: {
Expand All @@ -28,7 +30,10 @@ mockAxios.get.mockImplementation((url, data) => {
});
}
case 'https://apifail.watttime.org/v2/login':
if (data?.auth?.username === 'test1' && data?.auth?.password === 'test2') {
if (
data?.auth?.username === 'test1' &&
data?.auth?.password === 'test2'
) {
return Promise.resolve({
status: 200,
data: {
Expand All @@ -42,19 +47,19 @@ mockAxios.get.mockImplementation((url, data) => {
});
}
case 'https://apifail2.watttime.org/v2/login':
return Promise.resolve({
status: 200,
data: {
token: 'test_token',
},
});
return Promise.resolve({
status: 200,
data: {
token: 'test_token',
},
});
case 'https://apifail3.watttime.org/v2/login':
return Promise.resolve({
status: 200,
data: {
token: 'test_token',
},
});
return Promise.resolve({
status: 200,
data: {
token: 'test_token',
},
});
case 'https://api2.watttime.org/v2/data':
return Promise.resolve({
data: DATA,
Expand All @@ -69,39 +74,49 @@ mockAxios.get.mockImplementation((url, data) => {
return Promise.resolve({
status: 200,
data: {
none: {}
none: {},
},
});
case 'https://apifail3.watttime.org/v2/data':
return Promise.reject({
status: 401,
data: {
none: {}
none: {},
},
});
}
});
describe('watt-time:configure test', () => {
test('initialize and test', async () => {
await expect(new WattTimeGridEmissions().configure(undefined)).rejects.toThrow();
await expect(
new WattTimeGridEmissions().configure(undefined)
).rejects.toThrow();
const model = await new WattTimeGridEmissions().configure({
username: 'test1',
password: 'test2',
});
await expect(new WattTimeGridEmissions().configure({
username: 'test1',
password: 'test1',
})).rejects.toThrow();
await expect(new WattTimeGridEmissions().configure({
password: 'test1',
})).rejects.toThrow();
await expect(new WattTimeGridEmissions().configure({
username: 'ENV_WATT_USERNAME',
password: 'ENV_WATT_PASSWORD',
})).rejects.toThrow();
await expect(new WattTimeGridEmissions().configure({
token: 'ENV_WATT_TOKEN'
})).rejects.toThrow();
await expect(
new WattTimeGridEmissions().configure({
username: 'test1',
password: 'test1',
})
).rejects.toThrow();
await expect(
new WattTimeGridEmissions().configure({
password: 'test1',
})
).rejects.toThrow();
await expect(
new WattTimeGridEmissions().configure({
username: 'ENV_WATT_USERNAME',
password: 'ENV_WATT_PASSWORD',
})
).rejects.toThrow();
await expect(
new WattTimeGridEmissions().configure({
token: 'ENV_WATT_TOKEN',
})
).rejects.toThrow();
expect(model).toBeInstanceOf(WattTimeGridEmissions);
await expect(
model.execute([
Expand Down Expand Up @@ -168,10 +183,10 @@ describe('watt-time:configure test', () => {
},
]);
const modelFail = await new WattTimeGridEmissions().configure({
'baseUrl': 'https://apifail.watttime.org/v2',
'username': 'test1',
'password': 'test2',
})
baseUrl: 'https://apifail.watttime.org/v2',
username: 'test1',
password: 'test2',
});
await expect(
modelFail.execute([
{
Expand Down Expand Up @@ -250,7 +265,7 @@ describe('watt-time:configure test', () => {
const modelFail2 = await new WattTimeGridEmissions().configure({
username: 'test1',
password: 'test2',
baseUrl:'https://apifail2.watttime.org/v2'
baseUrl: 'https://apifail2.watttime.org/v2',
});
await expect(
modelFail2.execute([
Expand All @@ -269,7 +284,7 @@ describe('watt-time:configure test', () => {
const modelFail3 = await new WattTimeGridEmissions().configure({
username: 'test1',
password: 'test2',
baseUrl:'https://apifail3.watttime.org/v2'
baseUrl: 'https://apifail3.watttime.org/v2',
});
await expect(
modelFail3.execute([
Expand Down
35 changes: 19 additions & 16 deletions src/lib/boavizta/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ abstract class BoaviztaOutputModel implements ModelPluginInterface {
this.authCredentials = authParams;
}

async configure(
staticParams: object
): Promise<ModelPluginInterface> {
async configure(staticParams: object): Promise<ModelPluginInterface> {
this.sharedParams = await this.captureStaticParams(staticParams);

return this;
Expand Down Expand Up @@ -123,9 +121,7 @@ abstract class BoaviztaOutputModel implements ModelPluginInterface {
protected async calculateUsageForinput(
input: ModelParams
): Promise<KeyValuePair> {
if (
this.metricType in input
) {
if (this.metricType in input) {
const usageInput = this.transformToBoaviztaUsage(
input['duration'],
input[this.metricType]
Expand All @@ -145,7 +141,8 @@ abstract class BoaviztaOutputModel implements ModelPluginInterface {

export class BoaviztaCpuOutputModel
extends BoaviztaOutputModel
implements ModelPluginInterface {
implements ModelPluginInterface
{
sharedParams: object | undefined = undefined;
public name: string | undefined;
public verbose = false;
Expand Down Expand Up @@ -185,23 +182,26 @@ export class BoaviztaCpuOutputModel

protected async captureStaticParams(staticParams: object): Promise<object> {
// if verbose is defined in staticParams, remove it from staticParams and set verbose to the value defined in staticParams
if ('verbose' in staticParams && (staticParams.verbose === true || staticParams.verbose === false)) {
if (
'verbose' in staticParams &&
(staticParams.verbose === true || staticParams.verbose === false)
) {
this.verbose = staticParams.verbose;
staticParams.verbose = undefined;
}

if (!('physical-processor' in staticParams)) {
throw new InputValidationError(
this.errorBuilder({
message: 'Missing \'physical-processor\' parameter from configuration',
message: "Missing 'physical-processor' parameter from configuration",
})
);
}

if (!('core-units' in staticParams)) {
throw new InputValidationError(
this.errorBuilder({
message: 'Missing \'core-units\' parameter from configuration',
message: "Missing 'core-units' parameter from configuration",
})
);
}
Expand All @@ -218,7 +218,8 @@ export class BoaviztaCpuOutputModel

export class BoaviztaCloudOutputModel
extends BoaviztaOutputModel
implements ModelPluginInterface {
implements ModelPluginInterface
{
public sharedParams: object | undefined = undefined;
public instanceTypes: BoaviztaInstanceTypes = {};
public name: string | undefined;
Expand Down Expand Up @@ -248,15 +249,15 @@ export class BoaviztaCloudOutputModel
if (!('provider' in staticParamsCast)) {
throw new InputValidationError(
this.errorBuilder({
message: 'Missing \'provider\' parameter from configuration',
message: "Missing 'provider' parameter from configuration",
})
);
}

if (!('instance-type' in staticParamsCast)) {
throw new InputValidationError(
this.errorBuilder({
message: 'Missing \'instance-type\' parameter from configuration',
message: "Missing 'instance-type' parameter from configuration",
})
);
}
Expand Down Expand Up @@ -292,7 +293,7 @@ export class BoaviztaCloudOutputModel
if (!('provider' in staticParamsCast)) {
throw new InputValidationError(
this.errorBuilder({
message: 'Missing \'provider\' parameter from configuration',
message: "Missing 'provider' parameter from configuration",
})
);
} else {
Expand Down Expand Up @@ -355,9 +356,11 @@ export class BoaviztaCloudOutputModel
}

protected async captureStaticParams(staticParams: object): Promise<object> {
if ('verbose' in staticParams &&
if (
'verbose' in staticParams &&
staticParams.verbose !== undefined &&
(staticParams.verbose === true || staticParams.verbose === false)) {
(staticParams.verbose === true || staticParams.verbose === false)
) {
this.verbose = staticParams.verbose;
staticParams.verbose = undefined;
}
Expand Down
14 changes: 8 additions & 6 deletions src/lib/watt-time/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export class WattTimeGridEmissions implements ModelPluginInterface {
duration: fetchDuration,
});


// for each input block, calculate the average emission
return inputs.map((input, index) => {
const inputStart = dayjs(input.timestamp);
Expand Down Expand Up @@ -131,7 +130,7 @@ export class WattTimeGridEmissions implements ModelPluginInterface {
wattimedata: KeyValuePair[],
inputStart: dayjs.Dayjs,
inputEnd: dayjs.Dayjs
): { datapoints: number; data: number[] } {
): {datapoints: number; data: number[]} {
let datapoints = 0;

const data = wattimedata.map((data: KeyValuePair) => {
Expand Down Expand Up @@ -183,23 +182,23 @@ export class WattTimeGridEmissions implements ModelPluginInterface {
throw new InputValidationError(
this.errorBuilder({
message:
'\'location\' should be a comma separated string of \'latitude\' and \'longitude\'',
"'location' should be a comma separated string of 'latitude' and 'longitude'",
})
);
}

if (location[0] === '' || location[1] === '') {
throw new InputValidationError(
this.errorBuilder({
message: '\'latitude\' or \'longitude\' is missing',
message: "'latitude' or 'longitude' is missing",
})
);
}

if (location[0] === '0' || location[1] === '0') {
throw new InputValidationError(
this.errorBuilder({
message: '\'latitude\' or \'longitude\' is missing',
message: "'latitude' or 'longitude' is missing",
})
);
}
Expand All @@ -210,7 +209,10 @@ export class WattTimeGridEmissions implements ModelPluginInterface {
return {latitude, longitude};
}

private determineinputStartEnd(inputs: ModelParams[]): { startTime: dayjs.Dayjs, fetchDuration: number } {
private determineinputStartEnd(inputs: ModelParams[]): {
startTime: dayjs.Dayjs;
fetchDuration: number;
} {
let starttime = dayjs('9999-12-31'); // largest possible start time
let endtime = dayjs('1970-01-01'); // smallest possible end time

Expand Down

0 comments on commit 36068db

Please sign in to comment.